From 4cdf728b9341dcde89f2b5028ed8c6a0113a0816 Mon Sep 17 00:00:00 2001 From: Vidya m Date: Thu, 26 Nov 2026 13:40:29 +0530 Subject: [PATCH] escape CR, LF and quote in multipart field and file names --- src/changes/changes.xml | 6 ++++ .../java/org/htmlunit/HttpWebConnection.java | 32 ++++++++++++++++--- .../org/htmlunit/HttpWebConnectionTest.java | 32 +++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1e3e17f1fd..7baabda295 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -73,6 +73,12 @@ they are stored in the shared temp directory). The report folder of the DebuggingWebConnection is no longer used if someone else created it in the meantime. + + A carriage return, line feed or double quote in the field name or the file name of a + multipart/form-data part is now escaped (%0D, %0A, %22) like in the browsers; so far + such a character (e.g. from a script controlled FormData field or file name) broke out + of the Content-Disposition header and could inject additional part headers or parts. + A response that exceeds the max in memory size is stored in a temporary file that is only readable by the owner; so far the file was created in the shared temp directory with the diff --git a/src/main/java/org/htmlunit/HttpWebConnection.java b/src/main/java/org/htmlunit/HttpWebConnection.java index 22ef8e40e4..9b8e24873f 100644 --- a/src/main/java/org/htmlunit/HttpWebConnection.java +++ b/src/main/java/org/htmlunit/HttpWebConnection.java @@ -372,7 +372,7 @@ else if (FormEncodingType.MULTIPART == webRequest.getEncodingType()) { buildFilePart(dataPair, builder); } else { - builder.addTextBody(pair.getName(), pair.getValue(), + builder.addTextBody(escapeMimeField(pair.getName()), pair.getValue(), ContentType.create(MimeType.TEXT_PLAIN, charset)); } } @@ -432,6 +432,25 @@ private static String escapeQuery(final String query) { return query.replace("%%", "%25%25"); } + /** + * Escapes the field name and the file name of a multipart/form-data part like the browsers do: + * a line feed is replaced by '%0A', a carriage return by '%0D' and a double quote by '%22'. + * Without this a carriage return or line feed breaks out of the Content-Disposition header and + * a double quote out of the quoted name, so a script controlling a field name or a file name + * (e.g. via FormData) could inject additional part headers or parts into the request body. + * + * @see + * HTML Living Standard - multipart/form-data encoding algorithm + * @param value the field name or file name + * @return the escaped value + */ + private static String escapeMimeField(final String value) { + if (value == null) { + return null; + } + return value.replace("\n", "%0A").replace("\r", "%0D").replace("\"", "%22"); + } + private static Charset getCharset(final Charset charset, final List pairs) { for (final NameValuePair pair : pairs) { if (pair instanceof KeyDataPair pairWithFile) { @@ -457,13 +476,15 @@ void buildFilePart(final KeyDataPair pairWithFile, final MultipartEntityBuilder final ContentType contentType = ContentType.create(mimeType); + final String name = escapeMimeField(pairWithFile.getName()); + final File file = pairWithFile.getFile(); if (file != null) { String filename = pairWithFile.getFileName(); if (filename == null) { filename = pairWithFile.getFile().getName(); } - builder.addBinaryBody(pairWithFile.getName(), file, contentType, filename); + builder.addBinaryBody(name, file, contentType, escapeMimeField(filename)); return; } @@ -474,13 +495,14 @@ void buildFilePart(final KeyDataPair pairWithFile, final MultipartEntityBuilder filename = pairWithFile.getValue(); } - builder.addBinaryBody(pairWithFile.getName(), data, contentType, filename); + builder.addBinaryBody(name, data, contentType, escapeMimeField(filename)); return; } - builder.addPart(pairWithFile.getName(), + builder.addPart(name, // Overridden in order not to have a chunked response. - new InputStreamBody(new ByteArrayInputStream(new byte[0]), contentType, pairWithFile.getValue()) { + new InputStreamBody(new ByteArrayInputStream(new byte[0]), contentType, + escapeMimeField(pairWithFile.getValue())) { @Override public long getContentLength() { return 0; diff --git a/src/test/java/org/htmlunit/HttpWebConnectionTest.java b/src/test/java/org/htmlunit/HttpWebConnectionTest.java index f7f990f302..32448c46a5 100644 --- a/src/test/java/org/htmlunit/HttpWebConnectionTest.java +++ b/src/test/java/org/htmlunit/HttpWebConnectionTest.java @@ -260,6 +260,38 @@ public void buildFilePart() throws Exception { assertTrue(part, part.matches(expected)); } + /** + * A carriage return, line feed or double quote in the field name or the file name has to be + * escaped (%0D, %0A, %22) like in the browsers, otherwise it breaks out of the Content-Disposition + * header and injects additional part headers or parts into the multipart body. + * @throws Exception if the test fails + */ + @Test + public void buildFilePartEscapesNameAndFilename() throws Exception { + final String encoding = "ISO8859-1"; + final Path tempFile = Files.createTempFile("htmlunit", ".txt"); + try { + final KeyDataPair pair = new KeyDataPair("na\r\nme\"x", tempFile.toFile(), + "ev\r\nil\"name.txt", MimeType.TEXT_PLAIN, encoding); + final MultipartEntityBuilder builder = MultipartEntityBuilder.create().setLaxMode(); + final String part; + try (HttpWebConnection webConnection = new HttpWebConnection(getWebClient())) { + webConnection.buildFilePart(pair, builder); + } + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + builder.build().writeTo(baos); + part = baos.toString(encoding); + + assertTrue(part, part.contains( + "Content-Disposition: form-data; name=\"na%0D%0Ame%22x\"; " + + "filename=\"ev%0D%0Ail%22name.txt\"\r\n")); + assertFalse(part, part.contains("name=\"na\r\nme")); + } + finally { + Files.delete(tempFile); + } + } + /** * @throws Exception on failure */