feat(edge): compare API keys as fixed-width digests (#24)#37
Merged
Conversation
authenticate compared the presented key against every configured key with crypto/subtle.ConstantTimeCompare and no early exit, so response time did not leak which client matched or how far a guessed key got (ADR-0003). One residual signal remained: ConstantTimeCompare returns immediately when two byte slices differ in length, so the comparison cost still varied with the presented key's length relative to the configured keys'. Reduce each configured key to its SHA-256 digest at construction (in set, so it also covers SIGHUP reload), and per request compare sha256(presented) against those 32-byte digests. Every comparison is now equal-width, removing the length-dependent timing and the early-exit-on-length branch entirely. As a side benefit the guard no longer retains raw key bytes past construction. Behavior — which identities authenticate — is unchanged: the full existing guard suite stays green, and a new test pins that keys of very different lengths each resolve to their identity while a wrong same-length key is rejected. A note in authenticate and a Consequences bullet in ADR-0003 record that the length signal is removed. Closes #24 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Remove the residual key-length timing signal from edge client authentication (issue #24).
authenticatealready compares the presented key against every configured key withcrypto/subtle.ConstantTimeCompareand no early exit, so response time does not leak which client matched or how far a guessed key got (ADR-0003). One residual signal remained:ConstantTimeComparereturns immediately when two byte slices differ in length, so the comparison cost still varied with the presented key's length relative to the configured keys'.This PR reduces each configured key to its SHA-256 digest at construction (in
set, so SIGHUP reload is covered too), and per request comparessha256(presented)against those 32-byte digests. Every comparison is now equal-width — this removes the length-dependent timing and the early-exit-on-length branch entirely. As a side benefit, the guard no longer retains raw key bytes past construction.Acceptance criteria (issue #24)
authenticate's doc comment and a Consequences bullet in ADR-0003.Testing
TestGuardAuthenticatesVariableLengthKeyspins the property the digest comparison must preserve: keys of very different lengths each resolve to their identity, and a wrong key sharing a real key's length is rejected. (Added and confirmed green against the pre-change code first, then carried through the change.)make verify: build, vet,-racetests, coverage 95.3% (gate 80%),golangci-lintv2.12.2 clean,govulncheckclean.The public
ClientKeytype and theNewGuard/Reloadsignatures are unchanged, so the composition root needs no change.Closes #24
🤖 Generated with Claude Code