Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</action>
<action type="fix" dev="Vidya m">
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.
</action>
<action type="fix" dev="Vidya m">
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
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/org/htmlunit/HttpWebConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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 <a href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart-form-data">
* HTML Living Standard - multipart/form-data encoding algorithm</a>
* @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<NameValuePair> pairs) {
for (final NameValuePair pair : pairs) {
if (pair instanceof KeyDataPair pairWithFile) {
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/htmlunit/HttpWebConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading