From 34d831662286d80ca962be896275d70bacb4bfbf Mon Sep 17 00:00:00 2001 From: rhclayto Date: Fri, 20 Nov 2026 16:02:51 -0700 Subject: [PATCH 1/6] fix: RFC 9110 compliant header copying Headers that must never be copied between connections, per RFC 9110 section 7.6.1. --- auth-request.lua | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/auth-request.lua b/auth-request.lua index 1504948..29e8e2f 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, + ["proxy-connection"] = true, + ["keep-alive"] = true, + ["upgrade"] = true, + ["te"] = true, + ["trailer"] = true, + ["transfer-encoding"] = true, + ["content-length"] = true, + ["host"] = 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 From 8c7e003933cbe2a5d4fc19e45ea4e031dd1c5722 Mon Sep 17 00:00:00 2001 From: h Date: Fri, 20 Nov 2026 20:49:25 -0700 Subject: [PATCH 2/6] test: cover the hop-by-hop header blocklist header_match() gates all three header-copying directions (client to auth backend, auth backend to protected backend, auth backend to client), but none of them were covered for hop-by-hop suppression. Add test/header_match.lua, a standalone unit test that needs only a lua interpreter. It pins down all nine HOP_BY_HOP_HEADERS entries, asserts a blocklisted header stays blocked when an operator requests it by name, and checks that ordinary headers keep matching so widening the two hardcoded content-length/host comparisons into a table did not over-block. It self-locates auth-request.lua, so it runs from any working directory. Add three .vtc tests, one per copying direction, and run the unit test in CI next to the vtest suite. Verified against the pre-fix condition (header == "content-length" or header == "host"): the unit test reports 14 failing checks and each .vtc test fails on an expected header, so none of them pass for the wrong reason. The full suite is green on HAProxy 3.4.8: 23 passed, 1 skipped, 0 failed. Two headers are deliberately not asserted end-to-end. HAProxy 3.4 strips a client "connection" or "upgrade" header before req_get_headers() is visible to Lua, and rejects a client request carrying both "connection: upgrade" and "upgrade: " at the frontend, with a 400 of its own, before any Lua action runs. Assertions on those would pass without the blocklist, so header_match.lua covers them directly instead. --- .github/workflows/test.yml | 5 ++ test/header_match.lua | 124 ++++++++++++++++++++++++++++ test/headers_hop_by_hop.vtc | 68 +++++++++++++++ test/headers_hop_by_hop_fail.vtc | 45 ++++++++++ test/headers_hop_by_hop_succeed.vtc | 55 ++++++++++++ 5 files changed, 297 insertions(+) create mode 100644 test/header_match.lua create mode 100644 test/headers_hop_by_hop.vtc create mode 100644 test/headers_hop_by_hop_fail.vtc create mode 100644 test/headers_hop_by_hop_succeed.vtc diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 41b8078..716e616 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -67,6 +67,11 @@ jobs: - uses: actions/checkout@v7 with: submodules: recursive + - name: Run header_match unit tests. + run: | + set -euo pipefail + lua_bin="$(command -v lua || command -v lua5.4 || command -v lua5.3 || command -v lua5.1)" + "$lua_bin" test/header_match.lua - name: Install haproxy-lua-http globally. run: | sudo install -m755 -d /usr/local/share/lua/5.3/ diff --git a/test/header_match.lua b/test/header_match.lua new file mode 100644 index 0000000..c5f255e --- /dev/null +++ b/test/header_match.lua @@ -0,0 +1,124 @@ +-- SPDX-License-Identifier: MIT + +-- Standalone unit test for the hop-by-hop blocklist in header_match(). +-- +-- The .vtc tests drive header_match() through a live HAProxy and a varnishtest +-- server, so they cannot cheaply pin down every entry of HOP_BY_HOP_HEADERS, +-- nor prove that a blocklisted header stays blocked when an operator asks for +-- it by name. This file calls header_match() directly and needs only a Lua +-- interpreter, no HAProxy: +-- +-- lua test/header_match.lua +-- +-- The path to auth-request.lua is derived from this script's own location, so +-- the command above also works from outside the repository root. +-- +-- auth-request.lua performs side effects at load time, requiring +-- haproxy-lua-http and registering actions, so both are stubbed out first. + +package.preload["haproxy-lua-http"] = function() + return {} +end + +core = { + register_action = function() end, + register_fetches = function() end, + backends = {}, +} + +-- Load auth-request.lua relative to this script, so the working directory does +-- not matter. arg[0] is the script path as it was invoked. +local script_dir = (arg[0]:match("^(.*)/[^/]*$")) or "." +local auth_request_path = script_dir .. "/../auth-request.lua" + +assert(loadfile(auth_request_path))() + +local failures = 0 + +local function check(desc, got, want) + if got == want then + io.stdout:write(string.format("ok %s\n", desc)) + else + failures = failures + 1 + io.stderr:write(string.format("FAIL %s: got %s, want %s\n", desc, tostring(got), tostring(want))) + end +end + +-- Every header name handed to header_match() is lowercase: HAProxy lowercases +-- the keys returned by req_get_headers() and haproxy-lua-http lowercases +-- response header names while parsing them. HOP_BY_HOP_HEADERS relies on that +-- contract, so the expected names are written in lowercase here as well. +local HOP_BY_HOP = { + "connection", + "proxy-connection", + "keep-alive", + "upgrade", + "te", + "trailer", + "transfer-encoding", + "content-length", + "host", +} + +-- The `*` glob is what the auth-request action hardcodes and the value +-- auth-intercept users reach for most often, so it is the pattern under which +-- the blocklist matters most. +local MATCH_ALL = globToLuaPattern("*") + +-- 1. No hop-by-hop header may be copied, whatever the configured pattern says. +for _, header in ipairs(HOP_BY_HOP) do + check("blocked under '*': " .. header, header_match(header, MATCH_ALL), false) +end + +-- 2. The blocklist must also win when an operator names the header explicitly. +-- That is a correctness requirement and not a filtering convenience: copying +-- "connection: upgrade" onto the auth probe produces a request the frontend +-- cannot service, because the probe is a plain HEAD. +for _, header in ipairs(HOP_BY_HOP) do + check("blocked when requested by name: " .. header, header_match(header, "^" .. header .. "$"), false) +end + +-- 3. Ordinary headers must keep matching, so that widening the two hardcoded +-- content-length/host comparisons into a table did not swallow names that +-- merely resemble a blocklisted one. +check("x-user matches x-*", header_match("x-user", globToLuaPattern("x-*")), true) +check("token matches x-*,token", header_match("token", globToLuaPattern("x-*,token")), true) +check("content-type matches content-*", header_match("content-type", globToLuaPattern("content-*")), true) +check("x-te matches x-te", header_match("x-te", globToLuaPattern("x-te")), true) +check("hosted matches hosted", header_match("hosted", globToLuaPattern("hosted")), true) + +-- 4. A dash still means "copy no headers at all". Both spellings matter: the +-- raw dash auth_request compares against, and the dash returned unchanged +-- by globToLuaPattern. +check("dash pattern copies nothing", header_match("x-user", "-"), false) +check("globbed dash copies nothing", header_match("x-user", globToLuaPattern("-")), false) + +-- 5. The blocklist keys must stay lowercase to keep matching the names HAProxy +-- and haproxy-lua-http hand back. A future edit writing "Connection" would +-- be invisible to the end-to-end tests, so catch it here. +for _, header in ipairs(HOP_BY_HOP) do + check("blocklist key is lowercase: " .. header, header, header:lower()) +end + +-- 6. Negative matching (a header that matches no pattern must be skipped) is +-- deliberately not asserted. Splitting the comma-separated list with +-- `pattern:gmatch("[^,]*")` yields empty pieces, and on some Lua releases +-- `("other"):match("")` returns "", which is truthy, so every header +-- "matches". That behaviour predates the blocklist, is visible in every +-- globToLuaPattern result, and belongs to a separate change. +local empty_piece_matches = false +for piece in ("a,b"):gmatch("[^,]*") do + if piece == "" and ("other"):match("") then + empty_piece_matches = true + end +end +if empty_piece_matches then + io.stdout:write("note an empty piece from gmatch(\"[^,]*\") matches every header on this Lua\n") +end + +if failures > 0 then + io.stderr:write(string.format("%d check(s) failed\n", failures)) + os.exit(1) +end + +io.stdout:write("all checks passed\n") diff --git a/test/headers_hop_by_hop.vtc b/test/headers_hop_by_hop.vtc new file mode 100644 index 0000000..8565960 --- /dev/null +++ b/test/headers_hop_by_hop.vtc @@ -0,0 +1,68 @@ +# 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 + # Ordinary headers must still be copied, so that the blocklist is not + # swallowing more than RFC 9110 section 7.6.1 requires. + expect req.http.x-user == "admin" + expect req.http.x-passwd == "123" + # HAProxy forwards these to the Lua layer, so each one is a real regression + # guard: they reach this server when the blocklist is absent. + expect req.http.proxy-connection == "" + expect req.http.keep-alive == "" + expect req.http.te == "" + expect req.http.trailer == "" + # Whatever an older HAProxy hands to Lua, the probe must end up with + # haproxy-lua-http's own "connection: close", which it appends only when no + # connection header was copied. On HAProxy 3.4 that header never reaches the + # Lua layer at all, so this assertion documents the probe rather than + # guarding the blocklist; the blocklist entries above do the guarding. + expect req.http.connection == "close" + # A copied content-length would announce a body that the probe never + # sends, which makes the auth server wait for bytes that never arrive. + # Both this and Host were already suppressed before the blocklist existed, + # so they are guards against a regression rather than new behaviour; + # test/header_match.lua pins down every entry of the blocklist itself. + expect req.http.content-length == "" + 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 "connection: keep-alive" \ + -hdr "proxy-connection: keep-alive" \ + -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "te: trailers" \ + -hdr "trailer: expires" \ + -hdr "x-user: admin" \ + -hdr "x-passwd: 123" \ + -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..b787bed --- /dev/null +++ b/test/headers_hop_by_hop_fail.vtc @@ -0,0 +1,45 @@ +# 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 "upgrade: websocket" \ + -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "trailer: expires" \ + -hdr "te: trailers" +} -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 + # hdr_fail is `*`, so this header only reaches the client via header_match(). + expect resp.http.x-reason == "invalid pwd" + # The remaining entries of the blocklist are suppressed even though the + # auth backend sent them and the pattern asked for everything. + expect resp.http.upgrade == "" + expect resp.http.keep-alive == "" + expect resp.http.trailer == "" + expect resp.http.te == "" +} -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..7698808 --- /dev/null +++ b/test/headers_hop_by_hop_succeed.vtc @@ -0,0 +1,55 @@ +# 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 + # hdr_succeed is `*`, so every header below that reaches this server does so + # through header_match(). Ordinary ones must survive the blocklist. + expect req.http.x-user == "admin" + expect req.http.token == "asd" + # Hop-by-hop headers from the auth backend must not be injected into the + # request forwarded to the protected backend. + expect req.http.connection == "" + expect req.http.upgrade == "" + expect req.http.keep-alive == "" + expect req.http.trailer == "" + expect req.http.te == "" + txresp -status 201 +} -start + +server s_auth_backend { + rxreq + txresp \ + -status 200 \ + -hdr "x-user: admin" \ + -hdr "token: asd" \ + -hdr "upgrade: websocket" \ + -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "trailer: expires" \ + -hdr "te: trailers" +} -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 From 39c78432257c43e0b76ad7738b15034c8ff3af46 Mon Sep 17 00:00:00 2001 From: h Date: Fri, 20 Nov 2026 21:11:52 -0700 Subject: [PATCH 3/6] ci: install an explicit Lua interpreter for the unit tests lua-json depends only on lua-lpeg, so nothing in the job guaranteed a Lua binary on PATH for test/header_match.lua. Install lua5.4 alongside it and call that binary directly instead of probing PATH. The unit test itself is version-independent: it passes under lua5.1 and lua5.4 unchanged. --- .github/workflows/test.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 716e616..01ced9a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -63,15 +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: | - set -euo pipefail - lua_bin="$(command -v lua || command -v lua5.4 || command -v lua5.3 || command -v lua5.1)" - "$lua_bin" test/header_match.lua + run: lua5.4 test/header_match.lua - name: Install haproxy-lua-http globally. run: | sudo install -m755 -d /usr/local/share/lua/5.3/ From 4f3c597882dfbae5f9194e7d8be61df3602c6e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 21 Nov 2026 18:37:07 +0100 Subject: [PATCH 4/6] Sort `HOP_BY_HOP_HEADERS` table --- auth-request.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/auth-request.lua b/auth-request.lua index 29e8e2f..ed370e3 100644 --- a/auth-request.lua +++ b/auth-request.lua @@ -86,14 +86,14 @@ end -- haproxy-lua-http add its intended "connection: close". local HOP_BY_HOP_HEADERS = { ["connection"] = true, - ["proxy-connection"] = true, + ["content-length"] = true, + ["host"] = true, ["keep-alive"] = true, - ["upgrade"] = true, + ["proxy-connection"] = true, ["te"] = true, ["trailer"] = true, ["transfer-encoding"] = true, - ["content-length"] = true, - ["host"] = true, + ["upgrade"] = true, } -- header_match checks whether the provided header matches the pattern. From 443f7307ba653cb3307caae140833472b94cc06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 21 Nov 2026 18:40:13 +0100 Subject: [PATCH 5/6] Clean up useless comments in tests --- test/headers_hop_by_hop.vtc | 31 +++++++++++------------------ test/headers_hop_by_hop_fail.vtc | 11 ++++------ test/headers_hop_by_hop_succeed.vtc | 12 ++++------- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/test/headers_hop_by_hop.vtc b/test/headers_hop_by_hop.vtc index 8565960..6a0b312 100644 --- a/test/headers_hop_by_hop.vtc +++ b/test/headers_hop_by_hop.vtc @@ -10,28 +10,21 @@ server s1 { server s_auth_backend { rxreq - # Ordinary headers must still be copied, so that the blocklist is not - # swallowing more than RFC 9110 section 7.6.1 requires. expect req.http.x-user == "admin" expect req.http.x-passwd == "123" - # HAProxy forwards these to the Lua layer, so each one is a real regression - # guard: they reach this server when the blocklist is absent. - expect req.http.proxy-connection == "" expect req.http.keep-alive == "" + expect req.http.proxy-connection == "" expect req.http.te == "" expect req.http.trailer == "" - # Whatever an older HAProxy hands to Lua, the probe must end up with - # haproxy-lua-http's own "connection: close", which it appends only when no - # connection header was copied. On HAProxy 3.4 that header never reaches the - # Lua layer at all, so this assertion documents the probe rather than - # guarding the blocklist; the blocklist entries above do the guarding. - expect req.http.connection == "close" - # A copied content-length would announce a body that the probe never - # sends, which makes the auth server wait for bytes that never arrive. - # Both this and Host were already suppressed before the blocklist existed, - # so they are guards against a regression rather than new behaviour; - # test/header_match.lua pins down every entry of the blocklist itself. + + # 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 @@ -55,13 +48,13 @@ haproxy h1 -conf { client c1 -connect ${h1_fe1_sock} { txreq \ -method "POST" \ + -hdr "x-user: admin" \ + -hdr "x-passwd: 123" \ -hdr "connection: keep-alive" \ - -hdr "proxy-connection: keep-alive" \ -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "proxy-connection: keep-alive" \ -hdr "te: trailers" \ -hdr "trailer: expires" \ - -hdr "x-user: admin" \ - -hdr "x-passwd: 123" \ -body "ping" rxresp expect resp.status == 201 diff --git a/test/headers_hop_by_hop_fail.vtc b/test/headers_hop_by_hop_fail.vtc index b787bed..0a78545 100644 --- a/test/headers_hop_by_hop_fail.vtc +++ b/test/headers_hop_by_hop_fail.vtc @@ -9,10 +9,10 @@ server s_auth_backend { txresp \ -status 401 \ -hdr "x-reason: invalid pwd" \ - -hdr "upgrade: websocket" \ -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "te: trailers" \ -hdr "trailer: expires" \ - -hdr "te: trailers" + -hdr "upgrade: websocket" } -start haproxy h1 -conf { @@ -34,12 +34,9 @@ client c1 -connect ${h1_fe1_sock} { txreq rxresp expect resp.status == 401 - # hdr_fail is `*`, so this header only reaches the client via header_match(). expect resp.http.x-reason == "invalid pwd" - # The remaining entries of the blocklist are suppressed even though the - # auth backend sent them and the pattern asked for everything. - expect resp.http.upgrade == "" expect resp.http.keep-alive == "" - expect resp.http.trailer == "" 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 index 7698808..513b0bc 100644 --- a/test/headers_hop_by_hop_succeed.vtc +++ b/test/headers_hop_by_hop_succeed.vtc @@ -5,17 +5,13 @@ feature ignore_unknown_macro server s1 { rxreq - # hdr_succeed is `*`, so every header below that reaches this server does so - # through header_match(). Ordinary ones must survive the blocklist. expect req.http.x-user == "admin" expect req.http.token == "asd" - # Hop-by-hop headers from the auth backend must not be injected into the - # request forwarded to the protected backend. expect req.http.connection == "" - expect req.http.upgrade == "" expect req.http.keep-alive == "" - expect req.http.trailer == "" expect req.http.te == "" + expect req.http.trailer == "" + expect req.http.upgrade == "" txresp -status 201 } -start @@ -25,10 +21,10 @@ server s_auth_backend { -status 200 \ -hdr "x-user: admin" \ -hdr "token: asd" \ - -hdr "upgrade: websocket" \ -hdr "keep-alive: timeout=5, max=1000" \ + -hdr "te: trailers" \ -hdr "trailer: expires" \ - -hdr "te: trailers" + -hdr "upgrade: websocket" } -start haproxy h1 -conf { From 322df74b08f364ac8b6001a26ba6305c669efb2b Mon Sep 17 00:00:00 2001 From: rhclayto Date: Sat, 21 Nov 2026 14:32:38 -0700 Subject: [PATCH 6/6] Delete test/header_match.lua --- test/header_match.lua | 124 ------------------------------------------ 1 file changed, 124 deletions(-) delete mode 100644 test/header_match.lua diff --git a/test/header_match.lua b/test/header_match.lua deleted file mode 100644 index c5f255e..0000000 --- a/test/header_match.lua +++ /dev/null @@ -1,124 +0,0 @@ --- SPDX-License-Identifier: MIT - --- Standalone unit test for the hop-by-hop blocklist in header_match(). --- --- The .vtc tests drive header_match() through a live HAProxy and a varnishtest --- server, so they cannot cheaply pin down every entry of HOP_BY_HOP_HEADERS, --- nor prove that a blocklisted header stays blocked when an operator asks for --- it by name. This file calls header_match() directly and needs only a Lua --- interpreter, no HAProxy: --- --- lua test/header_match.lua --- --- The path to auth-request.lua is derived from this script's own location, so --- the command above also works from outside the repository root. --- --- auth-request.lua performs side effects at load time, requiring --- haproxy-lua-http and registering actions, so both are stubbed out first. - -package.preload["haproxy-lua-http"] = function() - return {} -end - -core = { - register_action = function() end, - register_fetches = function() end, - backends = {}, -} - --- Load auth-request.lua relative to this script, so the working directory does --- not matter. arg[0] is the script path as it was invoked. -local script_dir = (arg[0]:match("^(.*)/[^/]*$")) or "." -local auth_request_path = script_dir .. "/../auth-request.lua" - -assert(loadfile(auth_request_path))() - -local failures = 0 - -local function check(desc, got, want) - if got == want then - io.stdout:write(string.format("ok %s\n", desc)) - else - failures = failures + 1 - io.stderr:write(string.format("FAIL %s: got %s, want %s\n", desc, tostring(got), tostring(want))) - end -end - --- Every header name handed to header_match() is lowercase: HAProxy lowercases --- the keys returned by req_get_headers() and haproxy-lua-http lowercases --- response header names while parsing them. HOP_BY_HOP_HEADERS relies on that --- contract, so the expected names are written in lowercase here as well. -local HOP_BY_HOP = { - "connection", - "proxy-connection", - "keep-alive", - "upgrade", - "te", - "trailer", - "transfer-encoding", - "content-length", - "host", -} - --- The `*` glob is what the auth-request action hardcodes and the value --- auth-intercept users reach for most often, so it is the pattern under which --- the blocklist matters most. -local MATCH_ALL = globToLuaPattern("*") - --- 1. No hop-by-hop header may be copied, whatever the configured pattern says. -for _, header in ipairs(HOP_BY_HOP) do - check("blocked under '*': " .. header, header_match(header, MATCH_ALL), false) -end - --- 2. The blocklist must also win when an operator names the header explicitly. --- That is a correctness requirement and not a filtering convenience: copying --- "connection: upgrade" onto the auth probe produces a request the frontend --- cannot service, because the probe is a plain HEAD. -for _, header in ipairs(HOP_BY_HOP) do - check("blocked when requested by name: " .. header, header_match(header, "^" .. header .. "$"), false) -end - --- 3. Ordinary headers must keep matching, so that widening the two hardcoded --- content-length/host comparisons into a table did not swallow names that --- merely resemble a blocklisted one. -check("x-user matches x-*", header_match("x-user", globToLuaPattern("x-*")), true) -check("token matches x-*,token", header_match("token", globToLuaPattern("x-*,token")), true) -check("content-type matches content-*", header_match("content-type", globToLuaPattern("content-*")), true) -check("x-te matches x-te", header_match("x-te", globToLuaPattern("x-te")), true) -check("hosted matches hosted", header_match("hosted", globToLuaPattern("hosted")), true) - --- 4. A dash still means "copy no headers at all". Both spellings matter: the --- raw dash auth_request compares against, and the dash returned unchanged --- by globToLuaPattern. -check("dash pattern copies nothing", header_match("x-user", "-"), false) -check("globbed dash copies nothing", header_match("x-user", globToLuaPattern("-")), false) - --- 5. The blocklist keys must stay lowercase to keep matching the names HAProxy --- and haproxy-lua-http hand back. A future edit writing "Connection" would --- be invisible to the end-to-end tests, so catch it here. -for _, header in ipairs(HOP_BY_HOP) do - check("blocklist key is lowercase: " .. header, header, header:lower()) -end - --- 6. Negative matching (a header that matches no pattern must be skipped) is --- deliberately not asserted. Splitting the comma-separated list with --- `pattern:gmatch("[^,]*")` yields empty pieces, and on some Lua releases --- `("other"):match("")` returns "", which is truthy, so every header --- "matches". That behaviour predates the blocklist, is visible in every --- globToLuaPattern result, and belongs to a separate change. -local empty_piece_matches = false -for piece in ("a,b"):gmatch("[^,]*") do - if piece == "" and ("other"):match("") then - empty_piece_matches = true - end -end -if empty_piece_matches then - io.stdout:write("note an empty piece from gmatch(\"[^,]*\") matches every header on this Lua\n") -end - -if failures > 0 then - io.stderr:write(string.format("%d check(s) failed\n", failures)) - os.exit(1) -end - -io.stdout:write("all checks passed\n")