Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ protected void formatMultipartHeader(final MultipartPart part, final OutputStrea
final String value = parameter.getValue();
writeBytes("; ", out);
writeBytes(name, out);
writeBytes("=\"", out);
// RFC 8187 ext-value must not be quoted
final boolean quoted = !name.equalsIgnoreCase(MimeConsts.FIELD_PARAM_FILENAME_START);
writeBytes(quoted ? "=\"" : "=", out);
if (value != null) {
if (name.equalsIgnoreCase(MimeConsts.FIELD_PARAM_FILENAME_START)) {
if (value.startsWith("UTF-8''")) {
Expand All @@ -122,7 +124,9 @@ protected void formatMultipartHeader(final MultipartPart part, final OutputStrea
writeBytes(stripLineBreaks(escapeQuoted(value)), out);
}
}
writeBytes("\"", out);
if (quoted) {
writeBytes("\"", out);
}
}
writeBytes(CR_LF, out);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ public String getBody() {
parameters.forEach(parameter -> {
sb.append("; ");
sb.append(parameter.getName());
sb.append("=\"");
final String v = parameter.getValue();
if (MimeConsts.FIELD_PARAM_FILENAME_START.equalsIgnoreCase(parameter.getName()) && v.startsWith("UTF-8''")) {
// RFC 8187 ext-value must not be quoted
sb.append("=").append(v);
return;
}
sb.append("=\"");
for (int n = 0; n < v.length(); n++) {
final char ch = v.charAt(n);
if (ch == '"' || ch == '\\' ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ void testMultipartWriteToRFC7578ModeWithFilenameStar() throws Exception {
entity.writeTo(out);
out.close();
Assertions.assertEquals("--xxxxxxxxxxxxxxxxxxxxxxxx\r\n" +
"Content-Disposition: multipart/form-data; name=\"test\"; filename*=\"UTF-8''hello%20%CE%BA%CF%8C%CF%83%CE%BC%CE%B5!%25\"\r\n" +
"Content-Disposition: multipart/form-data; name=\"test\"; filename*=UTF-8''hello%20%CE%BA%CF%8C%CF%83%CE%BC%CE%B5!%25\r\n" +
"Content-Type: text/plain; charset=UTF-8\r\n" +
"\r\n" +
"hello \u00ce\u00ba\u00cf\u008c\u00cf\u0083\u00ce\u00bc\u00ce\u00b5!%\r\n" +
Expand Down Expand Up @@ -411,7 +411,7 @@ void testMultipartWriteToRFC7578ModeWithFilenameStarPreEncodedPassThrough() thro
final String wire = out.toString(StandardCharsets.ISO_8859_1.name());

// Pass-through EXACTLY the given value (no second prefix, no %25-escaping)
Assertions.assertTrue(wire.contains("filename*=\"" + preEncoded + "\""));
Assertions.assertTrue(wire.contains("filename*=" + preEncoded + "\r\n"));
Assertions.assertFalse(wire.contains("UTF-8''UTF-8%27%27"));
Assertions.assertFalse(wire.contains("%25F0%9F%90%99")); // octopus emoji must not be re-escaped as %25F0...
}
Expand Down Expand Up @@ -441,12 +441,29 @@ void testExtendedModeAddBinaryBodyAddsFilenameAndFilenameStar_NoDoubleEncoding()
Assertions.assertTrue(wire.contains("filename=\"" + pct + "\""));

// filename* param (single RFC 5987 value, no double prefix / no %25-escaping)
Assertions.assertTrue(wire.contains("filename*=\"UTF-8''" + pct + "\""));
Assertions.assertTrue(wire.contains("filename*=UTF-8''" + pct + "\r\n"));

// Guard against regressions
Assertions.assertFalse(wire.contains("UTF-8''UTF-8%27%27"));
Assertions.assertFalse(wire.contains("%25F0%9F%90%99"));
}

@Test
void testDefaultModeNonLatinFilenameWritesUnquotedFilenameStar() throws Exception {
// RFC 8187 ext-value must not be quoted, servers such as busboy (multer) drop the part otherwise
final MultipartFormEntity entity = MultipartEntityBuilder.create()
.setBoundary("xxxxxxxxxxxxxxxxxxxxxxxx")
.addBinaryBody("file", new byte[]{1, 2}, ContentType.APPLICATION_OCTET_STREAM,
"\uC6D4\uAC04 \uBCF4\uACE0\uC11C.pdf")
.buildEntity();

final ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
out.close();
final String wire = out.toString(StandardCharsets.ISO_8859_1.name());

Assertions.assertTrue(wire.contains("Content-Disposition: form-data; name=\"file\"; filename=\"?? ???.pdf\"; " +
"filename*=UTF-8''%EC%9B%94%EA%B0%84%20%EB%B3%B4%EA%B3%A0%EC%84%9C.pdf\r\n"), wire);
}

}
Loading