From: Christopher Faulet Date: Tue, 24 Nov 2026 14:21:04 +0000 (+0100) Subject: MEDIUM: http: reject percent-encoded chars in the authority and Host X-Git-Url: https://git.kaiwu.me/http/%22../static/gitweb.js?a=commitdiff_plain;h=ab7c4da93839af5e09f58d1519ade7615049d7f2;p=haproxy.git MEDIUM: http: reject percent-encoded chars in the authority and Host While RFC3986 permits percent-encoded characters in the authority for a general usage, it is safer to reject them in HTTP because it could be used to abuse ACLs based on the host header value. In fact, because it could be used as a vector of attacks, it seems commonly rejected/not-supported by servers. So it is safer to do so. The http_authority_has_forbidden_char() function already rejects a list of ambiguous characters in the authority and the Host header value for H1, H2 and H3, so percent ('%') is simply added to this list, covering all three protocol versions at once. This patch must not be backported. --- diff --git a/include/haproxy/http.h b/include/haproxy/http.h index a4c7baa95..ae0b58aae 100644 --- a/include/haproxy/http.h +++ b/include/haproxy/http.h @@ -324,6 +324,10 @@ static inline int http_path_has_forbidden_char(const struct ist ist, const char * deserve special case, that is better handled in the slow path. The function * returns 0 if no forbidden char is presnet, non-zero otherwise. * + * The percent ('%') is also rejected. While it is permitted in reg-names by + * RFC3986, it is safer to reject it in HTTP because it could be used to abuse + * ACLs based on the host header value. + * * There is a special case for the comma (','). While it is allowed, we reject * it because the authority is higly linked with the host header. The comma is * also the header value separator. So it is highly ambiguous to use it for the @@ -348,7 +352,7 @@ static inline int http_authority_has_forbidden_char(const struct ist ist) if (unlikely(c < 0x21 || c > 0x7e || c == ',' || /* Special case: forbidden because it is ambiguous for the host header value */ - c == '#' || c == '/' || c == '?' || c == '@' || + c == '#' || c == '/' || c == '%' || c == '?' || c == '@' || c == '[' || c == '\\' || c == ']')) { /* all of them must be rejected, except '[' which may * only appear at the beginning, and ']' which may diff --git a/reg-tests/http-messaging/authority_pct_encoding.vtc b/reg-tests/http-messaging/authority_pct_encoding.vtc new file mode 100644 index 000000000..e6068b0e7 --- /dev/null +++ b/reg-tests/http-messaging/authority_pct_encoding.vtc @@ -0,0 +1,85 @@ +varnishtest "Percent-encoding is rejected in the authority and the Host header" + +# RFC3986 permits percent-encoded characters in the authority, but they must +# not be used over HTTP: an escaped character may fool a URI parser on a +# backend server (e.g. an escaped "@" or "/" changing how the request is +# routed) and the authority is reassembled into an absolute URI and the Host +# header. This is checked on all three HTTP versions. + +feature ignore_unknown_macro + +# H2 frontend replying itself: the point is to check the requests are +# rejected or accepted, not what is forwarded. +haproxy h1 -conf { + global + .if feature(THREAD) + thread-groups 1 + .endif + + defaults + mode http + timeout connect "${HAPROXY_TEST_TIMEOUT-5s}" + timeout client "${HAPROXY_TEST_TIMEOUT-5s}" + timeout server "${HAPROXY_TEST_TIMEOUT-5s}" + + frontend fe_h1 + bind "fd@${fe_h1}" + http-request return status 200 + + frontend fe_h2 + bind "fd@${fe_h2}" proto h2 + http-request return status 200 +} -start + +# H1: percent-encoded char in the authority part of an absolute URI. +client c_uri_auth -connect ${h1_fe_h1_sock} { + txreq -req GET -url "http://ex%41mple.com/" -hdr "host: example.com" + rxresp + expect resp.status == 400 +} -run + +# H1: percent-encoded char in the Host header value. +client c_host_hdr -connect ${h1_fe_h1_sock} { + txreq -req GET -url / -hdr "host: ex%41mple.com" + rxresp + expect resp.status == 400 +} -run + +# H1: no percent char, the request must pass. +client c_valid -connect ${h1_fe_h1_sock} { + txreq -req GET -url "http://example.com/" -hdr "host: example.com" + rxresp + expect resp.status == 200 +} -run + +# H2: percent-encoded char in :authority +client c_h2_authority -connect ${h1_fe_h2_sock} { + txpri + stream 0 { + txsettings + rxsettings + txsettings -ack + } -run + + stream 1 { + txreq -req GET -url / -scheme http -hdr ":authority" "ex%41mple.com" + rxrst + expect rst.err == PROTOCOL_ERROR + } -run +} -run + +# H2: unencoded authority and Host, the request must pass. +client c_h2_valid -connect ${h1_fe_h2_sock} { + txpri + stream 0 { + txsettings + rxsettings + txsettings -ack + } -run + + stream 1 { + txreq -req GET -url / -scheme http -hdr ":authority" "example.com" -hdr "host" "example.com" + rxresp + expect resp.status == 200 + } -run +} -run