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 — HTTP/2: Lack of Host Header Deduplication in HTTP/2→HTTP/1.x Translat… · netty/netty@0ff87c6

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,12 +833,16 @@ private static final class Http2ToHttpHeaderTranslator {
833833
void translateHeaders(Iterable<Entry<CharSequence, CharSequence>> inputHeaders) throws Http2Exception {
834834
// lazily created as needed
835835
StringBuilder cookies = null;
836+
boolean hostHeaderFound = false;
836837

837838
for (Entry<CharSequence, CharSequence> entry : inputHeaders) {
838839
final CharSequence name = entry.getKey();
839840
final CharSequence value = entry.getValue();
840841
AsciiString translatedName = translations.get(name);
841842
if (translatedName != null) {
843+
if (translatedName.contentEqualsIgnoreCase(HttpHeaderNames.HOST)) {
844+
hostHeaderFound = true;
845+
}
842846
output.add(translatedName, AsciiString.of(value));
843847
} else if (!Http2Headers.PseudoHeaderName.isPseudoHeader(name)) {
844848
// https://tools.ietf.org/html/rfc7540#section-8.1.2.3
@@ -856,6 +860,20 @@ void translateHeaders(Iterable<Entry<CharSequence, CharSequence>> inputHeaders)
856860
cookies.append("; ");
857861
}
858862
cookies.append(value);
863+
} else if (contentEqualsIgnoreCase(HttpHeaderNames.HOST, name)) {
864+
// https://www.rfc-editor.org/rfc/rfc9113#section-8.3.1 requires that intermediaries
865+
// translating to HTTP/1.x treat a literal 'host' header that conflicts with ':authority'
866+
// as malformed, and RFC 9110 section 7.2 requires 'Host' be sent as a single field-value.
867+
// Reject the request rather than emitting an HTTP/1.x message with duplicate Host headers.
868+
if (hostHeaderFound) {
869+
if (!contentEqualsIgnoreCase(output.get(HttpHeaderNames.HOST), value)) {
870+
throw streamError(streamId, PROTOCOL_ERROR,
871+
"Conflicting ':authority' and 'host' headers found");
872+
}
873+
} else {
874+
hostHeaderFound = true;
875+
output.add(name, value);
876+
}
859877
} else {
860878
output.add(name, value);
861879
}
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/HttpConversionUtilTest.java
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,38 @@ public void addHttp2ToHttpHeadersCombinesCookies() throws Http2Exception {
381381
assertEquals("foo=bar; bax=baz", outHeaders.get(COOKIE.toString()));
382382
}
383383

384+
@Test
385+
public void addHttp2ToHttpHeadersDeduplicatesMatchingAuthorityAndHost() throws Http2Exception {
386+
Http2Headers inHeaders = new DefaultHttp2Headers();
387+
inHeaders.authority("example.com");
388+
inHeaders.add(HOST, "example.com");
389+
390+
HttpHeaders outHeaders = new DefaultHttpHeaders();
391+
392+
HttpConversionUtil.addHttp2ToHttpHeaders(5, inHeaders, outHeaders, HttpVersion.HTTP_1_1, false, true);
393+
assertEquals(1, outHeaders.getAll(HOST).size());
394+
assertEquals("example.com", outHeaders.get(HOST));
395+
}
396+
397+
@Test
398+
public void addHttp2ToHttpHeadersRejectsConflictingAuthorityAndHost() {
399+
final Http2Headers inHeaders = new DefaultHttp2Headers();
400+
inHeaders.authority("public.example.com");
401+
inHeaders.add(HOST, "internal-admin.local");
402+
403+
final HttpHeaders outHeaders = new DefaultHttpHeaders();
404+
405+
Http2Exception exception = assertThrows(Http2Exception.class, new Executable() {
406+
@Override
407+
public void execute() throws Http2Exception {
408+
HttpConversionUtil.addHttp2ToHttpHeaders(5, inHeaders, outHeaders, HttpVersion.HTTP_1_1, false, true);
409+
}
410+
});
411+
assertEquals(Http2Error.PROTOCOL_ERROR, exception.error());
412+
// No Host header should have leaked through in an inconsistent state.
413+
assertFalse(outHeaders.contains(HOST) && outHeaders.getAll(HOST).size() > 1);
414+
}
415+
384416
@Test
385417
public void connectionSpecificHeadersShouldBeRemoved() {
386418
HttpHeaders inHeaders = new DefaultHttpHeaders();

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.