Skip to content

fix(storage): reject chunks whose Content-Range total differs from the first - #14514

Merged
HarshMN2345 merged 7 commits into
mainfrom
fix-chunked-upload-total
Oct 15, 2026
Merged

HarshMN2345 merged 7 commits into
mainfrom
fix-chunked-upload-total

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Chunked uploads parsed the Content-Range total from every chunk but never compared it with the size the first chunk stored, so a chunk with a different total could complete a truncated file, leave a file pending forever with orphaned parts, or decide compression/encryption/antivirus from the wrong size.

  • Storage file create: a chunk whose total differs from the stored sizeOriginal is rejected with storage_invalid_content_range (400), after the resume permission check and before the completed-file short-circuit.
  • Storage file create: when a chunked upload is merged, a merged size that differs from the announced size removes the file and its document and returns the same error.
  • Function and site deployment uploads: the same total check against the stored sourceSize, after the resource ownership check.
  • Function and site deployment uploads: the same merged-size check. The source is removed and the deployment is marked failed with the reason in buildLogs (kept rather than deleted, since a site's first chunk already created its rule). It is marked fully uploaded, so a retried chunk returns the failed deployment instead of writing to the removed source.

Behaviour change: retrying the last chunk of a finished upload with a different total now returns 400 instead of the completed file.

Verified

On a local stack (Postgres, Redis, API, worker):

Case Result
Storage, second chunk announces a different total 400 storage_invalid_content_range
Storage, final chunk carries fewer bytes than its range 400, file is gone (404)
Function deployment, short final chunk 400; deployment failed with the reason in buildLogs; no source left; retrying the chunk returns the failed deployment (202)
Site deployment, short final chunk same as functions
Normal 2-chunk function and site uploads merged source and sourceSize both exactly 5,243,904 bytes
  • New e2e tests testChunkedUploadRejectsChangedTotal and testChunkedUploadRejectsShortChunk: OK (2 tests, 18 assertions).
  • New e2e test testCreateDeploymentRejectsShortChunk for functions (OK (1 test, 19 assertions)) and sites (OK (1 test, 24 assertions)): 400, deployment failed, a retried chunk returns the failed deployment.
  • Existing chunked tests in StorageCustomServerTest (out-of-order, parallel, large file, antivirus) pass; StorageCustomClientTest passes (42 tests, 974 assertions).
  • pint --test and phpstan analyse on the changed files.

Closes #14475

…e first

Every chunk after the first must announce the size stored by the first
chunk; a different total is rejected with storage_invalid_content_range
instead of silently completing a truncated file or leaving it pending
forever. A chunked file whose merged size differs from the announced
size is removed and rejected too. Function and site deployment uploads
get the same total check.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟡 1 medium

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Finalize size check missing in Functions/Sites · src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php

The PR's goal — reject uploads whose assembled bytes diverge from the announced Content-Range total — is enforced only in Storage, where after finalize() the actual file size is compared against $fileSize and the file/DB record are cleaned up on mismatch. The Functions/Http/Deployments/Create.php and Sites/Http/Deployments/Create.php finalize branches call finalize() and then overwrite sourceSize/totalSize with the assembled size, so a chunk that carried more or fewer bytes than its range announces silently persists a deployment of the wrong size — larger assemblies also bypass the deployment size limit, which is only validated against the declared total. The build then consumes a truncated or bloated source archive with no error surfaced to the client.


This PR hardens chunked uploads by checking that the assembled file's real size matches the announced Content-Range total before accepting the upload, deleting the file and record on mismatch.

Files Change
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php Adds post-finalize() size verification against the declared $fileSize, with file and DB cleanup on mismatch.
src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php Finalize branch records the assembled size without the same verification.
src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php Finalize branch records the assembled size without the same verification.

Reviewed commit: 93b47fc

@ghost

ghost commented Oct 15, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The pull request validates resumed Content-Range totals and checks that finalized upload sizes match the announced size for Storage files and Function/Site deployments. Invalid Storage uploads are removed, while invalid deployments are marked failed and retained so retries return the failed deployment. E2E coverage exercises the size-mismatch handling across all three upload surfaces.

Latest changes: The newest commits add Function and Site E2E tests for short final chunks, checking the 400 response, persisted failed status, and retry behavior.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 6
File Change
src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php Validates resumed upload totals and rejects finalized sources whose size differs from Content-Range, retaining a failed deployment record.
src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php Applies the deployment total and merged-size checks while retaining failed records and associated site rules.
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php Rejects changed resumed totals and removes file records and data when the merged size is inconsistent.
tests/e2e/Services/Storage/StorageCustomServerTest.php Covers changed Content-Range totals and short final chunks for Storage uploads.
tests/e2e/Services/Functions/FunctionsCustomServerTest.php Adds E2E coverage for short deployment chunks, failed status, and retry behavior.
tests/e2e/Services/Sites/SitesCustomServerTest.php Adds matching E2E coverage for short site deployment chunks, failed status, and retry behavior.
✅ Fixed since the last review · 1
  • Cover the deployment merged-size rejection path · src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php:335

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

@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

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • 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.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-chunked-upload-total (after).

Metric Before After Change
🚀 Requests/sec 192.71 198.74 ⚪ +3.1%
⏱️ Latency P50 88.47 ms 87.22 ms ⚪ -1.4%
⏱️ Latency P95 210.71 ms 202.35 ms ⚪ -4%
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 87.22 202.35 12,540 198.74 -8.36
Account 164.45 333.93 660 11.05 +11.83
TablesDB 85.36 161.98 6,820 110.25 -9.26
Storage 77.47 175.07 3,300 55.6 -8.18
Functions 122.96 232.71 1,760 30.32 -28.72

Top API waits (after)

API request Max wait (ms)
account.name.update 693.86
account.prefs.update 584.11
functions.delete 471.42
tablesdb.rows.delete 414.01
storage.buckets.create 393.02

HarshMN2345 and others added 2 commits October 15, 2026 16:09
…rom its range

Functions and sites deployments now get the same post-merge size check
as storage files: the source is removed and the deployment marked failed.
Adds e2e coverage for a changed total and a short final chunk.

@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

@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

@HarshMN2345
HarshMN2345 merged commit 829f384 into main Oct 15, 2026
13 of 14 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant