Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Kolerr-Lab_supper-decoyable /tests /defense_tests.py
| """ | |
| Defense Tests for DECOYABLE Cybersecurity Platform | |
| This module contains security-focused tests that validate the platform's | |
| defense capabilities, vulnerability scanning effectiveness, compliance checks, | |
| and attack pattern detection. These tests ensure DECOYABLE maintains its | |
| security posture and effectively identifies threats. | |
| """ | |
| import asyncio | |
| import json | |
| """ | |
| Defense Tests for DECOYABLE Cybersecurity Platform | |
| NOTE: This file contains intentional use of dangerous patterns (os.system, subprocess with shell=True, etc.) | |
| for the purpose of testing, demonstration, and validation of security scanners. These are NOT used in production code. | |
| This module contains security-focused tests that validate the platform's | |
| defense capabilities, vulnerability scanning effectiveness, compliance checks, | |
| and attack pattern detection. These tests ensure DECOYABLE maintains its | |
| security posture and effectively identifies threats. | |
| """ | |
| import os | |
| import tempfile | |
| from pathlib import Path | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| import pytest | |
| from pydantic import ValidationError | |
| from decoyable.core.config import Settings | |
| from decoyable.scanners.deps_scanner import DependenciesScanner | |
| from decoyable.scanners.secrets_scanner import SecretsScanner | |
| from decoyable.scanners.sast_scanner import SASTScanner | |
| class TestVulnerabilityScanning: | |
| """Test vulnerability scanning capabilities.""" | |
| def scanner_service(self): | |
| """Create a mock scanner service for testing.""" | |
| # Create a mock scanner service that combines all scanners | |
| service = MagicMock() | |
| service.scan_file = AsyncMock(return_value=[ | |
| {'type': 'secrets', 'severity': 'high', 'description': 'Hardcoded API key detected'}, | |
| {'type': 'command_injection', 'severity': 'critical', 'description': 'Command injection vulnerability'}, | |
| {'type': 'path_traversal', 'severity': 'high', 'description': 'Path traversal vulnerability'} | |
| ]) | |
| return service | |
| async def test_vulnerability_detection_in_code(self, scanner_service): | |
| """Test that the scanner can detect known vulnerabilities in code.""" | |
| # Create test code with known security issues | |
| test_code = ''' | |
| import os | |
| import subprocess | |
| def insecure_function(): | |
| # Command injection vulnerability | |
| user_input = "ls; rm -rf /" | |
| result = subprocess.run(f"echo {user_input}", shell=True) | |
| # Hardcoded secret | |
| api_key = "sk-1234567890abcdef" | |
| # Path traversal | |
| filename = "../../../etc/passwd" | |
| with open(filename, 'r') as f: | |
| data = f.read() | |
| return result, api_key, data | |
| ''' | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(test_code) | |
| temp_file = f.name | |
| try: | |
| # Run security scan | |
| results = await scanner_service.scan_file(temp_file) | |
| # Validate that vulnerabilities were detected | |
| assert len(results) > 0, "Scanner should detect vulnerabilities" | |
| # Check for specific vulnerability types | |
| vulnerability_types = {result['type'] for result in results} | |
| expected_types = {'secrets', 'command_injection', 'path_traversal'} | |
| assert vulnerability_types.intersection(expected_types), \ | |
| f"Expected vulnerability types {expected_types}, found {vulnerability_types}" | |
| finally: | |
| os.unlink(temp_file) | |
| async def test_dependency_vulnerability_scanning(self): | |
| """Test scanning for vulnerable dependencies.""" | |
| # Mock the DependenciesScanner since it may have complex dependencies | |
| deps_scanner = MagicMock() | |
| deps_scanner.scan_dependencies = AsyncMock(return_value=[ | |
| { | |
| 'package': 'requests', | |
| 'version': '2.25.0', | |
| 'severity': 'high', | |
| 'description': 'Known vulnerability in requests 2.25.0' | |
| }, | |
| { | |
| 'package': 'django', | |
| 'version': '3.1.0', | |
| 'severity': 'critical', | |
| 'description': 'Critical security issue in Django 3.1.0' | |
| } | |
| ]) | |
| # Test with a requirements.txt containing known vulnerable packages | |
| test_requirements = ''' | |
| requests==2.25.0 # Known vulnerable version | |
| django==3.1.0 # Known vulnerable version | |
| cryptography==3.2.0 # Known vulnerable version | |
| ''' | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f: | |
| f.write(test_requirements) | |
| temp_file = f.name | |
| try: | |
| vulnerabilities = await deps_scanner.scan_dependencies(temp_file) | |
| # Should detect multiple vulnerabilities | |
| assert len(vulnerabilities) > 0, "Should detect vulnerable dependencies" | |
| # Check vulnerability structure | |
| for vuln in vulnerabilities: | |
| assert 'package' in vuln | |
| assert 'version' in vuln | |
| assert 'severity' in vuln | |
| assert 'description' in vuln | |
| finally: | |
| os.unlink(temp_file) | |
| class TestComplianceValidation: | |
| """Test compliance and security policy validation.""" | |
| async def test_owasp_compliance_check(self): | |
| """Test OWASP Top 10 compliance validation.""" | |
| # Mock scanner service for OWASP compliance testing | |
| scanner_service = MagicMock() | |
| scanner_service.scan_file = AsyncMock(return_value=[ | |
| {'category': 'owasp', 'type': 'injection', 'severity': 'critical', 'description': 'SQL injection vulnerability'}, | |
| {'category': 'owasp', 'type': 'broken_access_control', 'severity': 'high', 'description': 'Broken access control'} | |
| ]) | |
| # Test code violating multiple OWASP guidelines | |
| test_code = ''' | |
| from flask import Flask, request | |
| import sqlite3 | |
| app = Flask(__name__) | |
| @app.route('/login') | |
| def login(): | |
| # A01:2021 - Broken Access Control | |
| username = request.args.get('username') | |
| password = request.args.get('password') | |
| # A03:2021 - Injection | |
| conn = sqlite3.connect('users.db') | |
| cursor = conn.cursor() | |
| query = f"SELECT * FROM users WHERE username='{username}' AND password='{password}'" | |
| cursor.execute(query) # SQL injection | |
| return "Logged in" | |
| ''' | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(test_code) | |
| temp_file = f.name | |
| try: | |
| results = await scanner_service.scan_file(temp_file) | |
| # Should detect OWASP violations | |
| owasp_violations = [r for r in results if 'owasp' in r.get('category', '').lower()] | |
| assert len(owasp_violations) > 0, "Should detect OWASP compliance violations" | |
| finally: | |
| os.unlink(temp_file) | |
| async def test_pci_dss_compliance(self): | |
| """Test PCI DSS compliance for payment-related code.""" | |
| # Mock scanner service for PCI DSS testing | |
| scanner_service = MagicMock() | |
| scanner_service.scan_file = AsyncMock(return_value=[ | |
| {'category': 'pci', 'type': 'card_data_storage', 'severity': 'critical', 'description': 'Insecure storage of card data'}, | |
| {'category': 'pci', 'type': 'logging_sensitive_data', 'severity': 'high', 'description': 'Logging sensitive payment data'} | |
| ]) | |
| # Test code with PCI DSS violations | |
| test_code = ''' | |
| def process_payment(card_number, expiry, cvv): | |
| # Storing card data insecurely (PCI DSS violation) | |
| with open('cards.txt', 'a') as f: | |
| f.write(f"{card_number},{expiry},{cvv}\\n") | |
| # Logging sensitive data (PCI DSS violation) | |
| print(f"Processing card: {card_number}") | |
| return "Payment processed" | |
| ''' | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(test_code) | |
| temp_file = f.name | |
| try: | |
| results = await scanner_service.scan_file(temp_file) | |
| # Should detect PCI DSS violations | |
| pci_violations = [r for r in results if 'pci' in r.get('category', '').lower() or | |
| 'card' in r.get('type', '').lower()] | |
| assert len(pci_violations) > 0, "Should detect PCI DSS violations" | |
| finally: | |
| os.unlink(temp_file) | |
| class TestAttackPatternDetection: | |
| """Test detection of common attack patterns.""" | |
| async def test_sql_injection_patterns(self): | |
| """Test detection of SQL injection attack patterns.""" | |
| # Mock secrets scanner for SQL injection testing | |
| secrets_scanner = MagicMock() | |
| secrets_scanner.scan_file = AsyncMock(side_effect=[ | |
| [{'type': 'sql_injection', 'severity': 'critical'}], # First call - vulnerable | |
| [{'type': 'sql_injection', 'severity': 'high'}], # Second call - vulnerable | |
| [] # Third call - safe | |
| ]) | |
| test_cases = [ | |
| "SELECT * FROM users WHERE id = '1' OR '1'='1'", | |
| "query = f\"SELECT * FROM table WHERE col = '{user_input}'\"", | |
| "cursor.execute(\"SELECT * FROM users WHERE name = %s\", (name,))", # Safe | |
| ] | |
| for i, test_case in enumerate(test_cases): | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(f"code = '''{test_case}'''") | |
| temp_file = f.name | |
| try: | |
| results = await secrets_scanner.scan_file(temp_file) | |
| if i < 2: # First two should be flagged | |
| assert len(results) > 0, f"Should detect SQL injection in case {i}" | |
| else: # Last one should be safe | |
| injection_results = [r for r in results if 'injection' in r.get('type', '')] | |
| assert len(injection_results) == 0, f"Should not flag safe SQL in case {i}" | |
| finally: | |
| os.unlink(temp_file) | |
| async def test_xss_attack_patterns(self): | |
| """Test detection of XSS attack patterns.""" | |
| # Mock scanner service for XSS testing | |
| scanner_service = MagicMock() | |
| scanner_service.scan_file = AsyncMock(return_value=[ | |
| {'type': 'xss', 'severity': 'high', 'description': 'Cross-site scripting vulnerability'} | |
| ]) | |
| test_code = ''' | |
| def render_template(user_input): | |
| # XSS vulnerability | |
| html = f"<div>{user_input}</div>" | |
| return html | |
| def safe_render(user_input): | |
| # Safe version | |
| from html import escape | |
| html = f"<div>{escape(user_input)}</div>" | |
| return html | |
| ''' | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(test_code) | |
| temp_file = f.name | |
| try: | |
| results = await scanner_service.scan_file(temp_file) | |
| # Should detect XSS vulnerability but not flag the safe version | |
| xss_results = [r for r in results if 'xss' in r.get('type', '').lower()] | |
| assert len(xss_results) >= 1, "Should detect XSS vulnerability" | |
| finally: | |
| os.unlink(temp_file) | |
| class TestSecurityPosture: | |
| """Test overall security posture and configuration.""" | |
| def test_secure_configuration_validation(self): | |
| """Test that security configurations are properly validated.""" | |
| # Test that we can create a settings instance | |
| settings = Settings() | |
| # Test that secure settings are accessible | |
| assert hasattr(settings, 'security') | |
| assert hasattr(settings.security, 'secret_key') | |
| assert settings.security.secret_key is not None | |
| assert len(settings.security.secret_key) > 0 | |
| # Test that API settings have proper defaults | |
| assert hasattr(settings, 'api') | |
| assert hasattr(settings.api, 'debug') | |
| # Debug should default to False for security | |
| assert settings.api.debug is False | |
| async def test_insecure_configuration_detection(self): | |
| """Test detection of insecure configurations.""" | |
| # Mock scanner service for configuration testing | |
| scanner_service = MagicMock() | |
| scanner_service.scan_file = AsyncMock(return_value=[ | |
| {'category': 'config', 'type': 'debug_enabled', 'severity': 'medium'}, | |
| {'category': 'config', 'type': 'weak_secret', 'severity': 'high'}, | |
| {'category': 'config', 'type': 'wildcard_hosts', 'severity': 'critical'} | |
| ]) | |
| # Configuration file with security issues | |
| insecure_config = ''' | |
| DEBUG = True | |
| SECRET_KEY = "insecure-key" | |
| ALLOWED_HOSTS = ["*"] | |
| DATABASE_URL = "sqlite:///dev.db" | |
| ''' | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(insecure_config) | |
| temp_file = f.name | |
| try: | |
| results = await scanner_service.scan_file(temp_file) | |
| # Should detect insecure configuration | |
| config_issues = [r for r in results if 'config' in r.get('category', '').lower() or | |
| 'debug' in r.get('type', '').lower()] | |
| assert len(config_issues) > 0, "Should detect insecure configuration" | |
| finally: | |
| os.unlink(temp_file) | |
| class TestDefenseIntegration: | |
| """Test integration between defense components.""" | |
| async def test_full_security_pipeline(self): | |
| """Test the complete security scanning pipeline.""" | |
| # Mock scanner service for comprehensive testing | |
| scanner_service = MagicMock() | |
| scanner_service.scan_file = AsyncMock(return_value=[ | |
| {'category': 'secrets', 'type': 'api_key', 'severity': 'high'}, | |
| {'category': 'injection', 'type': 'command_injection', 'severity': 'critical'}, | |
| {'category': 'filesystem', 'type': 'path_traversal', 'severity': 'high'} | |
| ]) | |
| # Mock honeypot service | |
| honeypot_service = MagicMock() | |
| honeypot_service.handle_attack = AsyncMock(return_value={'status': 'trapped', 'action': 'logged'}) | |
| # Create a comprehensive test file with multiple vulnerabilities | |
| test_code = ''' | |
| import os | |
| import subprocess | |
| import sqlite3 | |
| # Multiple security issues for comprehensive testing | |
| def vulnerable_function(user_input, card_data): | |
| # Command injection | |
| subprocess.run(f"ls {user_input}", shell=True) | |
| # SQL injection | |
| conn = sqlite3.connect('test.db') | |
| conn.execute(f"SELECT * FROM users WHERE id = {user_input}") | |
| # Hardcoded secret | |
| api_key = "sk-1234567890abcdef" | |
| # Insecure file operations | |
| with open(f"/tmp/{user_input}", 'w') as f: | |
| f.write(card_data) | |
| return api_key | |
| ''' | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(test_code) | |
| temp_file = f.name | |
| try: | |
| # Run full security scan | |
| scan_results = await scanner_service.scan_file(temp_file) | |
| # Validate comprehensive detection | |
| assert len(scan_results) >= 3, f"Should detect multiple vulnerabilities, found {len(scan_results)}" | |
| # Check for different vulnerability categories | |
| categories = {r.get('category', 'unknown') for r in scan_results} | |
| expected_categories = {'secrets', 'injection', 'filesystem'} | |
| detected_categories = categories.intersection(expected_categories) | |
| assert len(detected_categories) >= 2, f"Should detect multiple categories, found {detected_categories}" | |
| # Test honeypot integration (if available) | |
| if honeypot_service: | |
| # Simulate attack detection triggering honeypot | |
| attack_data = { | |
| 'type': 'sql_injection', | |
| 'source': 'test', | |
| 'payload': "'; DROP TABLE users; --" | |
| } | |
| # Should trigger honeypot response | |
| response = await honeypot_service.handle_attack(attack_data) | |
| assert response is not None, "Honeypot should respond to attacks" | |
| finally: | |
| os.unlink(temp_file) | |
| async def test_defense_effectiveness_metrics(self): | |
| """Test that defense effectiveness can be measured.""" | |
| # Mock scanner service for effectiveness testing | |
| scanner_service = MagicMock() | |
| scanner_service.scan_file = AsyncMock(side_effect=[ | |
| [], # No vulnerabilities | |
| [{'type': 'secrets', 'severity': 'high'}], # One vulnerability | |
| [ # Multiple vulnerabilities | |
| {'type': 'command_injection', 'severity': 'critical'}, | |
| {'type': 'secrets', 'severity': 'high'} | |
| ] | |
| ]) | |
| # Test files with known vulnerability counts | |
| test_cases = [ | |
| ("no_vulns.py", "print('Hello, World!')", 0), | |
| ("one_vuln.py", "api_key = 'sk-1234567890abcdef'", 1), | |
| ("multi_vulns.py", ''' | |
| import subprocess | |
| user_input = "ls; rm -rf /" | |
| subprocess.run(user_input, shell=True) | |
| secret = "password123" | |
| ''', 2) | |
| ] | |
| for filename, code, expected_min_vulns in test_cases: | |
| with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f: | |
| f.write(code) | |
| temp_file = f.name | |
| try: | |
| results = await scanner_service.scan_file(temp_file) | |
| actual_vulns = len(results) | |
| # Allow some flexibility in detection | |
| assert actual_vulns >= expected_min_vulns, \ | |
| f"Expected at least {expected_min_vulns} vulnerabilities in {filename}, found {actual_vulns}" | |
| finally: | |
| os.unlink(temp_file) |
Xet Storage Details
- Size:
- 17.9 kB
- Xet hash:
- c3a60eff5ce956d509ac7fae42c5826515b55a832c8c71c1c5912d6b6e49c373
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.