Skip to content

rgw: parse the Range header field per RFC 9110 - #71300

Open
MorganaFuture wants to merge 2 commits into
ceph:mainfrom
MorganaFuture:wip-13412
Open

MorganaFuture wants to merge 2 commits into
ceph:mainfrom
MorganaFuture:wip-13412

Conversation

@MorganaFuture

Copy link
Copy Markdown

Related to: https://tracker.ceph.com/issues/13412

RGWGetObj::parse_range() searches the header value for bytes=, then finds the first - after it and runs atoll() over the text on each side. atoll() stops at the first character it cannot use and tells nobody, so a malformed range-spec comes out looking plausible. Driving the current parser over a table of headers: bytes=abc-def is served as byte 0, bytes=0-100xyz as 0-100, bytes=bad,0-100 as 0-100, and bytes=- as byte 1 through the end of the object. The search for bytes= is unanchored too, so xbytes=0-100 is taken for a byte range. And because it keys on a single -, a range-set with more than one range-spec is never recognised as one: bytes=0-100,300-400 yields 0-100 and the rest of the header disappears during parsing.

The first commit adds rgw_parse_byte_ranges(), a header-only parser for the grammar in RFC 9110 14.1.1 that reads the whole range-set. It distinguishes the three outcomes a caller has to treat differently: a range unit that is not bytes, which 14.2 requires an origin server to ignore; an invalid ranges-specifier, which a server may ignore or reject; and a well formed range-set longer than the caller wants to hold, which is what a denial-of-service attempt looks like (17.15). The second commit moves parse_range() onto it. No caller is added in the first commit and nothing outside parse_range() changes in the second.

This does not implement multipart/byteranges, and multi-range behaviour is deliberately unchanged: the first range-spec is served and the rest ignored, which stays within 14.1.2 and 15.3.7 since a 206 is self-descriptive. I asked on the tracker which shape would be wanted for the real feature, because AWS does not support multi-range at all while upstream Swift does and doc/radosgw/swift.rst already advertises it. This series is the parsing groundwork either answer needs.

Positions too large for off_t are clamped, not refused. main gets that right by accident, since atoll() saturates at LLONG_MAX and range_to_ofs() then pulls the value back inside the object, and RFC 9110 14.1.2 requires it: an out-of-length last-pos means the remainder of the representation and an over-long suffix-length means the whole representation. Only first-pos is left for range_to_ofs() to reject once it knows the object size. Rejecting these would have turned a conformant 206 into a 416, so the parser saturates rather than failing, and the "invalid if last-pos is less than first-pos" rule falls back to comparing digits when either position has saturated.

Two classes of request change behaviour, both noted in PendingReleaseNotes. Malformed range-specs now take the path the function already used for the rejects it did catch, such as bytes=5-2, so rgw_ignore_get_invalid_range still decides between -ERANGE and ignoring the header; no second policy is introduced. And the range unit is matched in full rather than searched for, so a unit ending in but not equal to bytes is ignored per 14.2 instead of being parsed as a byte range.

To size the change, the text of parse_range() was compiled against stub members alongside a replica of the implementation it replaces and the two diffed over 45 header values under both settings of rgw_ignore_get_invalid_range: 65 of 90 outcomes identical, the 25 that differ coming from 13 inputs, 11 of them malformed specifiers and 2 units ending in bytes. bytesx=0-100 is unaffected, which pins that second class to exactly the shape it claims. Every well formed byte range and every out-of-range position produces identical state.

Against a vstart cluster serving a 100-byte object:

Range header                                   status Content-Range     body
bytes=0-99                                     206    bytes 0-99/100    100 B
bytes=-10                                      206    bytes 90-99/100    10 B
bytes=0-99999999999999999999                   206    bytes 0-99/100    100 B
bytes=-99999999999999999999                    206    bytes 0-99/100    100 B
bytes=100-                                     416    -
bytes=99999999999999999999-                    416    -
bytes=0-10,20-30                               206    bytes 0-10/100     11 B
bytes=abc-def                                  416    -
bytes=0-100xyz                                 416    -
bytes=5-2                                      416    -
xbytes=0-10                                    200    bytes 0-99/100    100 B

One thing that turned up while checking those responses and is worth raising separately: rgw_rest_s3.cc emits Content-Range under if (range_str), keying on the header being present rather than on partial_content, so a request whose Range was ignored still carries a Content-Range alongside its 200. That is pre-existing, items=0-10 behaves the same way on main, but this series does route a new class of request through it. I left it alone because the one-line fix would change every ignored-Range response, not just the ones here, and that seemed like a separate call to make.

The full s3-tests suite has not been run against this.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)

RGWGetObj::parse_range() reads the Range header field by hand: it searches the
value for "bytes=", then finds the first '-' after it and runs atoll() over the
text on each side. atoll() stops at the first character it cannot use and
reports it to nobody, so malformed range-specs are accepted as plausible ones.
Driving the current parser over a table of headers shows "bytes=abc-def"
accepted as byte 0, "bytes=0-100xyz" accepted as 0-100, and "bytes=-" accepted
as byte 1 through the end of the object. The search for "bytes=" is unanchored,
so a unit that merely ends in it, such as "xbytes=0-100", is taken for a byte
range. And keying on a single '-' means a range-set holding more than one
range-spec is never seen as one: for "bytes=0-100,300-400" the parser yields
0-100 and the rest of the header is dropped without the caller ever learning it
was there.

Add rgw_parse_byte_ranges(), a header-only parser for the grammar in RFC 9110
Section 14.1.1 that covers the whole range-set rather than its first element.
It separates the three outcomes a caller has to act on differently: a range
unit that is not "bytes", which Section 14.2 requires an origin server to
ignore; a syntactically invalid ranges-specifier, which a server may ignore or
reject; and a well formed range-set longer than the caller is willing to hold,
which is the shape a denial-of-service attempt takes (Section 17.15).

first-pos, last-pos and suffix-length are 1*DIGIT with no upper bound, so a
value too large for uint64_t is well formed and may not be rejected. It is
saturated instead, which preserves its meaning wherever it appears: Section
14.1.2 reads an out-of-length last-pos as the remainder of the representation
and an over-long suffix-length as the whole representation, and a first-pos
past the end is unsatisfiable either way. Saturation costs the ordering of two
such values, so "an int-range is invalid if the last-pos value is present and
less than the first-pos" is decided by comparing the digits when either
position has saturated. An absent last-pos is carried as a flag rather than a
sentinel value, so a saturated last-pos stays distinct from an open-ended
range.

Positions are not resolved against the length of the selected representation,
and no policy is applied to a range-set with more than one element. Both are
left to the caller, so this commit changes no behaviour and adds no caller.

Tests: new unittest_rgw_range_parse, 7 test blocks over 53 table cases covering
the grammar, saturation and the ordering of saturated positions, unit matching,
the range-set limit and output clearing. The parser has no ceph dependencies,
so the same table also runs standalone: g++ 13.3.0 and Apple clang 21.0.0, both
at -std=c++17 and -std=c++20, clean under -Wall -Wextra -Wpedantic, 101 checks
passing under -fsanitize=address,undefined. That table caught a real defect
while the parser was being written: a range-set whose second element was
invalid returned failure but left the first element in the caller's vector.

Related-to: https://tracker.ceph.com/issues/13412
Signed-off-by: MorganaFuture <[email protected]>
Move RGWGetObj::parse_range() onto rgw_parse_byte_ranges() instead of searching
the header value for "bytes=" and running atoll() over each side of the first
'-' that follows.

Behaviour for a well formed byte range is unchanged, including the handling of
a range-set with more than one range-spec: the first one is served and the rest
ignored. That stays within RFC 9110 15.3.7, which lets a server satisfy a
subset of the requested ranges because a 206 response is self-descriptive.
Serving the whole range-set needs multipart/byteranges and a decision about S3
compatibility, so it is left to the tracker below.

A position too large for off_t is clamped rather than refused. main did that by
accident, since atoll() saturated at LLONG_MAX and range_to_ofs() then brought
the value back inside the object, and RFC 9110 14.1.2 requires it: an
out-of-length last-pos means the remainder of the representation and an
over-long suffix-length means the whole representation. Only first-pos is left
for range_to_ofs() to reject, once it knows the size of the object. Refusing
these instead would have turned a conformant 206 into a 416.

Two classes of request do change. Malformed range-specs stop being accepted as
plausible ones: atoll() ignores trailing characters, so "bytes=abc-def" was
served as byte 0, "bytes=0-100xyz" as 0-100, "bytes=bad,0-100" as 0-100 and
"bytes=-" as byte 1 through the end of the object. And the range unit is now
matched in full rather than searched for, so "xbytes=0-100" is no longer taken
for a byte range; RFC 9110 14.2 requires an origin server to ignore a range
unit it does not understand, which means the whole object with 200.

Malformed specifiers take the path the function already used for the rejects it
did catch, such as "bytes=5-2", so rgw_ignore_get_invalid_range still decides
whether an invalid ranges-specifier is refused with -ERANGE or ignored in
favour of the whole object. No second policy is added. Both changes are noted
in PendingReleaseNotes.

partial_content is also set only once a range has been fully accepted, so a
failure no longer leaves it half-set the way "bytes=5-2" did.

Tests: unittest_rgw_range_parse covers the parser. For this change, the text of
parse_range() was extracted from rgw_op.cc, compiled against stub members
alongside a replica of the implementation it replaces, and the two were diffed
over 45 Range header values under both settings of rgw_ignore_get_invalid_range:
65 of the 90 outcomes identical, the 25 that differ coming from 13 inputs, of
which 11 are malformed ranges-specifiers and 2 are units ending in but not equal
to "bytes". "bytesx=0-100" is unaffected, which pins that second class to the
shape it claims. Every well formed byte range and every out-of-range position
produces identical state. Built and run on Linux under g++ 13.3.0 as well as
Apple clang 21.0.0.

Also exercised against a real gateway: a vstart cluster (1 mon, 1 osd, 1 mgr,
rgw) serving a 100-byte object answers "bytes=0-99999999999999999999" and
"bytes=-99999999999999999999" with 206 and Content-Range: bytes 0-99/100, so
the clamping reaches the wire as RFC 9110 14.1.2 requires; "bytes=abc-def",
"bytes=0-100xyz", "bytes=-" and "bytes=5-2" answer 416; "bytes=0-10,20-30"
answers 206 with bytes 0-10/100; and "xbytes=0-10" answers 200 with the whole
object. The full s3-tests suite has not been run.

Related-to: https://tracker.ceph.com/issues/13412
Signed-off-by: MorganaFuture <[email protected]>
@MorganaFuture
MorganaFuture requested a review from a team as a code owner August 24, 2026 21:31
@github-actions

Copy link
Copy Markdown

Thank you for your contribution. Since you are not yet a member of the Ceph organization with write permissions on ceph/ceph.git, our CI will not automatically run. Any member of the Ceph organization may label this PR ci-approved to allow Jenkins CI jobs to run.

@adamemerson

Copy link
Copy Markdown
Contributor

ok to test

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@RaminNietzsche

Copy link
Copy Markdown
Member

Ran the current parser against a vstart cluster on main (5ab2b0d) as a baseline for this change; all of these are answered 206 today:

Range answer on main
bytes=0-1,4-5 206, Content-Range: bytes 0-1/10
bytes=abc-def 206, Content-Range: bytes 0-0/10
b=0-1 206, Content-Range: bytes 0-1/10
bytes= 0-1 206, Content-Range: bytes 0-1/10

On multi-range: RFC 9110 14.2 lets a server ignore Range entirely, so answering 200 for a range-set with more than one range-spec is also compliant, and it avoids a 206 that holds less than the client asked for. Whichever way it goes, it would help to state the multi-range behavior in the PendingReleaseNotes entry so it is a documented decision rather than a side effect of the parser.

Unrelated to the parser, but in the same response path: the 416 answer is missing Content-Range: bytes */<length> (RFC 9110 14.4). Filed separately as https://tracker.ceph.com/issues/82229 / #73200 so it does not grow this PR.

Probe script for the broader RFC 9110 checks (including Range cases above): https://gist.github.com/RaminNietzsche/818ad8dd6b8d8093560183cd09d2b169

@RaminNietzsche

Copy link
Copy Markdown
Member

RFC 9110 RGW audit — series context

This PR is one focused change from an RFC 9110 conformance review of RGW (S3, Swift, Admin, STS). Each PR is meant to review and merge on its own; there is no batch dependency unless called out below.

Item PR Tracker
T1 — 416 + Content-Range #73200 #82229
T4b — CopyObject source preconditions → 412 #73201 #82230
T4a — If-Match before If-Modified-Since #73205 #82233 (Fixes #19965)
T5 — 405 + Allow, 501 for unknown methods #73206 #82235
T6 — Swift 401 + WWW-Authenticate #73209 #82242

Same audit, already tracked on other PRs (not part of this series authorship):

Item PR Tracker
T2 — Range header parsing #71300 #13412
T3 — whole-tag If-Match / If-None-Match #72117 #80924

Follow-ups from the audit not opened yet: T4d If-Range (best after #71300 and #72117 land), T8b header field section too large → 431, T9 admin GET ?sync=true side effects.

Shared repro script and sample output: https://gist.github.com/RaminNietzsche/818ad8dd6b8d8093560183cd09d2b169 (rgw-rfc9110-repro.py; cases T1–T8).

Audit cross-link (T2): This PR covers Range parsing (RFC 9110 §14). From the same audit: unsatisfiable-range Content-Range is handled in #73200 / #82229 so it does not need to ride along here. If-Range implementation (T4d) is planned as a follow-up after Range and ETag fixes (#72117) land.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants