From: Christopher Faulet Date: Tue, 24 Nov 2026 10:19:45 +0000 (+0100) Subject: BUG/MEDIUM: http-htx: normalize Host header carrying the scheme's default port X-Git-Url: https://git.kaiwu.me/http/%22../static/%7Bsubstring-before(@doc,%20'.xml')%7D.html?a=commitdiff_plain;h=548f1c4daef843cb9abd3378c4ec87808972787d;p=haproxy.git BUG/MEDIUM: http-htx: normalize Host header carrying the scheme's default port 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. --- diff --git a/reg-tests/http-messaging/h1_host_normalization.vtc b/reg-tests/http-messaging/h1_host_normalization.vtc index 70225998c..856e52c95 100644 --- a/reg-tests/http-messaging/h1_host_normalization.vtc +++ b/reg-tests/http-messaging/h1_host_normalization.vtc @@ -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" \ diff --git a/src/http_htx.c b/src/http_htx.c index e1170ca5e..2c6447527 100644 --- a/src/http_htx.c +++ b/src/http_htx.c @@ -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();