Files
filecodebox/tests/test_index_template_escaping.py
T
orion 41519afb6a ✨ feat: 整合 2.8.5 后端存储、安全与运行优化
* 禁用 OSS 非必需校验尾部,改用精确会话前缀逐对象清理
* 保护上传终态与分享事务,清理失败保留会话并支持安全重试
* 统一下载响应头,兼容旧下载器并防止文件名注入
* 限流改用单调时钟并限制 IP 记录,缓存模板与静态资源策略
* 统一 ORM 初始化与异常处理,新增查询索引及 SQLite 性能参数
* 固定 uvloop/httptools,统一 Docker keep-alive 与内存回收配置
* 同步版本与升级文档,补充 OSS、清理、缓存及数据库回归验证
2026-10-29 16:37:14 +08:00

71 lines
2.2 KiB
Python

import asyncio
import tempfile
import unittest
from pathlib import Path
import main 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>{{title}}</title>"
'<meta name="description" content="{{description}}">'
"<div>{{opacity}}</div>"
)
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 = "<script>alert(1)</script>"
settings.description = '"><img src=x onerror=alert(2)>'
html = self._render_index_html()
self.assertNotIn("<script>alert", html)
self.assertNotIn("<img", html)
self.assertIn("&lt;script&gt;", html)
def test_plain_site_config_renders_unchanged(self):
self._patch_template()
settings.name = "FileCodeBox"
settings.description = "A simple file share service"
settings.opacity = 0.9
html = self._render_index_html()
self.assertIn("<title>FileCodeBox</title>", html)
self.assertIn('content="A simple file share service"', html)
self.assertIn("<div>0.9</div>", html)
if __name__ == "__main__":
unittest.main()