From 9b6318114bb77374e0e5deef53dfc6c9222fb418 Mon Sep 17 00:00:00 2001 From: JohnnyMendesC <177888064+JohnnyMendesC@users.noreply.github.com> Date: Thu, 4 Sep 2025 11:10:46 -0300 Subject: [PATCH 1/3] fix(#11191): Align Content-Disposition with RFC 5987/6266 (cherry picked from commit fe4077acee30fac8fb5607821a7d6e1ee3bd9d88) (cherry picked from commit f3179117d46a466c30289c2679b408389bd78e1f) --- .../rest/utils/HttpHeadersInitializer.java | 54 +++++++++++++++++-- .../app/rest/BitstreamRestControllerIT.java | 11 ++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java index a69da4c5e86..0ed36e954a3 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java @@ -9,9 +9,11 @@ import static java.util.Objects.isNull; import static java.util.Objects.nonNull; -import static javax.mail.internet.MimeUtility.encodeText; import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.text.Normalizer; import java.util.Arrays; import java.util.Collections; import javax.servlet.http.HttpServletRequest; @@ -165,9 +167,16 @@ public HttpHeaders initialiseHeaders() throws IOException { httpHeaders.put(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, Collections.singletonList(HttpHeaders.ACCEPT_RANGES)); - httpHeaders.put(CONTENT_DISPOSITION, Collections.singletonList(String.format(CONTENT_DISPOSITION_FORMAT, - disposition, - encodeText(fileName)))); + String fallbackAsciiName = createFallbackAsciiName(this.fileName); + String encodedUtf8Name = createEncodedUtf8Name(this.fileName); + + String headerValue = String.format( + "%s; filename=\"%s\"; filename*=UTF-8''%s", + disposition, + fallbackAsciiName, + encodedUtf8Name + ); + httpHeaders.put(CONTENT_DISPOSITION, Collections.singletonList(headerValue)); log.debug("Content-Disposition : {}", disposition); // Content phase @@ -260,4 +269,41 @@ private static boolean matches(String matchHeader, String toMatch) { return Arrays.binarySearch(matchValues, toMatch) > -1 || Arrays.binarySearch(matchValues, "*") > -1; } + /** + * Creates a safe ASCII-only fallback filename by removing diacritics (accents) + * and replacing any remaining non-ASCII characters. + * E.g., "ä-ö-é.pdf" becomes "a-o-e.pdf". + * @param originalFilename The original filename. + * @return A string containing only ASCII characters. + */ + private String createFallbackAsciiName(String originalFilename) { + if (originalFilename == null) { + return ""; + } + String normalized = Normalizer.normalize(originalFilename, Normalizer.Form.NFD); + String withoutAccents = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); + return withoutAccents.replaceAll("[^\\x00-\\x7F]", ""); + } + + /** + * Creates a percent-encoded UTF-8 filename according to RFC 5987. + * This is for the `filename*` parameter. + * E.g., "ä ö é.pdf" becomes "%C3%A4%20%C3%B6%20%C3%A9.pdf". + * @param originalFilename The original filename. + * @return A percent-encoded string. + */ + private String createEncodedUtf8Name(String originalFilename) { + if (originalFilename == null) { + return ""; + } + try { + String encoded = URLEncoder.encode(originalFilename, StandardCharsets.UTF_8.toString()); + return encoded.replace("+", "%20"); + } catch (java.io.UnsupportedEncodingException e) { + // Fallback to a simple ASCII name if encoding fails. + log.error("UTF-8 encoding not supported, which should not happen.", e); + return createFallbackAsciiName(originalFilename); + } + } + } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java index b746ae28359..40a2c8b65d4 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/BitstreamRestControllerIT.java @@ -8,7 +8,6 @@ package org.dspace.app.rest; import static java.util.UUID.randomUUID; -import static javax.mail.internet.MimeUtility.encodeText; import static org.apache.commons.codec.CharEncoding.UTF_8; import static org.apache.commons.collections.CollectionUtils.isEmpty; import static org.apache.commons.io.IOUtils.toInputStream; @@ -332,7 +331,11 @@ public void testBitstreamName() throws Exception { //2. A public item with a bitstream String bitstreamContent = "0123456789"; - String bitstreamName = "ภาษาไทย"; + String bitstreamName = "ภาษาไทย-com-acentuação.pdf"; + String expectedAscii = "-com-acentuacao.pdf"; + String expectedUtf8Encoded = + "%E0%B8%A0%E0%B8%B2%E0%B8%A9%E0%B8%B2%E0%B9%84%E0%B8%97%E0%B8%A2-" + + "com-acentua%C3%A7%C3%A3o.pdf"; try (InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { @@ -356,7 +359,9 @@ public void testBitstreamName() throws Exception { //We expect the content disposition to have the encoded bitstream name .andExpect(header().string( "Content-Disposition", - "attachment;filename=\"" + encodeText(bitstreamName) + "\"" + String.format("attachment; filename=\"%s\"; filename*=UTF-8''%s", + expectedAscii, + expectedUtf8Encoded) )); } From f9343f850a50b418d0e7ba4f23078c55af0566db Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 16 Jul 2026 11:15:22 +0200 Subject: [PATCH 2/3] ZCU-PUB/fix: use RFC 5987 Content-Disposition for allzip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backport of the allzip half of #1368 (dtq-dev) to customer/zcu-pub. The allzip endpoint built its header as a bare `attachment;filename=""`, so item names with diacritics arrived mangled and a double quote in a name closed the quoted-string early (ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION). MetadataBitstreamController has no counterpart upstream, so it carries its own private copy of vanilla's createFallbackAsciiName / createEncodedUtf8Name rather than a shared fork utility — same as on dtq-dev. One deliberate deviation from vanilla, marked in the code: the ASCII fallback escapes \ and ". Vanilla omits this, so a name containing a quote closes the quoted-string early — exactly the bug #1267 was raised for. Differs from #1368 in one way: BitstreamByHandleRestController does not exist on this branch (the curl endpoint from #1252 was never backported here), so it is not touched. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../app/rest/MetadataBitstreamController.java | 50 +++++++++++++++- .../rest/MetadataBitstreamControllerIT.java | 59 +++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java index 5e670030695..a5fa455ab1a 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java @@ -11,7 +11,10 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.sql.SQLException; +import java.text.Normalizer; import java.util.List; import java.util.Objects; import java.util.UUID; @@ -110,7 +113,7 @@ public void downloadFileZip(@PathVariable UUID uuid, @RequestParam("handleId") S Item item = (Item) dso; name = item.getName() + ".zip"; - response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment;filename=\"%s\"", name)); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, buildContentDisposition(name)); response.setContentType("application/zip"); List bundles = item.getBundles("ORIGINAL"); @@ -134,4 +137,49 @@ public void downloadFileZip(@PathVariable UUID uuid, @RequestParam("handleId") S zip.close(); response.getOutputStream().flush(); } + + /** + * Build the Content-Disposition value the way vanilla's HttpHeadersInitializer does: an ASCII + * fallback in {@code filename} for clients that predate RFC 5987, plus the real UTF-8 name in + * {@code filename*} for everyone else. This endpoint has no upstream counterpart, so the logic + * is copied from vanilla rather than shared, to keep it tracking upstream's behaviour. + */ + private String buildContentDisposition(String name) { + return String.format("attachment; filename=\"%s\"; filename*=UTF-8''%s", + createFallbackAsciiName(name), createEncodedUtf8Name(name)); + } + + /** + * Creates a safe ASCII-only fallback filename by removing diacritics (accents) + * and replacing any remaining non-ASCII characters. + * E.g., "ä-ö-é.pdf" becomes "a-o-e.pdf". + * @param originalFilename The original filename. + * @return A string containing only ASCII characters. + */ + private String createFallbackAsciiName(String originalFilename) { + if (originalFilename == null) { + return ""; + } + String normalized = Normalizer.normalize(originalFilename, Normalizer.Form.NFD); + String withoutAccents = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); + // Deviates from vanilla by escaping \ and ": the value is a quoted-string, and an item name + // containing a quote closes it early. That is the bug #1267 fixed; vanilla still has it. + return withoutAccents.replaceAll("[^\\x00-\\x7F]", "") + .replace("\\", "\\\\") + .replace("\"", "\\\""); + } + + /** + * Creates a percent-encoded UTF-8 filename according to RFC 5987. + * This is for the `filename*` parameter. + * E.g., "ä ö é.pdf" becomes "%C3%A4%20%C3%B6%20%C3%A9.pdf". + * @param originalFilename The original filename. + * @return A percent-encoded string. + */ + private String createEncodedUtf8Name(String originalFilename) { + if (originalFilename == null) { + return ""; + } + return URLEncoder.encode(originalFilename, StandardCharsets.UTF_8).replace("+", "%20"); + } } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java index 63f7e62206f..8fa02154100 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java @@ -9,6 +9,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.io.ByteArrayOutputStream; @@ -97,4 +98,62 @@ public void downloadAllZip() throws Exception { .andExpect(content().bytes(byteArrayOutputStream.toByteArray())); } + + @Test + public void downloadAllZipWithDoubleQuotesInItemName() throws Exception { + context.turnOffAuthorisationSystem(); + + // Double quotes in the name used to close the header's quoted-string early, which browsers + // reported as ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION. + Item itemWithQuotes = ItemBuilder.createItem(context, col) + .withTitle("Supported data for manuscript \"Thermally-induced evolution\"") + .withAuthor(AUTHOR) + .build(); + + try (InputStream is = IOUtils.toInputStream("QuotedItemContent", CharEncoding.UTF_8)) { + BitstreamBuilder.createBitstream(context, itemWithQuotes, is) + .withName("data.csv") + .withMimeType("text/csv") + .build(); + } + context.restoreAuthSystemState(); + + String token = getAuthToken(admin.getEmail(), password); + getClient(token).perform(get(METADATABITSTREAM_ENDPOINT + "/" + itemWithQuotes.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, itemWithQuotes.getHandle())) + .andExpect(status().isOk()) + .andExpect(header().string("Content-Disposition", + "attachment; filename=\"Supported data for manuscript" + + " \\\"Thermally-induced evolution\\\".zip\";" + + " filename*=UTF-8''Supported%20data%20for%20manuscript" + + "%20%22Thermally-induced%20evolution%22.zip")); + } + + @Test + public void downloadAllZipWithNonAsciiItemName() throws Exception { + context.turnOffAuthorisationSystem(); + + Item itemWithDiacritics = ItemBuilder.createItem(context, col) + .withTitle("Příliš žluťoučký kůň") + .withAuthor(AUTHOR) + .build(); + + try (InputStream is = IOUtils.toInputStream("DiacriticsContent", CharEncoding.UTF_8)) { + BitstreamBuilder.createBitstream(context, itemWithDiacritics, is) + .withName("file.txt") + .withMimeType("text/plain") + .build(); + } + context.restoreAuthSystemState(); + + String token = getAuthToken(admin.getEmail(), password); + getClient(token).perform(get(METADATABITSTREAM_ENDPOINT + "/" + itemWithDiacritics.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, itemWithDiacritics.getHandle())) + .andExpect(status().isOk()) + // fallback transliterates the diacritics away; filename* carries the real name + .andExpect(header().string("Content-Disposition", + "attachment; filename=\"Prilis zlutoucky kun.zip\";" + + " filename*=UTF-8''P%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD" + + "%20k%C5%AF%C5%88.zip")); + } } From f047b48ff32091f49421875dd671508ce7e07d5c Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Tue, 21 Jul 2026 12:50:53 +0200 Subject: [PATCH 3/3] ZCU-PUB/fix: escape quoted-string + drop dead UTF-8 catch in HttpHeadersInitializer Address Copilot review on #1369: - createFallbackAsciiName now escapes \ and " so a filename containing a quote can't close the filename="..." quoted-string early (the ERR_RESPONSE_ HEADERS_MULTIPLE_CONTENT_DISPOSITION class of bug, #1267). Brings it to parity with the sibling method in MetadataBitstreamController. - createEncodedUtf8Name now uses URLEncoder.encode(String, Charset); UTF-8 is always supported, so the UnsupportedEncodingException catch was dead code. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../app/rest/utils/HttpHeadersInitializer.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java index 0ed36e954a3..21012671cdc 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/HttpHeadersInitializer.java @@ -282,7 +282,11 @@ private String createFallbackAsciiName(String originalFilename) { } String normalized = Normalizer.normalize(originalFilename, Normalizer.Form.NFD); String withoutAccents = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); - return withoutAccents.replaceAll("[^\\x00-\\x7F]", ""); + // Escape \ and ": the value is placed in a quoted-string, so a filename containing a quote + // would close it early and break the header. Mirrors MetadataBitstreamController (bug #1267). + return withoutAccents.replaceAll("[^\\x00-\\x7F]", "") + .replace("\\", "\\\\") + .replace("\"", "\\\""); } /** @@ -296,14 +300,7 @@ private String createEncodedUtf8Name(String originalFilename) { if (originalFilename == null) { return ""; } - try { - String encoded = URLEncoder.encode(originalFilename, StandardCharsets.UTF_8.toString()); - return encoded.replace("+", "%20"); - } catch (java.io.UnsupportedEncodingException e) { - // Fallback to a simple ASCII name if encoding fails. - log.error("UTF-8 encoding not supported, which should not happen.", e); - return createFallbackAsciiName(originalFilename); - } + return URLEncoder.encode(originalFilename, StandardCharsets.UTF_8).replace("+", "%20"); } }