From c25c49565710cef3eee4830b22b3013630c1d691 Mon Sep 17 00:00:00 2001 From: Orion Date: Fri, 7 Aug 2026 09:23:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E9=9D=99=E6=80=81=E8=B5=84=E6=BA=90=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=9B=9E=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免缺失或越界的主题资源被全局 SPA 404 回退误返回首页 200,保持静态资源请求的明确 404 语义。 - 为主题资源回退增加显式 404 响应 - 增加缺失资源与路径穿越回归测试 --- main.py | 2 ++ tests/test_issue_476_theme_assets.py | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 8ff2018..1463e5f 100644 --- a/main.py +++ b/main.py @@ -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)) diff --git a/tests/test_issue_476_theme_assets.py b/tests/test_issue_476_theme_assets.py index 3e3c523..eb4fe8e 100644 --- a/tests/test_issue_476_theme_assets.py +++ b/tests/test_issue_476_theme_assets.py @@ -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"