Skip to content

Accept a delimiter without a preceding CRLF only at the start of a body - #2

Open
wtn wants to merge 1 commit into
socketry:multipart-empty-part-delimiterfrom
wtn:multipart-empty-part-delimiter
Open

wtn wants to merge 1 commit into
socketry:multipart-empty-part-delimiterfrom
wtn:multipart-empty-part-delimiter

Conversation

@wtn

@wtn wtn commented Oct 30, 2026 •

Copy link
Copy Markdown

For PR rack#2541.

@body_regex is /(?:\r\n|\A)--BOUNDARY(?:\r\n|--)/, so a delimiter matches after a CRLF, or with no CRLF at the scan position. rack#2541 relies on the second case for a part with no body. The scan position is also where the retained data starts after handle_mime_body trims the buffer between reads, which can be anywhere inside a part body.

On main, file content with --BOUNDARY\r\n and no CRLF before it at that offset truncates the upload. With rack#2541's change to the CRLF skip, the bytes after it are parsed as a new part, so the content of an uploaded file can add a parameter:

file content: "aaa…--AaB03x\r\ncontent-disposition: form-data; name=\"role\"\r\n\r\nadmin…"
main:   {"file" => <truncated upload>}
#2541:  {"file" => <truncated upload>, "role" => "admin…"}

With default settings, the offset is BUFSIZE minus the first part's headers minus boundary.bytesize + 6, so a plain Rack::Request#POST reaches it.

This change accepts a delimiter without a CRLF only until body bytes have been passed to the collector, and requires the CRLF after that. The changelog entry for rack#2541 now mentions it.

Types of Changes

  • Bug fix.

Contribution

The body regex matches a delimiter after a CRLF, or with no CRLF at
the scan position. The scan position is the start of a part body,
and also the start of the data the parser keeps when it trims its
buffer between reads, which can fall anywhere inside a body.

Content with "--boundary\r\n" and no CRLF before it at that offset
ends the part there. The parser used to skip two bytes after every
match and jump to the next delimiter, which truncated the upload.
With the CRLF skipped only when present, the bytes after the false
delimiter are parsed as a new part, and an uploaded file can add
parameters.

Accept a delimiter without a CRLF only until body bytes have been
passed to the collector, and require the CRLF after that.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@wtn
wtn marked this pull request as ready for review October 30, 2026 20:06
@wtn
wtn force-pushed the multipart-empty-part-delimiter branch from 7fee39a to 7745eef Compare October 30, 2026 20:08

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