Skip to content

Fix chunked multipart bodies with empty or partly read files - #373

Open
orien wants to merge 1 commit into
ruby:masterfrom
orien:chunked-multipart-empty-file
Open

orien wants to merge 1 commit into
ruby:masterfrom
orien:chunked-multipart-empty-file

Conversation

@orien

@orien orien commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Context

Sending a multipart form with Transfer-Encoding: chunked corrupts the request when a file part is empty or already partly read. Each File or StringIO value is written as one chunk sized by #size, and that size can be wrong:

  • An empty file gives a zero-size chunk, 0\r\n\r\n, which marks the end of a chunked body. The server stops reading there and treats the remaining form parts as a new request on the same connection.
  • A partly read value declares more bytes than it sends. The server then reads the start of the next part as file data.

Change

Every IO value is now streamed through flush_buffer, which sizes each chunk by the bytes it actually read. The special case for values that respond to size is removed.

  • flush_buffer skips empty buffers, so the only zero-size chunk is the final one.
  • flush_buffer no longer clears the strings it writes, so a value whose read returns a frozen string is sent instead of raising FrozenError.
  • A new test covers an empty file, a partly read file, a partly read StringIO and an object whose first read returns "". It checks the decoded body and that nothing follows the last chunk.
  • The stale TODO: test with chunked is removed.

Considerations

  • Large chunked file uploads now make more, smaller socket writes: one per 4 KB read instead of IO.copy_stream's 16 KB writes. Each chunk's size line, data and CRLF go out in a single write, as Chunker#write does.
  • Copying through Chunker with IO.copy_stream would restore larger writes, but Chunker#write has the same empty-chunk problem, so it would need the same guard first.
  • Non-chunked multipart bodies are unchanged; that path never reached the removed branch.

@orien
orien force-pushed the chunked-multipart-empty-file branch 2 times, most recently from 7ed6b0f to 25ba273 Compare November 27, 2026 13:42
@orien orien changed the title Frame file parts of a chunked multipart body by bytes read Fix chunked multipart bodies with empty or partly read files Nov 27, 2026
@orien
orien force-pushed the chunked-multipart-empty-file branch 2 times, most recently from 58d9de0 to bc1bd40 Compare November 27, 2026 13:48
With Transfer-Encoding: chunked, a File or StringIO value in set_form was
written as one chunk whose size came from #size. An empty file produced
a zero-size chunk, which ends the chunked body, so the server read the
remaining form parts as the start of a new request. For a value whose
read position was past the start, the chunk-size line counted the whole
file but only the unread bytes followed. Separately, any other IO whose
read returned an empty string before end of stream wrote a zero-size
chunk in the same way.

Stream every IO value through flush_buffer instead, so each chunk is
sized by the bytes actually read. flush_buffer now writes nothing for an
empty buffer, so the only zero-size chunk in a multipart body is the
final one. It also no longer clears the strings it writes, so a value
whose read returns a frozen string is sent rather than raising
FrozenError.
@orien
orien force-pushed the chunked-multipart-empty-file branch from bc1bd40 to 1b3f354 Compare November 27, 2026 14:18
@orien
orien marked this pull request as ready for review November 27, 2026 14:21
@orien

orien commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

/cc @mame

This branch has not been deployed

No deployments
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.

1 participant