diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 41b8078..01ced9a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -63,10 +63,12 @@ jobs: install_vtest: yes use_lua: yes - name: Install apt dependencies. - run: sudo apt-get install -y lua-json + run: sudo apt-get install -y lua-json lua5.4 - uses: actions/checkout@v7 with: submodules: recursive + - name: Run header_match unit tests. + run: lua5.4 test/header_match.lua - name: Install haproxy-lua-http globally. run: | sudo install -m755 -d /usr/local/share/lua/5.3/ diff --git a/auth-request.lua b/auth-request.lua index 1504948..ed370e3 100644 --- a/auth-request.lua +++ b/auth-request.lua @@ -77,10 +77,29 @@ function sanitize_header_for_variable(header) return header:gsub("[^a-zA-Z0-9]", "_") end +-- Headers that must never be copied between connections, per RFC 9110 +-- section 7.6.1. "connection" and "upgrade" are not merely unclean here: +-- copying "connection: upgrade" plus "upgrade: websocket" from the client +-- makes HAProxy's strict H1 parser reject the synthesized auth probe with +-- , because the probe is a plain HEAD that the frontend cannot +-- service as an upgrade request. Suppressing "connection" also lets +-- haproxy-lua-http add its intended "connection: close". +local HOP_BY_HOP_HEADERS = { + ["connection"] = true, + ["content-length"] = true, + ["host"] = true, + ["keep-alive"] = true, + ["proxy-connection"] = true, + ["te"] = true, + ["trailer"] = true, + ["transfer-encoding"] = true, + ["upgrade"] = true, +} + -- header_match checks whether the provided header matches the pattern. -- pattern is a comma-separated list of Lua Patterns. function header_match(header, pattern) - if header == "content-length" or header == "host" or pattern == "-" then + if HOP_BY_HOP_HEADERS[header] or pattern == "-" then return false end for p in pattern:gmatch("[^,]*") do diff --git a/test/headers_hop_by_hop.vtc b/test/headers_hop_by_hop.vtc new file mode 100644 index 0000000..6a0b312 --- /dev/null +++ b/test/headers_hop_by_hop.vtc @@ -0,0 +1,61 @@ +# SPDX-License-Identifier: MIT + +varnishtest "Verify that hop-by-hop headers are not copied into the auth request." +feature ignore_unknown_macro + +server s1 { + rxreq + txresp -status 201 +} -start + +server s_auth_backend { + rxreq + expect req.http.x-user == "admin" + expect req.http.x-passwd == "123" + expect req.http.keep-alive == "" + expect req.http.proxy-connection == "" + expect req.http.te == "" + expect req.http.trailer == "" + + # The body is not forwarded, thus the `content-length` must + # be absent + expect req.http.content-length == "" + + # haproxy-lua-http appends its own `connection: close` that + # must not be dropped or modified. + expect req.http.connection == "close" + + txresp +} -start + +haproxy h1 -conf { + global + lua-prepend-path ${testdir}/../?/http.lua + lua-load ${testdir}/../auth-request.lua + + listen fe1 + mode http + bind "fd@${fe1}" + http-request lua.auth-intercept auth_backend / * * - - + http-request deny if ! { var(txn.auth_response_successful) -m bool } + server s1 ${s1_addr}:${s1_port} + + backend auth_backend + mode http + server auth_backend ${s_auth_backend_addr}:${s_auth_backend_port} +} -start + +client c1 -connect ${h1_fe1_sock} { + txreq \ + -method "POST" \ + -hdr "x-user: admin" \ + -hdr "x-passwd: 123" \ + -hdr "connection: keep-alive" \ + -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "proxy-connection: keep-alive" \ + -hdr "te: trailers" \ + -hdr "trailer: expires" \ + -body "ping" + rxresp + expect resp.status == 201 +} -run diff --git a/test/headers_hop_by_hop_fail.vtc b/test/headers_hop_by_hop_fail.vtc new file mode 100644 index 0000000..0a78545 --- /dev/null +++ b/test/headers_hop_by_hop_fail.vtc @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: MIT + +varnishtest "Verify that hop-by-hop headers are not copied into the failure response." +feature ignore_unknown_macro +feature cmd "dpkg --compare-versions ${haproxy_version} ge 2.2" + +server s_auth_backend { + rxreq + txresp \ + -status 401 \ + -hdr "x-reason: invalid pwd" \ + -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "te: trailers" \ + -hdr "trailer: expires" \ + -hdr "upgrade: websocket" +} -start + +haproxy h1 -conf { + global + lua-prepend-path ${testdir}/../?/http.lua + lua-load ${testdir}/../auth-request.lua + + listen fe1 + mode http + bind "fd@${fe1}" + http-request lua.auth-intercept auth_backend / * * - * + + backend auth_backend + mode http + server auth_backend ${s_auth_backend_addr}:${s_auth_backend_port} +} -start + +client c1 -connect ${h1_fe1_sock} { + txreq + rxresp + expect resp.status == 401 + expect resp.http.x-reason == "invalid pwd" + expect resp.http.keep-alive == "" + expect resp.http.te == "" + expect resp.http.trailer == "" + expect resp.http.upgrade == "" +} -run diff --git a/test/headers_hop_by_hop_succeed.vtc b/test/headers_hop_by_hop_succeed.vtc new file mode 100644 index 0000000..513b0bc --- /dev/null +++ b/test/headers_hop_by_hop_succeed.vtc @@ -0,0 +1,51 @@ +# SPDX-License-Identifier: MIT + +varnishtest "Verify that hop-by-hop headers are not copied into the forwarded request." +feature ignore_unknown_macro + +server s1 { + rxreq + expect req.http.x-user == "admin" + expect req.http.token == "asd" + expect req.http.connection == "" + expect req.http.keep-alive == "" + expect req.http.te == "" + expect req.http.trailer == "" + expect req.http.upgrade == "" + txresp -status 201 +} -start + +server s_auth_backend { + rxreq + txresp \ + -status 200 \ + -hdr "x-user: admin" \ + -hdr "token: asd" \ + -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "te: trailers" \ + -hdr "trailer: expires" \ + -hdr "upgrade: websocket" +} -start + +haproxy h1 -conf { + global + lua-prepend-path ${testdir}/../?/http.lua + lua-load ${testdir}/../auth-request.lua + + listen fe1 + mode http + bind "fd@${fe1}" + http-request lua.auth-intercept auth_backend / * * * - + http-request deny if ! { var(txn.auth_response_successful) -m bool } + server s1 ${s1_addr}:${s1_port} + + backend auth_backend + mode http + server auth_backend ${s_auth_backend_addr}:${s_auth_backend_port} +} -start + +client c1 -connect ${h1_fe1_sock} { + txreq + rxresp + expect resp.status == 201 +} -run