Skip to content

Commit 2576aa4

Browse files
committed
ContentDisposition trims charset in filename
Backport of c8bce96 Closes gh-24112
1 parent 3fbe762 commit 2576aa4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public static ContentDisposition parse(String contentDisposition) {
277277
}
278278
else if (attribute.equals("filename*") ) {
279279
filename = decodeHeaderFieldParam(value);
280-
charset = Charset.forName(value.substring(0, value.indexOf('\'')));
280+
charset = Charset.forName(value.substring(0, value.indexOf('\'')).trim());
281281
Assert.isTrue(UTF_8.equals(charset) || ISO_8859_1.equals(charset),
282282
"Charset should be UTF-8 or ISO-8859-1");
283283
}
@@ -371,7 +371,7 @@ private static String decodeHeaderFieldParam(String input) {
371371
if (firstQuoteIndex == -1 || secondQuoteIndex == -1) {
372372
return input;
373373
}
374-
Charset charset = Charset.forName(input.substring(0, firstQuoteIndex));
374+
Charset charset = Charset.forName(input.substring(0, firstQuoteIndex).trim());
375375
Assert.isTrue(UTF_8.equals(charset) || ISO_8859_1.equals(charset),
376376
"Charset should be UTF-8 or ISO-8859-1");
377377
byte[] value = input.substring(secondQuoteIndex + 1, input.length()).getBytes(charset);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,6 +71,14 @@ public void parseAndIgnoreEmptyParts() {
7171
.name("foo").filename("foo.txt").size(123L).build(), disposition);
7272
}
7373

74+
@Test // gh-24112
75+
public void parseEncodedFilenameWithPaddedCharset() {
76+
ContentDisposition disposition = ContentDisposition
77+
.parse("attachment; filename*= UTF-8''some-file.zip");
78+
assertEquals(ContentDisposition.builder("attachment")
79+
.filename("some-file.zip", StandardCharsets.UTF_8).build(), disposition);
80+
}
81+
7482
@Test
7583
public void parseEncodedFilename() {
7684
ContentDisposition disposition = ContentDisposition

0 commit comments

Comments
 (0)