Skip to content

🐛 Bug Report: Chunked upload never compares the Content-Range total with the first chunk: truncated file stored as complete, orphaned parts, last chunk decides encryption #14475

Closed

Description

@JoshiJoshiJoshi

👟 Reproduction steps

  1. Self-hosted Appwrite 2.5.0, local storage device, bucket with default settings (encryption: true, maximumFileSize 100 MB).
  2. Upload a file of 13 754 945 bytes (random data) in three chunks like the SDKs do (5 MiB, 5 MiB, rest) with Content-Range: bytes <start>-<end>/<total> and X-Appwrite-ID from the second chunk on.
  3. Vary the <total> that one of the chunks claims, then read the file document (sizeOriginal, sizeActual, chunksTotal, chunksUploaded), download it and compare the sha256 with the original.

👍 Expected behavior

A chunk whose Content-Range total differs from the total of the first chunk (the one that created the file document) is rejected, e.g. with storage_invalid_content_range (400). A file is only stored as complete when the merged content has the size that was announced.

👎 Actual Behavior

The total is parsed from every request (Files/Create.php L219) and only checked against its own end (L222). It is never compared with the stored sizeOriginal/chunksTotal. Two runs each, always the same result (13 754 945 bytes, 3 chunks, node-local storage):

Variant (claimed total) HTTP Stored file
control: correct on all chunks 201, 201, 201 sizeOriginal 13 754 945, 3/3, download identical (sha256)
chunk 3: +1, chunk 3: −1, chunk 2: +1, chunk 2: +20 MiB 201 each 3/3, download identical
chunk 3: +10 MiB 201 each 3/3, download identical, but stored unencrypted: openSSLCipher empty, sizeActual 13 754 945 (the control has aes-128-gcm, 18 339 928)
chunk 1: 10 MiB (too small), chunks 2 and 3 with the true total 201, 201, 200 sizeOriginal 10 485 760, chunksTotal 2, 2/2 "complete"; the third chunk is dropped silently; download is the first 10 MiB, sha256 differs from the original
chunk 1: +20 MiB (too large), chunks 2 and 3 with the true total 201, 201, 201 sizeOriginal 34 726 465, chunksTotal 7, 3/7, sizeActual 0, download 404; the file stays in the bucket and tmp_<id>.bin/ with three .part.N files (≈ 13 MB) stays on the device

Where it comes from:

  • chunksTotal and sizeOriginal are fixed by the first chunk (L324, L328); later chunks take $chunks from the document (L289, L351) but keep their own $fileSize. Once chunksUploaded === chunksTotal, every further chunk is answered with 200 and ignored (L366–L375). Nothing verifies the merged size against sizeOriginal.
  • When the file is finalized, the last chunk's claimed size decides compression (L434), encryption (L453, L475, APP_STORAGE_READ_BUFFER = 20 MB) and, by the same pattern, the ClamAV scan (L387, APP_LIMIT_ANTIVIRUS = 20 MB; not tested, antivirus is off on our cluster). A small file whose last chunk claims a large total skips all three.
  • I found no cleanup of unfinished uploads in the deletes worker (Workers/Deletes.php has no handling for chunksUploaded < chunksTotal or tmp_* directories), so the orphaned parts stay for as long as the unfinished file exists (deleting the file via the API removes them).

Normal SDK uploads (consistent totals, retries, out-of-order and repeated chunks) work correctly. This is about inconsistent clients: a proxy or client that rewrites or miscomputes the header, or a file that grows while it is uploaded. The result is a silently truncated file that is reported as complete. The same code is on main (checked 14.10.2026, same line numbers).

🎲 Appwrite version

2.5.0

💻 Operating system

Linux (Debian 12), Docker

🧱 Your Environment

Self-hosted Appwrite 2.5.0, Postgres adapter, _APP_STORAGE_DEVICE=local, two API nodes behind Traefik, requests sent directly to the API container.

💡 Suggested fix

  • On every chunk after the first, compare the Content-Range total with sizeOriginal of the stored document and respond with STORAGE_INVALID_CONTENT_RANGE if they differ. Use the stored sizeOriginal (not the request's total) for the compression, encryption and antivirus decisions.
  • When the last chunk is merged, check that the merged size equals sizeOriginal and fail the upload otherwise.
  • Reject further chunks for a completed file with an error instead of 200, or at least compare their range with the stored size, so a client notices that its data was not stored.
  • Clean up unfinished chunked uploads (document and tmp_* parts) after a retention time, for example in the maintenance job.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find a similar issue

🏢 Have you read the Code of Conduct?

  • I have read the Code of Conduct

Activity

  1. added
    product / databases
    product / storage
    product / vcs
    sdk / cli
    on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / databasesproduct / storageproduct / vcssdk / cli

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions