GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Pin multipart Content-Type / Content-Transfer-Encoding case folding t… · netty/netty@0da3f84

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http/multipart
      • HttpPostMultipartRequestDecoder.java
      • HttpPostRequestEncoder.java
    • test/java/io/netty/handler/codec/http/multipart
      • HttpPostMultipartLocaleDecoderTest.java
      • HttpPostMultipartLocaleEncoderTest.java
      • HttpPostRequestEncoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostMultipartRequestDecoder.java
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.nio.charset.UnsupportedCharsetException;
4242
import java.util.ArrayList;
4343
import java.util.List;
44+
import java.util.Locale;
4445
import java.util.Map;
4546
import java.util.TreeMap;
4647

@@ -899,7 +900,11 @@ protected InterfaceHttpData getFileUpload(String delimiter) {
899900
if (encoding != null) {
900901
String code;
901902
try {
902-
code = encoding.getValue().toLowerCase();
903+
// RFC 2045 Content-Transfer-Encoding values are case-insensitive ASCII tokens.
904+
// toLowerCase() without a Locale would corrupt them under Turkish (tr_TR) locale,
905+
// where 'I' lowercases to 'ı' (U+0131) and "BINARY" becomes "bınary" - never
906+
// matching the lowercase ASCII constants compared against below.
907+
code = encoding.getValue().toLowerCase(Locale.US);
903908
} catch (IOException e) {
904909
throw new ErrorDataDecoderException(e);
905910
}
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.ArrayList;
4646
import java.util.List;
4747
import java.util.ListIterator;
48+
import java.util.Locale;
4849
import java.util.concurrent.ThreadLocalRandom;
4950

5051
import static io.netty.buffer.Unpooled.wrappedBuffer;
@@ -753,7 +754,12 @@ public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
753754
headers.remove(HttpHeaderNames.CONTENT_TYPE);
754755
for (String contentType : contentTypes) {
755756
// "multipart/form-data; boundary=--89421926422648"
756-
String lowercased = contentType.toLowerCase();
757+
// toLowerCase() without a Locale would corrupt the comparison under Turkish
758+
// (tr_TR) locale, where 'I' -> 'ı' (U+0131): a request that sets
759+
// Content-Type: MULTIPART/form-data would lowercase to "multıpart/form-data" and
760+
// miss the prefix check, leaving the original header in place alongside the
761+
// multipart one this encoder is about to add.
762+
String lowercased = contentType.toLowerCase(Locale.US);
757763
if (lowercased.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString()) ||
758764
lowercased.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
759765
// ignore
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostMultipartLocaleDecoderTest.java‎

Copy file name to clipboard
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.http.multipart;
17+
18+
import io.netty.buffer.Unpooled;
19+
import io.netty.handler.codec.http.DefaultFullHttpRequest;
20+
import io.netty.handler.codec.http.FullHttpRequest;
21+
import io.netty.handler.codec.http.HttpMethod;
22+
import io.netty.handler.codec.http.HttpVersion;
23+
import io.netty.util.CharsetUtil;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.parallel.Isolated;
26+
27+
import java.util.Locale;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertNotNull;
31+
32+
/**
33+
* Decoder-side test that flips {@link Locale#setDefault(Locale)} to Turkish to exercise the
34+
* {@code Content-Transfer-Encoding} normalization path. Marked {@link Isolated} because the JVM
35+
* default Locale is process-global state and codec-http runs tests with
36+
* {@code junit.jupiter.execution.parallel.mode.default = concurrent}.
37+
*/
38+
@Isolated("Mutates Locale.getDefault() which is JVM-global state.")
39+
class HttpPostMultipartLocaleDecoderTest {
40+
41+
@Test
42+
void testUppercaseBinaryTransferEncodingUnderTurkishLocale() {
43+
// Repro: a part declares an uppercase Content-Transfer-Encoding (RFC 2045 mechanism
44+
// tokens are case-insensitive). On a Turkish-locale JVM the decoder used to call
45+
// toLowerCase() without a Locale and produced "bınary" (U+0131) which then failed the
46+
// compare against the lowercase ASCII constants and threw
47+
// "TransferEncoding Unknown: bınary".
48+
Locale original = Locale.getDefault();
49+
try {
50+
Locale.setDefault(new Locale("tr", "TR"));
51+
String content = "\n--861fbeab-cd20-470c-9609-d40a0f704466\r\n" +
52+
"content-disposition: form-data; " +
53+
"name=\"file\"; filename=\"myfile.ogg\"\r\n" +
54+
"content-type: audio/ogg; codecs=opus; charset=UTF8\r\n" +
55+
"Content-Transfer-Encoding: BINARY\r\n" +
56+
"\r\n\r\n--861fbeab-cd20-470c-9609-d40a0f704466--\r\n";
57+
58+
FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/upload",
59+
Unpooled.copiedBuffer(content, CharsetUtil.US_ASCII));
60+
req.headers().set("content-type", "multipart/form-data; boundary=861fbeab-cd20-470c-9609-d40a0f704466");
61+
req.headers().set("content-length", content.length());
62+
63+
HttpPostMultipartRequestDecoder decoder = new HttpPostMultipartRequestDecoder(req);
64+
FileUpload httpData = (FileUpload) decoder.getBodyHttpDatas("file").get(0);
65+
assertNotNull(httpData);
66+
assertEquals("audio/ogg", httpData.getContentType());
67+
decoder.destroy();
68+
} finally {
69+
Locale.setDefault(original);
70+
}
71+
}
72+
}
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostMultipartLocaleEncoderTest.java‎

Copy file name to clipboard
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.http.multipart;
17+
18+
import io.netty.handler.codec.http.DefaultFullHttpRequest;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.parallel.Isolated;
21+
22+
import java.util.List;
23+
import java.util.Locale;
24+
25+
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
26+
import static io.netty.handler.codec.http.HttpMethod.POST;
27+
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
31+
/**
32+
* Encoder-side test that flips {@link Locale#setDefault(Locale)} to Turkish to exercise the
33+
* {@code Content-Type} de-duplication path inside
34+
* {@link HttpPostRequestEncoder#finalizeRequest()}. Marked {@link Isolated} because the JVM
35+
* default Locale is process-global state and codec-http runs tests with
36+
* {@code junit.jupiter.execution.parallel.mode.default = concurrent}.
37+
*/
38+
@Isolated("Mutates Locale.getDefault() which is JVM-global state.")
39+
class HttpPostMultipartLocaleEncoderTest {
40+
41+
@Test
42+
void testFinalizeRemovesPreexistingMultipartContentTypeUnderTurkishLocale() throws Exception {
43+
// Repro: a caller pre-sets `Content-Type: MULTIPART/form-data` (uppercase, RFC-allowed,
44+
// case-insensitive). When the JVM default Locale is Turkish, the encoder's
45+
// contentType.toLowerCase() used to map 'I' -> 'ı' (U+0131), the resulting
46+
// "multıpart/form-data" missed the prefix check, and the original mixed-case header was
47+
// left alongside the new multipart Content-Type the encoder is about to set - producing
48+
// two Content-Type headers on the request.
49+
Locale original = Locale.getDefault();
50+
try {
51+
Locale.setDefault(new Locale("tr", "TR"));
52+
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "http://localhost");
53+
request.headers().add(CONTENT_TYPE, "MULTIPART/form-data; boundary=preexisting");
54+
HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
55+
encoder.finalizeRequest();
56+
List<String> contentTypes = request.headers().getAll(CONTENT_TYPE);
57+
assertEquals(1, contentTypes.size(),
58+
"encoder must drop the pre-existing multipart Content-Type regardless of JVM locale");
59+
assertTrue(contentTypes.get(0).startsWith("multipart/form-data"),
60+
"the surviving Content-Type must be the encoder's freshly-built multipart header");
61+
request.release();
62+
} finally {
63+
Locale.setDefault(original);
64+
}
65+
}
66+
}
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoderTest.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,5 @@ private String generateString(int length) {
519519
Arrays.fill(array, 'a');
520520
return new String(array);
521521
}
522+
522523
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.