diff --git a/server/app.py b/server/app.py index 31a6f99..b183564 100644 --- a/server/app.py +++ b/server/app.py @@ -32,7 +32,7 @@ from sqlalchemy.orm import Session from starlette.middleware.sessions import SessionMiddleware from .db import Base, engine, get_db -from . import models, auth, notify, assets_db, okta_auth, okta_fake +from . import models, auth, notify, assets_db, okta_auth, okta_fake, files_http log = logging.getLogger("wpsuite.app") @@ -2931,10 +2931,9 @@ def get_wp_file(file_id: str, user: models.User = Depends(auth.get_current_user) if not row: raise HTTPException(status_code=404, detail="File not found") require_project_access(db, user, row.project_id) - return Response(content=row.data, media_type=row.mime or "application/octet-stream", - headers={"Content-Disposition": - f'inline; filename="{(row.name or "file").replace(chr(34), "")}"', - "Cache-Control": "private, max-age=86400"}) + # D19: PDF and raster images inline as before; anything else (SVG included) + # downloads as a sandboxed attachment, and every name is a valid header. + return files_http.file_response(row) @app.patch("/api/files/{file_id}") diff --git a/server/files_http.py b/server/files_http.py new file mode 100644 index 0000000..e5c8a17 --- /dev/null +++ b/server/files_http.py @@ -0,0 +1,66 @@ +"""Safe HTTP responses for uploaded drawings — D19 (T16.2). + +Security review 2026-10-01, SEC-01 and SEC-15e. + +WHY THIS EXISTS + Uploads accept `image/*`, which includes `image/svg+xml`. An SVG is a + document, not a picture: it can carry ') + check("D19", "an SVG upload is still accepted (decided: accept, download only)", code == 200, f"status {code}") + r = as_member.get(f"/api/files/{svg}") + disp, csp = r.headers.get("content-disposition", ""), r.headers.get("content-security-policy", "") + check("D19", "an SVG downloads as an attachment, never inline", disp.startswith("attachment"), disp) + check("D19", "an SVG download carries a sandbox CSP", "sandbox" in csp, csp or "no CSP") + check("D19", "file responses carry nosniff", r.headers.get("x-content-type-options") == "nosniff") + + for name, mime, data in (("plan.png", "image/png", b"\x89PNG\r\n\x1a\n"), + ("photo.jpg", "image/jpeg", b"\xff\xd8\xff"), + ("sheet.pdf", "application/pdf", b"%PDF-1.4")): + _, fid = upload(name, mime, data) + r = as_member.get(f"/api/files/{fid}") + check("D19", f"{mime} still opens inline, no sandbox", + r.status_code == 200 and r.headers.get("content-disposition", "").startswith("inline") + and "content-security-policy" not in r.headers, r.headers.get("content-disposition", "")) + check("D19", f"{mime} keeps the offline cache header", + r.headers.get("cache-control") == "private, max-age=86400", r.headers.get("cache-control", "")) + + _, fid = upload("Plan é – Nivel 2.pdf", "application/pdf", b"%PDF-1.4") + r = as_member.get(f"/api/files/{fid}") + check("D19", "a drawing with a non-Latin-1 name downloads (was 500)", r.status_code == 200, + f"status {r.status_code}") + check("D19", "the real name rides in filename*", + "filename*=UTF-8''Plan%20%C3%A9" in r.headers.get("content-disposition", ""), + r.headers.get("content-disposition", "")) + + _, fid = upload('a\r\nSet-Cookie: x=1;".pdf', "application/pdf", b"%PDF-1.4") + r = as_member.get(f"/api/files/{fid}") + disp = r.headers.get("content-disposition", "") + check("D19", "CR/LF and quotes in a name cannot reach the header", + r.status_code == 200 and "\r" not in disp and "\n" not in disp and "set-cookie" not in r.headers, + repr(disp)) + r = client(account("user_outsider")).get(f"/api/files/{svg}") + check("D19", "a non-member still gets 403 (access check unchanged)", r.status_code == 403, + f"status {r.status_code}") + + +SECTIONS = [d21, d19] def main() -> int: