Skip to content

ServeDir/ServeFile validate multipart Range requests before rejecting them with 416 #660

Closed

Description

@shblue21
  • I have looked for existing issues (including closed) about this

Bug Report

Version

tower-http 0.6.8

cargo tree | grep tower-http:

  • tower-http v0.6.8

Platform

Linux 6.14.0-1017-oracle (Ubuntu 24.04.1)

Crates

tower-http (services::fs::ServeDir, services::fs::ServeFile)

Description

ServeDir / ServeFile do not support multipart range responses, but they still parse and validate multipart Range headers before later rejecting them with 416 Range Not Satisfiable.

Current flow is roughly:

  • open_file.rs::try_parse_range() calls http_range_header::parse_range_header(...).and_then(|p| p.validate(file_size))
  • later, future.rs::build_response() rejects ranges.len() > 1 with 416

So a request like:

Range: bytes=0-0,2-2,4-4,6-6

is guaranteed to be rejected, but still pays multipart validation cost first, with the validation step doing O(n²) pairwise overlap checks across ranges.

Since multipart responses are not supported here, it seems better to reject ranges.len() > 1 before calling validate(file_size).

A minimal fix seems to be:

  • parse the header
  • if there is more than one range, immediately return unsatisfiable
  • only call validate(file_size) for single-range requests

Activity

  1. changed the title [-]ServeDir/ServeFile do unnecessary multipart Range validation before returning 416[/-] [+]ServeDir/ServeFile validate multipart Range requests before rejecting them with 416[/+] on Apr 20, 2026
  2. seanmonstar commented on Apr 20, 2026

    @seanmonstar
    Collaborator

    It sounds reasonable to return an error after noticing a second range. Want to submit a PR?

  3. shblue21 commented on Apr 20, 2026

    @shblue21
    ContributorAuthor

    Sure, I can send a PR.

  4. jlizen commented on Aug 16, 2026

    @jlizen
    Member

    fixed by #661

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions