GeezerVison is showing github.com — Pin HTTP/RTSP version + method normalization to Locale.US by daguimu · Pull Request #16765 · netty/netty
Pin HTTP/RTSP version + method normalization to Locale.US
#16765
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.java
(5)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -19,6 +19,8 @@
import io.netty.util.CharsetUtil;
import io.netty.util.internal.ObjectUtil;
import java.util.Locale;
import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
Expand Down
Expand Up
@@ -119,7 +121,10 @@ public HttpVersion(String text, boolean keepAliveDefault) {
}
HttpVersion(String text, boolean strict, boolean keepAliveDefault) {
text = checkNonEmptyAfterTrim(text, "text").toUpperCase();
// toUpperCase() without an explicit Locale uses the JVM default. In Turkish locale
// (tr_TR) 'i' uppercases to 'İ' (U+0130), which would corrupt protocol strings such
// as "icap/1.0" or any custom HTTP-derived scheme that contains a lowercase 'i'.
text = checkNonEmptyAfterTrim(text, "text").toUpperCase(Locale.US);
if (strict) {
// Only single digit major / minor version is allowed.
Expand Down
Expand Up
@@ -197,7 +202,9 @@ public HttpVersion(
private HttpVersion(
String protocolName, int majorVersion, int minorVersion,
boolean keepAliveDefault, boolean bytes) {
protocolName = checkNonEmptyAfterTrim(protocolName, "protocolName").toUpperCase();
// See the comment in the (text, strict, keepAliveDefault) constructor for why this needs
// an explicit Locale.US: avoids the Turkish-locale 'i' -> 'İ' corruption.
protocolName = checkNonEmptyAfterTrim(protocolName, "protocolName").toUpperCase(Locale.US);
for (int i = 0; i < protocolName.length(); i ++) {
if (Character.isISOControl(protocolName.charAt(i)) ||
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -20,6 +20,7 @@
import io.netty.handler.codec.http.HttpMethod;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
Expand Down
Expand Up
@@ -119,7 +120,10 @@ public final class RtspMethods {
* will be returned. Otherwise, a new instance will be returned.
*/
public static HttpMethod valueOf(String name) {
name = checkNonEmptyAfterTrim(name, "name").toUpperCase();
// RFC 2326 RTSP method names are ASCII tokens. toUpperCase() without an explicit Locale
// uses the JVM default, which in Turkish (tr_TR) maps 'i' to 'İ' (U+0130) and breaks the
// lookup of methods such as "describe" or "redirect" against the cached uppercase keys.
name = checkNonEmptyAfterTrim(name, "name").toUpperCase(Locale.US);
HttpMethod result = methodMap.get(name);
if (result != null) {
return result;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -18,6 +18,8 @@
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.internal.ObjectUtil;
import java.util.Locale;
/**
* The version of RTSP.
*/
Expand All
@@ -37,7 +39,9 @@ public final class RtspVersions {
public static HttpVersion valueOf(String text) {
ObjectUtil.checkNotNull(text, "text");
text = text.trim().toUpperCase();
// toUpperCase() must specify Locale.US so the comparison against "RTSP/1.0" is not
// affected by the JVM default locale (e.g. Turkish, where 'i' uppercases to 'İ').
text = text.trim().toUpperCase(Locale.US);
if ("RTSP/1.0".equals(text)) {
return RTSP_1_0;
}
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2026 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.http;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
import java.util.Locale;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Tests that exercise {@link HttpVersion} parsing with the JVM-default Locale flipped to a value
* that exposes the Turkish dotted-I problem (U+0130). {@link Locale#setDefault} is process-global
* mutable state, so this class is marked {@link Isolated} to keep it from leaking into the rest
* of the codec-http suite, which runs in concurrent mode
* ({@code junit.jupiter.execution.parallel.mode.default = concurrent}).
*/
@Isolated("Mutates Locale.getDefault() which is JVM-global state.")
class HttpVersionLocaleTest {
@Test
void testLowercaseIotaProtocolNameUnderTurkishLocale() {
// Turkish locale maps 'i' -> 'İ' (U+0130) under the JVM-default toUpperCase().
// The constructor must use Locale.US so an HTTP-derived protocol name like "icap"
// round-trips to the ASCII "ICAP" instead of being corrupted to "İCAP".
Locale original = Locale.getDefault();
try {
Locale.setDefault(new Locale("tr", "TR"));
HttpVersion version = HttpVersion.valueOf("icap/1.0");
assertEquals("ICAP", version.protocolName());
for (int i = 0; i < version.protocolName().length(); i++) {
assertTrue(version.protocolName().charAt(i) < 0x80,
"protocolName must remain ASCII regardless of JVM default locale");
}
assertEquals("ICAP/1.0", version.text());
} finally {
Locale.setDefault(original);
}
}
@Test
void testProtocolNameConstructorUnderTurkishLocale() {
// Same Locale.US guarantee for the (protocolName, major, minor, ...) constructor.
Locale original = Locale.getDefault();
try {
Locale.setDefault(new Locale("tr", "TR"));
HttpVersion version = new HttpVersion("icap", 1, 0, true);
assertEquals("ICAP", version.protocolName());
assertEquals("ICAP/1.0", version.text());
} finally {
Locale.setDefault(original);
}
}
}
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2026 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.rtsp;
import io.netty.handler.codec.http.HttpMethod;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
import java.util.Locale;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
@Isolated("valueOfNormalizesLowercaseInputUnderTurkishLocale flips the JVM-default Locale, "
+ "which is process-global state, so the class must not run alongside the rest of the "
+ "codec-http suite (junit.jupiter.execution.parallel.mode.default = concurrent).")
class RtspMethodsTest {
@Test
void valueOfReturnsCachedInstanceForUppercaseName() {
assertSame(RtspMethods.DESCRIBE, RtspMethods.valueOf("DESCRIBE"));
assertSame(RtspMethods.SETUP, RtspMethods.valueOf("SETUP"));
assertSame(RtspMethods.GET_PARAMETER, RtspMethods.valueOf("GET_PARAMETER"));
}
@Test
void valueOfNormalizesLowercaseInputUnderUsLocale() {
assertSame(RtspMethods.DESCRIBE, RtspMethods.valueOf("describe"));
assertSame(RtspMethods.PLAY, RtspMethods.valueOf("play"));
assertSame(RtspMethods.REDIRECT, RtspMethods.valueOf("redirect"));
}
@Test
void valueOfNormalizesLowercaseInputUnderTurkishLocale() {
// In Turkish locale (tr_TR), 'i' uppercases to 'İ' (U+0130) under the JVM default.
// RtspMethods.valueOf must pin Locale.US so RTSP method names that contain 'i' such as
// "describe" or "redirect" continue to resolve to the cached uppercase entries.
Locale original = Locale.getDefault();
try {
Locale.setDefault(new Locale("tr", "TR"));
Comment thread
chrisvest marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
HttpMethod describe = RtspMethods.valueOf("describe");
HttpMethod redirect = RtspMethods.valueOf("redirect");
assertSame(RtspMethods.DESCRIBE, describe);
assertSame(RtspMethods.REDIRECT, redirect);
// Sanity-check: with the locale-default toUpperCase the names would have contained
// U+0130 instead of plain 'I'. Asserting the resolved names round-trip to ASCII
// pins down that the fix is taking effect.
assertEquals("DESCRIBE", describe.name());
assertEquals("REDIRECT", redirect.name());
} finally {
Locale.setDefault(original);
}
}
}
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.