Skip to content

Commit 26a96a6

Browse files
committed
fix: follow the redirect only as an http->https upgrade (Copilot review)
An already-https LZ_HOST_URL that answers with a redirect (e.g. canonicalization) must not re-enter the upgrade path - on a loopback host it would grant the -k cert-skip to a probe that never needed upgrading. A non-http:// base now refuses the redirect loudly (rc 2) with a warn telling the operator to set LZ_HOST_URL to the URL the server expects. Harness: new always-redirect mock mode + 3 assertions; 18/18 at HEAD; the two behavioral assertions fail against the pre-fix commit (the -k retry fired, no refusal warn).
1 parent 2f64f6e commit 26a96a6

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

install.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ validate_token() {
141141
fi
142142
case "$_PROBE_HTTP" in
143143
301|302|307|308)
144+
# Follow the redirect ONLY as an HTTP->HTTPS upgrade. An already-https
145+
# base that redirects (e.g. canonicalization) must NOT re-enter this
146+
# path - it could grant the loopback cert-skip to a probe that never
147+
# needed upgrading. It falls through to the non-200 fail-loud below.
148+
case "$base" in
149+
http://*) : ;;
150+
*)
151+
warn "the LZ API at ${base} answered HTTP ${_PROBE_HTTP} (a redirect); refusing to follow it from a non-http:// base - set LZ_HOST_URL to the URL the server expects"
152+
return 2
153+
;;
154+
esac
144155
# Strip trailing slashes BEFORE the :80 strip, or a trailing slash
145156
# shields the port ('localhost:80/' must become 'localhost', not
146157
# 'localhost:80' - retrying https against the plaintext port always

tests/validate-token.test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ case "${MOCK_MODE}" in
3535
http://*) printf '\n301' ;;
3636
https://*) echo "curl: (7) Failed to connect to localhost port 443" >&2; exit 7 ;;
3737
esac ;;
38+
always-redirect)
39+
printf '\n301' ;;
3840
esac
3941
MOCK
4042
chmod +x "${WORK}/curl"
@@ -107,6 +109,14 @@ export MOCK_MODE=redirect-then-ok LZ_HOST_URL="http://localhost:80@evil.com/"
107109
validate_token tok; check "userinfo-host 301 -> verified https retry -> 0" 0 $?
108110
assert_not "userinfo host must NOT get the loopback cert-skip" retry_used_k "@evil.com"
109111

112+
# 8. Already-https base that redirects: NOT an http->https upgrade - refuse
113+
# to follow (rc 2) and never grant the loopback -k retry
114+
export MOCK_LOG="${WORK}/log8"; : > "$MOCK_LOG"
115+
export MOCK_MODE=always-redirect LZ_HOST_URL="https://localhost"
116+
validate_token tok; check "https base 301 -> refused, rc 2" 2 $?
117+
assert_not "https-base redirect must NOT trigger a -k retry" retry_used_k "https://localhost"
118+
assert "https-base redirect warns instead of following" grep -q "WARN: .*refusing to follow" "$MOCK_LOG"
119+
110120
echo
111121
echo "passed=$pass failed=$fail"
112122
[ "$fail" -eq 0 ]

0 commit comments

Comments
 (0)