From 60e3ff4edebc11161d220325e25e512c09c95ac5 Mon Sep 17 00:00:00 2001 From: Naveed Khan Date: Fri, 4 Dec 2026 16:59:24 +0530 Subject: [PATCH] validate request pseudo-header values in DefaultH2RequestConverter --- .../http2/impl/DefaultH2RequestConverter.java | 6 ++ .../http2/impl/FieldValidationSupport.java | 79 +++++++++++++++++++ .../impl/TestDefaultH2RequestConverter.java | 29 +++++++ .../impl/TestFieldValidationSupport.java | 42 ++++++++++ 4 files changed, 156 insertions(+) diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java index 20f6dde522..8bcc69640a 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java @@ -82,12 +82,18 @@ public HttpRequest convert(final List
headers) throws HttpException { if (method != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } + if (!FieldValidationSupport.isTokenValid(value)) { + throw new ProtocolException("':method' pseudo-header field must be a token"); + } method = value; break; case H2PseudoRequestHeaders.SCHEME: if (scheme != null) { throw new ProtocolException("Multiple '%s' request headers are illegal", name); } + if (!FieldValidationSupport.isSchemeValid(value)) { + throw new ProtocolException("':scheme' pseudo-header field must be a valid URI scheme"); + } scheme = value; break; case H2PseudoRequestHeaders.PATH: diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/FieldValidationSupport.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/FieldValidationSupport.java index e67b6892d2..9e351be94d 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/FieldValidationSupport.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/FieldValidationSupport.java @@ -27,11 +27,58 @@ package org.apache.hc.core5.http2.impl; +import java.util.BitSet; + import org.apache.hc.core5.annotation.Internal; @Internal public class FieldValidationSupport { + private static final BitSet TOKEN_CHARS = new BitSet(128); + private static final BitSet SCHEME_CHARS = new BitSet(128); + + static { + // tchar as per RFC 9110 section 5.6.2 + for (int i = 'a'; i <= 'z'; i++) { + TOKEN_CHARS.set(i); + } + for (int i = 'A'; i <= 'Z'; i++) { + TOKEN_CHARS.set(i); + } + for (int i = '0'; i <= '9'; i++) { + TOKEN_CHARS.set(i); + } + TOKEN_CHARS.set('!'); + TOKEN_CHARS.set('#'); + TOKEN_CHARS.set('$'); + TOKEN_CHARS.set('%'); + TOKEN_CHARS.set('&'); + TOKEN_CHARS.set('\''); + TOKEN_CHARS.set('*'); + TOKEN_CHARS.set('+'); + TOKEN_CHARS.set('-'); + TOKEN_CHARS.set('.'); + TOKEN_CHARS.set('^'); + TOKEN_CHARS.set('_'); + TOKEN_CHARS.set('`'); + TOKEN_CHARS.set('|'); + TOKEN_CHARS.set('~'); + + // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) as per RFC 3986 section 3.1 + for (int i = 'a'; i <= 'z'; i++) { + SCHEME_CHARS.set(i); + } + for (int i = 'A'; i <= 'Z'; i++) { + SCHEME_CHARS.set(i); + } + for (int i = '0'; i <= '9'; i++) { + SCHEME_CHARS.set(i); + } + SCHEME_CHARS.set('+'); + SCHEME_CHARS.set('-'); + SCHEME_CHARS.set('.'); + } + public static boolean isWhitespace(final char ch) { return ch == 0x20 || ch == 0x09; } @@ -89,4 +136,36 @@ public static boolean isValueValid(final CharSequence s) { return true; } + public static boolean isTokenCharValid(final char ch) { + return TOKEN_CHARS.get(ch); + } + + public static boolean isTokenValid(final CharSequence s) { + if (s.length() == 0) { + return false; + } + for (int i = 0; i < s.length(); i++) { + if (!isTokenCharValid(s.charAt(i))) { + return false; + } + } + return true; + } + + public static boolean isSchemeValid(final CharSequence s) { + if (s.length() == 0 || !isAlpha(s.charAt(0))) { + return false; + } + for (int i = 1; i < s.length(); i++) { + if (!SCHEME_CHARS.get(s.charAt(i))) { + return false; + } + } + return true; + } + + private static boolean isAlpha(final char ch) { + return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'; + } + } diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestDefaultH2RequestConverter.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestDefaultH2RequestConverter.java index 97075889a2..501ff94141 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestDefaultH2RequestConverter.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestDefaultH2RequestConverter.java @@ -613,6 +613,35 @@ void testConvertFromFieldsMultipleAuthority() { Assertions.assertThat(ex.getMessage().contains("Multiple ':authority' request headers are illegal")).withFailMessage(ex::getMessage).isTrue(); } + @Test + void testConvertFromFieldsMethodNotToken() { + final List
headers = Arrays.asList( + new BasicHeader(":method", "GET /admin HTTP/1.1"), + new BasicHeader(":scheme", "https"), + new BasicHeader(":authority", "www.example.com"), + new BasicHeader(":path", "/public")); + + final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); + Assertions.assertThatThrownBy(() -> + converter.convert(headers)) + .isInstanceOf(ProtocolException.class) + .hasMessage("':method' pseudo-header field must be a token"); + } + + @Test + void testConvertFromFieldsSchemeInvalid() { + final List
headers = Arrays.asList( + new BasicHeader(":method", "GET"), + new BasicHeader(":scheme", "https x"), + new BasicHeader(":authority", "www.example.com"), + new BasicHeader(":path", "/")); + + final DefaultH2RequestConverter converter = new DefaultH2RequestConverter(); + Assertions.assertThatThrownBy(() -> + converter.convert(headers)) + .isInstanceOf(ProtocolException.class) + .hasMessage("':scheme' pseudo-header field must be a valid URI scheme"); + } } diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestFieldValidationSupport.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestFieldValidationSupport.java index d8bfcff6bd..d71d294551 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestFieldValidationSupport.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/TestFieldValidationSupport.java @@ -95,5 +95,47 @@ void testValueValid() throws Exception { Assertions.assertThat(FieldValidationSupport.isValueValid("\rBlah")).isFalse(); } + @Test + void testTokenCharValid() throws Exception { + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('a')).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('Z')).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('0')).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('-')).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('!')).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('~')).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid(' ')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('\t')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('/')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid(':')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('"')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('(')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('@')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('\\')).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid((char) 0x7f)).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenCharValid('รค')).isFalse(); + } + + @Test + void testTokenValid() throws Exception { + Assertions.assertThat(FieldValidationSupport.isTokenValid("GET")).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenValid("M-SEARCH")).isTrue(); + Assertions.assertThat(FieldValidationSupport.isTokenValid("")).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenValid("GET /admin HTTP/1.1")).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenValid("GET\t/admin")).isFalse(); + Assertions.assertThat(FieldValidationSupport.isTokenValid("G\"E(T)")).isFalse(); + } + + @Test + void testSchemeValid() throws Exception { + Assertions.assertThat(FieldValidationSupport.isSchemeValid("http")).isTrue(); + Assertions.assertThat(FieldValidationSupport.isSchemeValid("HTTPS")).isTrue(); + Assertions.assertThat(FieldValidationSupport.isSchemeValid("svn+ssh")).isTrue(); + Assertions.assertThat(FieldValidationSupport.isSchemeValid("x-1.0")).isTrue(); + Assertions.assertThat(FieldValidationSupport.isSchemeValid("")).isFalse(); + Assertions.assertThat(FieldValidationSupport.isSchemeValid("1http")).isFalse(); + Assertions.assertThat(FieldValidationSupport.isSchemeValid("https x")).isFalse(); + Assertions.assertThat(FieldValidationSupport.isSchemeValid("http://")).isFalse(); + } + }