import os import re import tempfile from unittest.mock import AsyncMock import pytest from fastapi.testclient import TestClient from theHarvester.__main__ import sanitize_filename, sanitize_for_xml class TestCORSConfiguration: """Check CORS configuration.""" def test_api_does_not_enable_cross_origin_requests(self): from theHarvester.lib.api.api import app assert all('CORSMiddleware' not in str(middleware.cls) for middleware in app.user_middleware) class TestXMLInjectionPrevention: """Check XML escaping.""" def test_sanitize_for_xml_escapes_special_characters(self): """Escape XML special characters.""" # Test all XML special characters test_cases = [ ('&', '&'), ('<', '<'), ('>', '>'), ('"', '"'), ("'", '''), ('', '<script>alert("XSS")</script>'), ('user@example.com & ', 'user@example.com & <test>'), ('Normal text', 'Normal text'), ] for input_text, expected_output in test_cases: result = sanitize_for_xml(input_text) assert result == expected_output, f'Failed to properly escape: {input_text}' def test_sanitize_for_xml_prevents_xml_entity_injection(self): """Escape XML entity declarations and references.""" malicious_inputs = [ ']>', '', '', '<script>', ] for malicious_input in malicious_inputs: result = sanitize_for_xml(malicious_input) # Ensure dangerous characters are escaped assert '<' in result or '&' in result, f'Failed to sanitize: {malicious_input}' assert '<' not in result or result == malicious_input.replace('<', '<'), f'XML tags not escaped: {malicious_input}' def test_command_line_args_are_sanitized_in_xml_output(self): """Escape command-line arguments before writing them to XML.""" # Simulate dangerous command line arguments dangerous_args = [ '--domain=test.com', "--source=''", '--output="; rm -rf /', '--domain=example.com¶m=', ] for arg in dangerous_args: sanitized = sanitize_for_xml(arg) # Verify no unescaped XML special characters remain assert '