""" Security tests for delete anime feature. Tests path traversal protection, confirm_text validation, and other security controls around the delete anime feature. """ import os import pytest class TestDeleteAnimeSecurity: """Security tests for the delete anime feature.""" @pytest.fixture def anime_service_code(self): """Read the anime_service.py source code for security checks.""" path = os.path.join( os.path.dirname(__file__), '..', '..', 'src', 'server', 'services', 'anime_service.py' ) with open(path, 'r') as f: return f.read() @pytest.fixture def delete_modal_code(self): """Read the delete-modal.js source code for security checks.""" path = os.path.join( os.path.dirname(__file__), '..', '..', 'src', 'server', 'web', 'static', 'js', 'index', 'delete-modal.js' ) with open(path, 'r') as f: return f.read() def test_delete_series_uses_is_safe_path(self, anime_service_code): """delete_series uses is_safe_path before deleting folders.""" assert 'is_safe_path' in anime_service_code assert 'folder_path' in anime_service_code def test_delete_series_checks_anime_base_directory(self, anime_service_code): """delete_series validates paths against the anime base directory.""" # Should reference the anime directory for path comparison assert 'anime_base_dir' in anime_service_code or 'directory_to_search' in anime_service_code def test_delete_series_no_hardcoded_paths(self, anime_service_code): """delete_series has no hardcoded dangerous paths.""" dangerous = ['/etc/passwd', '/root/.ssh', 'C:\\Windows\\System32'] for path in dangerous: assert path not in anime_service_code def test_delete_modal_encodes_key_in_url(self, delete_modal_code): """delete-modal.js encodes the series key in the API URL.""" # Should use encodeURIComponent or similar for the key assert 'encodeURIComponent' in delete_modal_code def test_delete_modal_no_inner_html_with_user_data(self, delete_modal_code): """delete-modal.js does not use innerHTML with unsanitized user data.""" # innerHTML should not be used with direct variable interpolation # that could allow XSS lines = delete_modal_code.split('\n') dangerous_lines = [ line for line in lines if 'innerHTML' in line and 'currentSeriesName' in line and 'escapeHtml' not in line ] assert len(dangerous_lines) == 0, \ "innerHTML used with currentSeriesName without escapeHtml" def test_delete_modal_uses_textContent_for_user_visible_text(self, delete_modal_code): """User-visible text in modal uses safe DOM methods.""" # Should use textContent or similar instead of innerHTML for data # This is implicit in using template literals with ${} - but check no obvious XSS assert '