Webhooks: constant-time HMAC signature verification#11
Merged
Conversation
Constant-time HMAC-SHA256 verification with a timestamp-tolerance window; Sign helper and typed sentinel errors. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…olerance; add golden test
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.
Summary
Implements the
webhookpackage: constant-time HMAC-SHA256 verification ofinbound webhook signatures, with a timestamp-tolerance window to defeat replay.
This completes the package set — no placeholder packages remain.
What's included
Sign(secret, payload)— lowercase hex HMAC-SHA256 (locked against thecanonical published test vector).
Verifier(NewVerifier(secret, WithTolerance(d))) —Verify(payload, sigHex)compares withhmac.Equalon the raw MAC bytes(constant-time); invalid hex maps to a mismatch with no information leak.
VerifyTimestamp(body, timestamp, now, sigHex)implements the common"<unix>.<body>"scheme, rejecting timestamps outside the tolerance window ineither direction before checking the signature.
nowis injected fordeterministic verification.
ErrSignatureMismatchandErrTimestampOutsideToleranceforerrors.Is.VerifyTimestampstreams the HMAC over the timestamp/body parts (no payload copy).A tolerance of
<= 0disables the window — clearly warned in the GoDoc.Behavior changes
None — new package only (
webhookwas a placeholder).Deferred (not in this PR)
signature they extracted).
Test plan
go build ./...go vet ./...gofmt -l .(clean)go test -race ./...(all 18 packages pass)mismatch; timestamp within/outside (stale and future) tolerance; tolerance
disabled; a known-answer HMAC golden vector; and the hex output format.
🤖 Generated with Claude Code