Repository navigation
fix: reject multipart ranges before validation - #661
Conversation
I think it would make sense to keep the specific error message body. Can you bring it back? |
|
Thanks. I’ll keep the multipart-specific body so the response stays behavior-preserving. That will require a small |
|
Added the small |
There was a problem hiding this comment.
@shblue21 do you see a reason not to support the behavior of flattening to single-part GET instead? Even if opt-in due to DOS risk (in which case your approach seems fine for rejection, though I have a small nit on internal error modeling).
IETF seems to prefer that,the RFC states:
A server MAY ignore the Range header field. However, origin servers
and intermediate caches ought to support byte ranges when possible,
since Range supports efficient recovery from partially failed
transfers and partial retrieval of large representations. A server
MUST ignore a Range header field received with a request method other
than GET.
Range requests are an OPTIONAL feature of HTTP, designed so that recipients not implementing this feature (or not supporting it for the target resource) can respond as if it is a normal GET request without impacting interoperability.
| if parsed.ranges.len() > 1 { | ||
| // ServeDir/ServeFile do not support multipart responses, so reject | ||
| // multi-range requests before validate() runs overlap checks. | ||
| return Err(RangeUnsatisfiableError::OverlappingRanges); |
There was a problem hiding this comment.
This doesn't seem like quite the right error, this refers to overlapping ranges within a multipart request. Meanwhile we don't support them at all.
Can we have a local error type instead that more clearly denotes what's happening, if we preserve this behavior?
|
@shblue21 are you still interested in pursuing this PR? |
|
@jlizen Sorry, I missed this. I’ll take another look at the comments now. |
|
@jlizen For this PR, I’d prefer to keep returning 416 and only skip the unnecessary validation. We can discuss ignoring the |
There was a problem hiding this comment.
The only blocking feedback is that we don't have proper test coverage for this new behavior, the rest are quick nits.
I'm fine to punt on changing away from the 416 representation in favor of this refactor staying perf only.Do you have cycles to file the issue? I can if not. Ideally we would serve the non-range 200 where possible to stay RFC compliant.
Also given such a change change, for cases where we do still have an unsatisfiable 416, we are improperly preserving content headers here (Content-Type, Content-Encoding, etc), which could result in the plain text (non-gzipped) body being improperly parsed if the original request was with gzip or a different content type.
(And the above header issue applies to the present handling too, but it's fine to keep your PR scoped more narrowly to saving the extra validate() if preferred)
There was a problem hiding this comment.
This appears to be dead code now?
| ) | ||
| } | ||
|
|
||
| #[tokio::test] |
There was a problem hiding this comment.
This tests non-overlapping multi-range, but this PR is mostly touching the overlapping case (which previously reached validate()). It's good to have, thanks, but we should cover the new behavior as well.
(IE, this test would pass on main even without this change)
|
|
||
| #[tokio::test] | ||
| async fn multipart_range_valid_returns_multipart_error_body() { | ||
| let svc = ServeDir::new(".."); |
There was a problem hiding this comment.
Can we use REPO_ROOT and README_PATH instead?
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [tower-http](https://github.com/tower-rs/tower-http) | dependencies | patch | `0.7.0` → `0.7.1` | --- ### Release Notes <details> <summary>tower-rs/tower-http (tower-http)</summary> ### [`v0.7.1`](https://github.com/tower-rs/tower-http/releases/tag/tower-http-0.7.1) [Compare Source](tower-rs/tower-http@tower-http-0.7.0...tower-http-0.7.1) #### Added - `fs`: add `ServeDir::redirect_to_trailing_slash()` to serve directory indexes directly instead of first redirecting to the trailing-slash path. The redirect remains the default ([#​728]) - `fs`: add `ignore_multi_range_requests()` to `ServeDir` and `ServeFile`, serving the full representation when a request asks for multiple byte ranges. The existing `416 Range Not Satisfiable` response remains the default ([#​727]) - `request-id`: the constructors and accessors on the request-id layers, services, and `RequestId` are now `const fn`, so they can be used in const context ([#​716]) #### Changed - `fs`: the minimum `http-range-header` requirement is now 0.4.2 ([#​661]) #### Fixed - **behavioral change:** `fs`: make `ServeDir::try_call` propagate expected filesystem I/O errors when no fallback is configured, as documented, instead of converting them to `404 Not Found` responses ([#​718]) - `decompression`: don't end the body when a data frame with no remaining bytes arrives after the decompressor reports end-of-stream. Trailers following such a frame were dropped and could not be recovered ([#​722]) - `decompression`: return a body error when a data frame with remaining bytes arrives after the decompressor reports end-of-stream, rather than silently truncating. This regressed in 0.7.0 ([#​712]) - `fs`: multipart range requests are now rejected before range validation, so they consistently return `416 Range Not Satisfiable` with a `Cannot serve multipart range requests` body instead of a generic unsatisfiable-range response ([#​661]) - `fs`: range error responses no longer carry representation headers such as `Content-Type` and `Content-Encoding` ([#​727]) - `set-header`: `SetMultipleResponseHeadersLayer` and `SetMultipleResponseHeader` are now `Clone` regardless of the response body type, matching the fix applied to the request-side types in 0.7.0 ([#​714]) [#​661]: tower-rs/tower-http#661 [#​712]: tower-rs/tower-http#712 [#​714]: tower-rs/tower-http#714 [#​716]: tower-rs/tower-http#716 [#​718]: tower-rs/tower-http#718 [#​722]: tower-rs/tower-http#722 [#​727]: tower-rs/tower-http#727 [#​728]: tower-rs/tower-http#728 #### All the changes - chore(deps): bump actions/checkout from 6 to 7 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​708](tower-rs/tower-http#708) - fix(compression): reject non-empty data frames after codec is EOF by [@​seanmonstar](https://github.com/seanmonstar) in [#​712](tower-rs/tower-http#712) - Remove Clone derivations from SetMultipleResponseHeader\* types by [@​skeet70](https://github.com/skeet70) in [#​714](tower-rs/tower-http#714) - feat(request-id): Allow request-id constructor in const context by [@​tottoto](https://github.com/tottoto) in [#​716](tower-rs/tower-http#716) - fs: Avoid unnecessary conversions between SystemTime and HttpDate by [@​tottoto](https://github.com/tottoto) in [#​717](tower-rs/tower-http#717) - fs: Use HeaderValue::is_empty to check for empty values by [@​tottoto](https://github.com/tottoto) in [#​720](tower-rs/tower-http#720) - fs: Use HeaderValue::to_str for date headers by [@​tottoto](https://github.com/tottoto) in [#​721](tower-rs/tower-http#721) - chore(deps): bump taiki-e/install-action from 2 to 2.85.4 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​715](tower-rs/tower-http#715) - chore(deps): bump taiki-e/install-action from 2.85.4 to 2.85.12 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​723](tower-rs/tower-http#723) - docs(example)/custom future with multiple bodies by [@​Reza-Darius](https://github.com/Reza-Darius) in [#​711](tower-rs/tower-http#711) - fix(decompression): don't end the body on an empty data frame by [@​clemenslosbichler-cloud](https://github.com/clemenslosbichler-cloud) in [#​722](tower-rs/tower-http#722) - Propagate ServeDir::try_call I/O errors by [@​Boulea7](https://github.com/Boulea7) in [#​718](tower-rs/tower-http#718) - ci: Update to cargo-check-external-types 0.5.0 by [@​tottoto](https://github.com/tottoto) in [#​724](tower-rs/tower-http#724) - fix: reject multipart ranges before validation by [@​shblue21](https://github.com/shblue21) in [#​661](tower-rs/tower-http#661) - chore(deps): bump taiki-e/install-action from 2.85.12 to 2.86.3 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​726](tower-rs/tower-http#726) - feat(services): configure directory redirects by [@​BreezeDelegate](https://github.com/BreezeDelegate) in [#​728](tower-rs/tower-http#728) - Allow ignoring unsupported multi-range requests by [@​BreezeDelegate](https://github.com/BreezeDelegate) in [#​727](tower-rs/tower-http#727) - chore(deps): bump taiki-e/install-action from 2.86.3 to 2.86.8 by [@​dependabot](https://github.com/dependabot)\[bot] in [#​730](tower-rs/tower-http#730) - chore(release): prepare 0.7.1 by [@​jlizen](https://github.com/jlizen) in [#​729](tower-rs/tower-http#729) #### New Contributors - [@​skeet70](https://github.com/skeet70) made their first contribution in [#​714](tower-rs/tower-http#714) - [@​Reza-Darius](https://github.com/Reza-Darius) made their first contribution in [#​711](tower-rs/tower-http#711) - [@​clemenslosbichler-cloud](https://github.com/clemenslosbichler-cloud) made their first contribution in [#​722](tower-rs/tower-http#722) - [@​Boulea7](https://github.com/Boulea7) made their first contribution in [#​718](tower-rs/tower-http#718) - [@​shblue21](https://github.com/shblue21) made their first contribution in [#​661](tower-rs/tower-http#661) - [@​BreezeDelegate](https://github.com/BreezeDelegate) made their first contribution in [#​728](tower-rs/tower-http#728) </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - "before 9am on tuesday" - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xMzMuMCIsInVwZGF0ZWRJblZlciI6IjQ0LjE1NS4xIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZSJdfQ==--> Reviewed-on: https://code.futo.org/harbor/harbor/pulls/1799 Reviewed-by: Thomas de Zeeuw <[email protected]>
Motivation
ServeDir/ServeFiledo not support multipart range responses, but they validate multi-rangeRangeheaders before later rejecting them with416 Range Not Satisfiable.This means requests that are guaranteed to be rejected still pay the cost of
validate(file_size), including its pairwise overlap checks.Solution
Reject multiple ranges before calling
validate(file_size):MultipleRangesNotSupportederror when more than one range is presentvalidate(file_size)for single-range requests416response body for valid multi-range requestsThis also adds regression coverage for the multipart-specific response body.
Note: syntactically parsed multi-range requests that would previously fail semantic validation, such as overlapping or reversed ranges, are now rejected as unsupported multiple ranges before validation. The status and
Content-Rangeremain unchanged, but the416body becomes the multipart-specific message.Refs: #660