From: Willy Tarreau Date: Wed, 25 Nov 2026 18:07:51 +0000 (+0000) Subject: REGTESTS: http-messaging: test reject-unexpected-request-upgrade X-Git-Tag: v3.5-dev14~3 X-Git-Url: https://git.kaiwu.me/http/%22../static/gitweb.js?a=commitdiff_plain;h=ffb76d3535c40b6b80c9ca97c3a6a84e576523ba;p=haproxy.git REGTESTS: http-messaging: test reject-unexpected-request-upgrade 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. --- diff --git a/reg-tests/http-messaging/http_hardening_h1.vtc b/reg-tests/http-messaging/http_hardening_h1.vtc index 90a550884..00373d64f 100644 --- a/reg-tests/http-messaging/http_hardening_h1.vtc +++ b/reg-tests/http-messaging/http_hardening_h1.vtc @@ -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 == + expect resp.http.x-hdrs == +} -run + # # Responses # diff --git a/reg-tests/http-messaging/http_hardening_h2.vtc b/reg-tests/http-messaging/http_hardening_h2.vtc index e11129a51..6aa9beca1 100644 --- a/reg-tests/http-messaging/http_hardening_h2.vtc +++ b/reg-tests/http-messaging/http_hardening_h2.vtc @@ -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 #