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: don't follow symlinks in assets.py (#9134)

Myles Scolnick046cc4291896
diff --git a/marimo/_server/api/endpoints/assets.py b/marimo/_server/api/endpoints/assets.py
index a9e60beb12..ff04d89127 100644
--- a/marimo/_server/api/endpoints/assets.py
+++ b/marimo/_server/api/endpoints/assets.py
@@ -529,14 +529,26 @@ async def serve_public_file(request: Request) -> Response:
public_dir = notebook_dir / "public"
file_path = public_dir / filepath
- # Security check: ensure file is inside public directory
+ # Security check: ensure file is inside public directory.
+ # validate_inside_directory preserves symlinks, so also verify the
+ # resolved target stays within public_dir to avoid symlinks in
+ # public/ pointing at files outside the notebook's public directory.
try:
PathValidator().validate_inside_directory(public_dir, file_path)
except HTTPException:
return Response(status_code=403, content="Access denied")
- if file_path.is_file():
- return FileResponse(file_path)
+ try:
+ resolved_file = file_path.resolve(strict=True)
+ resolved_public = public_dir.resolve(strict=True)
+ resolved_file.relative_to(resolved_public)
+ except (OSError, ValueError):
+ raise HTTPException(
+ status_code=404, detail="File not found"
+ ) from None
+
+ if resolved_file.is_file():
+ return FileResponse(resolved_file)
raise HTTPException(status_code=404, detail="File not found")
diff --git a/tests/_server/api/endpoints/test_assets.py b/tests/_server/api/endpoints/test_assets.py
index 0ec79e96b2..e5276df076 100644
--- a/tests/_server/api/endpoints/test_assets.py
+++ b/tests/_server/api/endpoints/test_assets.py
@@ -264,9 +264,19 @@ def test_public_file_security(client: TestClient) -> None:
)
assert response.status_code == 404
- # Test symlink attempt
+ # Symlinks in public/ that point outside public/ are rejected,
+ # even though the symlink itself lives inside public/.
response = client.get("/public/symlink.txt", headers=headers)
+ assert response.status_code == 404
+
+ # A symlink whose target is still inside public/ is allowed.
+ os.symlink(
+ str(public_dir / "safe.txt"),
+ str(public_dir / "inside_link.txt"),
+ )
+ response = client.get("/public/inside_link.txt", headers=headers)
assert response.status_code == 200
+ assert response.text == "public content"
finally:
# Cleanup