From 735e193abf05262c5b34c347e2ca7ec6c06eca38 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 1 Mar 2021 14:07:20 +0100 Subject: [PATCH] Support quoted boundary in DefaultPartHttpMessageReader This commit makes sure that quoted boundary parameters are supported in the DefaultPartHttpMessageReader. Closes gh-26616 --- .../DefaultPartHttpMessageReader.java | 4 ++++ .../DefaultPartHttpMessageReaderTests.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/spring-web/src/main/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReader.java b/spring-web/src/main/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReader.java index 175c7cdbedde..5aa3761fc47d 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReader.java +++ b/spring-web/src/main/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReader.java @@ -227,6 +227,10 @@ private static byte[] boundary(HttpMessage message) { if (contentType != null) { String boundary = contentType.getParameter("boundary"); if (boundary != null) { + int len = boundary.length(); + if (len > 2 && boundary.charAt(0) == '"' && boundary.charAt(len - 1) == '"') { + boundary = boundary.substring(1, len - 1); + } return boundary.getBytes(StandardCharsets.ISO_8859_1); } } diff --git a/spring-web/src/test/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReaderTests.java b/spring-web/src/test/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReaderTests.java index 0226ea7e5577..075ad7c24f4c 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReaderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/multipart/DefaultPartHttpMessageReaderTests.java @@ -233,6 +233,25 @@ public void tooManyParts() throws InterruptedException { latch.await(); } + @ParameterizedDefaultPartHttpMessageReaderTest + public void quotedBoundary(String displayName, DefaultPartHttpMessageReader reader) throws InterruptedException { + MockServerHttpRequest request = createRequest( + new ClassPathResource("simple.multipart", getClass()), "\"simple-boundary\""); + + Flux result = reader.read(forClass(Part.class), request, emptyMap()); + + CountDownLatch latch = new CountDownLatch(2); + StepVerifier.create(result) + .consumeNextWith(part -> testPart(part, null, + "This is implicitly typed plain ASCII text.\r\nIt does NOT end with a linebreak.", latch)).as("Part 1") + .consumeNextWith(part -> testPart(part, null, + "This is explicitly typed plain ASCII text.\r\nIt DOES end with a linebreak.\r\n", latch)).as("Part 2") + .verifyComplete(); + + latch.await(); + } + + private void testBrowser(DefaultPartHttpMessageReader reader, Resource resource, String boundary) throws InterruptedException {