Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
21 changes: 20 additions & 1 deletion auth-request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
-- <BADREQ>, 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
Expand Down
61 changes: 61 additions & 0 deletions test/headers_hop_by_hop.vtc
Original file line number Diff line number Diff line change
@@ -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 == "<undef>"
expect req.http.proxy-connection == "<undef>"
expect req.http.te == "<undef>"
expect req.http.trailer == "<undef>"

# The body is not forwarded, thus the `content-length` must
# be absent
expect req.http.content-length == "<undef>"

# 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
42 changes: 42 additions & 0 deletions test/headers_hop_by_hop_fail.vtc
Original file line number Diff line number Diff line change
@@ -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 == "<undef>"
expect resp.http.te == "<undef>"
expect resp.http.trailer == "<undef>"
expect resp.http.upgrade == "<undef>"
} -run
51 changes: 51 additions & 0 deletions test/headers_hop_by_hop_succeed.vtc
Original file line number Diff line number Diff line change
@@ -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 == "<undef>"
expect req.http.keep-alive == "<undef>"
expect req.http.te == "<undef>"
expect req.http.trailer == "<undef>"
expect req.http.upgrade == "<undef>"
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
Loading