T16.2: D19 uploaded files download safely
New server/files_http.py; get_wp_file uses it.
- PDF and raster images (png, jpeg, gif, webp, bmp, avif) open inline,
as before; Cache-Control unchanged for the offline drawing cache.
- Every other type, SVG included, downloads as an attachment with
Content-Security-Policy: sandbox; default-src 'none'.
- SVG uploads stay accepted (decided 2026-10-02: accept, download only).
- Content-Disposition: ASCII fallback + RFC 5987 filename*, control
characters and quotes replaced. nosniff from the API too.
Security review SEC-01 (stored XSS through an SVG drawing) and SEC-15e
(non-Latin-1 names gave 500; CR/LF reached the header).
security_check.py D19: 20/20 total with D21. Against da1325c: 6 fails
(the findings); every inline/cache/access check passes on both.
This commit is contained in:
66
server/files_http.py
Normal file
66
server/files_http.py
Normal file
@@ -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 <script>. GET /api/files/{id} used to
|
||||
return every file `inline` with the uploader's content type, on the app's own
|
||||
origin, so a script inside an SVG ran as whoever opened the drawing - with
|
||||
their session, and with an admin's power if an admin opened it.
|
||||
|
||||
The download name was built by hand with only `"` removed. A name outside
|
||||
Latin-1 (`Plan é.pdf`) made the header unencodable, so the download failed
|
||||
with 500; CR/LF in a name went into the header as typed.
|
||||
|
||||
THE RULE (D19, decided 2026-10-02)
|
||||
* PDF and raster images open INLINE, exactly as before. That is what the
|
||||
creator's drawing list, the printed package and Field View link to
|
||||
(CR-007), and the offline drawing cache in sw.js stores.
|
||||
* Every other type, SVG included, is an ATTACHMENT inside a sandbox: the
|
||||
browser saves it instead of rendering it on our origin, and
|
||||
`Content-Security-Policy: sandbox` strips script and same-origin access
|
||||
even if someone opens the saved response directly.
|
||||
* SVG UPLOADS STAY ACCEPTED (decided: accept, download only). The fix is on
|
||||
the way out, so SVGs already in the database are covered too.
|
||||
* `Cache-Control` is unchanged - the offline drawing cache depends on it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi.responses import Response
|
||||
|
||||
# Formats a browser shows as inert pixels, or in its own PDF viewer.
|
||||
INLINE_SAFE_MIME = frozenset({
|
||||
"application/pdf",
|
||||
"image/png", "image/jpeg", "image/gif", "image/webp", "image/bmp", "image/avif",
|
||||
})
|
||||
|
||||
# Control characters, and the characters that end or escape a quoted
|
||||
# header parameter. Replaced, never passed through.
|
||||
_UNSAFE_NAME = re.compile(r'[\x00-\x1f\x7f"\\;]')
|
||||
|
||||
|
||||
def content_disposition(disposition: str, name: str) -> str:
|
||||
"""A header value that is valid for ANY user-supplied file name: an ASCII
|
||||
fallback for old clients plus RFC 5987 `filename*` with the real name."""
|
||||
name = _UNSAFE_NAME.sub("_", name or "file").strip() or "file"
|
||||
ascii_name = name.encode("ascii", "replace").decode("ascii").replace("?", "_")
|
||||
return f"{disposition}; filename=\"{ascii_name}\"; filename*=UTF-8''{quote(name, safe='')}"
|
||||
|
||||
|
||||
def file_response(row) -> Response:
|
||||
"""The response for GET /api/files/{id}. `row` is a models.WpFile."""
|
||||
mime = (row.mime or "application/octet-stream").lower()
|
||||
headers = {
|
||||
"Cache-Control": "private, max-age=86400", # unchanged: sw.js caches drawings offline
|
||||
"X-Content-Type-Options": "nosniff", # NGINX adds it too; the dev server did not
|
||||
}
|
||||
if mime in INLINE_SAFE_MIME:
|
||||
headers["Content-Disposition"] = content_disposition("inline", row.name)
|
||||
else:
|
||||
headers["Content-Disposition"] = content_disposition("attachment", row.name)
|
||||
headers["Content-Security-Policy"] = "sandbox; default-src 'none'"
|
||||
return Response(content=row.data, media_type=mime, headers=headers)
|
||||
Reference in New Issue
Block a user