Skip to content

Commit 604cf2e

Browse files
authored
ASCII encode ContentDisposition file name (#5514)
Signed-off-by: Maxim Nesen <[email protected]>
1 parent e4c3bf5 commit 604cf2e

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

connectors/jdk-connector/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<!--
33
4-
Copyright (c) 2011, 2023 Oracle and/or its affiliates. All rights reserved.
4+
Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.
55
66
This program and the accompanying materials are made available under the
77
terms of the Eclipse Public License v. 2.0, which is available at
@@ -98,6 +98,7 @@
9898
<reuseForks>false</reuseForks>
9999
<excludes>
100100
<exclude>**/SslFilterTLS13Test.java</exclude>
101+
<exclude>**/SslFilterTLS13UrlStoresTest.java</exclude>
101102
</excludes>
102103
</configuration>
103104
</plugin>

media/multipart/src/main/java/org/glassfish/jersey/media/multipart/ContentDisposition.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -28,8 +28,6 @@
2828
import org.glassfish.jersey.message.internal.HttpHeaderReader;
2929
import org.glassfish.jersey.uri.UriComponent;
3030

31-
import javax.ws.rs.core.HttpHeaders;
32-
3331
/**
3432
* A content disposition header.
3533
*
@@ -60,10 +58,13 @@ public class ContentDisposition {
6058
private static final Pattern FILENAME_VALUE_CHARS_PATTERN =
6159
Pattern.compile("(%[a-f0-9]{2}|[a-z0-9!#$&+.^_`|~-])+", Pattern.CASE_INSENSITIVE);
6260

61+
private static final char QUOTE = '"';
62+
private static final char BACK_SLASH = '\\';
63+
6364
protected ContentDisposition(final String type, final String fileName, final Date creationDate,
6465
final Date modificationDate, final Date readDate, final long size) {
6566
this.type = type;
66-
this.fileName = fileName;
67+
this.fileName = encodeAsciiFileName(fileName);
6768
this.creationDate = creationDate;
6869
this.modificationDate = modificationDate;
6970
this.readDate = readDate;
@@ -211,6 +212,23 @@ protected void addLongParameter(final StringBuilder sb, final String name, final
211212
}
212213
}
213214

215+
protected String encodeAsciiFileName(String fileName) {
216+
if (fileName == null
217+
|| (fileName.indexOf(QUOTE) == -1
218+
&& fileName.indexOf(BACK_SLASH) == -1)) {
219+
return fileName;
220+
}
221+
final char[] chars = fileName.toCharArray();
222+
final StringBuilder encodedBuffer = new StringBuilder();
223+
for (char c : chars) {
224+
if (c == QUOTE || c == BACK_SLASH) {
225+
encodedBuffer.append(BACK_SLASH);
226+
}
227+
encodedBuffer.append(c);
228+
}
229+
return encodedBuffer.toString();
230+
}
231+
214232
private void createParameters() throws ParseException {
215233
defineFileName();
216234

@@ -229,7 +247,7 @@ private void defineFileName() throws ParseException {
229247
final String fileNameExt = parameters.get("filename*");
230248

231249
if (fileNameExt == null) {
232-
this.fileName = fileName;
250+
this.fileName = encodeAsciiFileName(fileName);
233251
return;
234252
}
235253

tests/e2e/src/test/java/org/glassfish/jersey/tests/api/ContentDispositionTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -88,6 +88,14 @@ public void testCreate() {
8888
}
8989
}
9090

91+
@Test
92+
void testContentDispositionEncoded() {
93+
final Date date = new Date();
94+
final ContentDisposition contentDisposition = ContentDisposition.type(contentDispositionType).fileName("\"rm\\ -rf\".sh")
95+
.creationDate(date).modificationDate(date).readDate(date).size(312).build();
96+
assertEquals("\\\"rm\\\\ -rf\\\".sh", contentDisposition.getFileName());
97+
}
98+
9199
@Test
92100
public void testToString() {
93101
final Date date = new Date();

0 commit comments

Comments
 (0)