]> git.kaiwu.me - haproxy.git/commitdiff
REGTESTS: http-messaging: test reject-unexpected-request-upgrade
authorWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 18:07:51 +0000 (18:07 +0000)
committerWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 18:31:09 +0000 (19:31 +0100)
This adds the "reject-unexpected-request-upgrade" check to the HTTP/1
and H2 tests, in its own profile on its own bind line, and an upgrade
to an unknown protocol to the reference connection which has everything
off. Only "websocket" and "webtransport" are kept in the Upgrade header,
which is removed along with "connection: upgrade" once empty, and the
request is not rejected. The frontend now also reports the Upgrade
header in "x-upg". For H2, this is done via extended CONNECT, which is
accepted for "websocket" and "webtransport" and rejected for other
protocols. For websocket, the server is a frontend of the same instance
which computes the Sec-WebSocket-Accept header that haproxy verifies,
as done in websocket.vtc.

reg-tests/http-messaging/http_hardening_h1.vtc
reg-tests/http-messaging/http_hardening_h2.vtc

index 90a5508841b7deb0a974151a081f78a32f3fce45..00373d64fb9434b96c755d03ca55e01cc5514154 100644 (file)
@@ -6,9 +6,9 @@ varnishtest "HTTP hardening checks on HTTP/1 headers"
 # reference showing that each offending message is accepted by default.
 #
 # On the request side there is no server: the frontend replies itself and
-# reports the method, the header names it saw and the hex-encoded value of
-# header "foo", which reflect what remains after the checks ran in the H1
-# parser.
+# reports the method, the header names it saw, the hex-encoded value of
+# header "foo" and the full value of header "upgrade", which reflect what
+# remains after the checks ran in the H1 parser.
 #
 # On the response side, each profile's server first sends a valid response,
 # then the offending ones, which must turn into 502 when the check is enabled.
@@ -156,6 +156,9 @@ haproxy h1 -conf {
     http-hardening req-pld
        reject-unexpected-request-payload on
 
+    http-hardening req-upg
+       reject-unexpected-request-upgrade on
+
     defaults
        mode http
        http-reuse never
@@ -178,7 +181,8 @@ haproxy h1 -conf {
        bind "fd@${req_cl0}"      http-hardening cl0
        bind "fd@${req_adv_meth}" http-hardening adv-meth
        bind "fd@${req_pld}"      http-hardening req-pld
-       http-request return status 200 hdr x-meth "%[method]" hdr x-hdrs "%[req.hdr_names]" hdr x-foo "%[req.hdr(foo),hex]"
+       bind "fd@${req_upg}"      http-hardening req-upg
+       http-request return status 200 hdr x-meth "%[method]" hdr x-hdrs "%[req.hdr_names]" hdr x-foo "%[req.hdr(foo),hex]" hdr x-upg "%[req.fhdr(upgrade)]"
 
     # response side: one listener per server profile
     listen res_off
@@ -276,6 +280,11 @@ client c_req_off -connect ${h1_req_off_sock} {
        rxresp
        expect resp.status == 200
 
+       txreq -nouseragent -hdr "connection: upgrade" -hdr "upgrade: foo, websocket" -hdr "sec-websocket-key: dGhlIHNhbXBsZSBub25jZQ=="
+       rxresp
+       expect resp.status == 200
+       expect resp.http.x-upg == "foo, websocket"
+
        # T-E + C-L forces a close
        txreq -nouseragent -req POST -nolen -hdr "content-length: 2" -hdr "transfer-encoding: chunked" -body "2\r\nok\r\n0\r\n\r\n"
        rxresp
@@ -605,6 +614,32 @@ client c_req_pld7 -connect ${h1_req_pld_sock} {
        expect_close
 } -run
 
+# only websocket and webtransport remain in the Upgrade header, which is
+# removed once empty, without rejecting the request.
+client c_req_upg -connect ${h1_req_upg_sock} {
+       txreq -nouseragent -nohost -hdr "connection: upgrade" -hdr "upgrade: websocket" -hdr "sec-websocket-key: dGhlIHNhbXBsZSBub25jZQ=="
+       rxresp
+       expect resp.status == 200
+       expect resp.http.x-upg == "websocket"
+
+       txreq -nouseragent -nohost -hdr "connection: upgrade" -hdr "upgrade: WebTransport"
+       rxresp
+       expect resp.status == 200
+       expect resp.http.x-upg == "WebTransport"
+
+       txreq -nouseragent -nohost -hdr "connection: upgrade" -hdr "upgrade: foo, websocket, bar/1" -hdr "sec-websocket-key: dGhlIHNhbXBsZSBub25jZQ=="
+       rxresp
+       expect resp.status == 200
+       expect resp.http.x-upg == "websocket"
+
+       txreq -nouseragent -nohost -hdr "connection: upgrade" -hdr "upgrade: foo, bar/1"
+       rxresp
+       expect resp.status == 200
+       # "connection: upgrade" goes away with the Upgrade header
+       expect resp.http.x-upg == <undef>
+       expect resp.http.x-hdrs == <undef>
+} -run
+
 #
 # Responses
 #
index e11129a51bd778fa492c623ad904d92a468783ea..6aa9beca1e4d57c6ed060f200d25c38553102f40 100644 (file)
@@ -11,7 +11,11 @@ varnishtest "HTTP hardening checks on HTTP/2 headers"
 # and a rejected response causes a 502.
 #
 # On the request side the frontend replies itself and reports the method, the
-# header names it saw and the hex-encoded value of header "foo".
+# header names it saw and the hex-encoded value of header "foo". Extended
+# CONNECT requests (RFC8441) are passed to an HTTP/1 server instead, which
+# accepts the upgrade. For websocket, this server is a frontend of the same
+# instance which computes the Sec-WebSocket-Accept header that haproxy
+# verifies, as in websocket.vtc.
 
 feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(3.5-dev10)'"
 feature ignore_unknown_macro
@@ -226,6 +230,19 @@ server s_cl0 {
        } -run
 } -start
 
+# extended CONNECT turned into an HTTP/1 upgrade, for the accepted protocols
+server s_upg_off {
+       rxreq
+       expect req.http.upgrade == "foo"
+       txresp -status 101 -hdr "connection: upgrade" -hdr "upgrade: foo"
+} -start
+
+server s_upg_ok {
+       rxreq
+       expect req.http.upgrade == "webtransport"
+       txresp -status 101 -hdr "connection: upgrade" -hdr "upgrade: webtransport"
+} -start
+
 haproxy h1 -conf {
     global
     .if feature(THREAD)
@@ -266,6 +283,9 @@ haproxy h1 -conf {
     http-hardening req-pld
        reject-unexpected-request-payload on
 
+    http-hardening req-upg
+       reject-unexpected-request-upgrade on
+
     defaults
        mode http
        http-reuse never
@@ -288,6 +308,31 @@ haproxy h1 -conf {
        bind "fd@${req_pld}" proto h2 http-hardening req-pld
        http-request return status 200 hdr x-meth "%[method]" hdr x-hdrs "%[req.hdr_names]" hdr x-foo "%[req.hdr(foo),hex]"
 
+    # extended CONNECT: one listener per server
+    listen req_upg_off
+       bind "fd@${req_upg_off}" proto h2 http-hardening off
+       server s ${s_upg_off_addr}:${s_upg_off_port} http-hardening off
+
+    listen req_upg_ok
+       bind "fd@${req_upg_ok}" proto h2 http-hardening req-upg
+       server s ${s_upg_ok_addr}:${s_upg_ok_port} http-hardening off
+
+    listen req_upg_ws
+       bind "fd@${req_upg_ws}" proto h2 http-hardening req-upg
+       server s ${h1_ws_srv_addr}:${h1_ws_srv_port} http-hardening off
+
+    # websocket server: returns a valid handshake response
+    frontend ws_srv
+       bind "fd@${ws_srv}" http-hardening off
+       capture request header sec-websocket-key len 128
+       http-request return status 200 hdr connection upgrade hdr upgrade websocket hdr sec-websocket-accept "%[capture.req.hdr(0),concat(258EAFA5-E914-47DA-95CA-C5AB0DC85B11,,),sha1,base64]"
+       http-after-response set-status 101 if { status eq 200 }
+
+    # the server is never reached
+    listen req_upg
+       bind "fd@${req_upg}" proto h2 http-hardening req-upg
+       server s 127.0.0.1:1 http-hardening off
+
     # response side: one listener per server
     listen s_off_us
        bind "fd@${s_off_us}" proto h2 http-hardening off
@@ -420,6 +465,24 @@ client c_req_off -connect ${h1_req_off_sock} {
        } -run
 } -run
 
+client c_req_upg_off -connect ${h1_req_upg_off_sock} {
+       txpri
+       stream 0 {
+               txsettings
+               rxsettings
+               txsettings -ack
+               rxsettings
+               expect settings.ack == true
+       } -run
+
+       stream 1 {
+               txreq -req CONNECT -scheme http -url / -hdr ":authority" "a" -hdr ":protocol" "foo" -nostrend
+               rxwinup
+               rxhdrs
+               expect resp.status == 200
+       } -run
+} -run
+
 # without :authority, Host is the only source of the authority
 client c_req_dup_host -connect ${h1_req_dup_host_sock} {
        txpri
@@ -759,6 +822,60 @@ client c_req_pld -connect ${h1_req_pld_sock} {
        } -run
 } -run
 
+# extended CONNECT is only accepted for websocket and webtransport
+client c_req_upg_ok -connect ${h1_req_upg_ok_sock} {
+       txpri
+       stream 0 {
+               txsettings
+               rxsettings
+               txsettings -ack
+               rxsettings
+               expect settings.ack == true
+       } -run
+
+       stream 1 {
+               txreq -req CONNECT -scheme http -url / -hdr ":authority" "a" -hdr ":protocol" "webtransport" -nostrend
+               rxwinup
+               rxhdrs
+               expect resp.status == 200
+       } -run
+} -run
+
+client c_req_upg_ws -connect ${h1_req_upg_ws_sock} {
+       txpri
+       stream 0 {
+               txsettings
+               rxsettings
+               txsettings -ack
+               rxsettings
+               expect settings.ack == true
+       } -run
+
+       stream 1 {
+               txreq -req CONNECT -scheme http -url / -hdr ":authority" "a" -hdr ":protocol" "websocket" -nostrend
+               rxwinup
+               rxhdrs
+               expect resp.status == 200
+       } -run
+} -run
+
+client c_req_upg -connect ${h1_req_upg_sock} {
+       txpri
+       stream 0 {
+               txsettings
+               rxsettings
+               txsettings -ack
+               rxsettings
+               expect settings.ack == true
+       } -run
+
+       stream 1 {
+               txreq -req CONNECT -scheme http -url / -hdr ":authority" "a" -hdr ":protocol" "foo" -nostrend
+               rxrst
+               expect rst.err == PROTOCOL_ERROR
+       } -run
+} -run
+
 #
 # Responses
 #