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 — Reject control characters at the boundary of HTTP method names · netty/netty@5daffbc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http
      • HttpMethod.java
    • test/java/io/netty/handler/codec/http
      • HttpMethodTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpMethod.java
+25-5Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import io.netty.util.AsciiString;
1919

20-
import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
20+
import static io.netty.util.internal.ObjectUtil.checkNotNull;
2121

2222
/**
2323
* The request method of HTTP or its derived protocols, such as
@@ -118,13 +118,33 @@ public static HttpMethod valueOf(String name) {
118118
* <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
119119
*/
120120
public HttpMethod(String name) {
121-
name = checkNonEmptyAfterTrim(name, "name");
122-
int index = HttpUtil.validateToken(name);
121+
checkNotNull(name, "name");
122+
// Strip leading/trailing SP and HT only. String.trim() would silently strip
123+
// any character <= 0x20 (including NUL and other control bytes), which can mask
124+
// request smuggling vectors when the parsed method name is later compared as a
125+
// valid HTTP token.
126+
int start = 0;
127+
int end = name.length();
128+
while (start < end && isSpaceOrHorizontalTab(name.charAt(start))) {
129+
start++;
130+
}
131+
while (end > start && isSpaceOrHorizontalTab(name.charAt(end - 1))) {
132+
end--;
133+
}
134+
if (start == end) {
135+
throw new IllegalArgumentException("name cannot be empty");
136+
}
137+
String trimmed = start == 0 && end == name.length() ? name : name.substring(start, end);
138+
int index = HttpUtil.validateToken(trimmed);
123139
if (index != -1) {
124140
throw new IllegalArgumentException(
125-
"Illegal character in HTTP Method: 0x" + Integer.toHexString(name.charAt(index)));
141+
"Illegal character in HTTP Method: 0x" + Integer.toHexString(trimmed.charAt(index)));
126142
}
127-
this.name = AsciiString.cached(name);
143+
this.name = AsciiString.cached(trimmed);
144+
}
145+
146+
private static boolean isSpaceOrHorizontalTab(char c) {
147+
return c == ' ' || c == '\t';
128148
}
129149

130150
/**
Collapse file

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

Copy file name to clipboard
+150Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 io.netty.buffer.Unpooled;
19+
import io.netty.channel.embedded.EmbeddedChannel;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.function.Executable;
22+
23+
import java.nio.charset.StandardCharsets;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertSame;
29+
import static org.junit.jupiter.api.Assertions.assertThrows;
30+
31+
public class HttpMethodTest {
32+
33+
private static final String NUL = String.valueOf((char) 0x00);
34+
35+
@Test
36+
public void valueOfReturnsCachedInstanceForKnownMethods() {
37+
assertSame(HttpMethod.GET, HttpMethod.valueOf("GET"));
38+
assertSame(HttpMethod.POST, HttpMethod.valueOf("POST"));
39+
}
40+
41+
@Test
42+
public void constructorAcceptsCustomMethodName() {
43+
HttpMethod custom = new HttpMethod("CUSTOM");
44+
assertEquals("CUSTOM", custom.name());
45+
}
46+
47+
@Test
48+
public void constructorTrimsLeadingAndTrailingSpaces() {
49+
HttpMethod method = new HttpMethod(" GET ");
50+
assertEquals("GET", method.name());
51+
}
52+
53+
@Test
54+
public void constructorTrimsLeadingAndTrailingTabs() {
55+
HttpMethod method = new HttpMethod("\tGET\t");
56+
assertEquals("GET", method.name());
57+
}
58+
59+
@Test
60+
public void constructorRejectsLeadingNul() {
61+
assertThrows(IllegalArgumentException.class, newInstance(NUL + "GET"));
62+
}
63+
64+
@Test
65+
public void constructorRejectsTrailingNul() {
66+
assertThrows(IllegalArgumentException.class, newInstance("GET" + NUL));
67+
}
68+
69+
@Test
70+
public void constructorRejectsLeadingAndTrailingNul() {
71+
assertThrows(IllegalArgumentException.class, newInstance(NUL + "GET" + NUL));
72+
}
73+
74+
@Test
75+
public void constructorRejectsEmbeddedNul() {
76+
assertThrows(IllegalArgumentException.class, newInstance("GE" + NUL + "T"));
77+
}
78+
79+
@Test
80+
public void constructorRejectsCarriageReturn() {
81+
assertThrows(IllegalArgumentException.class, newInstance("GET\r"));
82+
}
83+
84+
@Test
85+
public void constructorRejectsLineFeed() {
86+
assertThrows(IllegalArgumentException.class, newInstance("GET\n"));
87+
}
88+
89+
@Test
90+
public void constructorRejectsVerticalTab() {
91+
assertThrows(IllegalArgumentException.class, newInstance("GET" + (char) 0x0B));
92+
}
93+
94+
@Test
95+
public void constructorRejectsFormFeed() {
96+
assertThrows(IllegalArgumentException.class, newInstance("GET\f"));
97+
}
98+
99+
@Test
100+
public void constructorRejectsEmbeddedSpace() {
101+
assertThrows(IllegalArgumentException.class, newInstance("GE T"));
102+
}
103+
104+
@Test
105+
public void constructorRejectsEmptyString() {
106+
assertThrows(IllegalArgumentException.class, newInstance(""));
107+
}
108+
109+
@Test
110+
public void constructorRejectsBlankString() {
111+
assertThrows(IllegalArgumentException.class, newInstance(" "));
112+
}
113+
114+
@Test
115+
public void requestDecoderRejectsNulPaddedMethod() {
116+
// RFC 7230 forbids any non-token character in the method. NUL-padded methods are
117+
// a known request-smuggling vector if silently stripped, so the decoder must
118+
// surface a decoder failure rather than producing a valid GET message.
119+
EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder());
120+
byte[] data = (NUL + "GET" + NUL + " / HTTP/1.1\r\nHost: x\r\n\r\n")
121+
.getBytes(StandardCharsets.US_ASCII);
122+
ch.writeInbound(Unpooled.wrappedBuffer(data));
123+
HttpRequest req = ch.readInbound();
124+
assertNotNull(req);
125+
assertFalse(req.decoderResult().isSuccess(),
126+
"decoder must reject method names containing NUL bytes");
127+
ch.finishAndReleaseAll();
128+
}
129+
130+
@Test
131+
public void requestDecoderAcceptsCleanMethod() {
132+
// Regression: ordinary GET requests must still parse normally.
133+
EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder());
134+
byte[] data = "GET / HTTP/1.1\r\nHost: x\r\n\r\n".getBytes(StandardCharsets.US_ASCII);
135+
ch.writeInbound(Unpooled.wrappedBuffer(data));
136+
HttpRequest req = ch.readInbound();
137+
assertNotNull(req);
138+
assertEquals(HttpMethod.GET, req.method());
139+
ch.finishAndReleaseAll();
140+
}
141+
142+
private static Executable newInstance(final String name) {
143+
return new Executable() {
144+
@Override
145+
public void execute() {
146+
new HttpMethod(name);
147+
}
148+
};
149+
}
150+
}

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.