GeezerVison is showing github.com — Pin HTTP/RTSP version + method normalization to Locale.US · netty/netty@64c47e0
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec test/java/io/netty/handler/codec
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpVersion.java +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import io.netty.util.CharsetUtil; | 20 | 20 | import io.netty.util.internal.ObjectUtil; | 21 | 21 |
| | 22 | +import java.util.Locale; | | 23 | + | 22 | 24 | import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim; | 23 | 25 | import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero; | 24 | 26 |
| @@ -119,7 +121,10 @@ public HttpVersion(String text, boolean keepAliveDefault) { | 119 | 121 | } | 120 | 122 |
| 121 | 123 | HttpVersion(String text, boolean strict, boolean keepAliveDefault) { | 122 | | - text = checkNonEmptyAfterTrim(text, "text").toUpperCase(); | | 124 | + // toUpperCase() without an explicit Locale uses the JVM default. In Turkish locale | | 125 | + // (tr_TR) 'i' uppercases to 'İ' (U+0130), which would corrupt protocol strings such | | 126 | + // as "icap/1.0" or any custom HTTP-derived scheme that contains a lowercase 'i'. | | 127 | + text = checkNonEmptyAfterTrim(text, "text").toUpperCase(Locale.US); | 123 | 128 |
| 124 | 129 | if (strict) { | 125 | 130 | // Only single digit major / minor version is allowed. | @@ -197,7 +202,9 @@ public HttpVersion( | 197 | 202 | private HttpVersion( | 198 | 203 | String protocolName, int majorVersion, int minorVersion, | 199 | 204 | boolean keepAliveDefault, boolean bytes) { | 200 | | - protocolName = checkNonEmptyAfterTrim(protocolName, "protocolName").toUpperCase(); | | 205 | + // See the comment in the (text, strict, keepAliveDefault) constructor for why this needs | | 206 | + // an explicit Locale.US: avoids the Turkish-locale 'i' -> 'İ' corruption. | | 207 | + protocolName = checkNonEmptyAfterTrim(protocolName, "protocolName").toUpperCase(Locale.US); | 201 | 208 |
| 202 | 209 | for (int i = 0; i < protocolName.length(); i ++) { | 203 | 210 | if (Character.isISOControl(protocolName.charAt(i)) || | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspMethods.java +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import io.netty.handler.codec.http.HttpMethod; | 21 | 21 |
| 22 | 22 | import java.util.HashMap; | | 23 | +import java.util.Locale; | 23 | 24 | import java.util.Map; | 24 | 25 |
| 25 | 26 | /** | @@ -119,7 +120,10 @@ public final class RtspMethods { | 119 | 120 | * will be returned. Otherwise, a new instance will be returned. | 120 | 121 | */ | 121 | 122 | public static HttpMethod valueOf(String name) { | 122 | | - name = checkNonEmptyAfterTrim(name, "name").toUpperCase(); | | 123 | + // RFC 2326 RTSP method names are ASCII tokens. toUpperCase() without an explicit Locale | | 124 | + // uses the JVM default, which in Turkish (tr_TR) maps 'i' to 'İ' (U+0130) and breaks the | | 125 | + // lookup of methods such as "describe" or "redirect" against the cached uppercase keys. | | 126 | + name = checkNonEmptyAfterTrim(name, "name").toUpperCase(Locale.US); | 123 | 127 | HttpMethod result = methodMap.get(name); | 124 | 128 | if (result != null) { | 125 | 129 | return result; | |
Collapse file Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/rtsp/RtspVersions.java +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | import io.netty.handler.codec.http.HttpVersion; | 19 | 19 | import io.netty.util.internal.ObjectUtil; | 20 | 20 |
| | 21 | +import java.util.Locale; | | 22 | + | 21 | 23 | /** | 22 | 24 | * The version of RTSP. | 23 | 25 | */ | @@ -37,7 +39,9 @@ public final class RtspVersions { | 37 | 39 | public static HttpVersion valueOf(String text) { | 38 | 40 | ObjectUtil.checkNotNull(text, "text"); | 39 | 41 |
| 40 | | - text = text.trim().toUpperCase(); | | 42 | + // toUpperCase() must specify Locale.US so the comparison against "RTSP/1.0" is not | | 43 | + // affected by the JVM default locale (e.g. Turkish, where 'i' uppercases to 'İ'). | | 44 | + text = text.trim().toUpperCase(Locale.US); | 41 | 45 | if ("RTSP/1.0".equals(text)) { | 42 | 46 | return RTSP_1_0; | 43 | 47 | } | |
Collapse file Copy file name to clipboard +69Lines changed: 69 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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; | | 17 | + | | 18 | +import org.junit.jupiter.api.Test; | | 19 | +import org.junit.jupiter.api.parallel.Isolated; | | 20 | + | | 21 | +import java.util.Locale; | | 22 | + | | 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; | | 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; | | 25 | + | | 26 | +/** | | 27 | + * Tests that exercise {@link HttpVersion} parsing with the JVM-default Locale flipped to a value | | 28 | + * that exposes the Turkish dotted-I problem (U+0130). {@link Locale#setDefault} is process-global | | 29 | + * mutable state, so this class is marked {@link Isolated} to keep it from leaking into the rest | | 30 | + * of the codec-http suite, which runs in concurrent mode | | 31 | + * ({@code junit.jupiter.execution.parallel.mode.default = concurrent}). | | 32 | + */ | | 33 | +@Isolated("Mutates Locale.getDefault() which is JVM-global state.") | | 34 | +class HttpVersionLocaleTest { | | 35 | + | | 36 | + @Test | | 37 | + void testLowercaseIotaProtocolNameUnderTurkishLocale() { | | 38 | + // Turkish locale maps 'i' -> 'İ' (U+0130) under the JVM-default toUpperCase(). | | 39 | + // The constructor must use Locale.US so an HTTP-derived protocol name like "icap" | | 40 | + // round-trips to the ASCII "ICAP" instead of being corrupted to "İCAP". | | 41 | + Locale original = Locale.getDefault(); | | 42 | + try { | | 43 | + Locale.setDefault(new Locale("tr", "TR")); | | 44 | + HttpVersion version = HttpVersion.valueOf("icap/1.0"); | | 45 | + assertEquals("ICAP", version.protocolName()); | | 46 | + for (int i = 0; i < version.protocolName().length(); i++) { | | 47 | + assertTrue(version.protocolName().charAt(i) < 0x80, | | 48 | + "protocolName must remain ASCII regardless of JVM default locale"); | | 49 | + } | | 50 | + assertEquals("ICAP/1.0", version.text()); | | 51 | + } finally { | | 52 | + Locale.setDefault(original); | | 53 | + } | | 54 | + } | | 55 | + | | 56 | + @Test | | 57 | + void testProtocolNameConstructorUnderTurkishLocale() { | | 58 | + // Same Locale.US guarantee for the (protocolName, major, minor, ...) constructor. | | 59 | + Locale original = Locale.getDefault(); | | 60 | + try { | | 61 | + Locale.setDefault(new Locale("tr", "TR")); | | 62 | + HttpVersion version = new HttpVersion("icap", 1, 0, true); | | 63 | + assertEquals("ICAP", version.protocolName()); | | 64 | + assertEquals("ICAP/1.0", version.text()); | | 65 | + } finally { | | 66 | + Locale.setDefault(original); | | 67 | + } | | 68 | + } | | 69 | +} |
Collapse file Copy file name to clipboard +67Lines changed: 67 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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.rtsp; | | 17 | + | | 18 | +import io.netty.handler.codec.http.HttpMethod; | | 19 | +import org.junit.jupiter.api.Test; | | 20 | +import org.junit.jupiter.api.parallel.Isolated; | | 21 | + | | 22 | +import java.util.Locale; | | 23 | + | | 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; | | 25 | +import static org.junit.jupiter.api.Assertions.assertSame; | | 26 | + | | 27 | +@Isolated("valueOfNormalizesLowercaseInputUnderTurkishLocale flips the JVM-default Locale, " | | 28 | + + "which is process-global state, so the class must not run alongside the rest of the " | | 29 | + + "codec-http suite (junit.jupiter.execution.parallel.mode.default = concurrent).") | | 30 | +class RtspMethodsTest { | | 31 | + | | 32 | + @Test | | 33 | + void valueOfReturnsCachedInstanceForUppercaseName() { | | 34 | + assertSame(RtspMethods.DESCRIBE, RtspMethods.valueOf("DESCRIBE")); | | 35 | + assertSame(RtspMethods.SETUP, RtspMethods.valueOf("SETUP")); | | 36 | + assertSame(RtspMethods.GET_PARAMETER, RtspMethods.valueOf("GET_PARAMETER")); | | 37 | + } | | 38 | + | | 39 | + @Test | | 40 | + void valueOfNormalizesLowercaseInputUnderUsLocale() { | | 41 | + assertSame(RtspMethods.DESCRIBE, RtspMethods.valueOf("describe")); | | 42 | + assertSame(RtspMethods.PLAY, RtspMethods.valueOf("play")); | | 43 | + assertSame(RtspMethods.REDIRECT, RtspMethods.valueOf("redirect")); | | 44 | + } | | 45 | + | | 46 | + @Test | | 47 | + void valueOfNormalizesLowercaseInputUnderTurkishLocale() { | | 48 | + // In Turkish locale (tr_TR), 'i' uppercases to 'İ' (U+0130) under the JVM default. | | 49 | + // RtspMethods.valueOf must pin Locale.US so RTSP method names that contain 'i' such as | | 50 | + // "describe" or "redirect" continue to resolve to the cached uppercase entries. | | 51 | + Locale original = Locale.getDefault(); | | 52 | + try { | | 53 | + Locale.setDefault(new Locale("tr", "TR")); | | 54 | + HttpMethod describe = RtspMethods.valueOf("describe"); | | 55 | + HttpMethod redirect = RtspMethods.valueOf("redirect"); | | 56 | + assertSame(RtspMethods.DESCRIBE, describe); | | 57 | + assertSame(RtspMethods.REDIRECT, redirect); | | 58 | + // Sanity-check: with the locale-default toUpperCase the names would have contained | | 59 | + // U+0130 instead of plain 'I'. Asserting the resolved names round-trip to ASCII | | 60 | + // pins down that the fix is taking effect. | | 61 | + assertEquals("DESCRIBE", describe.name()); | | 62 | + assertEquals("REDIRECT", redirect.name()); | | 63 | + } finally { | | 64 | + Locale.setDefault(original); | | 65 | + } | | 66 | + } | | 67 | +} |
|