Skip to content

Commit 4f05da7

Browse files
committed
Support escape character in ContentDisposition
Closes gh-23077
1 parent 4523d01 commit 4f05da7

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -331,16 +331,18 @@ private static List<String> tokenize(String headerValue) {
331331
do {
332332
int nextIndex = index + 1;
333333
boolean quoted = false;
334+
boolean escaped = false;
334335
while (nextIndex < headerValue.length()) {
335336
char ch = headerValue.charAt(nextIndex);
336337
if (ch == ';') {
337338
if (!quoted) {
338339
break;
339340
}
340341
}
341-
else if (ch == '"') {
342+
else if (!escaped && ch == '"') {
342343
quoted = !quoted;
343344
}
345+
escaped = (!escaped && ch == '\\');
344346
nextIndex++;
345347
}
346348
String part = headerValue.substring(index + 1, nextIndex).trim();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public void parseEncodedFilename() {
7979
.filename("中文.txt", StandardCharsets.UTF_8).build(), disposition);
8080
}
8181

82+
@Test // gh-23077
83+
public void parseWithEscapedQuote() {
84+
ContentDisposition disposition = ContentDisposition.parse(
85+
"form-data; name=\"file\"; filename=\"\\\"The Twilight Zone\\\".txt\"; size=123");
86+
assertEquals(ContentDisposition.builder("form-data").name("file")
87+
.filename("\\\"The Twilight Zone\\\".txt").size(123L).build(), disposition);
88+
}
89+
8290
@Test(expected = IllegalArgumentException.class)
8391
public void parseEmpty() {
8492
ContentDisposition.parse("");

0 commit comments

Comments
 (0)