You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2026/03/04 09:34:49 UTC

(tomcat) branch 11.0.x updated: Use HpackException to report HPACK issues

This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/11.0.x by this push:
     new 23b24edb8d Use HpackException to report HPACK issues
23b24edb8d is described below

commit 23b24edb8d6aababc27ef45d6ebab5ea6333a709
Author: remm <re...@apache.org>
AuthorDate: Wed Mar 4 10:30:41 2026 +0100

    Use HpackException to report HPACK issues
    
    The end result is the same though and the connection ends up being
    closed.
---
 java/org/apache/coyote/http2/HpackDecoder.java |  5 +++++
 java/org/apache/coyote/http2/Stream.java       | 26 +++++++++++++-------------
 webapps/docs/changelog.xml                     |  4 ++++
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/java/org/apache/coyote/http2/HpackDecoder.java b/java/org/apache/coyote/http2/HpackDecoder.java
index b25554582b..528f71481a 100644
--- a/java/org/apache/coyote/http2/HpackDecoder.java
+++ b/java/org/apache/coyote/http2/HpackDecoder.java
@@ -262,6 +262,11 @@ public class HpackDecoder {
         if (index <= Hpack.STATIC_TABLE_LENGTH) {
             addStaticTableEntry(index);
         } else {
+            // index is 1 based
+            if (index > Hpack.STATIC_TABLE_LENGTH + filledTableSlots) {
+                throw new HpackException(sm.getString("hpackdecoder.headerTableIndexInvalid", Integer.valueOf(index),
+                        Integer.valueOf(Hpack.STATIC_TABLE_LENGTH), Integer.valueOf(filledTableSlots)));
+            }
             int adjustedIndex = getRealIndex(index - Hpack.STATIC_TABLE_LENGTH);
             if (log.isTraceEnabled()) {
                 log.trace(sm.getString("hpackdecoder.useDynamic", Integer.valueOf(adjustedIndex)));
diff --git a/java/org/apache/coyote/http2/Stream.java b/java/org/apache/coyote/http2/Stream.java
index d50d2135de..1edaadc2dd 100644
--- a/java/org/apache/coyote/http2/Stream.java
+++ b/java/org/apache/coyote/http2/Stream.java
@@ -494,17 +494,17 @@ class Stream extends AbstractNonZeroStream implements HeaderEmitter {
         int i;
         try {
             i = Host.parse(value);
+            if (i > -1) {
+                coyoteRequest.serverName().setString(value.substring(0, i));
+                coyoteRequest.setServerPort(Integer.parseInt(value.substring(i + 1)));
+            } else {
+                coyoteRequest.serverName().setString(value);
+            }
         } catch (IllegalArgumentException iae) {
             // Host value invalid
             throw new HpackException(sm.getString("stream.header.invalid", getConnectionId(), getIdAsString(),
                     host ? "host" : ":authority", value));
         }
-        if (i > -1) {
-            coyoteRequest.serverName().setString(value.substring(0, i));
-            coyoteRequest.setServerPort(Integer.parseInt(value.substring(i + 1)));
-        } else {
-            coyoteRequest.serverName().setString(value);
-        }
         // Match host name with SNI if required
         if (!handler.getProtocol().getHttp11Protocol().checkSni(handler.getSniHostName(), coyoteRequest.serverName().getString())) {
             throw new HpackException(sm.getString("stream.host.sni", getConnectionId(), getIdAsString(), value,
@@ -517,18 +517,18 @@ class Stream extends AbstractNonZeroStream implements HeaderEmitter {
         int i;
         try {
             i = Host.parse(value);
+            if (i == -1 && (!value.equals(coyoteRequest.serverName().getString()) || coyoteRequest.getServerPort() != -1) ||
+                    i > -1 && ((!value.substring(0, i).equals(coyoteRequest.serverName().getString()) ||
+                            Integer.parseInt(value.substring(i + 1)) != coyoteRequest.getServerPort()))) {
+                // Host value inconsistent
+                throw new HpackException(sm.getString("stream.host.inconsistent", getConnectionId(), getIdAsString(), value,
+                        coyoteRequest.serverName().getString(), Integer.toString(coyoteRequest.getServerPort())));
+            }
         } catch (IllegalArgumentException iae) {
             // Host value invalid
             throw new HpackException(
                     sm.getString("stream.header.invalid", getConnectionId(), getIdAsString(), "host", value));
         }
-        if (i == -1 && (!value.equals(coyoteRequest.serverName().getString()) || coyoteRequest.getServerPort() != -1) ||
-                i > -1 && ((!value.substring(0, i).equals(coyoteRequest.serverName().getString()) ||
-                        Integer.parseInt(value.substring(i + 1)) != coyoteRequest.getServerPort()))) {
-            // Host value inconsistent
-            throw new HpackException(sm.getString("stream.host.inconsistent", getConnectionId(), getIdAsString(), value,
-                    coyoteRequest.serverName().getString(), Integer.toString(coyoteRequest.getServerPort())));
-        }
 
     }
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 16bc068507..6c6dce01f9 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -167,6 +167,10 @@
         if getting the bytes from the certificate somehow fails.
         Pull request <pr>951</pr> provided by Chenjp. (remm)
       </fix>
+      <fix>
+        Improve HPACK exception use, making sure <code>HpackException</code>
+        is thrown instead of unexpected types. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org