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 non-token characters in HTTP/2 header names · netty/netty@7157e5d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • DefaultHttp2Headers.java
    • test/java/io/netty/handler/codec/http2
      • DefaultHttp2HeadersTest.java
      • InboundHttp2ToHttpAdapterTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public void validateName(CharSequence name) {
5454
return;
5555
}
5656

57+
// RFC 9113 Section 8.2.1: HTTP/2 field names are valid HTTP/1.1 tokens (RFC 7230 Section 3.2.6)
58+
// with the additional constraint that they MUST be lowercase. Reject anything outside the token
59+
// grammar (non-ASCII, control characters, SP/HTAB, separators) before the lowercase check.
60+
int tokenIndex = HttpHeaderValidationUtil.validateToken(name);
61+
if (tokenIndex != -1) {
62+
PlatformDependent.throwException(connectionError(PROTOCOL_ERROR,
63+
"invalid header name [%s]", name));
64+
}
65+
5766
if (name instanceof AsciiString) {
5867
final int index;
5968
try {
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2HeadersTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2HeadersTest.java
+123Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,129 @@ public void execute() throws Throwable {
161161
});
162162
}
163163

164+
@Test
165+
public void rejectNonAsciiHeaderNameAsciiString() {
166+
// U+1F631 ("😱") encoded as F0 9F 98 B1 — bytes outside the token grammar that previously
167+
// slipped past the upper-case-only validator. See issue #11975.
168+
final byte[] buf = {(byte) 0xF0, (byte) 0x9F, (byte) 0x98, (byte) 0xB1};
169+
final Http2Headers headers = new DefaultHttp2Headers();
170+
assertThrows(Http2Exception.class, new Executable() {
171+
@Override
172+
public void execute() throws Throwable {
173+
headers.add(new AsciiString(buf), of("test"));
174+
}
175+
});
176+
}
177+
178+
@Test
179+
public void rejectNonAsciiHeaderNameCharSequence() {
180+
final Http2Headers headers = new DefaultHttp2Headers();
181+
assertThrows(Http2Exception.class, new Executable() {
182+
@Override
183+
public void execute() throws Throwable {
184+
// Non-ASCII char via the CharSequence path.
185+
headers.add("naÿme", "test");
186+
}
187+
});
188+
}
189+
190+
@Test
191+
public void rejectHighBitHeaderNameCharSequence() {
192+
final Http2Headers headers = new DefaultHttp2Headers();
193+
assertThrows(Http2Exception.class, new Executable() {
194+
@Override
195+
public void execute() throws Throwable {
196+
// U+0100 — above 0xFF, must be rejected by the CharSequence path.
197+
headers.add("naĀme", "test");
198+
}
199+
});
200+
}
201+
202+
@ParameterizedTest(name = "{displayName} [{index}] byte=0x{0}")
203+
@MethodSource("nonTokenBytesInHeaderName")
204+
void rejectNonTokenCharactersInHeaderName(int illegalByte) {
205+
final String name = "n" + (char) illegalByte + "ame";
206+
final Http2Headers headers = new DefaultHttp2Headers();
207+
assertThrows(Http2Exception.class, new Executable() {
208+
@Override
209+
public void execute() throws Throwable {
210+
headers.add(name, "test");
211+
}
212+
});
213+
}
214+
215+
static Stream<Arguments> nonTokenBytesInHeaderName() {
216+
return Stream.of(
217+
// Control characters: every byte in the C0 range that the previous validator silently let through.
218+
Arguments.of(0x00), // NUL
219+
Arguments.of(0x01), // SOH
220+
Arguments.of(0x07), // BEL
221+
Arguments.of(0x08), // BS
222+
Arguments.of(0x09), // HTAB
223+
Arguments.of(0x0A), // LF
224+
Arguments.of(0x0B), // VT
225+
Arguments.of(0x0C), // FF
226+
Arguments.of(0x0D), // CR
227+
Arguments.of(0x1F), // US
228+
Arguments.of(0x7F), // DEL
229+
// Whitespace and RFC 7230 separators that are not valid token characters.
230+
Arguments.of(0x20), // SP
231+
Arguments.of((int) ','),
232+
Arguments.of((int) ';'),
233+
Arguments.of((int) ':'),
234+
Arguments.of((int) '/'),
235+
Arguments.of((int) '='),
236+
Arguments.of((int) '?'),
237+
Arguments.of((int) '@'),
238+
Arguments.of((int) '('),
239+
Arguments.of((int) ')'),
240+
Arguments.of((int) '['),
241+
Arguments.of((int) ']'),
242+
Arguments.of((int) '{'),
243+
Arguments.of((int) '}'),
244+
Arguments.of((int) '<'),
245+
Arguments.of((int) '>'),
246+
Arguments.of((int) '\\'),
247+
Arguments.of((int) '"')
248+
);
249+
}
250+
251+
@Test
252+
public void uppercaseHeaderNameStillRejected() {
253+
// Regression: pre-existing rejection of upper-case header names must continue to fire.
254+
final Http2Headers headers = new DefaultHttp2Headers();
255+
assertThrows(Http2Exception.class, new Executable() {
256+
@Override
257+
public void execute() throws Throwable {
258+
headers.add(of("X-Custom"), of("test"));
259+
}
260+
});
261+
}
262+
263+
@Test
264+
public void acceptValidLowercaseTokenHeaderName() {
265+
// Regression: valid RFC 7230 token (lower-case ALPHA, DIGIT, "!#$%&'*+-.^_`|~") must be accepted.
266+
Http2Headers headers = new DefaultHttp2Headers();
267+
headers.add(of("x-custom-header"), of("v"));
268+
headers.add(of("2name"), of("v"));
269+
headers.add(of("a!#$%&'*+-.^_`|~b"), of("v"));
270+
assertTrue(headers.contains("x-custom-header"));
271+
assertTrue(headers.contains("2name"));
272+
assertTrue(headers.contains("a!#$%&'*+-.^_`|~b"));
273+
}
274+
275+
@Test
276+
public void acceptPseudoHeaderName() {
277+
// Regression: leading-colon pseudo-header names must remain accepted even though ":" is not a token char.
278+
Http2Headers headers = new DefaultHttp2Headers();
279+
headers.method(of("GET"));
280+
headers.path(of("/"));
281+
headers.scheme(of("https"));
282+
headers.authority(of("example.com"));
283+
assertEquals(of("GET"), headers.method());
284+
assertEquals(of("/"), headers.path());
285+
}
286+
164287
@Test
165288
public void testClearResetsPseudoHeaderDivision() {
166289
DefaultHttp2Headers http2Headers = new DefaultHttp2Headers();
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/InboundHttp2ToHttpAdapterTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/InboundHttp2ToHttpAdapterTest.java
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.junit.jupiter.api.AfterEach;
5151
import org.junit.jupiter.api.BeforeEach;
5252
import org.junit.jupiter.api.Test;
53+
import org.junit.jupiter.api.function.Executable;
5354
import org.mockito.ArgumentCaptor;
5455
import org.mockito.Mock;
5556
import org.mockito.MockitoAnnotations;
@@ -64,6 +65,7 @@
6465
import static java.util.concurrent.TimeUnit.SECONDS;
6566
import static org.junit.jupiter.api.Assertions.assertEquals;
6667
import static org.junit.jupiter.api.Assertions.assertNotNull;
68+
import static org.junit.jupiter.api.Assertions.assertThrows;
6769
import static org.junit.jupiter.api.Assertions.assertTrue;
6870
import static org.mockito.Mockito.times;
6971
import static org.mockito.Mockito.verify;
@@ -240,22 +242,20 @@ public void run() throws Http2Exception {
240242
@Test
241243
public void clientRequestSingleHeaderNonAsciiShouldThrow() throws Exception {
242244
boostrapEnv(1, 1, 1);
245+
// Adding a non-ASCII byte sequence as a header name now fails immediately at validation time
246+
// (RFC 9113 §8.2.1 requires HTTP/2 field names to be valid HTTP/1.1 tokens).
243247
final Http2Headers http2Headers = new DefaultHttp2Headers()
244248
.method(new AsciiString("GET"))
245249
.scheme(new AsciiString("https"))
246250
.authority(new AsciiString("example.org"))
247-
.path(new AsciiString("/some/path/resource2"))
248-
.add(new AsciiString("çã".getBytes(CharsetUtil.UTF_8)),
249-
new AsciiString("Ãã".getBytes(CharsetUtil.UTF_8)));
250-
runInChannel(clientChannel, new Http2Runnable() {
251+
.path(new AsciiString("/some/path/resource2"));
252+
assertThrows(Http2Exception.class, new Executable() {
251253
@Override
252-
public void run() throws Http2Exception {
253-
clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, true, newPromiseClient());
254-
clientChannel.flush();
254+
public void execute() {
255+
http2Headers.add(new AsciiString("çã".getBytes(CharsetUtil.UTF_8)),
256+
new AsciiString("Ãã".getBytes(CharsetUtil.UTF_8)));
255257
}
256258
});
257-
awaitResponses();
258-
assertTrue(isStreamError(clientException));
259259
}
260260

261261
@Test

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.