From d5422b0ecd391ec3da7577613266434347f56cef Mon Sep 17 00:00:00 2001 From: yml Date: Mon, 23 Nov 2026 16:56:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(http):=20=E4=BF=9D=E7=95=99=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E5=93=8D=E5=BA=94=E7=BC=93=E5=AD=98=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_middleware_websocket.py | 55 ++++++++++++++++++++++++++++++ web/backend/main.py | 18 +++++----- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/tests/test_middleware_websocket.py b/tests/test_middleware_websocket.py index 0a918df..51d8386 100644 --- a/tests/test_middleware_websocket.py +++ b/tests/test_middleware_websocket.py @@ -35,3 +35,58 @@ def test_websocket_bypasses_http_middleware(): {"type": "websocket.accept"}, {"type": "websocket.close", "code": 1000}, ] + + +async def _http_app(scope, receive, send): + assert scope["type"] == "http" + await send( + { + "type": "http.response.start", + "status": 200, + "headers": [ + (b"cache-control", b"no-store, max-age=0"), + (b"x-frame-options", b"SAMEORIGIN"), + ], + } + ) + await send({"type": "http.response.body", "body": b"ok"}) + + +def _run_http(path: str) -> dict: + messages: list[dict] = [] + + async def send(message): + messages.append(message) + + async def receive(): + return {"type": "http.request", "body": b"", "more_body": False} + + scope = { + "type": "http", + "method": "GET", + "path": path, + "headers": [], + "scheme": "http", + "server": ("testserver", 80), + "client": ("testclient", 123), + "query_string": b"", + } + asyncio.run(SecurityHeadersMiddleware(_http_app)(scope, receive, send)) + return messages[0] + + +def test_http_response_keeps_application_cache_control(): + message = _run_http("/api/accounts") + headers = dict(message["headers"]) + + assert headers[b"cache-control"] == b"no-store, max-age=0" + assert headers[b"x-frame-options"] == b"DENY" + assert headers[b"x-content-type-options"] == b"nosniff" + + +def test_asset_response_gets_immutable_cache_control(): + message = _run_http("/assets/index-abc123.js") + headers = dict(message["headers"]) + + assert headers[b"cache-control"] == b"public, max-age=31536000, immutable" + assert headers[b"x-frame-options"] == b"DENY" diff --git a/web/backend/main.py b/web/backend/main.py index 6ed9f9d..a22dd99 100644 --- a/web/backend/main.py +++ b/web/backend/main.py @@ -118,19 +118,21 @@ class SecurityHeadersMiddleware: await self.app(scope, receive, send) return path = str(scope.get("path") or "") + is_asset = path.startswith("/assets/") async def send_wrapper(message: Any): if message["type"] == "http.response.start": + replaced_headers = { + b"x-content-type-options", + b"x-frame-options", + b"referrer-policy", + } + if is_asset: + replaced_headers.add(b"cache-control") headers = [ (key, value) for key, value in message.get("headers", []) - if key - not in { - b"x-content-type-options", - b"x-frame-options", - b"referrer-policy", - b"cache-control", - } + if key not in replaced_headers ] headers.extend( [ @@ -142,7 +144,7 @@ class SecurityHeadersMiddleware: ), ] ) - if path.startswith("/assets/"): + if is_asset: headers.append( ( b"cache-control",