Return 416 for invalid range requests in Plug.Static#1317
Merged
Conversation
Member
|
💚 💙 💜 💛 ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/6then 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.