~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(security): preserve asset behavior when serving legacy static URLs (#6641)
LearningCircuit56ba45a9ed14
diff --git a/changelog.d/+runtime-security-hardening.breaking.md b/changelog.d/+runtime-security-hardening.breaking.mdindex cce2ce71da..6f33ebae6e 100644--- a/changelog.d/+runtime-security-hardening.breaking.md+++ b/changelog.d/+runtime-security-hardening.breaking.md@@ -12,6 +12,6 @@ previously answered 500 "An internal error occurred". `suggest-tags` alreadygave an accurate message, but as a 400 — the same status it uses for amalformed request — so a client couldn't tell "LLM not configured" apartfrom "your request body was invalid" without parsing the text.-`GET /redirect-static/<path>` now answers **404** rather than redirecting when-the path contains `.`, `..` or empty segments, a backslash, or a control-character; ordinary legacy paths such as `css/styles.css` still redirect.+`GET /redirect-static/<path>` now answers **404** when the path contains `.`,+`..` or empty segments, a backslash, or a control character; ordinary legacy+paths such as `css/styles.css` are served in place (200, no redirect).diff --git a/changelog.d/6641.security.md b/changelog.d/6641.security.mdnew file mode 100644index 0000000000..5629cb789b--- /dev/null+++ b/changelog.d/6641.security.md@@ -0,0 +1 @@+Legacy `/redirect-static/` URLs now serve assets directly using the same build lookup and cache policy as `/static/`. `/redirect-static/` rejects filenames containing path segments (`.`, `..`, empty segments), backslashes, or control characters; both `/static/` and `/redirect-static/` now refuse to serve any file that resolves outside the static directory, including one reached through a symlink. This means `/static/` itself 404s on a symlinked asset even though the file exists on disk, so installs that symlink static assets from elsewhere — for example a symlink-mode package install (`uv pip install --link-mode symlink`) or a symlinked `dist/` — must copy those assets into the static directory instead. Static responses served by either path also now send `Content-Disposition: inline` with the asset's filename.diff --git a/src/local_deep_research/web/dependencies/threadpool.py b/src/local_deep_research/web/dependencies/threadpool.pyindex 4230e32dbf..3f5a650239 100644--- a/src/local_deep_research/web/dependencies/threadpool.py+++ b/src/local_deep_research/web/dependencies/threadpool.py@@ -98,12 +98,15 @@ def wrap_sync_route_with_cleanup(dependant: "Dependant") -> None:here, so the bytes are already materialized. ``FileResponse`` is theexception, NOT covered by that claim: its ``__init__`` only setsheaders (starlette ``responses.py``); the file is opened and streamed- later, in its async ``__call__``. That distinction is moot for this- wrapper specifically -- every route in this app that returns a- ``FileResponse`` (``favicon``, ``serve_static`` in- ``web/fastapi_app.py``) is ``async def``, so this sync-only wrapper- never touches them -- but do not extend the "renders in ``__init__``"- claim to ``FileResponse`` if a future sync route ever returns one.+ later, in its async ``__call__``, i.e. AFTER this wrapper's cleanup.+ ``favicon`` and ``serve_static`` (``web/fastapi_app.py``) are+ ``async def``, so this sync-only wrapper never touches them. The+ legacy ``redirect_static`` shim (``web/routers/research.py``) is a+ plain ``def`` that returns one and so does pass through here; that is+ safe only because its body is a static file opened by path, needing no+ DB session or other resource this cleanup releases. Do not extend the+ "renders in ``__init__``" claim to ``FileResponse``: any sync route+ returning one must likewise not depend on request-scoped resources.Everything else (a plain dict, list, Pydantic model, ...) gets encodedhere, on this worker, before cleanup runs. This is NOT, contrary to anearlier version of this docstring, simply moving forward adiff --git a/src/local_deep_research/web/fastapi_app.py b/src/local_deep_research/web/fastapi_app.pyindex 7d57244895..f205cc0c3a 100644--- a/src/local_deep_research/web/fastapi_app.py+++ b/src/local_deep_research/web/fastapi_app.py@@ -631,7 +631,7 @@ class SecurityHeadersMiddleware:them itself; keeping one source stops the two copies drifting.Excludes the two conditional headers (HSTS, which needs a secure- scheme, and cache-control, which is skipped for /static/).+ scheme, and cache-control, which is skipped for static asset routes)."""return [(b"content-security-policy", cls.CSP.encode()),@@ -646,7 +646,7 @@ class SecurityHeadersMiddleware:@classmethoddef cache_headers(cls) -> list[tuple[bytes, bytes]]:- """No-store cache headers stamped on every non-``/static/`` response.+ """No-store cache headers stamped on responses outside static routes.Split out from ``unconditional_headers()`` (Cache-Control/Pragma/Expires are conditional on path, not truly unconditional) so the@@ -695,8 +695,12 @@ class SecurityHeadersMiddleware:))- # Cache control for non-static routes- if not path.startswith("/static/"):+ # Successful legacy assets use the shared static cache+ # policy. Errors retain the existing no-store headers.+ legacy_asset = path.startswith("/redirect-static/") and (+ message["status"] in (200, 206, 304)+ )+ if not path.startswith("/static/") and not legacy_asset:headers.extend(self.cache_headers())# Remove server header@@ -2049,15 +2053,12 @@ def _setup_rate_limiting(app: FastAPI) -> None:# Static file serving# ----------------------------------------------------------------------------_HASHED_FILENAME_RE = re.compile(r"\.[A-Za-z0-9_-]{8,}\.")-def _add_static_routes(app: FastAPI) -> None:"""Add static file serving routes with cache control."""- from ..security.path_validator import PathValidator+ from .static_files import static_file_responsestatic_dir = Path(STATIC_DIR)- dist_dir = static_dir / "dist"# Static assets must not share the API's default per-IP bucket: base.html# alone pulls ~29 assets, served must-revalidate, so a few page loads@@ -2077,73 +2078,9 @@ def _add_static_routes(app: FastAPI) -> None:@app.get("/static/{path:path}", include_in_schema=False)@_limiter.exemptasync def serve_static(path: str):- from fastapi.responses import FileResponse-- # Try dist directory first (Vite-built assets)- dist_prefix = "dist/"- if path.startswith(dist_prefix):- rel_path = path[len(dist_prefix) :]- try:- validated = PathValidator.validate_safe_path(- rel_path, dist_dir, allow_absolute=False- )- if validated and validated.is_file():- headers = {}- if _HASHED_FILENAME_RE.search(rel_path):- headers["Cache-Control"] = (- "public, max-age=31536000, immutable"- )- else:- headers["Cache-Control"] = (- "public, max-age=0, must-revalidate"- )- return FileResponse(str(validated), headers=headers)- except (ValueError, OSError):- # OSError too: is_file() stats the path, so a segment longer- # than the filesystem's NAME_MAX raises ENAMETOOLONG (errno- # 36), which `except ValueError` does not catch. This route- # is unauthenticated AND rate-limit exempt, so an uncaught- # one is an anonymous 500 for any long path.- pass-- # Try dist directory for Vite assets (fonts, etc.)- try:- validated = PathValidator.validate_safe_path(- path, dist_dir, allow_absolute=False- )- if validated and validated.is_file():- headers = {}- if _HASHED_FILENAME_RE.search(path):- headers["Cache-Control"] = (- "public, max-age=31536000, immutable"- )- else:- headers["Cache-Control"] = (- "public, max-age=0, must-revalidate"- )- return FileResponse(str(validated), headers=headers)- except (ValueError, OSError):- # is_file() can raise OSError (ENAMETOOLONG), not just- # ValueError -- see the note on the first handler above.- pass-- # Fall back to regular static directory- try:- validated = PathValidator.validate_safe_path(- path, static_dir, allow_absolute=False- )- if validated and validated.is_file():- return FileResponse(- str(validated),- headers={- "Cache-Control": "public, max-age=0, must-revalidate"- },- )- except (ValueError, OSError):- # is_file() can raise OSError (ENAMETOOLONG), not just- # ValueError -- see the note on the first handler above.- pass-+ response = static_file_response(path)+ if response is not None:+ return responsereturn JSONResponse({"error": "Not found"}, status_code=404)diff --git a/src/local_deep_research/web/routers/research.py b/src/local_deep_research/web/routers/research.pyindex b207d56a5b..6e764adafc 100644--- a/src/local_deep_research/web/routers/research.py+++ b/src/local_deep_research/web/routers/research.py@@ -1,7 +1,6 @@from fastapi import APIRouter, Depends, HTTPException, Requestfrom fastapi.responses import (JSONResponse,- RedirectResponse,)from python_multipart.exceptions import FormParserErrorfrom starlette.exceptions import HTTPException as StarletteHTTPException@@ -109,37 +108,17 @@ router = APIRouter(tags=["research"])# fastapi_app.py. A `:path` converter cannot be represented in OpenAPI, so a# schema-included route using one fails test_route_contracts.py's# "every schema-included APIRoute appears in OpenAPI" check. This is a legacy-# redirect shim for bookmarked URLs, not part of the documented API surface,+# shim for bookmarked URLs, not part of the documented API surface,# so excluding it is correct rather than a workaround.@router.get("/redirect-static/{path:path}", include_in_schema=False)def redirect_static(path: str):- """Redirect old static URLs to new static URLs.+ """Serve legacy URLs with the same asset and cache policy as /static/.- Two things here are load-bearing and were both wrong in the initial port,- which left the route enumerable (so route-parity checks passed) but- functionally dead:-- * ``{path:path}`` — Flask's ``<path:path>`` matches slashes; Starlette's- plain ``{path}`` does not. Every realistic legacy URL has at least one- (``css/styles.css``), so they all 404'd.- * the captured ``path`` must actually be used. The port ignored it and- redirected to a bare ``/static``, dropping the filename even in the- single-segment case.-- Flask did ``redirect(url_for("static", filename=path))``.+ Serving in place removes the user-controlled Location header (CodeQL+ alert #8204). The shared resolver confines files to the static root,+ prefers built assets and rejects symlink escapes and missing files."""- # Quote so a `?` or `#` in a legacy filename cannot truncate the target- # into a query or fragment. `safe="/"` keeps directory separators intact.- #- # `lstrip("/")` plus the fixed `/static/` prefix already kept the target- # on this origin (a protocol-relative `//host` needs a leading `//`, and- # both slashes are stripped) — that part is unchanged. What the guard- # below adds is canonicalisation: `..` and `.` segments normalize out of- # `/static` in the client, and clients/proxies do not all treat- # backslashes, empty segments and control characters identically. Reject- # those ambiguous forms so every accepted target is a canonical static- # path.- from urllib.parse import quote+ from ..static_files import static_file_responsepath = path.lstrip("/")segments = path.split("/")@@ -159,9 +138,10 @@ def redirect_static(path: str):# browser to the one audience the route has.raise HTTPException(status_code=404, detail="Not found")- return RedirectResponse(- url=f"/static/{quote(path, safe='/')}", status_code=302- )+ response = static_file_response(path)+ if response is None:+ raise HTTPException(status_code=404, detail="Not found")+ return response@router.get("/progress/{research_id}")diff --git a/src/local_deep_research/web/static_files.py b/src/local_deep_research/web/static_files.pynew file mode 100644index 0000000000..c59ae593d1--- /dev/null+++ b/src/local_deep_research/web/static_files.py@@ -0,0 +1,68 @@+"""Shared asset lookup for current and legacy static URLs."""++import re+from pathlib import Path++from fastapi.responses import FileResponse++from ..security.path_validator import PathValidator+++_HASHED_FILENAME_RE = re.compile(r"\.[A-Za-z0-9_-]{8,}\.")+_REVALIDATE = "public, max-age=0, must-revalidate"+_IMMUTABLE = "public, max-age=31536000, immutable"+++def static_file_response(path: str) -> FileResponse | None:+ """Serve a contained asset, preferring Vite output over source files.++ The root is always the app's ``STATIC_DIR``, read per call; there is+ deliberately no directory parameter, so no caller can point this+ helper at another tree (e.g. the shared research-outputs directory).++ Explicit ``dist/`` URLs and implicit build paths use the same cache+ policy. Invalid paths, failed lookups and symlink escapes are misses;+ callers retain their route's existing 404 response format.+ """+ from .fastapi_app import STATIC_DIR++ try:+ root = Path(STATIC_DIR).resolve(strict=True)+ except (ValueError, OSError, RuntimeError):+ return None++ dist = root / "dist"+ lookups = []+ if path.startswith("dist/"):+ lookups.append((dist, path[len("dist/") :], True))+ lookups.extend(((dist, path, True), (root, path, False)))++ for base, relative_path, is_dist in lookups:+ try:+ validated = PathValidator.validate_safe_path(+ relative_path, base, allow_absolute=False+ )+ if validated is None:+ continue+ candidate = validated.resolve(strict=True)+ if not candidate.is_relative_to(root) or not candidate.is_file():+ continue+ except (ValueError, OSError, RuntimeError):+ # Includes NUL bytes, overlong names, missing files and loops.+ continue++ cache_control = (+ _IMMUTABLE+ if is_dist and _HASHED_FILENAME_RE.search(relative_path)+ else _REVALIDATE+ )+ return FileResponse(+ str(candidate),+ # Infer the asset type from its public name while opening only+ # the resolved, contained target. Static assets render inline.+ filename=validated.name,+ content_disposition_type="inline",+ headers={"Cache-Control": cache_control},+ )++ return Nonediff --git a/tests/deployment/test_deployment_surface_contracts.py b/tests/deployment/test_deployment_surface_contracts.pyindex 24fea74719..0fb4a72e6e 100644--- a/tests/deployment/test_deployment_surface_contracts.py+++ b/tests/deployment/test_deployment_surface_contracts.py@@ -1104,7 +1104,10 @@ class TestReverseProxyDoc:"the proxy; that only holds while the app sends this exact one")cache = "public, max-age=31536000, immutable"- assert cache in doc and f'"{cache}"' in app_source+ asset_source = FASTAPI_APP_PY.with_name("static_files.py").read_text(+ encoding="utf-8"+ )+ assert cache in doc and f'"{cache}"' in asset_sourcedef test_documented_trust_toggle_matches_both_readers(self):doc = REVERSE_PROXY_DOC.read_text(encoding="utf-8")diff --git a/tests/security/test_auth_dependencies_fastapi.py b/tests/security/test_auth_dependencies_fastapi.pyindex 67bca678bc..7c4b059cde 100644--- a/tests/security/test_auth_dependencies_fastapi.py+++ b/tests/security/test_auth_dependencies_fastapi.py@@ -263,7 +263,7 @@ PUBLIC_ROUTES: dict[tuple[str, str], str] = {"the static-asset handler — CSS/JS for the login page itself."),("GET", "/redirect-static/{path:path}"): (- "compatibility shim that 302s to /static/<path>; serves no user data."+ "compatibility shim that serves static assets at legacy URLs; no user data."),}diff --git a/tests/security/test_cross_user_isolation_census.py b/tests/security/test_cross_user_isolation_census.pyindex 66f0ab71fb..e30e809ae4 100644--- a/tests/security/test_cross_user_isolation_census.py+++ b/tests/security/test_cross_user_isolation_census.py@@ -153,7 +153,8 @@ PUBLIC_ROUTES = frozenset(# requiring auth would make logout un-callable from a dead session.("POST", "/auth/logout"),("GET", "/auth/check"),- # Legacy static-asset redirect; serves no user data.+ # Legacy static-asset URL; serves the asset directly (no redirect),+ # no user data.("GET", "/redirect-static/{path:path}"),})diff --git a/tests/security/test_unauthenticated_reachability_census.py b/tests/security/test_unauthenticated_reachability_census.pyindex e4f2c50bf2..6040d41529 100644--- a/tests/security/test_unauthenticated_reachability_census.py+++ b/tests/security/test_unauthenticated_reachability_census.py@@ -549,7 +549,8 @@ DECLARED_PUBLIC = frozenset(# requiring auth would make logout un-callable from a dead session.("POST", "/auth/logout"),("GET", "/auth/check"),- # Legacy static-asset redirect; serves no user data.+ # Legacy static-asset URL; serves the asset directly (no redirect),+ # no user data.("GET", "/redirect-static/{path:path}"),# Registered directly on the app object in fastapi_app.py:# the root page checks request.session itself and 302s todiff --git a/tests/web/routers/test_redirect_static.py b/tests/web/routers/test_redirect_static.pyindex 3ba6b13a15..464e05186a 100644--- a/tests/web/routers/test_redirect_static.py+++ b/tests/web/routers/test_redirect_static.py@@ -1,4 +1,4 @@-"""``/redirect-static/<path>`` must preserve the path it was given.+"""``/redirect-static/<path>`` must serve the asset it names.This route exists so bookmarked or externally-linked legacy static URLs keepworking. The initial FastAPI port broke it in two independent ways while@@ -11,7 +11,11 @@ while the behaviour was gone:* the handler ignored the captured parameter entirely and redirected to abare ``/static``, dropping the filename even for single-segment paths.-Flask's version was ``redirect(url_for("static", filename=path))``.+It later became a hand-built 302 to ``/static/<path>``; that put a+remote-controlled string into the ``Location`` header (CodeQL+``py/unvalidated-url-redirection``, alert #8204), so the shim now serves the+asset directly at the legacy URL — same outcome for a bookmarked link, one+response class fewer, and the filesystem decides what is served."""import pytest@@ -30,51 +34,106 @@ def client():["css/styles.css","js/components/details.js",- "favicon.ico",+ "favicon.png","js/services/socket.js",- "css/themes/dark.css",+ "css/components/pagination.css",],)-def test_multi_segment_paths_redirect_with_path_intact(client, legacy_path):- """The whole path survives, slashes included."""- resp = client.get(f"/redirect-static/{legacy_path}", follow_redirects=False)+def test_multi_segment_paths_serve_the_named_asset(client, legacy_path):+ """The legacy URL serves the same asset the /static/ mount would."""+ direct = client.get(f"/static/{legacy_path}")+ assert direct.status_code == 200, (+ f"asset {legacy_path} not present in this checkout"+ )++ resp = client.get(f"/redirect-static/{legacy_path}")- assert resp.status_code == 302, (+ assert resp.status_code == 200, (f"/redirect-static/{legacy_path} returned {resp.status_code}; a "- f"legacy static URL must redirect, not 404"- )- assert resp.headers["location"] == f"/static/{legacy_path}", (- f"redirect dropped or mangled the path: {resp.headers['location']!r}"+ f"legacy static URL must serve its asset, not 404")+ assert resp.content == direct.content+ assert resp.headers["content-type"] == direct.headers["content-type"]-def test_redirect_target_stays_on_this_origin(client):- """A leading slash in the captured path must not produce a- protocol-relative (``//host``) off-site redirect."""- resp = client.get(- "/redirect-static//evil.example.com/x.css", follow_redirects=False- )+def test_traversal_out_of_the_static_root_is_refused(client):+ """``../`` segments must never escape the static root.- location = resp.headers.get("location", "")- assert location.startswith("/static/"), location- assert not location.startswith("//"), (- f"redirect target is protocol-relative and would leave this origin: "- f"{location!r}"- )+ The dot segments are percent-encoded because httpx removes literal+ ``..`` client-side, so a plain ``../`` payload would 404 at routing and+ never reach the handler. ``%2e%2e`` is sent verbatim and decoded to+ ``..`` by the server, so the route matches and the handler must refuse+ it. The last payload resolves back inside the static root to an asset+ that exists, so only the handler's own guard can turn it into a 404.+ """+ for legacy_path in (+ "%2e%2e/web/templates/pages/details.html",+ "css/%2e%2e/%2e%2e/routers/research.py",+ "css/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd",+ "css/%2e%2e/css/styles.css",+ ):+ resp = client.get(f"/redirect-static/{legacy_path}")+ assert resp.status_code == 404, (+ f"traversal payload {legacy_path!r} returned "+ f"{resp.status_code}, not 404"+ )+++def test_directories_and_missing_files_are_404(client):+ """Only real files inside the static root are served."""+ assert client.get("/redirect-static/css").status_code == 404+ assert client.get("/redirect-static/no/such/file.css").status_code == 404+ assert client.get("/redirect-static/").status_code == 404+++def test_no_redirect_is_involved(client):+ """The shim answers directly; no Location header is produced.++ Pins the #8204 fix at the contract level: no remote-controlled string+ ever reaches a redirect target.+ """+ resp = client.get("/redirect-static/css/styles.css")+ if resp.status_code == 404:+ pytest.skip("css/styles.css not present in this checkout")+ assert "location" not in resp.headers-def test_query_and_fragment_characters_cannot_truncate_the_target(client):- """``?`` / ``#`` in a filename must be encoded, not treated as syntax."""- resp = client.get(- "/redirect-static/css/a%3Fb%23c.css", follow_redirects=False- )+def test_hostile_static_trees_yield_404_not_500(app, tmp_path, monkeypatch):+ """Pathological filesystem states must 404, never raise.- location = resp.headers.get("location", "")- assert location.startswith("/static/css/"), location- assert "?" not in location and "#" not in location, (- f"unencoded query/fragment character survived into the redirect "- f"target: {location!r}"- )+ The shim resolves the request path against STATIC_DIR on the fly, so a+ static tree containing a symlink loop (resolve() raises RuntimeError) or+ a symlink escaping the root must degrade to Not Found instead of a 500.+ STATIC_DIR is read per-request by the route, so monkeypatching the+ module attribute is enough.+ """+ from fastapi.testclient import TestClient++ from local_deep_research.web import fastapi_app++ (tmp_path / "ok.css").write_text("body{color:red}", encoding="utf-8")+ (tmp_path / "loop.css").symlink_to(tmp_path / "loop.css")+ outside = tmp_path.parent / f"{tmp_path.name}-outside.css"+ outside.write_text("outside-static-root", encoding="utf-8")+ (tmp_path / "escape.css").symlink_to(outside)+ monkeypatch.setattr(fastapi_app, "STATIC_DIR", str(tmp_path))++ client = TestClient(app, raise_server_exceptions=False)++ served = client.get("/redirect-static/ok.css")+ assert served.status_code == 200+ assert served.text == "body{color:red}"++ for hostile in (+ "loop.css",+ "escape.css",+ "missing.css",+ "ok.css%00ignored",+ ):+ resp = client.get(f"/redirect-static/{hostile}")+ assert resp.status_code == 404, (+ f"{hostile}: expected 404, got {resp.status_code}"+ )@pytest.mark.parametrize(diff --git a/tests/web/test_route_table_parity.py b/tests/web/test_route_table_parity.pyindex ed03ddb3a1..cf8aaac0e6 100644--- a/tests/web/test_route_table_parity.py+++ b/tests/web/test_route_table_parity.py@@ -1602,6 +1602,12 @@ EXPECTED_AUTH_GATE_ADDED = [# Status codes the Flask view could return that no FastAPI counterpart can.# {(METHOD, normalised path): [codes]}.EXPECTED_STATUS_CODES_LOST = {+ # The legacy static shim intentionally serves assets in place (#6641),+ # removing the Location header. Existing files now return 200 with the+ # same asset and cache policy as /static/; missing or invalid paths use+ # the shared 404 handler. Runtime parity and no-redirect coverage live+ # in test_static_file_serving.py and routers/test_redirect_static.py.+ ("GET", "/redirect-static/{:path}"): [302],# --- The notes blueprint's oversized-body 413, on non-mutating methods.# Flask armed it with a blueprint-wide `@notes_bp.before_request`# (`_reject_oversized_bodies`), which ran for EVERY method. FastAPI@@ -1686,8 +1692,8 @@ EXPECTED_STATUS_CODES_GAINED = {# The legacy static-URL shim now rejects paths that a client or proxy# could normalize outside `/static/` (dot/empty segments, backslashes,# control characters) instead of redirecting them. Flask redirected- # unconditionally, so this 404 is new; ordinary legacy paths such as- # `css/styles.css` still 302. Raised as `HTTPException(404)` so the app's+ # unconditionally, so this 404 is new; existing assets are now served+ # directly with 200 (#6641). Raised as `HTTPException(404)` so the app's# shared 404 handler renders it. web/routers/research.py:redirect_static.("GET", "/redirect-static/{:path}"): [404],# An explicit `is_configured` pre-check before any Zotero network call.diff --git a/tests/web/test_security_headers_matrix.py b/tests/web/test_security_headers_matrix.pyindex d03205b1b2..f18a6eaabc 100644--- a/tests/web/test_security_headers_matrix.py+++ b/tests/web/test_security_headers_matrix.py@@ -191,15 +191,13 @@ _RESPONSE_CLASSES = {200,),"302 redirect": (- lambda c: c.get(- "/redirect-static/css/styles.css", follow_redirects=False- ),- 302,- ),- "302 auth redirect": (lambda c: c.get("/settings/", follow_redirects=False),302,),+ "200 static asset (legacy shim)": (+ lambda c: c.get("/redirect-static/css/styles.css"),+ 200,+ ),"404 HTML branch": (lambda c: c.get("/no-such-route-sec-matrix"), 404),"404 JSON branch": (lambda c: c.get("/api/no-such-route-sec-matrix"), 404),"403 CSRF short-circuit": (lambda c: c.post("/auth/logout"), 403),@@ -263,22 +261,28 @@ class TestStampedHeadersAppearExactlyOnce:@pytest.mark.parametrize("label", sorted(_RESPONSE_CLASSES))def test_each_cache_header_exactly_once(self, label, http_client):- """None of these paths is under ``/static/``, so all three- no-store headers apply to all of them."""+ """Successful legacy assets cache; dynamic/error responses do not."""resp = _produce(http_client, label)counts = {name: len(_raw(resp, name)) for name in _CACHE_HEADERS}- assert counts == dict.fromkeys(_CACHE_HEADERS, 1), (- f"{label}: every cache header must appear exactly once; "- f"got {counts!r}"+ expected = (+ {"cache-control": 1, "pragma": 0, "expires": 0}+ if label == "200 static asset (legacy shim)"+ else dict.fromkeys(_CACHE_HEADERS, 1)+ )+ assert counts == expected, (+ f"{label}: unexpected cache header multiplicity; got {counts!r}")- def test_static_keeps_only_its_own_single_cache_control(self, http_client):+ @pytest.mark.parametrize("prefix", ["static", "redirect-static"])+ def test_static_keeps_only_its_own_single_cache_control(+ self, http_client, prefix+ ):"""The carve-out's job is to leave ``serve_static``'s owncaching policy intact. If the middleware appended its no-storehere, the asset would carry two ``Cache-Control`` headers whoseunion is no-store, quietly destroying static caching — theappend-vs-replace failure with a real cost attached."""- resp = http_client.get(_STATIC_PATH)+ resp = http_client.get(_STATIC_PATH.replace("/static/", f"/{prefix}/"))assert resp.status_code == 200, (f"{_STATIC_PATH} did not serve; this test needs a real static "f"asset to be meaningful (got {resp.status_code})"diff --git a/tests/web/test_static_file_serving.py b/tests/web/test_static_file_serving.pyindex 678c50e495..220af6e8ec 100644--- a/tests/web/test_static_file_serving.py+++ b/tests/web/test_static_file_serving.py@@ -26,9 +26,8 @@ What is covered here* ``Accept-Ranges`` / 206 / 416 / multipart byteranges / ``If-Range``;* the 404 path (missing asset, directory, empty path) and the``dist/`` cache-control branches;-* the ``/redirect-static/<path>`` legacy shim actually round-trips- (``tests/web/routers/test_redirect_static.py`` pins the ``Location``- string; nothing pinned that following it yields the asset).+* the ``/redirect-static/<path>`` legacy shim serves the same asset,+ content type and cache policy without a redirect.Two test styles are used:@@ -127,6 +126,10 @@ def isolated_static(tmp_path, monkeypatch):(static_dir / "favicon.ico").write_bytes(b"\x00\x00\x01\x00icodata")(static_dir / "blob.bin").write_bytes(bytes(range(256)) * 40)+ (static_dir / "js").mkdir()+ (static_dir / "js" / "plain.js").write_text("var unbuilt=3;\n")+ (static_dir / "js" / "source.abcdef123456.js").write_text("var source=4;\n")+dist = static_dir / "dist"(dist / "js").mkdir(parents=True)(dist / "js" / "app.abcdef123456.js").write_text("var hashed=1;\n")@@ -142,6 +145,14 @@ def isolated_static(tmp_path, monkeypatch):monkeypatch.setattr(fastapi_app_module, "STATIC_DIR", str(static_dir))sub = FastAPI()fastapi_app_module._add_static_routes(sub)+ from local_deep_research.web.routers.research import redirect_static++ sub.add_api_route("/redirect-static/{path:path}", redirect_static)+ sub.add_middleware(fastapi_app_module.SecurityHeadersMiddleware)++ @sub.get("/private-cache-probe")+ def private_cache_probe():+ return {"private": "fixture"}return SimpleNamespace(client=TestClient(sub, raise_server_exceptions=False),@@ -271,43 +282,15 @@ class TestTraversalAgainstARealEscapeTarget:SENTINEL,)- @pytest.mark.xfail(- strict=True,- reason=(- "serve_static resolves paths lexically (werkzeug safe_join) "- "and then stats the join with is_file(), which follows "- "symlinks -- so a link inside the served root that points "- "outside it serves the out-of-root file. Parity with main, "- "not a migration regression, but starlette's own StaticFiles "- "(unused by this route) refuses exactly this via realpath + "- "commonpath in lookup_path()."- ),- )- def test_symlink_escape_is_refused(self, isolated_static, validator_spy):- """A symlink inside the served root that points outside it.-- ``safe_join`` is purely lexical -- it never calls ``realpath`` --- and the handler stats the joined path with ``is_file()``, which- follows the link. So the file OUTSIDE the root is served with a- 200.-- This is parity with main (werkzeug's ``send_from_directory`` is- lexical too), i.e. NOT a migration regression -- but it is a real- containment gap, and the port had a free fix available: Starlette's- ``StaticFiles.lookup_path`` resolves ``realpath`` and compares- ``commonpath`` against the root, refusing exactly this. The- hand-written handler that replaced Flask's route does not.-- Strict-xfail: when the containment check is added this test- starts passing, pytest reports XPASS as a failure, and that is- the signal to drop the marker.- """+ @pytest.mark.parametrize("prefix", ["static", "redirect-static"])+ def test_symlink_escape_is_refused(self, isolated_static, prefix):+ """Both routes reject a symlink to a readable file outside the root."""link = isolated_static.static_dir / "escape"link.symlink_to(isolated_static.outside, target_is_directory=True)# Control: the link really does resolve outside the root.assert (link / "secret.txt").resolve() == isolated_static.secret- resp = isolated_static.client.get("/static/escape/secret.txt")+ resp = isolated_static.client.get(f"/{prefix}/escape/secret.txt")assert SENTINEL not in resp.text, (f"symlink escape served the out-of-root file with status "f"{resp.status_code}"@@ -833,21 +816,177 @@ class TestDistBranches:)+def test_static_file_response_can_only_serve_static_dir(isolated_static):+ """The shared helper takes no directory, so no caller can point it at+ another tree (e.g. the research-outputs directory) and bypass the+ reports-dir fence that allowlists ``static_files.py``."""+ import inspect++ from local_deep_research.web.static_files import static_file_response++ assert list(inspect.signature(static_file_response).parameters) == ["path"]+ assert static_file_response("css/site.css") is not None+ assert static_file_response("../outside/secret.txt") is None++# ---------------------------------------------------------------------------# Legacy /redirect-static shim# ----------------------------------------------------------------------------def test_legacy_redirect_static_round_trips_to_a_real_asset(app):- """``tests/web/routers/test_redirect_static.py`` pins the ``Location``- header; nothing pinned that following it actually yields the asset."""+def test_legacy_redirect_static_serves_the_real_asset(app):+ """``tests/web/routers/test_redirect_static.py`` pins the response; this+ proves the served bytes are the real asset the /static/ mount serves."""client = TestClient(app, raise_server_exceptions=False)- hop = client.get("/redirect-static/css/styles.css", follow_redirects=False)- assert hop.status_code == 302, hop.status_code- assert hop.headers["location"] == "/static/css/styles.css"+ legacy = client.get("/redirect-static/css/styles.css")+ assert legacy.status_code == 200, legacy.status_code+ assert legacy.headers["content-type"] == "text/css; charset=utf-8"++ direct = client.get("/static/css/styles.css")+ assert direct.status_code == 200, direct.status_code+ assert legacy.content == direct.content- followed = client.get(- "/redirect-static/css/styles.css", follow_redirects=True++class TestLegacyStaticAssetParity:+ @pytest.mark.parametrize(+ "path, expected, cache_control",+ [+ (+ "css/site.css",+ b"body{color:red}\n",+ "public, max-age=0, must-revalidate",+ ),+ (+ "fonts/inter.woff2",+ b"woff2-payload",+ "public, max-age=0, must-revalidate",+ ),+ (+ "dist/fonts/inter.woff2",+ b"woff2-payload",+ "public, max-age=0, must-revalidate",+ ),+ (+ "js/plain.js",+ b"var plain=2;\n",+ "public, max-age=0, must-revalidate",+ ),+ (+ "dist/js/plain.js",+ b"var plain=2;\n",+ "public, max-age=0, must-revalidate",+ ),+ (+ "js/app.abcdef123456.js",+ b"var hashed=1;\n",+ "public, max-age=31536000, immutable",+ ),+ (+ "dist/js/app.abcdef123456.js",+ b"var hashed=1;\n",+ "public, max-age=31536000, immutable",+ ),+ (+ "js/source.abcdef123456.js",+ b"var source=4;\n",+ "public, max-age=0, must-revalidate",+ ),+ ],+ )+ def test_assets_and_cache_policy_match(+ self, isolated_static, path, expected, cache_control+ ):+ direct = isolated_static.client.get(f"/static/{path}")+ legacy = isolated_static.client.get(+ f"/redirect-static/{path}", follow_redirects=False+ )+ assert direct.status_code == legacy.status_code == 200+ assert direct.content == legacy.content == expected+ for response in (direct, legacy):+ assert response.headers.get_list("cache-control") == [cache_control]+ assert "pragma" not in response.headers+ assert "expires" not in response.headers+ assert response.headers["x-content-type-options"] == "nosniff"+ assert "location" not in response.headers+ assert direct.headers["content-type"] == legacy.headers["content-type"]++ @pytest.mark.parametrize("prefix", ["static", "redirect-static"])+ @pytest.mark.parametrize(+ "path",+ [+ "css/site.css%00ignored",+ "css/" + "x" * 300 + ".css",+ "%2e%2e/outside/secret.txt",+ "css/%2e%2e/%2e%2e/outside/secret.txt",+ "dist/%2e%2e/%2e%2e/outside/secret.txt",+ "loop.css",+ ],)- assert followed.status_code == 200, followed.status_code- assert followed.headers["content-type"] == "text/css; charset=utf-8"+ def test_invalid_paths_are_404(self, isolated_static, prefix, path):+ root = isolated_static.static_dir+ (root / "loop.css").symlink_to(root / "loop.css")+ response = isolated_static.client.get(+ f"/{prefix}/{path}", follow_redirects=False+ )+ assert response.status_code == 404+ assert SENTINEL not in response.text+ assert "location" not in response.headers++ @pytest.mark.parametrize("prefix", ["static", "redirect-static"])+ @pytest.mark.parametrize("path", ["escape.txt", "dist/escape.txt"])+ def test_built_asset_symlinks_cannot_escape(+ self, isolated_static, prefix, path+ ):+ link = isolated_static.static_dir / "dist" / "escape.txt"+ link.symlink_to(isolated_static.secret)+ assert link.read_text().strip() == SENTINEL+ response = isolated_static.client.get(f"/{prefix}/{path}")+ assert response.status_code == 404+ assert SENTINEL not in response.text++ @pytest.mark.parametrize("prefix", ["static", "redirect-static"])+ def test_internal_symlinks_keep_the_asset_content_type(+ self, isolated_static, prefix+ ):+ root = isolated_static.static_dir+ (root / "styles.bin").write_text("body{color:blue}")+ (root / "alias.css").symlink_to(root / "styles.bin")+ response = isolated_static.client.get(f"/{prefix}/alias.css")+ assert response.status_code == 200+ assert response.text == "body{color:blue}"+ assert response.headers["content-type"].startswith("text/css")+ assert (+ response.headers["cache-control"]+ == "public, max-age=0, must-revalidate"+ )++ @pytest.mark.parametrize(+ "path, status",+ [("/private-cache-probe", 200), ("/redirect-static/missing.css", 404)],+ )+ def test_non_asset_responses_remain_uncacheable(+ self, isolated_static, path, status+ ):+ response = isolated_static.client.get(path)+ assert response.status_code == status+ assert response.headers.get_list("cache-control") == [+ "no-store, no-cache, must-revalidate, max-age=0"+ ]+ assert response.headers.get_list("pragma") == ["no-cache"]+ assert response.headers.get_list("expires") == ["0"]++ @pytest.mark.parametrize("prefix", ["static", "redirect-static"])+ def test_partial_assets_keep_the_same_cache_policy(+ self, isolated_static, prefix+ ):+ response = isolated_static.client.get(+ f"/{prefix}/js/app.abcdef123456.js",+ headers={"Range": "bytes=0-2"},+ )+ assert response.status_code == 206+ assert response.content == b"var"+ assert response.headers.get_list("cache-control") == [+ "public, max-age=31536000, immutable"+ ]+ assert "pragma" not in response.headers+ assert "expires" not in response.headersdiff --git a/tests/web/test_vite_asset_pipeline.py b/tests/web/test_vite_asset_pipeline.pyindex 94a609e565..68309b7781 100644--- a/tests/web/test_vite_asset_pipeline.py+++ b/tests/web/test_vite_asset_pipeline.py@@ -419,7 +419,10 @@ class TestManifestPathContract:every built asset silently drops to ``must-revalidate``."""pattern = re.compile(- _literal_module_regex(FASTAPI_APP_PY, "_HASHED_FILENAME_RE")+ _literal_module_regex(+ FASTAPI_APP_PY.with_name("static_files.py"),+ "_HASHED_FILENAME_RE",+ ))config = VITE_CONFIG_JS.read_text(encoding="utf-8")assert "js/[name].[hash].js" in config