Skip to content

Escape CR, LF and double quote in multipart/form-data field and file names - #1210

Open
Vidya-m78 wants to merge 1 commit into
HtmlUnit:masterfrom
Vidya-m78:multipart-field-name-escape
Open

Vidya-m78 wants to merge 1 commit into
HtmlUnit:masterfrom
Vidya-m78:multipart-field-name-escape

Conversation

@Vidya-m78

Copy link
Copy Markdown
Contributor

CRLF and quote injection into multipart/form-data part headers
HttpWebConnection builds multipart bodies with Apache MultipartEntityBuilder in lax (BROWSER_COMPATIBLE) mode, which writes each part's field name and file name straight into the Content-Disposition header. A carriage return, line feed or double quote in a name is passed through unescaped, so a script that controls a FormData field name or a Blob/File name can break out of the header and inject extra part headers or whole parts. multipart/form-data is a CORS-safelisted content type, so this travels on a cross-origin POST without a preflight.

Minimal trigger, fd.append('upload', new Blob(['x']), 'a\r\nX-Injected-Part: evil\r\n\r\n...') plus fd.append('na\r\nme', 'val'), produced a body on master with the injected header on its own line and the field name split across two lines. Browsers escape the three bytes in both the field name and the file name (%0D, %0A, %22) per the HTML multipart/form-data encoding algorithm; the fix does the same in escapeMimeField before the names reach the builder. A legitimate name without those characters serializes exactly as before.

HttpWebConnectionTest.buildFilePartEscapesNameAndFilename fails on master and passes with the change; the HttpWebConnection, HtmlFileInput and CodeStyle tests pass and checkstyle and pmd are clean. I also added a changes.xml entry.

@sonarqubecloud

Copy link
Copy Markdown

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