Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.6k
Validate converted HTTP/2 headers and trailers#2275
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
Merged
hyperxpro
merged 3 commits into
AsyncHttpClient:main
from
maygemdev:perf/http2-response-header-validationJul 25, 2026
+133
−16
Merged
Changes from all commits
Commits
File filter
Filter by extension
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
Diff view
There are no files selected for viewing
54 changes: 38 additions & 16 deletions
54 client/src/main/java/org/asynchttpclient/netty/handler/Http2Handler.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,9 +19,11 @@ | ||
| import io.netty.channel.Channel; | ||
| import io.netty.channel.ChannelHandler.Sharable; | ||
| import io.netty.channel.ChannelHandlerContext; | ||
| import io.netty.handler.codec.http.DefaultHttpHeaders; | ||
| import io.netty.handler.codec.http.DefaultHttpHeadersFactory; | ||
| import io.netty.handler.codec.http.DefaultHttpResponse; | ||
| import io.netty.handler.codec.http.HttpHeaderNames; | ||
| import io.netty.handler.codec.http.HttpHeaders; | ||
| import io.netty.handler.codec.http.HttpHeadersFactory; | ||
| import io.netty.handler.codec.http.HttpResponse; | ||
| import io.netty.handler.codec.http.HttpResponseStatus; | ||
| import io.netty.handler.codec.http.HttpVersion; | ||
| @@ -41,6 +43,7 @@ | ||
| import org.asynchttpclient.netty.request.NettyRequestSender; | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| /** | ||
| * HTTP/2 channel handler for stream child channels created by {@link io.netty.handler.codec.http2.Http2MultiplexHandler}. | ||
| @@ -56,6 +59,7 @@ | ||
| public final class Http2Handler extends AsyncHttpClientHandler { | ||
| private static final HttpVersion HTTP_2 = new HttpVersion("HTTP", 2, 0, true); | ||
| private static final HttpHeadersFactory HEADERS_FACTORY = DefaultHttpHeadersFactory.headersFactory(); | ||
| public Http2Handler(AsyncHttpClientConfig config, ChannelManager channelManager, NettyRequestSender requestSender) { | ||
| super(config, channelManager, requestSender); | ||
| @@ -146,14 +150,7 @@ private void handleHttp2HeadersFrame(Http2HeadersFrame headersFrame, Channel cha | ||
| } | ||
| HttpResponseStatus nettyStatus = HttpResponseStatus.valueOf(statusCode); | ||
| // Build HTTP/1.1-style headers, skipping HTTP/2 pseudo-headers (start with ':') | ||
| HttpHeaders responseHeaders = new DefaultHttpHeaders(); | ||
| h2Headers.forEach(entry -> { | ||
| CharSequence name = entry.getKey(); | ||
| if (name.length() > 0 && name.charAt(0) != ':') { | ||
| responseHeaders.add(name, entry.getValue()); | ||
| } | ||
| }); | ||
| HttpHeaders responseHeaders = copyHttp2Headers(h2Headers); | ||
| // Build a synthetic HttpResponse so the existing interceptor chain can be reused unchanged | ||
| HttpResponse syntheticResponse = new DefaultHttpResponse(HTTP_2, nettyStatus, responseHeaders); | ||
| @@ -222,13 +219,7 @@ private void handleHttp2TrailingHeadersFrame(Http2HeadersFrame headersFrame, Cha | ||
| NettyResponseFuture<?> future, AsyncHandler<?> handler) throws Exception { | ||
| Http2Headers h2Headers = headersFrame.headers(); | ||
| HttpHeaders trailingHeaders = new DefaultHttpHeaders(); | ||
| h2Headers.forEach(entry -> { | ||
| CharSequence name = entry.getKey(); | ||
| if (name.length() > 0 && name.charAt(0) != ':') { | ||
| trailingHeaders.add(name, entry.getValue()); | ||
| } | ||
| }); | ||
| HttpHeaders trailingHeaders = copyHttp2Trailers(h2Headers); | ||
| boolean abort = false; | ||
| if (!trailingHeaders.isEmpty()) { | ||
| @@ -240,6 +231,37 @@ private void handleHttp2TrailingHeadersFrame(Http2HeadersFrame headersFrame, Cha | ||
| } | ||
| } | ||
| static HttpHeaders copyHttp2Headers(Http2Headers h2Headers) { | ||
| return copyHttp2Headers(h2Headers, false); | ||
| } | ||
| static HttpHeaders copyHttp2Trailers(Http2Headers h2Headers) { | ||
| return copyHttp2Headers(h2Headers, true); | ||
| } | ||
| private static HttpHeaders copyHttp2Headers(Http2Headers h2Headers, boolean trailers) { | ||
| // HTTP/2 validation does not enforce HTTP token syntax or header values, so validate both while converting. | ||
hyperxpro marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| HttpHeaders headers = HEADERS_FACTORY.newHeaders(); | ||
| for (Map.Entry<CharSequence, CharSequence> entry : h2Headers) { | ||
| CharSequence name = entry.getKey(); | ||
| if (name.length() == 0 || name.charAt(0) == ':') { | ||
| continue; | ||
| } | ||
| // The restriction is on the sender; drop prohibited trailers instead of failing after delivering the body. | ||
| if (trailers && !isPermittedTrailingHeader(name)) { | ||
| continue; | ||
| } | ||
| headers.add(name, entry.getValue()); | ||
| } | ||
| return headers; | ||
| } | ||
| private static boolean isPermittedTrailingHeader(CharSequence name) { | ||
| return !HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(name) | ||
| && !HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(name) | ||
| && !HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(name); | ||
| } | ||
| /** | ||
| * Processes an HTTP/2 RST_STREAM frame, which indicates the server aborted the stream. | ||
| */ | ||
95 changes: 95 additions & 0 deletions
95 client/src/test/java/org/asynchttpclient/netty/handler/Http2HandlerTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright (c) 2026 AsyncHttpClient Project. All rights reserved. | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * http://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 org.asynchttpclient.netty.handler; | ||
| import io.netty.handler.codec.http.HttpHeaders; | ||
| import io.netty.handler.codec.http2.DefaultHttp2Headers; | ||
| import io.netty.handler.codec.http2.Http2Headers; | ||
| import org.junit.jupiter.api.Test; | ||
| import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; | ||
| import static io.netty.handler.codec.http.HttpHeaderNames.TRAILER; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| class Http2HandlerTest { | ||
| @Test | ||
| void copiesRegularHeadersAndSkipsPseudoHeaders() { | ||
hyperxpro marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Http2Headers source = new DefaultHttp2Headers(false) | ||
| .status("200") | ||
| .add(CONTENT_LENGTH, "5") | ||
| .add("x-test", "value"); | ||
| HttpHeaders copy = Http2Handler.copyHttp2Headers(source); | ||
| assertEquals("5", copy.get(CONTENT_LENGTH)); | ||
| assertEquals("value", copy.get("x-test")); | ||
| assertFalse(copy.contains(":status")); | ||
| } | ||
| @Test | ||
| void rejectsInvalidHeaderNames() { | ||
hyperxpro marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Http2Headers crlf = new DefaultHttp2Headers(false) | ||
| .add("invalid\r\nname", "value"); | ||
| Http2Headers space = new DefaultHttp2Headers(false) | ||
| .add("invalid name", "value"); | ||
| Http2Headers parenthesis = new DefaultHttp2Headers(false) | ||
| .add("invalid(name", "value"); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Headers(crlf)); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Trailers(crlf)); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Headers(space)); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Trailers(space)); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Headers(parenthesis)); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Trailers(parenthesis)); | ||
| } | ||
| @Test | ||
| void skipsEmptyHeaderNames() { | ||
| Http2Headers source = new DefaultHttp2Headers(false) | ||
| .add("", "value"); | ||
| assertTrue(Http2Handler.copyHttp2Headers(source).isEmpty()); | ||
| } | ||
| @Test | ||
| void rejectsInvalidHeaderValues() { | ||
| Http2Headers source = new DefaultHttp2Headers(false) | ||
| .add("x-test", "value\r\ninjected"); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Headers(source)); | ||
| assertThrows(IllegalArgumentException.class, () -> Http2Handler.copyHttp2Trailers(source)); | ||
| } | ||
| @Test | ||
| void dropsProhibitedTrailingHeaders() { | ||
| Http2Headers source = new DefaultHttp2Headers(false) | ||
| .status("200") | ||
| .add("grpc-status", "0") | ||
| .add(CONTENT_LENGTH, "5") | ||
| .add(TRAILER, "x-checksum"); | ||
| HttpHeaders copy = Http2Handler.copyHttp2Trailers(source); | ||
| assertEquals("0", copy.get("grpc-status")); | ||
| assertFalse(copy.contains(CONTENT_LENGTH)); | ||
| assertFalse(copy.contains(TRAILER)); | ||
| assertFalse(copy.contains(":status")); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.