Skip to content

Return 416 for invalid range requests in Plug.Static#1317

Merged
josevalim merged 2 commits into
elixir-plug:mainfrom
braidonw:main
Jun 18, 2026
Merged

Return 416 for invalid range requests in Plug.Static#1317
josevalim merged 2 commits into
elixir-plug:mainfrom
braidonw:main

Conversation

@braidonw

Copy link
Copy Markdown
Contributor

Currently, when receiving a range request, Plug.Static never validates that the range start falls within the file.

When it receives a Range header where the first-byte position is at or past the end of the file, it produces a {start, end} pair where start > end. send_range/6 then computes a negative length (range_end - range_start + 1) and forwards it to the adapter's send_file/6, which raises a RuntimeError.

This change adds logic to reject invalid ranges before trying to handle a negative length. It will now return a 416 + Content-Range: bytes */<size> instead of a 500, per RFC 9110 section 14.4.

I have also added a carve-out for empty files, similar to some other languages / libraries (e.g. Go's net/http), where the range parameter is ignored. This is because a) many clients attach a range header to all requests, and b) there's no valid range for an empty file, so we might as well just return the file they've requested.

@josevalim josevalim merged commit 4bb005a into elixir-plug:main Jun 18, 2026
2 checks passed
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

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.

2 participants