]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: http-htx: normalize Host header carrying the scheme's default port
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 24 Nov 2026 10:19:45 +0000 (11:19 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 26 Nov 2026 06:32:21 +0000 (07:32 +0100)
An HTTP/1 absolute-form request-target whose authority carries no port may
be sent with a Host header value carrying the scheme's default port (or an
empty one). h1_validate_mismatch_authority() deliberately accepts both
spellings as equivalent, but http_scheme_based_normalize() only removed the
port when it was found in the URI authority, so the raw ":443" remained in
the Host header value.

As a result the ACLs, the routing rules and the cache key were evaluated on
different values and this could be confusing.

Now the Host header value is also normalized when it carries the scheme's
default port and matches the authority, so the analyser, the rules, the
cache and the forwarded request all see the same canonical host value.

Thanks to Christos Papakonstantinou for the report.

This patch could be backported to stable versions. But it could be good to
wait for a period of observation first.

reg-tests/http-messaging/h1_host_normalization.vtc
src/http_htx.c

index 70225998ca1862929c9e63a06bafb7922f3fe7b8..856e52c95b544d461f6d504494ec85a4894a589e 100644 (file)
@@ -125,12 +125,12 @@ syslog S1 -level info {
 
     # C22
     recv
-    expect ~ "^.* uri: GET http://hostname/c22 HTTP/1.1; host: {hostname:80}$"
+    expect ~ "^.* uri: GET http://hostname/c22 HTTP/1.1; host: {hostname}$"
     barrier b1 sync
 
     # C23
     recv
-    expect ~ "^.* uri: GET https://hostname/c23 HTTP/1.1; host: {hostname:443}$"
+    expect ~ "^.* uri: GET https://hostname/c23 HTTP/1.1; host: {hostname}$"
     barrier b1 sync
 
     # C24
@@ -145,12 +145,12 @@ syslog S1 -level info {
 
     # C26
     recv
-    expect ~ "^.* uri: GET http://hostname/c26 HTTP/1.1; host: {hostname:}$"
+    expect ~ "^.* uri: GET http://hostname/c26 HTTP/1.1; host: {hostname}$"
     barrier b1 sync
 
     # C27
     recv
-    expect ~ "^.* uri: GET https://hostname/c27 HTTP/1.1; host: {hostname:}$"
+    expect ~ "^.* uri: GET https://hostname/c27 HTTP/1.1; host: {hostname}$"
     barrier b1 sync
 
     # C28
@@ -620,7 +620,7 @@ client c21 -connect ${h1_fe_sock} {
 barrier b1 sync
 
 
-# http scheme, no port for the authority but default port for host value => no normalization
+# http scheme, no port for the authority but default port for host value => should be normalized
 client c22 -connect ${h1_fe_sock} {
     txreq \
       -req "GET" \
@@ -634,7 +634,7 @@ client c22 -connect ${h1_fe_sock} {
 # Wait matching on log message
 barrier b1 sync
 
-# https scheme, no port for the authority but default port for host value => no normalization
+# https scheme, no port for the authority but default port for host value => should be normalized
 client c23 -connect ${h1_fe_sock} {
     txreq \
       -req "GET" \
@@ -677,7 +677,7 @@ client c25 -connect ${h1_fe_sock} {
 # Wait matching on log message
 barrier b1 sync
 
-# http scheme, no port for the authority and empty port for host value => no normalization
+# http scheme, no port for the authority and empty port for host value => should be normalized
 client c26 -connect ${h1_fe_sock} {
     txreq \
       -req "GET" \
@@ -691,7 +691,7 @@ client c26 -connect ${h1_fe_sock} {
 # Wait matching on log message
 barrier b1 sync
 
-# https scheme, no port for the authority and empty port for host value => no normalization
+# https scheme, no port for the authority and empty port for host value => should be normalized
 client c27 -connect ${h1_fe_sock} {
     txreq \
       -req "GET" \
index e1170ca5e61a967ad29d6a942baf65293d65dabb..2c644752753613e3d1aee46c14ee192963b91369 100644 (file)
@@ -1872,9 +1872,12 @@ struct http_reply *http_parse_http_reply(const char **args, int *orig_arg, struc
  *
  * The normalization is processed on the target-uri at the condition that it is
  * in absolute-form. In the case where the target-uri was normalized, every
- * host headers values found are also replaced by the normalized hostname. This
- * assumes that the target-uri and host headers were properly identify as
- * similar before calling this function.
+ * host headers values found are also replaced by the normalized hostname. In
+ * addition, a host header value carrying the scheme's default port (or an
+ * empty one) is normalized as well even when the authority does not carry any
+ * port, so that the analyser, the ACLs, the cache and the forwarded request
+ * all see the same host value. This assumes that the target-uri and host
+ * headers were properly identify as similar before calling this function.
  */
 int http_scheme_based_normalize(struct htx *htx)
 {
@@ -1913,6 +1916,23 @@ int http_scheme_based_normalize(struct htx *htx)
                normalize = 1;
        }
 
+       /* Check for host header normalization if not performe on the authority */
+       if (!normalize) {
+               ctx.blk = NULL;
+               while (http_find_header(htx, ist("host"), &ctx, 1)) {
+                       struct ist hval = ctx.value;
+                       struct ist hport = http_get_host_port(hval);
+
+                       if (!isttest(hport) || !http_is_default_port(scheme, hport))
+                               continue;
+                       hval = isttrim(hval, istlen(hval) - istlen(hport) - 1);
+                       if (!isteqi(hval, host))
+                               continue;
+                       normalize = 1;
+                       break;
+               }
+       }
+
        if (normalize) {
                /* reconstruct the uri with removal of the port */
                struct buffer *temp = alloc_trash_chunk();