Skip to content

Ignore well-formed multi-range on FileResponse instead of 416 - #14129

Open
vyrnsynx wants to merge 19 commits into
aio-libs:masterfrom
vyrnsynx:fix/fileresponse-multi-range-14124
Open

vyrnsynx wants to merge 19 commits into
aio-libs:masterfrom
vyrnsynx:fix/fileresponse-multi-range-14124

Conversation

@vyrnsynx

Copy link
Copy Markdown

What do these changes do?

BaseRequest.http_range only accepts a single byte range, so a header like
Range: bytes=0-1,4-5 raises ValueError. FileResponse._prepare_open_file
turned every such ValueError into 416 with Content-Range: bytes */N.

A multi-range set can still be satisfiable. RFC 9110 §14.2 lets a server ignore
Range and send 200 with the full representation; the comment already next
to the 416 path quotes the same idea from RFC 7233. This change recognises a
well-formed multi-range set and falls through to a full 200 instead of 416.
Malformed ranges (bytes=a-f, bytes=100-0, bytes=0-1,a-b, …) are unchanged.

http_range itself stays single-range on purpose (same choice as #2805).

Are there changes in behavior for the user?

Yes, intentional: Range: bytes=0-1,4-5 (and other well-formed multi-range
sets) against FileResponse now returns 200 with the full body instead of
416. Single-range and malformed Range behaviour is unchanged. A
multipart/byteranges 206 would also be valid; ignoring the header is the
smaller change.

Is it a substantial burden for the maintainers to support this?

No. One regex check on the existing ValueError path; no new public API;
http_range stays single-range.

Related issue number

Fixes #14124.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder
    • 14124.bugfix.rst

FileResponse only implements a single byte range. A Range header with
multiple byte-range-specs (for example bytes=0-1,4-5) made http_range
raise ValueError, and every ValueError was turned into 416. A
satisfiable multi-range set must not get 416; ignore Range and return
the full file as 200 instead (RFC 9110 §14.2). Malformed ranges still
get 416.

Fixes aio-libs#14124.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Oct 26, 2026
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.13%. Comparing base (b5acd43) to head (3cf027f).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14129   +/-   ##
=======================================
  Coverage   99.13%   99.13%           
=======================================
  Files         135      135           
  Lines       54630    54657   +27     
  Branches     2808     2809    +1     
=======================================
+ Hits        54158    54185   +27     
  Misses        357      357           
  Partials      115      115           
Flag Coverage Δ
Autobahn 21.53% <9.67%> (-0.01%) ⬇️
CI-GHA 99.06% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.75% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.53% <100.00%> (+<0.01%) ⬆️
OS-macOS 98.38% <100.00%> (-0.01%) ⬇️
Py-3.11 98.32% <100.00%> (+<0.01%) ⬆️
Py-3.12 98.39% <100.00%> (-0.02%) ⬇️
Py-3.13 98.39% <100.00%> (-0.02%) ⬇️
Py-3.14 98.45% <100.00%> (-0.02%) ⬇️
Py-3.15 98.45% <100.00%> (+<0.01%) ⬆️
Py-3.15t 98.30% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.12 97.00% <100.00%> (-0.01%) ⬇️
VM-macos-latest 98.38% <100.00%> (-0.01%) ⬇️
VM-ubuntu-latest 98.75% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 97.53% <100.00%> (+<0.01%) ⬆️
cython-coverage 84.03% <40.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@greptile-apps

greptile-apps Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 Tier: apexRetriggerView in Greptile

[Medium impact] The change affects clients receiving files through `FileResponse`.

Not ready to merge: valid spaced range lists still return 416, and required release updates remain missing.

Reviews (17) · Last reviewed commit: "Merge remote-tracking branch 'upstream/m..." · Reviewed by Greptile

# single range; a satisfiable multi-range must not become 416 (RFC 9110 §14.2
# allows ignoring Range and returning 200 with the full representation).
_MULTI_BYTE_RANGE_RE = re.compile(
r"^bytes=(?:\d+-\d*|-\d+)(?:,(?:\d+-\d*|-\d+))+$",

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.

P1 Spaced range lists still fail

A valid header such as Range: bytes=0-1, 4-5 still gets 416 for a file with at least six bytes. HTTP list syntax allows spaces or tabs around commas, but this pattern rejects them. Clients using that spacing still get an error instead of the full file. Allow that whitespace and add a regression test.

Knowledge Base Used: Server routing, responses, and static files

Comment on lines +358 to +359
if range_hdr is not None and _MULTI_BYTE_RANGE_RE.fullmatch(range_hdr):
pass

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 Backwards ranges download full files

The pattern matches bytes=100-0,0-1, although its first range runs backwards. The new fallback returns 200 with the full file instead of the previous 416, contrary to the promised unchanged behavior for malformed ranges. Check each bounded range's start and end before accepting the set, and add a regression test.

Knowledge Base Used: Server routing, responses, and static files

Comment thread CHANGES/14124.bugfix.rst
Comment on lines +1 to +3
Fixed ``FileResponse`` answering a well-formed multi-range request with
``416 Range Not Satisfiable`` instead of ignoring ``Range`` and returning the
full file -- by :user:`vyrnsynx`.

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 Required release updates are missing

AGENTS.md requires docs for user-visible API changes and news entries under both the issue and PR numbers. This status-code change has no docs/ update and only the issue-numbered fragment. Before merging, document the new behavior under FileResponse and add CHANGES/14129.bugfix.rst as a symlink to 14124.bugfix.rst.

Context Used: AGENTS.md (source)

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!

@codspeed

codspeed Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 49 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 102 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing vyrnsynx:fix/fileresponse-multi-range-14124 (3cf027f) with master (b5acd43)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Initialize start/end before parsing http_range so ignoring a
well-formed multi-range header can fall through to a full 200
without leaving those names unbound for mypy.
start is already declared at the top of _prepare_open_file;
only end needed a matching None default so the multi-range
ignore path stays typed without redefining start.
@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • The new multi-range fall-through serves exactly the same bytes as a plain GET with no Range header, so no client can read anything it could not read before.
  • Malformed Range headers (letters, reversed ranges without commas, empty specs, stray whitespace) still get 416; only comma-separated digit ranges fall through to 200.
  • The raw Range header is only matched against a regex and never copied into any response header or body, so there is no header injection.
  • The new regex has no catastrophic backtracking; adversarial inputs near the 8190-byte header cap match in under a millisecond.
  • Single-range behavior (206 responses, 416 for starts past the end of the file, If-Range gating) is unchanged.

…multi-range-14124

Signed-off-by: vyrnsynx <153433026+vyrnsynx@users.noreply.github.com>
…multi-range-14124

Signed-off-by: vyrnsynx <153433026+vyrnsynx@users.noreply.github.com>
…multi-range-14124

Signed-off-by: vyrnsynx <153433026+vyrnsynx@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileResponse answers a satisfiable multi-range request with 416

1 participant