Repository navigation
Auto-port 4.1: Reject non-token characters in HTTP/2 header names - #17022
Merged
Merged
Conversation
## Problem
`DefaultHttp2Headers` accepts header names that contain bytes outside
the HTTP token grammar — non-ASCII (e.g. `0xF0`), control characters
(NUL/CR/LF/VT/FF/HTAB/...), SP, DEL, and the RFC 7230 separators
(`"(),/:;<=>?@[\\]{}\""`). The current `HTTP2_NAME_VALIDATOR` only
rejects upper-case ASCII and lets everything else through, so something
like `headers.add(new AsciiString(new byte[]{(byte)0xF0}), "v")`
succeeds and is then encoded onto the wire.
This was reported in #11975 with maintainer agreement on direction:
- @idelpivnitskiy: confirmed the field-name grammar
- @ejona86: "good and appropriate to restrict keys to the values
permitted in HTTP/1, with little risk"
## Root Cause
`DefaultHttp2Headers.HTTP2_NAME_VALIDATOR_PROCESSOR` returns
`!isUpperCase(value)` and the non-`AsciiString` fallback only loops on
`isUpperCase(charAt(i))`. Both paths therefore enforce the lower-case
rule but skip the rest of RFC 9113 §8.2.1, which inherits the RFC 7230
token grammar.
## Fix
In
`codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java`:
- After the empty/null and pseudo-header checks, run
`HttpHeaderValidationUtil.validateToken(name)` and throw
`connectionError(PROTOCOL_ERROR, ...)` when it returns a non-`-1` index.
- Keep the existing upper-case `ByteProcessor` / fallback loop. The
token check is additive: pseudo-headers still short-circuit, valid
lower-case tokens still pass, upper-case ASCII still gets rejected (now
at the upper-case check, not the token check).
Pseudo-headers (`:method`, `:path`, ...) deliberately bypass the token
check via the existing `hasPseudoHeaderFormat` early return, since `:`
is not a valid token character but is required for pseudo-headers.
## Tests Added
| Change point | Test |
|--------------|------|
| Reject non-ASCII via the `AsciiString` byte path |
`rejectNonAsciiHeaderNameAsciiString` (uses the U+1F631 / `0xF0 0x9F
0x98 0xB1` reproducer from #11975) |
| Reject non-ASCII via the `CharSequence` `charAt` path |
`rejectNonAsciiHeaderNameCharSequence`,
`rejectHighBitHeaderNameCharSequence` (covers char ≤ 0xFF and char >
0xFF) |
| Reject every C0 control byte and every RFC 7230 separator |
`rejectNonTokenCharactersInHeaderName` (parameterized — 28 cases: NUL,
SOH, BEL, BS, HTAB, LF, VT, FF, CR, US, DEL, SP, and `, ; : / = ? @ ( )
[ ] { } < > \ "`) |
| Regression: upper-case rejection unchanged |
`uppercaseHeaderNameStillRejected` |
| Regression: full RFC 7230 lower-case token still accepted |
`acceptValidLowercaseTokenHeaderName` (covers `x-custom-header`,
`2name`, and a name using every special token char `!#$%&'*+-.^_\`|~`) |
| Regression: pseudo-headers still accepted | `acceptPseudoHeaderName` |
| Existing wire-level test now exercises the validator |
`InboundHttp2ToHttpAdapterTest.clientRequestSingleHeaderNonAsciiShouldThrow`
updated — the rejection now fires at `headers.add(...)` instead of at
the HPACK encoder |
`mvn -pl codec-http2 test -Drevapi.skip=true` runs 1512 tests with 0
failures locally; `codec-http` regression run is also clean (8891 tests,
0 failures).
## Impact
- **API**: `new DefaultHttp2Headers().add(name, value)` now throws
`Http2Exception(PROTOCOL_ERROR, ...)` for any name that is not a valid
lower-case RFC 7230 token. Code that was relying on the previous lax
behaviour (passing non-ASCII or separator bytes) needs to either (a)
clean up the name before adding, or (b) construct the headers with `new
DefaultHttp2Headers(false)` to opt out of validation, which already
exists.
- **Wire effect**: Inbound HPACK-decoded headers with malformed names
now produce an `Http2Exception` at validation time and are surfaced via
the existing decoder failure path. The previous behaviour silently
accepted them and could allow malformed bytes to reach the application
as parsed `Http2Headers`.
- **Hot path**: Standard requests are unaffected — `DefaultHttp2Headers`
validation only runs when adding non-pseudo headers, and the new
`validateToken` call is a single forward scan that returns `-1` on the
first invalid byte for clean tokens.
Fixes #11975
---------
Co-authored-by: Norman Maurer <[email protected]>
(cherry picked from commit 6661ee4)
chrisvest
enabled auto-merge (squash)
June 30, 2026 20:43
lltopk
pushed a commit
to lltopk/netty
that referenced
this pull request
Oct 24, 2026
…tty#17022) Auto-port of netty#16762 to 4.1 Cherry-picked commit: 6661ee4 --- ## Problem `DefaultHttp2Headers` accepts header names that contain bytes outside the HTTP token grammar — non-ASCII (e.g. `0xF0`), control characters (NUL/CR/LF/VT/FF/HTAB/...), SP, DEL, and the RFC 7230 separators (`"(),/:;<=>?@[\\]{}\""`). The current `HTTP2_NAME_VALIDATOR` only rejects upper-case ASCII and lets everything else through, so something like `headers.add(new AsciiString(new byte[]{(byte)0xF0}), "v")` succeeds and is then encoded onto the wire. This was reported in netty#11975 with maintainer agreement on direction: - @idelpivnitskiy: confirmed the field-name grammar - @ejona86: "good and appropriate to restrict keys to the values permitted in HTTP/1, with little risk" ## Root Cause `DefaultHttp2Headers.HTTP2_NAME_VALIDATOR_PROCESSOR` returns `!isUpperCase(value)` and the non-`AsciiString` fallback only loops on `isUpperCase(charAt(i))`. Both paths therefore enforce the lower-case rule but skip the rest of RFC 9113 §8.2.1, which inherits the RFC 7230 token grammar. ## Fix In `codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java`: - After the empty/null and pseudo-header checks, run `HttpHeaderValidationUtil.validateToken(name)` and throw `connectionError(PROTOCOL_ERROR, ...)` when it returns a non-`-1` index. - Keep the existing upper-case `ByteProcessor` / fallback loop. The token check is additive: pseudo-headers still short-circuit, valid lower-case tokens still pass, upper-case ASCII still gets rejected (now at the upper-case check, not the token check). Pseudo-headers (`:method`, `:path`, ...) deliberately bypass the token check via the existing `hasPseudoHeaderFormat` early return, since `:` is not a valid token character but is required for pseudo-headers. ## Tests Added | Change point | Test | |--------------|------| | Reject non-ASCII via the `AsciiString` byte path | `rejectNonAsciiHeaderNameAsciiString` (uses the U+1F631 / `0xF0 0x9F 0x98 0xB1` reproducer from netty#11975) | | Reject non-ASCII via the `CharSequence` `charAt` path | `rejectNonAsciiHeaderNameCharSequence`, `rejectHighBitHeaderNameCharSequence` (covers char ≤ 0xFF and char > 0xFF) | | Reject every C0 control byte and every RFC 7230 separator | `rejectNonTokenCharactersInHeaderName` (parameterized — 28 cases: NUL, SOH, BEL, BS, HTAB, LF, VT, FF, CR, US, DEL, SP, and `, ; : / = ? @ ( ) [ ] { } < > \ "`) | | Regression: upper-case rejection unchanged | `uppercaseHeaderNameStillRejected` | | Regression: full RFC 7230 lower-case token still accepted | `acceptValidLowercaseTokenHeaderName` (covers `x-custom-header`, `2name`, and a name using every special token char `!#$%&'*+-.^_\`|~`) | | Regression: pseudo-headers still accepted | `acceptPseudoHeaderName` | | Existing wire-level test now exercises the validator | `InboundHttp2ToHttpAdapterTest.clientRequestSingleHeaderNonAsciiShouldThrow` updated — the rejection now fires at `headers.add(...)` instead of at the HPACK encoder | `mvn -pl codec-http2 test -Drevapi.skip=true` runs 1512 tests with 0 failures locally; `codec-http` regression run is also clean (8891 tests, 0 failures). ## Impact - **API**: `new DefaultHttp2Headers().add(name, value)` now throws `Http2Exception(PROTOCOL_ERROR, ...)` for any name that is not a valid lower-case RFC 7230 token. Code that was relying on the previous lax behaviour (passing non-ASCII or separator bytes) needs to either (a) clean up the name before adding, or (b) construct the headers with `new DefaultHttp2Headers(false)` to opt out of validation, which already exists. - **Wire effect**: Inbound HPACK-decoded headers with malformed names now produce an `Http2Exception` at validation time and are surfaced via the existing decoder failure path. The previous behaviour silently accepted them and could allow malformed bytes to reach the application as parsed `Http2Headers`. - **Hot path**: Standard requests are unaffected — `DefaultHttp2Headers` validation only runs when adding non-pseudo headers, and the new `validateToken` call is a single forward scan that returns `-1` on the first invalid byte for clean tokens. Fixes netty#11975 --------- Co-authored-by: Guimu <[email protected]> Co-authored-by: Norman Maurer <[email protected]> Co-authored-by: Chris Vest <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Auto-port of #16762 to 4.1
Cherry-picked commit: 6661ee4
Problem
DefaultHttp2Headersaccepts header names that contain bytes outside the HTTP token grammar — non-ASCII (e.g.0xF0), control characters (NUL/CR/LF/VT/FF/HTAB/...), SP, DEL, and the RFC 7230 separators ("(),/:;<=>?@[\\]{}\""). The currentHTTP2_NAME_VALIDATORonly rejects upper-case ASCII and lets everything else through, so something likeheaders.add(new AsciiString(new byte[]{(byte)0xF0}), "v")succeeds and is then encoded onto the wire.This was reported in #11975 with maintainer agreement on direction:
Root Cause
DefaultHttp2Headers.HTTP2_NAME_VALIDATOR_PROCESSORreturns!isUpperCase(value)and the non-AsciiStringfallback only loops onisUpperCase(charAt(i)). Both paths therefore enforce the lower-case rule but skip the rest of RFC 9113 §8.2.1, which inherits the RFC 7230 token grammar.Fix
In
codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java:HttpHeaderValidationUtil.validateToken(name)and throwconnectionError(PROTOCOL_ERROR, ...)when it returns a non--1index.ByteProcessor/ fallback loop. The token check is additive: pseudo-headers still short-circuit, valid lower-case tokens still pass, upper-case ASCII still gets rejected (now at the upper-case check, not the token check).Pseudo-headers (
:method,:path, ...) deliberately bypass the token check via the existinghasPseudoHeaderFormatearly return, since:is not a valid token character but is required for pseudo-headers.Tests Added
AsciiStringbyte pathrejectNonAsciiHeaderNameAsciiString(uses the U+1F631 /0xF0 0x9F 0x98 0xB1reproducer from #11975)CharSequencecharAtpathrejectNonAsciiHeaderNameCharSequence,rejectHighBitHeaderNameCharSequence(covers char ≤ 0xFF and char > 0xFF)rejectNonTokenCharactersInHeaderName(parameterized — 28 cases: NUL, SOH, BEL, BS, HTAB, LF, VT, FF, CR, US, DEL, SP, and, ; : / = ? @ ( ) [ ] { } < > \ ")uppercaseHeaderNameStillRejectedacceptValidLowercaseTokenHeaderName(coversx-custom-header,2name, and a name using every special token char!#$%&'*+-.^_\acceptPseudoHeaderNameInboundHttp2ToHttpAdapterTest.clientRequestSingleHeaderNonAsciiShouldThrowupdated — the rejection now fires atheaders.add(...)instead of at the HPACK encodermvn -pl codec-http2 test -Drevapi.skip=trueruns 1512 tests with 0 failures locally;codec-httpregression run is also clean (8891 tests, 0 failures).Impact
new DefaultHttp2Headers().add(name, value)now throwsHttp2Exception(PROTOCOL_ERROR, ...)for any name that is not a valid lower-case RFC 7230 token. Code that was relying on the previous lax behaviour (passing non-ASCII or separator bytes) needs to either (a) clean up the name before adding, or (b) construct the headers withnew DefaultHttp2Headers(false)to opt out of validation, which already exists.Http2Exceptionat validation time and are surfaced via the existing decoder failure path. The previous behaviour silently accepted them and could allow malformed bytes to reach the application as parsedHttp2Headers.DefaultHttp2Headersvalidation only runs when adding non-pseudo headers, and the newvalidateTokencall is a single forward scan that returns-1on the first invalid byte for clean tokens.Fixes #11975