fix(security): replace timing-unsafe token comparisons with hmac.Equal#426
Merged
Conversation
Bearer and admin token comparisons used plain == / != operators, which are not constant-time and leak timing information via early-exit string comparison. Replace all four comparison sites with hmac.Equal([]byte(a), []byte(b)) as required by issue #350. Add correctness guard tests for each site to ensure accept/reject behavior is preserved.
There was a problem hiding this comment.
Pull request overview
This PR hardens Engram’s cloud authentication by replacing timing-unsafe string token comparisons (==/!=) with constant-time comparisons via crypto/hmac.Equal, reducing timing-oracle risk across bearer/admin token checks in the cloud surface.
Changes:
- Replace bearer token comparison in
internal/cloud/authwithhmac.Equal. - Replace dashboard admin token comparisons in
internal/cloud/cloudserver(login, session auth, admin check) withhmac.Equal. - Add targeted regression tests to preserve accept/reject behavior at each modified comparison site.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| internal/cloud/cloudserver/cloudserver.go | Uses hmac.Equal for admin token checks (login/session/admin) and adds crypto/hmac import. |
| internal/cloud/cloudserver/cloudserver_test.go | Adds regression tests ensuring correct admin token behavior remains unchanged after switching comparisons. |
| internal/cloud/auth/auth.go | Uses hmac.Equal for bearer token validation to avoid timing-unsafe comparisons. |
| internal/cloud/auth/auth_test.go | Adds regression test covering accept/reject behavior (including prefix/superset cases) for bearer token comparison. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| // TestValidateLoginTokenAdminComparisonGuard is a correctness guard for the | ||
| // constant-time admin token comparison inside validateLoginToken (cloudserver.go:160). |
| } | ||
|
|
||
| // TestIsDashboardAdminComparisonGuard is a correctness guard for the | ||
| // constant-time admin token comparison inside isDashboardAdmin (cloudserver.go:320). |
Comment on lines
+256
to
+258
| // constant-time token comparison at auth.go:253. A correct token must be accepted | ||
| // and any wrong token must be rejected, preserving behavior after the | ||
| // timing-safe hmac.Equal replacement (security issue #350). |
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
Replaces timing-unsafe
==/!=bearer and admin token comparisons withhmac.Equal([]byte(a), []byte(b))(constant-time), eliminating the timing oracle. Fixes the three sites in #350 plus one additional matching site inauthorizeDashboardRequest. Adds thecrypto/hmacimport tocloudserver.go.Test plan
TestAuthorizeBearerTokenConstantTimeComparison(auth),TestValidateLoginTokenAdminComparisonGuardandTestIsDashboardAdminComparisonGuard(cloudserver): accept/reject correctness at each sitego test ./... && go vet ./... && go build ./...cleanCloses #350