Skip to content

test(api): pin the constant-time basic credential compare - #79

Merged
josuegiron merged 1 commit into
masterfrom
test/pin-constant-time-basic-compare
Nov 21, 2026
Merged

josuegiron merged 1 commit into
masterfrom
test/pin-constant-time-basic-compare

Conversation

@josuegiron

Copy link
Copy Markdown
Member

Scope

One PR (1 of 1), module api only, test-only. Includes: structural tests that pin the constant-time basic credential compare. Leaves out: any change to security.go. No further changes planned on top.

Reason

Nothing failed if validateCredentials were reverted to == or to a short-circuiting compare: every case in TestValidateCredentialsMatchesUsernameAndPassword also passes with == (epic-security-fixes retrospective, finding 4 / action item 2). A timing test would be flaky, and a counting hook would change production code, so the tests read the source.

Changes

  • New security_constant_time_test.go (go/parser + go/ast, standard library only):
    • TestValidateCredentialsComparesBothFieldsWithoutShortCircuit: the body is exactly the credentialsConfigured guard, constantTimeEqual(username, Username) and constantTimeEqual(password, Password) as two unconditional assignments, and one return; no &&/||; the four credential identifiers appear only as compare arguments.
    • TestConstantTimeEqualHashesAndUsesSubtle: the body is two sha256.Sum256 calls and return subtle.ConstantTimeCompare(...); no ==/!=; got/want used only inside the hashes.

Release notes

  • Tests: the constant-time basic credential compare is pinned by structural tests. No behavior change.

Review

Quick review lens. Applied: an early return between the two compares (if usernameMatch != 1 { return false }) and a switch on the sums before the subtle call both slipped through the first version; both function bodies are now pinned to their exact statement shape. Rejected: test file name differs from the plan (plan-only issue).

Verification

Mutation-tested: each of these edits to security.go makes the new tests fail, and the original passes:
== compare · && on the results · early return after the username compare · if usernameMatch != 1 { return false } between compares · bytes.Equal on the sums · no hashing · switch gotSum { case wantSum: } before the subtle call.

Local runs:

  • gofmt -l . — no output
  • go vet ./... — ok
  • go test -race ./... — ok for every package
  • govulncheck ./... — No vulnerabilities found

Trade-off: the tests are structural, so a harmless refactor of these two functions also fails them; the failure message states the expected shape.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@josuegiron
josuegiron merged commit c039f57 into master Nov 21, 2026
2 checks passed
@josuegiron
josuegiron deleted the test/pin-constant-time-basic-compare branch November 21, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant