Skip to content

reverseproxy: compare sticky-session cookie hash in constant time - #7853

Merged
steadytao merged 2 commits into
caddyserver:masterfrom
alhudz:cookie-hash-constant-time
Jul 9, 2026
Merged

steadytao merged 2 commits into
caddyserver:masterfrom
alhudz:cookie-hash-constant-time

Conversation

@alhudz

@alhudz alhudz commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor
  1. CookieHashSelection.Select matches the sticky-session cookie by comparing the server-computed HMAC of the chosen upstream's dial address (sha) against the client-supplied cookie value with sha == cookieValue, which short-circuits at the first differing byte.
  2. The HMAC key is the configured secret, present so a client cannot forge the cookie to pin itself onto a chosen backend, so the byte-dependent timing leaks the valid digest over the request cookie. Swapped to hmac.Equal (crypto/hmac is already imported), mirroring the constant-time password compare in caddyauth/argon2id.go.

repro: configure the cookie policy with a secret; a matching cookie still sticks to its host and a tampered value still falls back (added TestCookieHashPolicyWithSecret). Only the comparison timing changes: the old == leaked how many leading hex bytes matched, hmac.Equal does not.

Assistance Disclosure

I used an LLM tool to research this issue and draft the patch and the regression test; I reviewed the change and verified it locally (go test -run CookieHash -race ./modules/caddyhttp/reverseproxy/ plus a full build).

@alhudz

alhudz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

any update?

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the narrow hardening change.

@steadytao
steadytao merged commit 75c988d into caddyserver:master Jul 9, 2026
24 checks passed
@francislavoie francislavoie added the optimization 📉 Performance or cost improvements label Jul 10, 2026
@francislavoie francislavoie added this to the v2.11.5 milestone Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimization 📉 Performance or cost improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants