import asyncio import tempfile import unittest from pathlib import Path import apps.base.pages as pages from core.settings import settings class IndexTemplateEscapingTests(unittest.TestCase): """Regression: site config must be html.escape-d before template injection (stored XSS prevention). Uses a temp file as a stand-in for themes/*/index.html so the test also runs in a bare checkout without built frontends. """ def setUp(self): self._original_user_config = dict(settings.user_config) self._original_resolve_theme_file = pages.resolve_theme_file pages._render_index_html.cache_clear() tmp = tempfile.NamedTemporaryFile( "w", suffix=".html", delete=False, encoding="utf-8" ) tmp.write( "{{title}}" '' "
{{opacity}}
" ) tmp.close() self._template_path = Path(tmp.name) def tearDown(self): settings.user_config = self._original_user_config pages.resolve_theme_file = self._original_resolve_theme_file pages._render_index_html.cache_clear() self._template_path.unlink() def _patch_template(self): pages.resolve_theme_file = lambda *args, **kwargs: self._template_path def _render_index_html(self) -> str: return asyncio.run(pages.index()).body.decode("utf-8") def test_malicious_site_config_is_escaped(self): self._patch_template() settings.name = "" settings.description = '">' html = self._render_index_html() self.assertNotIn("