Repository navigation
fix(storage): build Content-Disposition per RFC 6266 and validate file names - #14789
Conversation
…control characters in file names Download and push concatenated the stored file name into Content-Disposition, while view and the S3 gateway each carried their own sanitizer. One Response::setContentDisposition() now emits a printable ASCII fallback plus an RFC 5987 filename* for every file-serving endpoint. File names are validated against control characters on create, rename and S3 keys, so they never reach response headers. Downloads also send nosniff and a sandboxing CSP, and push forces SVG to attachment like view does.
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
This PR hardens how file names flow into HTTP responses: a new
Reviewed commit: 8129bba |
🟢 Tier S · Ready to merge
This PR centralizes RFC 6266 Latest changes: The newest commit validates the key as a new-object name during multipart completion when the existing destination is no longer present.
📂 Walkthrough · 13
✅ Fixed since the last review · 1
Reviewed the commits since |
There was a problem hiding this comment.
Adds a Filename control-character validator and RFC 6266/5987-compliant Content-Disposition sanitization across the Storage and S3 endpoints, with hardening CSP headers and matching unit/e2e tests.
Key findings
- 🟡 S3
objectPathnow 400s legacy objects with control chars — Base.php:172
Lookups and deletes resolve keys through the same path parser, so rejecting there would strand objects stored before the check existed.
An upload started against an existing object creates a new one if that object is deleted before completion.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
…e names (backport #14789) Download and push concatenated the raw stored file name into Content-Disposition, so a name with quotes, semicolons or line breaks could rewrite the header. View, download and push now share Response::setContentDisposition(), which sends a printable ASCII fallback plus an RFC 5987 UTF-8 name. Download also sends nosniff and a sandbox CSP, push forces attachment for SVG, accepts only inline or attachment and quotes its CSP keyword, and file names with control characters are refused on create and update. The S3 gateway does not exist on 1.9; view keeps its 1.9 inline disposition and CSP. The tests use logo.png because the HTML and SVG fixtures are not on 1.9. (cherry picked from commit abc6b6f)
…e names (backport #14789) Download and push concatenated the raw stored file name into Content-Disposition, so a name with quotes, semicolons or line breaks could rewrite the header. View, download and push now share Response::setContentDisposition(), which sends a printable ASCII fallback plus an RFC 5987 UTF-8 name. Download also sends nosniff and a sandbox CSP, push forces attachment for SVG, accepts only inline or attachment and quotes its CSP keyword, and file names with control characters are refused on create and update. The S3 gateway does not exist on 1.9; view keeps its 1.9 inline disposition and CSP. The tests use logo.png because the HTML and SVG fixtures are not on 1.9. (cherry picked from commit abc6b6f)
…e names (backport #14789) Download and push concatenated the raw stored file name into Content-Disposition, so a name with quotes, semicolons or line breaks could rewrite the header. View, download and push now share Response::setContentDisposition(), which sends a printable ASCII fallback plus an RFC 5987 UTF-8 name. Download also sends nosniff and a sandbox CSP, push forces attachment for SVG, accepts only inline or attachment and quotes its CSP keyword, and file names with control characters are refused on create and update. The S3 gateway does not exist on 1.9; view keeps its 1.9 inline disposition and CSP. The tests use logo.png because the HTML and SVG fixtures are not on 1.9. (cherry picked from commit abc6b6f)
…e names (backport #14789) Download and push concatenated the raw stored file name into Content-Disposition, so a name with quotes, semicolons or line breaks could rewrite the header. View, download and push now share Response::setContentDisposition(), which sends a printable ASCII fallback plus an RFC 5987 UTF-8 name. Download also sends nosniff and a sandbox CSP, push forces attachment for SVG, accepts only inline or attachment and quotes its CSP keyword, and file names with control characters are refused on create and update. The S3 gateway does not exist on 1.9; view keeps its 1.9 inline disposition and CSP. The tests use logo.png because the HTML and SVG fixtures are not on 1.9. (cherry picked from commit abc6b6f)
…e names (backport #14789) Download and push concatenated the raw stored file name into Content-Disposition, so a name with quotes, semicolons or line breaks could rewrite the header. View, download and push now share Response::setContentDisposition(), which sends a printable ASCII fallback plus an RFC 5987 UTF-8 name. Download also sends nosniff and a sandbox CSP, push forces attachment for SVG, accepts only inline or attachment and quotes its CSP keyword, and file names with control characters are refused on create and update. The S3 gateway does not exist on 1.9; view keeps its 1.9 inline disposition and CSP. The tests use logo.png because the HTML and SVG fixtures are not on 1.9. (cherry picked from commit abc6b6f)
…e names (backport #14789) Download and push concatenated the raw stored file name into Content-Disposition, so a name with quotes, semicolons or line breaks could rewrite the header. View, download and push now share Response::setContentDisposition(), which sends a printable ASCII fallback plus an RFC 5987 UTF-8 name. Download also sends nosniff and a sandbox CSP, push forces attachment for SVG, accepts only inline or attachment and quotes its CSP keyword, and file names with control characters are refused on create and update. The S3 gateway does not exist on 1.9; view keeps its 1.9 inline disposition and CSP. The tests use logo.png because the HTML and SVG fixtures are not on 1.9. (cherry picked from commit abc6b6f)
…isposition fix(storage): build Content-Disposition per RFC 6266 and validate file names (cherry picked from commit abc6b6f)
What does this PR do?
Hardens how Storage builds
Content-Dispositionfor file responses and validates file names.Appwrite\Utopia\Response::setContentDisposition()builds the header per RFC 6266: a quoted fallback restricted to printable ASCII (control characters,",\and;dropped, non-ASCII characters replaced with_) plus an RFC 5987filename*=UTF-8''…with the exact name. View, download, push and the S3 gatewayGetObjectnow share it; the two private copies of the sanitizer in View and S3 are removed.X-Content-Type-Options: nosniff(set rather than appended) andContent-Security-Policy: sandbox; script-src 'none';. It always sendsattachment.attachmentforimage/svg+xmllike view does, only acceptsinline/attachmentas the disposition, and fixes its CSP toscript-src 'none';(the keyword was unquoted).Appwrite\Utopia\Database\Validator\Filename(aTextthat also rejects ASCII control characters) is used for thenameparam of file update, the uploaded file name on file create, and the object name segment of S3 keys. These now return 400.Behaviour change: the quoted
filename=fallback is ASCII-only, so a name such ascafé.pngbecomesfilename="caf_.png"; filename*=UTF-8''caf%C3%A9.png. Browsers usefilename*. Existing download assertions are updated for the addedfilename*parameter.Test Plan
tests/unit/Utopia/ResponseTest.php::testSetContentDispositioncovers plain, quote/backslash/semicolon, control character, non-ASCII and empty names.StorageBase::testFileDownloadContentDisposition: upload an HTML file, rename it to a name with quotes, a backslash, a semicolon and non-ASCII, check the download sends the exactattachment; filename="…"; filename*=UTF-8''…,nosniffand the sandbox CSP; renames containing LF, CRLF or NUL return 400 and leave the name unchanged.S3CustomServerTest::testPutObjectControlCharacterKey: a key with spaces and quotes is accepted; keys with%0A,%0D%0A,%00,%7Freturn 400.phpstan.neon) and Rector pass on touched files. E2E is left to the CI lanes; I didn't bring up a local stack.The create-path check isn't covered by E2E because curl escapes CR/LF in multipart file names before sending them, so the test client can't produce such a name.
Related PRs and Issues
None.
Related: appwrite-labs/cloud#6674