Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: static path handling (#9281)

Dylan Madisettic3c62b687035
diff --git a/marimo/_server/api/endpoints/assets.py b/marimo/_server/api/endpoints/assets.py
index 14b1f1f741..3936cf05b7 100644
--- a/marimo/_server/api/endpoints/assets.py
+++ b/marimo/_server/api/endpoints/assets.py
@@ -595,7 +595,12 @@ async def serve_public_file(request: Request) -> Response:
@router.get("/{path:path}")
async def serve_static(request: Request) -> FileResponse:
path = str(request.path_params["path"])
- if any(re.match(pattern, path) for pattern in STATIC_FILES):
+ if any(re.fullmatch(pattern, path) for pattern in STATIC_FILES):
+ file_path = Path(path)
+ try:
+ PathValidator().validate_inside_directory(root, file_path)
+ except Exception:
+ raise HTTPException(status_code=404, detail="Not Found") from None
return FileResponse(root / path)
raise HTTPException(status_code=404, detail="Not Found")
diff --git a/tests/_server/api/endpoints/test_assets.py b/tests/_server/api/endpoints/test_assets.py
index 2a7e525847..217b2a829b 100644
--- a/tests/_server/api/endpoints/test_assets.py
+++ b/tests/_server/api/endpoints/test_assets.py
@@ -513,3 +513,47 @@ def test_index_lsp_workspace_fallback_to_cwd(client: TestClient) -> None:
document_uri = json.dumps(document_path.as_uri())
assert f'"rootUri": {root_uri}' in response.text
assert f'"documentUri": {document_uri}' in response.text
+
+
+def test_serve_static_path_traversal(client: TestClient) -> None:
+ # Prefix-match bypass (GHSA-3rj5-4vhf-pm45): re.match would allow
+ # "favicon.ico/../../anything" because it only checks the start of the
+ # string. re.fullmatch rejects it outright.
+ traversal_paths = [
+ "favicon.ico/../../etc/passwd",
+ "favicon.ico/../../../etc/passwd",
+ "manifest.json/../../etc/passwd",
+ "favicon.ico/%2e%2e/%2e%2e/etc/passwd",
+ "favicon.ico/evil",
+ "favicon.icoevil",
+ ]
+ for path in traversal_paths:
+ response = client.get(f"/{path}", follow_redirects=False)
+ assert response.status_code == 404, (
+ f"Expected 404 for traversal path {path!r}, got {response.status_code}"
+ )
+
+
+def test_serve_static_allowed_files(client: TestClient) -> None:
+ # Exact matches from STATIC_FILES should resolve without error (the file
+ # may not exist in the test environment, but we should not get a 404 from
+ # the allowlist check itself — a missing-file 404 from FileResponse is
+ # fine; what we're testing is that the allowlist admits the right names).
+ allowed = [
+ "favicon.ico",
+ "manifest.json",
+ "android-chrome-192x192.png",
+ "android-chrome-512x512.png",
+ "apple-touch-icon.png",
+ "logo.png",
+ ]
+ for name in allowed:
+ response = client.get(f"/{name}", follow_redirects=False)
+ # 200 if the file exists in the package, 404 if not — either is
+ # acceptable here; the important thing is it is NOT rejected by the
+ # traversal guard (which would also 404, but for a different reason).
+ # We verify the path was at least considered by checking it didn't
+ # fall through to the catch-all "Not Found" with an allowlist miss.
+ assert response.status_code in (200, 404), (
+ f"Unexpected status {response.status_code} for {name!r}"
+ )