Skip to content

fix(storage): accept single-byte and past-EOF ranges - #13824

Merged
HarshMN2345 merged 15 commits into
mainfrom
fix/issue-8600
Sep 24, 2026
Merged

HarshMN2345 merged 15 commits into
mainfrom
fix/issue-8600

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

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.

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 lint on all five changed files → {"tool":"pint","result":"passed"} each.
  • Range parsing traced by hand for: 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 StorageCustomServerTest rather than the shared StorageBase trait: 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 testCreateDraftPushWithImage in 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 against data.image.url exercises 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:

  1. 416 responses still omit Content-Range: bytes */<size>, which RFC 9110 15.5.17 says a 416 SHOULD carry. The repo's own S3 gateway does this correctly at src/Appwrite/Platform/Modules/S3/Http/S3/Get.php:199; fixing it here needs different plumbing since the throw goes through the generic exception handler.
  2. On the two deployment download actions the span is capped only when the end is absent, so an explicit end is never bounded by MAX_OUTPUT_CHUNK_SIZE. The ceiling is unchanged — bytes=0-<size-1> already did this on main — so this is not a new class of read, but it is worth a maintainer's eye.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? — no metadata change

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.
@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the corrected range semantics are consistently applied and covered through the affected HTTP workflows.

Fix All in Claude CodeFindings

  1. P2 Missing Range Regression Tests ▶
  2. P2 Test Depends on Internals ▶
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php:142-145
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.

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!

### Issue 2
tests/e2e/Services/Storage/StorageBase.php:undefined-1173
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.

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!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR corrects byte-range handling across storage and deployment download actions by accepting inclusive one-byte ranges and clamping explicit end positions to EOF.

  • Applies the same corrected validation order to Storage View, Download, and Push plus Functions and Sites deployment downloads.
  • Adds real-API E2E assertions for successful partial responses, response headers and bodies, and continued rejection of ranges starting at EOF.
  • Reuses existing user-visible workflows for push image and deployment download coverage rather than reconstructing authentication or storage internals.

Reviews (12) · Last reviewed commit: "Merge branch 'main' into fix/issue-8600"

Comment on lines +142 to +145
// 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 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!

Fix in Claude Code Fix in Codex

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/issue-8600 (after).

Metric Before After Change
🚀 Requests/sec 180.81 186.96 ⚪ +3.4%
⏱️ Latency P50 93.33 ms 93.15 ms ⚪ -0.2%
⏱️ Latency P95 225.97 ms 216.06 ms ⚪ -4.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 93.15 216.06 11,571 186.96 -9.91
Account 181.05 321.9 609 10.12 -34.47
TablesDB 90.22 171.42 6,293 103.31 -12.36
Storage 85.98 179.66 3,045 51.91 -4.26
Functions 134.65 252.11 1,624 28.51 -17.71

Top API waits (after)

API request Max wait (ms)
account.prefs.update 453.69
tablesdb.rows.update 440.74
tablesdb.rows.decrement 435.33
functions.variables.delete 413.46
account.name.update 413.24

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 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)

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!

Fix in Claude Code Fix in Codex

HarshMN2345 and others added 5 commits September 22, 2026 18:45
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.
HarshMN2345 and others added 5 commits September 24, 2026 11:26
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.
@HarshMN2345
HarshMN2345 merged commit 693d4c5 into main Sep 24, 2026
50 checks passed
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.

🐛 Bug Report: Playing Video URL (From Appwrite Storage Service) Network in Flutter

2 participants