Skip to content
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

ASCII encode ContentDisposition file name #5514

Merged
merged 2 commits into from
Jan 24, 2024
Merged
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
3 changes: 2 additions & 1 deletion connectors/jdk-connector/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!--

Copyright (c) 2011, 2023 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -98,6 +98,7 @@
<reuseForks>false</reuseForks>
<excludes>
<exclude>**/SslFilterTLS13Test.java</exclude>
<exclude>**/SslFilterTLS13UrlStoresTest.java</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -28,8 +28,6 @@
import org.glassfish.jersey.message.internal.HttpHeaderReader;
import org.glassfish.jersey.uri.UriComponent;

import javax.ws.rs.core.HttpHeaders;

/**
* A content disposition header.
*
Expand Down Expand Up @@ -60,10 +58,13 @@ public class ContentDisposition {
private static final Pattern FILENAME_VALUE_CHARS_PATTERN =
Pattern.compile("(%[a-f0-9]{2}|[a-z0-9!#$&+.^_`|~-])+", Pattern.CASE_INSENSITIVE);

private static final char QUOTE = '"';
private static final char BACK_SLASH = '\\';

protected ContentDisposition(final String type, final String fileName, final Date creationDate,
final Date modificationDate, final Date readDate, final long size) {
this.type = type;
this.fileName = fileName;
this.fileName = encodeAsciiFileName(fileName);
this.creationDate = creationDate;
this.modificationDate = modificationDate;
this.readDate = readDate;
Expand Down Expand Up @@ -211,6 +212,23 @@ protected void addLongParameter(final StringBuilder sb, final String name, final
}
}

protected String encodeAsciiFileName(String fileName) {
if (fileName == null
|| (fileName.indexOf(QUOTE) == -1
&& fileName.indexOf(BACK_SLASH) == -1)) {
return fileName;
}
final char[] chars = fileName.toCharArray();
final StringBuilder encodedBuffer = new StringBuilder();
for (char c : chars) {
if (c == QUOTE || c == BACK_SLASH) {
encodedBuffer.append(BACK_SLASH);
}
encodedBuffer.append(c);
}
return encodedBuffer.toString();
}

private void createParameters() throws ParseException {
defineFileName();

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

if (fileNameExt == null) {
this.fileName = fileName;
this.fileName = encodeAsciiFileName(fileName);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -88,6 +88,14 @@ public void testCreate() {
}
}

@Test
void testContentDispositionEncoded() {
final Date date = new Date();
final ContentDisposition contentDisposition = ContentDisposition.type(contentDispositionType).fileName("\"rm\\ -rf\".sh")
.creationDate(date).modificationDate(date).readDate(date).size(312).build();
assertEquals("\\\"rm\\\\ -rf\\\".sh", contentDisposition.getFileName());
}

@Test
public void testToString() {
final Date date = new Date();
Expand Down