Repository navigation
fix(storage): accept single-byte and past-EOF ranges - #13824
Conversation
Ranged reads rejected two kinds of request that RFC 9110 defines as valid: - Range bounds are inclusive, so `bytes=0-0` or `bytes=<last>-<last>` asks for one byte. The guard treated `start == end` as malformed. - A last-byte-pos past the end of the file is clamped to the final byte, not rejected. The clamp only ran when the end was absent or the span exceeded the read buffer, so any explicit end overshooting EOF by less than the buffer returned 416 instead. Media players are the usual victim: an mp4 demuxer probes the tail of a file to locate the moov atom and issues single-byte and final-byte ranges while seeking, all of which failed, while a plain sequential read kept working. Hoist the EOF clamp out of the conditional so it always applies, then reject only when the start lands past the clamped end. Malformed ranges, unsupported units, and starts at or beyond EOF still return 416, and no read grows unbounded since the clamp caps every span at the file size. The same guard is duplicated across the file view, download and push actions and both deployment download actions; all five are updated identically.
|
| // RFC 9110: a last-byte-pos past the end of the file is clamped, not rejected. | ||
| $end = min($end, $size - 1); | ||
|
|
||
| if ($unit !== 'bytes' || $start > $end) { |
There was a problem hiding this comment.
Missing Range Regression Tests
This changes observable HTTP behavior across all five range-serving actions without adding the required E2E regression coverage. The repository requires HTTP/API changes to be tested through the real API, so this requirement must be satisfied before merging. Add E2E cases that verify the status, body, Content-Range, and Content-Length for a single-byte range, the final byte, and an explicit end beyond EOF, plus continued rejection when the start is at or beyond EOF. Existing storage tests do not cover these cases, and hand-tracing and linting do not validate the HTTP contract. The same omission applies to Storage View/Push and Functions/Sites deployment downloads.
Context Used: AGENTS.md (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php
Line: 142-145
Comment:
**Missing Range Regression Tests**
This changes observable HTTP behavior across all five range-serving actions without adding the required E2E regression coverage. The repository requires HTTP/API changes to be tested through the real API, so this requirement must be satisfied before merging. Add E2E cases that verify the status, body, `Content-Range`, and `Content-Length` for a single-byte range, the final byte, and an explicit end beyond EOF, plus continued rejection when the start is at or beyond EOF. Existing storage tests do not cover these cases, and hand-tracing and linting do not validate the HTTP contract. The same omission applies to Storage View/Push and Functions/Sites deployment downloads.
**Context Used:** AGENTS.md ([source](https://github.com/appwrite/appwrite/blob/main/AGENTS.md))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
The range assertions pinned logo.png at 47218 bytes, so re-encoding the fixture would fail the test for a reason unrelated to range handling. Read the size off the file instead and build every bound from it.
The range coverage uploaded logo.png to a default bucket, and a file at or below the read buffer is encrypted on create. An encrypted file is decrypted whole and sliced in memory, so the assertions passed without the ranged read on the storage device ever running. Upload the same fixture to a bucket with encryption off, which leaves the stored bytes identical to the uploaded ones and sends the range to the device. Add the start-past-EOF rejection next to the start-at-EOF one, and cover the final byte with an overshooting end on the function and site deployment downloads, where the artifact size comes from the unranged body.
| 'bucketId' => ID::unique(), | ||
| 'name' => 'Test Bucket Range', | ||
| 'fileSecurity' => true, | ||
| 'encryption' => false, |
There was a problem hiding this comment.
This test disables encryption specifically to force the request through the storage device's ranged-read path. That couples the test to the current encryption and buffering implementation instead of testing only the observable HTTP contract. A refactor that preserves the range behavior could therefore break this test for an irrelevant internal reason. This violates the repository directive against implementation-coupled tests and must be addressed before merging.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Storage/StorageBase.php
Line: 1173
Comment:
**Test Depends on Internals**
This test disables encryption specifically to force the request through the storage device's ranged-read path. That couples the test to the current encryption and buffering implementation instead of testing only the observable HTTP contract. A refactor that preserves the range behavior could therefore break this test for an irrelevant internal reason. This violates the repository directive against implementation-coupled tests and must be addressed before merging.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
The push route shares the range guard with view and download but had no e2e coverage at all. It authorizes on a token signed with the project's OpenSSL key rather than the session, so the range cases mint one for the file under test and run against all three routes. Functions and Sites deployment downloads only asserted the clamped end; add the matching rejection for a start with nothing left to read, which also walks the open-ended branch. Delete the bucket the range test creates instead of leaking it.
The push route is labelled with the 'public' scope, which ROLE_KEYS never holds, so every server-side run of the shared trait sent an API key and got "missing scopes ([\"public\"])" instead of a 206. Push authorizes on its signed token, so the request wants no key at all. Range handling does not vary by side, so the cases move out of StorageBase and run once rather than three times. Dropped the content-length assertions, which only restate the length of the body already compared byte for byte.
Three things the range cases did not need: The push case minted an HS256 token from _APP_OPENSSL_KEY_V1 against the route's private claim schema, so a change to the key, algorithm or claims would have broken a range regression test. Its guard is the same four lines as view and download, which stay covered. The bucket disabled encryption to steer the request onto the device's ranged read. Content-Range and Content-Length come from sizeOriginal either way, and a decrypted body is sliced with the same offsets, so the HTTP contract does not depend on it. Content-Length is back alongside Content-Range: it is the header a client reads to size the chunk, not a restatement of the body.
The push route only hands out its URL through a push message, which mints the token itself. Asserting the range contract against that URL covers the fifth call site without a test rebuilding the claim schema by hand.
…live testGetBucketFile has carried the ranged-download block since before this change, covering bytes=0-99, an open end and two malformed headers off the shared setupBucketFile fixture. The inclusive and clamped cases belong in that run, not in a second test that builds its own bucket and file to say the same thing.
What does this PR do?
Ranged reads rejected two kinds of request that RFC 9110 defines as valid:
bytes=0-0orbytes=<last>-<last>asks for one byte. The guard treatedstart == endas malformed.Media players are the usual victim: an mp4 demuxer probes the tail of a file to locate the moov atom and issues single-byte and final-byte ranges while seeking, all of which failed, while a plain sequential read kept working.
Hoists the EOF clamp out of the conditional so it always applies, then rejects only when the start lands past the clamped end. Malformed ranges, unsupported units, and starts at or beyond EOF still return 416, and no read grows unbounded since the clamp caps every span at the file size. The same guard is duplicated across the file view, download and push actions and both deployment download actions; all five are updated identically.
Test Plan
composer linton all five changed files →{"tool":"pint","result":"passed"}each.bytes=0-0,bytes=<last>-<last>, an end past EOF, a start past EOF (still 416), a malformed range (still 416), and an unsupported unit (still 416).The range cases live in
StorageCustomServerTestrather than the sharedStorageBasetrait: range handling does not vary by side, so running them in all three suites was three times the requests for the same coverage.Push is covered through
testCreateDraftPushWithImagein the messaging suite. That route only ever hands out its URL via a push message, which mints the token itself, so asserting the range contract againstdata.image.urlexercises the fifth call site without a test rebuilding the JWT claim schema by hand.Not run: no e2e against a live stack for the range assertions themselves. CI covers
tests/e2e/Services/Storage.Related PRs and Issues
Two known gaps, both pre-existing and left alone deliberately:
Content-Range: bytes */<size>, which RFC 9110 15.5.17 says a 416 SHOULD carry. The repo's own S3 gateway does this correctly atsrc/Appwrite/Platform/Modules/S3/Http/S3/Get.php:199; fixing it here needs different plumbing since the throw goes through the generic exception handler.MAX_OUTPUT_CHUNK_SIZE. The ceiling is unchanged —bytes=0-<size-1>already did this onmain— so this is not a new class of read, but it is worth a maintainer's eye.Checklist