Skip to content

fix: reject multipart ranges before validation - #661

Merged
jlizen merged 5 commits into
tower-rs:mainfrom
shblue21:reject-multipart-ranges-before-validation
Aug 16, 2026
Merged

jlizen merged 5 commits into
tower-rs:mainfrom
shblue21:reject-multipart-ranges-before-validation

Conversation

@shblue21

@shblue21 shblue21 commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

ServeDir / ServeFile do not support multipart range responses, but they validate multi-range Range headers before later rejecting them with 416 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):

  • parse the range header as before
  • return a local MultipleRangesNotSupported error when more than one range is present
  • only call validate(file_size) for single-range requests
  • preserve the multipart-specific 416 response body for valid multi-range requests

This 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-Range remain unchanged, but the 416 body becomes the multipart-specific message.

Refs: #660

@jplatte

jplatte commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Note: this changes the 416 response body for valid multipart range requests from Cannot serve multipart range requests to empty, since they now fall through the existing generic Err(_) -> 416 path in future.rs.

I think it would make sense to keep the specific error message body. Can you bring it back?

@shblue21

Copy link
Copy Markdown
Contributor Author

Thanks. I’ll keep the multipart-specific body so the response stays behavior-preserving. That will require a small future.rs update, and I’ll keep the patch as minimal as possible.

@shblue21

Copy link
Copy Markdown
Contributor Author

Added the small future.rs change. Multi-range requests still skip validate(file_size), and the regression test now checks the multipart-specific 416 body.

@jlizen jlizen left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@jlizen

jlizen commented Jul 5, 2026

Copy link
Copy Markdown
Member

@shblue21 are you still interested in pursuing this PR?

@shblue21

shblue21 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@jlizen Sorry, I missed this. I’ll take another look at the comments now.

@shblue21

Copy link
Copy Markdown
Contributor Author

@jlizen For this PR, I’d prefer to keep returning 416 and only skip the unnecessary validation. We can discuss ignoring the Range header separately.

@jlizen jlizen left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This appears to be dead code now?

)
}

#[tokio::test]

@jlizen jlizen Jul 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also appears unreachable?


#[tokio::test]
async fn multipart_range_valid_returns_multipart_error_body() {
let svc = ServeDir::new("..");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use REPO_ROOT and README_PATH instead?

@jlizen jlizen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks!

@jlizen
jlizen merged commit d154adb into tower-rs:main Aug 16, 2026
16 checks passed
markharding pushed a commit to futo-org/Harbor that referenced this pull request Oct 13, 2026
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 ([#&#8203;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 ([#&#8203;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 ([#&#8203;716])

#### Changed

- `fs`: the minimum `http-range-header` requirement is now 0.4.2 ([#&#8203;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 ([#&#8203;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 ([#&#8203;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 ([#&#8203;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 ([#&#8203;661])
- `fs`: range error responses no longer carry representation headers such as
  `Content-Type` and `Content-Encoding` ([#&#8203;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 ([#&#8203;714])

[#&#8203;661]: tower-rs/tower-http#661

[#&#8203;712]: tower-rs/tower-http#712

[#&#8203;714]: tower-rs/tower-http#714

[#&#8203;716]: tower-rs/tower-http#716

[#&#8203;718]: tower-rs/tower-http#718

[#&#8203;722]: tower-rs/tower-http#722

[#&#8203;727]: tower-rs/tower-http#727

[#&#8203;728]: tower-rs/tower-http#728

#### All the changes

- chore(deps): bump actions/checkout from 6 to 7 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;708](tower-rs/tower-http#708)
- fix(compression): reject non-empty data frames after codec is EOF by [@&#8203;seanmonstar](https://github.com/seanmonstar) in [#&#8203;712](tower-rs/tower-http#712)
- Remove Clone derivations from SetMultipleResponseHeader\* types by [@&#8203;skeet70](https://github.com/skeet70) in [#&#8203;714](tower-rs/tower-http#714)
- feat(request-id): Allow request-id constructor in const context by [@&#8203;tottoto](https://github.com/tottoto) in [#&#8203;716](tower-rs/tower-http#716)
- fs: Avoid unnecessary conversions between SystemTime and HttpDate by [@&#8203;tottoto](https://github.com/tottoto) in [#&#8203;717](tower-rs/tower-http#717)
- fs: Use HeaderValue::is_empty to check for empty values by [@&#8203;tottoto](https://github.com/tottoto) in [#&#8203;720](tower-rs/tower-http#720)
- fs: Use HeaderValue::to_str for date headers by [@&#8203;tottoto](https://github.com/tottoto) in [#&#8203;721](tower-rs/tower-http#721)
- chore(deps): bump taiki-e/install-action from 2 to 2.85.4 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;715](tower-rs/tower-http#715)
- chore(deps): bump taiki-e/install-action from 2.85.4 to 2.85.12 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;723](tower-rs/tower-http#723)
- docs(example)/custom future with multiple bodies by [@&#8203;Reza-Darius](https://github.com/Reza-Darius) in [#&#8203;711](tower-rs/tower-http#711)
- fix(decompression): don't end the body on an empty data frame by [@&#8203;clemenslosbichler-cloud](https://github.com/clemenslosbichler-cloud) in [#&#8203;722](tower-rs/tower-http#722)
- Propagate ServeDir::try_call I/O errors by [@&#8203;Boulea7](https://github.com/Boulea7) in [#&#8203;718](tower-rs/tower-http#718)
- ci: Update to cargo-check-external-types 0.5.0 by [@&#8203;tottoto](https://github.com/tottoto) in [#&#8203;724](tower-rs/tower-http#724)
- fix: reject multipart ranges before validation by [@&#8203;shblue21](https://github.com/shblue21) in [#&#8203;661](tower-rs/tower-http#661)
- chore(deps): bump taiki-e/install-action from 2.85.12 to 2.86.3 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;726](tower-rs/tower-http#726)
- feat(services): configure directory redirects by [@&#8203;BreezeDelegate](https://github.com/BreezeDelegate) in [#&#8203;728](tower-rs/tower-http#728)
- Allow ignoring unsupported multi-range requests by [@&#8203;BreezeDelegate](https://github.com/BreezeDelegate) in [#&#8203;727](tower-rs/tower-http#727)
- chore(deps): bump taiki-e/install-action from 2.86.3 to 2.86.8 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;730](tower-rs/tower-http#730)
- chore(release): prepare 0.7.1 by [@&#8203;jlizen](https://github.com/jlizen) in [#&#8203;729](tower-rs/tower-http#729)

#### New Contributors

- [@&#8203;skeet70](https://github.com/skeet70) made their first contribution in [#&#8203;714](tower-rs/tower-http#714)
- [@&#8203;Reza-Darius](https://github.com/Reza-Darius) made their first contribution in [#&#8203;711](tower-rs/tower-http#711)
- [@&#8203;clemenslosbichler-cloud](https://github.com/clemenslosbichler-cloud) made their first contribution in [#&#8203;722](tower-rs/tower-http#722)
- [@&#8203;Boulea7](https://github.com/Boulea7) made their first contribution in [#&#8203;718](tower-rs/tower-http#718)
- [@&#8203;shblue21](https://github.com/shblue21) made their first contribution in [#&#8203;661](tower-rs/tower-http#661)
- [@&#8203;BreezeDelegate](https://github.com/BreezeDelegate) made their first contribution in [#&#8203;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]>
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.

3 participants