Skip to content

Commit f0e7b42

Browse files
committed
Encode non-printable character in Content-Disposition parameter
Prior to this commit, the "filename" parameter value for the "Content-Disposition" header would contain non-printable characters, causing parsing issues for HTTP clients. This commit ensures that all non-printable characters are encoded. Fixes gh-35034
1 parent e86dc9a commit f0e7b42

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

spring-web/src/main/java/org/springframework/http/ContentDisposition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public final class ContentDisposition {
6868
for (int i=33; i<= 126; i++) {
6969
PRINTABLE.set(i);
7070
}
71+
PRINTABLE.set(34, false); // "
7172
PRINTABLE.set(61, false); // =
7273
PRINTABLE.set(63, false); // ?
7374
PRINTABLE.set(95, false); // _

spring-web/src/test/java/org/springframework/http/ContentDispositionTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ void formatWithFilenameWithQuotes() {
313313
tester.accept("foo.txt\\\\\\", "foo.txt\\\\\\\\\\\\");
314314
}
315315

316+
@Test
317+
void formatWithUtf8FilenameWithQuotes() {
318+
String filename = "\"中文.txt";
319+
assertThat(ContentDisposition.formData().filename(filename, StandardCharsets.UTF_8).build().toString())
320+
.isEqualTo("form-data; filename=\"=?UTF-8?Q?=22=E4=B8=AD=E6=96=87.txt?=\"; filename*=UTF-8''%22%E4%B8%AD%E6%96%87.txt");
321+
}
322+
316323
@Test
317324
void formatWithEncodedFilenameUsingInvalidCharset() {
318325
assertThatIllegalArgumentException().isThrownBy(() ->

0 commit comments

Comments
 (0)