Repository navigation
Description
👟 Reproduction steps
- Self-hosted Appwrite 2.5.0, local storage device, bucket with default settings (
encryption: true,maximumFileSize100 MB). - 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>andX-Appwrite-IDfrom the second chunk on. - 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:
chunksTotalandsizeOriginalare fixed by the first chunk (L324, L328); later chunks take$chunksfrom the document (L289, L351) but keep their own$fileSize. OncechunksUploaded === chunksTotal, every further chunk is answered with200and ignored (L366–L375). Nothing verifies the merged size againstsizeOriginal.- 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.phphas no handling forchunksUploaded < chunksTotalortmp_*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-Rangetotal withsizeOriginalof the stored document and respond withSTORAGE_INVALID_CONTENT_RANGEif they differ. Use the storedsizeOriginal(not the request's total) for the compression, encryption and antivirus decisions. - When the last chunk is merged, check that the merged size equals
sizeOriginaland 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