🐛 修复主题静态资源错误回退

避免缺失或越界的主题资源被全局 SPA 404 回退误返回首页 200,保持静态资源请求的明确 404 语义。

- 为主题资源回退增加显式 404 响应
- 增加缺失资源与路径穿越回归测试
This commit is contained in:
orion committed 2026-08-07 09:23:56 +08:00
1 parent 0be5a31a59
commit c25c495657
2 files changed
+22 -1

No files matched your search

+2
View File
@@ -891,6 +891,8 @@ async def theme_asset(asset_path: str):
@app.exception_handler(404)
@app.get("/")
async def index(request=None, exc=None):
if request is not None and request.url.path.startswith(("/assets/", "/theme-assets/")):
return PlainTextResponse("Not found", status_code=404)
theme_dir = resolve_theme_root()
index_file = resolve_theme_file("index.html")
asset_version = str(int(index_file.stat().st_mtime))
+20 -1
View File
@@ -1,10 +1,11 @@
import asyncio
import unittest
from types import SimpleNamespace
from fastapi import HTTPException
from core.settings import BASE_DIR, settings
from main import index, resolve_theme_file
from main import index, resolve_theme_file, theme_asset
class SettingsOverrideMixin:
@@ -43,6 +44,24 @@ class ThemeAssetTests(SettingsOverrideMixin, unittest.TestCase):
self.assertEqual(error.exception.status_code, 404)
def test_theme_asset_returns_404_for_missing_or_traversal_paths(self):
settings.themesSelect = "themes/2023"
for asset_path in ("missing.js", "../../core/settings.py"):
with self.subTest(asset_path=asset_path):
with self.assertRaises(HTTPException) as error:
asyncio.run(theme_asset(asset_path))
self.assertEqual(error.exception.status_code, 404)
response = asyncio.run(
index(
request=SimpleNamespace(
url=SimpleNamespace(path="/theme-assets/missing.js")
)
)
)
self.assertEqual(response.status_code, 404)
def test_index_rewrites_assets_to_theme_specific_urls(self):
settings.themesSelect = "themes/2023"