Fix truncated byte range responses in DefaultServlet - #1067
Open
aoto-tech wants to merge 1 commit into
Open
Conversation
Generated-by: OpenAI Codex
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.
Summary
Fix
DefaultServletbyte range responses so a legal short read from theresource stream no longer truncates the response. Also report a premature end
of stream as an I/O error instead of passing
-1as the output length andtriggering an unchecked exception.
Impact
Range requests are commonly used for resumed downloads and media seeking. In
these flows, a valid range can produce an incomplete 206 response if the
resource stream performs a legal short read.
DefaultServletwrites the firstshort chunk and reports no I/O error, leaving the client with fewer bytes than
the selected range.
Problem
DefaultServlet.copyNoThrow(InputStream, ServletOutputStream, long, long)usesthe length returned by the previous read as part of its loop condition:
InputStream.read(byte[])may legally return a positive value smaller than therequested length without reaching EOF. The current code writes the short chunk
and exits the loop while
bytesToReadis still positive. The method returnsnull, leaving the caller with no I/O error for the incomplete 206 responsebody.
If the stream reaches EOF before the requested range is complete,
read()returns
-1. The current comparison enters the write branch and callsServletOutputStream.write(..., -1), resulting in anIndexOutOfBoundsException.Both the single-range and multipart-range paths use this method. Production
callers wrap the resource stream in
BufferedInputStream, but the wrapper givesno guarantee of filling the requested buffer on every read.
Reproduction
A focused reproducer models this flow with an eight-byte resource, a
Range: bytes=0-7request, and a buffered resource stream returning at mostthree bytes per read:
For a source ending before the requested range is complete, the current
implementation throws:
The premature EOF case models a resource stream ending after the range length
has been established.
Change
The range copy loop is now driven only by the number of bytes remaining. Each
read is limited to the smaller of the buffer size and the remaining range
length. A positive short read is written and copying continues. Premature EOF
returns an
EOFExceptionthrough the existingIOExceptionresult path.No public API is changed. Streams already filling the requested buffer keep the
same behavior.
Tests