Skip to content

Support multiple segments in encoded Content-Disposition #28236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 @@ -371,9 +371,15 @@ else if (attribute.equals("filename") && (filename == null)) {
if (value.startsWith("=?") ) {
Matcher matcher = BASE64_ENCODED_PATTERN.matcher(value);
if (matcher.find()) {
String match1 = matcher.group(1);
Charset match1 = Charset.forName(matcher.group(1));
String match2 = matcher.group(2);
filename = new String(Base64.getDecoder().decode(match2), Charset.forName(match1));
StringBuilder sb = new StringBuilder(new String(Base64.getDecoder().decode(match2), match1));
while (matcher.find()){
match1 = Charset.forName(matcher.group(1));
match2 = matcher.group(2);
sb.append(new String(Base64.getDecoder().decode(match2), match1));
}
filename = sb.toString();
}
else {
filename = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ void parseBase64EncodedFilename() {
assertThat(parse(input).getFilename()).isEqualTo("日本語.csv");
}

@Test
void parseBase64EncodedFilenameHasMoreSegments() {
/* https://datatracker.ietf.org/doc/html/rfc2047#section-2
* An 'encoded-word' may not be more than 75 characters long */
String input = "attachment; filename=\"=?utf-8?B?U3ByaW5n5qGG5p625Li65Z+65LqOSmF2YeeahOeOsOS7o+S8geS4muW6lA==?= =?utf-8?B?55So56iL5bqP5o+Q5L6b5LqG5YWo6Z2i55qE57yW56iL5ZKM6YWN572u5qih?= =?utf-8?B?5Z6LLnR4dA==?=\"";
assertThat(parse(input).getFilename()).isEqualTo("Spring框架为基于Java的现代企业应用程序提供了全面的编程和配置模型.txt");
}

@Test // gh-26463
void parseBase64EncodedShiftJISFilename() {
String input = "attachment; filename=\"=?SHIFT_JIS?B?k/qWe4zqLmNzdg==?=\"";
Expand Down