Skip to content

fix(storage): build Content-Disposition per RFC 6266 and validate file names - #14789

Merged
ChiragAgg5k merged 4 commits into
mainfrom
fix/storage-content-disposition
Oct 22, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
fix/storage-content-disposition

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Hardens how Storage builds Content-Disposition for 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 5987 filename*=UTF-8''… with the exact name. View, download, push and the S3 gateway GetObject now share it; the two private copies of the sanitizer in View and S3 are removed.
  • Download previously concatenated the raw stored name into the header. Push did too. Both now go through the shared method.
  • Download now also sends X-Content-Type-Options: nosniff (set rather than appended) and Content-Security-Policy: sandbox; script-src 'none';. It always sends attachment.
  • Push forces attachment for image/svg+xml like view does, only accepts inline / attachment as the disposition, and fixes its CSP to script-src 'none'; (the keyword was unquoted).
  • New Appwrite\Utopia\Database\Validator\Filename (a Text that also rejects ASCII control characters) is used for the name param 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 as café.png becomes filename="caf_.png"; filename*=UTF-8''caf%C3%A9.png. Browsers use filename*. Existing download assertions are updated for the added filename* parameter.

Test Plan

  • Unit: tests/unit/Utopia/ResponseTest.php::testSetContentDisposition covers plain, quote/backslash/semicolon, control character, non-ASCII and empty names.
  • E2E 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 exact attachment; filename="…"; filename*=UTF-8''…, nosniff and the sandbox CSP; renames containing LF, CRLF or NUL return 400 and leave the name unchanged.
  • E2E S3CustomServerTest::testPutObjectControlCharacterKey: a key with spaces and quotes is accepted; keys with %0A, %0D%0A, %00, %7F return 400.
  • Existing view and S3 disposition tests updated for the ASCII fallback.
  • Pint, PHPStan (host, 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

…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.
@luxor-code-review

luxor-code-review Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 S3 objectPath now 400s legacy objects with control chars — Base.php:172
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Inconsistent Filename validator configs across call sites (Create.php:252) — The new Filename validator is wired with contradictory settings: Create.php:252 uses new Filename(0, 0) (unlimited length, min=0, so an empty file name passes), while Update.php:66 uses new Filename(128) and S3/Base.php:172 uses new Filename(0) whose default min=1 rejects empty names.

This PR hardens how file names flow into HTTP responses: a new Filename validator rejects control bytes at upload and S3-key entry points, Response::setContentDisposition() now emits a sanitized ASCII fallback alongside an RFC 5987 filename* parameter, and file-serving endpoints gain consistent disposition and tightened Content-Security-Policy/nosniff headers. E2E and unit tests lock the new header contracts.

Files Change
src/Appwrite/Utopia/Response.php, tests/unit/Utopia/ResponseTest.php Rewrite setContentDisposition() with byte-level fallback sanitization, filename*=UTF-8'' encoding, and a data-provider unit suite covering unicode, quotes, control chars, and empty names.
src/Appwrite/Utopia/Database/Validator/Filename.php, .../Storage/Http/Buckets/Files/{Create,Update}.php New Filename validator rejecting bytes < 32 and 127, wired into file create/update routes (Update swaps Text(128) for Filename(128)).
.../Storage/Http/Buckets/Files/{Download/Get,Push/Get,View/Get}.php Refactor all serving endpoints through the new disposition helper, force SVG to attachment on push, and add sandbox CSP plus nosniff on inline responses.
src/Appwrite/Platform/Modules/S3/Http/S3/{Base,Get}.php Validate S3 object keys with Filename in objectPath() and route GetObject disposition through the shared sanitizer with forced-attachment precedence.
tests/e2e/Services/Storage/{StorageBase,S3CustomServerTest}.php Update disposition assertions to the dual-filename format and add download-disposition plus control-char rejection tests.

Reviewed commit: 8129bba

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The multipart completion path now validates the name after the destination disappears, and no new issues remain.

This PR centralizes RFC 6266 Content-Disposition generation across Storage and S3 responses, adds filename validation for uploads, renames, and new S3 objects, and hardens download and push headers. It also adds unit and E2E coverage for disposition formatting, filename validation, and response behavior. The latest change revalidates the S3 key at multipart completion when the destination object has disappeared since upload initiation.

Latest changes: The newest commit validates the key as a new-object name during multipart completion when the existing destination is no longer present.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 13
File Change
src/Appwrite/Platform/Modules/S3/Http/S3/Base.php Validates object names for new S3 objects, uses shared disposition generation, and validates multipart completions that create a new object.
src/Appwrite/Platform/Modules/S3/Http/S3/Get.php Uses the shared RFC 6266 disposition builder for S3 object responses.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php Validates uploaded filenames against ASCII control characters.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php Uses shared disposition generation and adds safer download response headers.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php Normalizes push dispositions, forces SVG attachments, and sets the CSP.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Update.php Validates filenames when renaming files.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php Replaces local disposition sanitization with the shared response method.
src/Appwrite/Utopia/Database/Validator/Filename.php Adds a text validator that rejects ASCII control characters.
src/Appwrite/Utopia/Response.php Adds shared RFC 6266 disposition generation with an ASCII fallback and UTF-8 extended filename.
tests/e2e/Services/Storage/S3CustomServerTest.php Covers S3 response dispositions and control-character object keys.
tests/e2e/Services/Storage/StorageBase.php Covers download dispositions, security headers, and invalid rename behavior.
tests/e2e/Services/Storage/StorageCustomServerTest.php Adds push response disposition, SVG attachment, and JWT-related E2E coverage.
tests/unit/Utopia/ResponseTest.php Tests shared disposition generation for plain, special, control, non-ASCII, and empty names.
✅ Fixed since the last review · 1
  • Multipart completion can create an unvalidated legacy name · src/Appwrite/Platform/Modules/S3/Http/S3/Base.php:212

Reviewed the commits since 4b00ec6 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 objectPath now 400s legacy objects with control chars — Base.php:172

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Lookups and deletes resolve keys through the same path parser, so rejecting there would strand objects stored before the check existed.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

An upload started against an existing object creates a new one if that object is deleted before completion.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/storage-content-disposition (after).

Metric Before After Change
🚀 Requests/sec 189.82 191.99 ⚪ +1.1%
⏱️ Latency P50 89.56 ms 89.77 ms ⚪ +0.2%
⏱️ Latency P95 217.73 ms 207.87 ms ⚪ -4.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 89.77 207.87 12,198 191.99 -9.87
Account 167.07 305.2 642 10.64 -55.83
TablesDB 86.97 162.52 6,634 106.92 -13.47
Storage 83.91 193.06 3,210 53.49 +4.89
Functions 124.63 243.32 1,712 29.15 +0.45

Top API waits (after)

API request Max wait (ms)
account.name.update 433.02
functions.create 403.28
storage.files.create 399.78
storage.buckets.create 384.63
account.get 383.52

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@ChiragAgg5k
ChiragAgg5k merged commit abc6b6f into main Oct 22, 2026
71 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/storage-content-disposition branch October 22, 2026 16:55
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…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)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…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)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…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)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…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)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…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)
ChiragAgg5k added a commit that referenced this pull request Oct 24, 2026
…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)
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…isposition

fix(storage): build Content-Disposition per RFC 6266 and validate file names

(cherry picked from commit abc6b6f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant