Repository navigation
rgw: parse the Range header field per RFC 9110 - #71300
MorganaFuture wants to merge 2 commits into
Conversation
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]>
|
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 |
|
ok to test |
|
This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved |
|
This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved |
|
Ran the current parser against a vstart cluster on main (5ab2b0d) as a baseline for this change; all of these are answered 206 today:
On multi-range: RFC 9110 14.2 lets a server ignore Unrelated to the parser, but in the same response path: the 416 answer is missing Probe script for the broader RFC 9110 checks (including Range cases above): https://gist.github.com/RaminNietzsche/818ad8dd6b8d8093560183cd09d2b169 |
RFC 9110 RGW audit — series contextThis 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.
Same audit, already tracked on other PRs (not part of this series authorship):
Follow-ups from the audit not opened yet: T4d Shared repro script and sample output: https://gist.github.com/RaminNietzsche/818ad8dd6b8d8093560183cd09d2b169 ( Audit cross-link (T2): This PR covers Range parsing (RFC 9110 §14). From the same audit: unsatisfiable-range |
Related to: https://tracker.ceph.com/issues/13412
RGWGetObj::parse_range()searches the header value forbytes=, then finds the first-after it and runsatoll()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-defis served as byte 0,bytes=0-100xyzas 0-100,bytes=bad,0-100as 0-100, andbytes=-as byte 1 through the end of the object. The search forbytes=is unanchored too, soxbytes=0-100is 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-400yields 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 notbytes, 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 movesparse_range()onto it. No caller is added in the first commit and nothing outsideparse_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 anddoc/radosgw/swift.rstalready advertises it. This series is the parsing groundwork either answer needs.Positions too large for
off_tare clamped, not refused. main gets that right by accident, sinceatoll()saturates atLLONG_MAXandrange_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 forrange_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 asbytes=5-2, sorgw_ignore_get_invalid_rangestill decides between-ERANGEand 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 tobytesis 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 ofrgw_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 inbytes.bytesx=0-100is 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:
One thing that turned up while checking those responses and is worth raising separately:
rgw_rest_s3.ccemitsContent-Rangeunderif (range_str), keying on the header being present rather than onpartial_content, so a request whose Range was ignored still carries aContent-Rangealongside its 200. That is pre-existing,items=0-10behaves 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