-
Notifications
You must be signed in to change notification settings - Fork 0
fix(auth): reject API keys minted before the owner's last password change #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //go:build integration | ||
|
|
||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const ( | ||
| rotationPassword = "SyntheticPassword123!" | ||
| rotationNewPassword = "RotatedPassword456#" | ||
| ) | ||
|
|
||
| // failingKeyListStore makes the best-effort revocation scan after a password | ||
| // change fail, as a transient database error would. | ||
| type failingKeyListStore struct{ *PostgresStore } | ||
|
|
||
| func (failingKeyListStore) ListAPIKeysByUser(context.Context, string) ([]*UserAPIKey, error) { | ||
| return nil, errors.New("synthetic list failure") | ||
| } | ||
|
|
||
| // userReadHook runs onRead once, right after the first GetUserByID returns. | ||
| type userReadHook struct { | ||
| *PostgresStore | ||
| onRead func() | ||
| } | ||
|
|
||
| func (s *userReadHook) GetUserByID(ctx context.Context, id string) (*User, error) { | ||
| user, err := s.PostgresStore.GetUserByID(ctx, id) | ||
| if hook := s.onRead; hook != nil { | ||
| s.onRead = nil | ||
| hook() | ||
| } | ||
| return user, err | ||
| } | ||
|
|
||
| func newRotationService(store StoreInterface) *Service { | ||
| svc := NewService(ServiceConfig{Store: store}) | ||
| svc.bcryptCostOverride = 4 | ||
| return svc | ||
| } | ||
|
|
||
| func createRotationUser(t *testing.T, svc *Service, store *PostgresStore, email string) *User { | ||
| t.Helper() | ||
| hash, err := svc.hashPassword(rotationPassword) | ||
| require.NoError(t, err) | ||
| user := &User{Email: email, PasswordHash: hash, Active: true, GroupIDs: []string{DefaultPurchaserGroupID}} | ||
| require.NoError(t, store.CreateUser(t.Context(), user)) | ||
| return user | ||
| } | ||
|
|
||
| func TestIntegration_APIKeyRejectedAfterPasswordRotation(t *testing.T) { | ||
| store := NewPostgresStore(setupAuthTestDB(t)) | ||
| svc := newRotationService(store) | ||
| ctx := t.Context() | ||
| expiry := time.Now().Add(time.Hour) | ||
| perms := []Permission{{Action: ActionView, Resource: ResourceRecommendations}} | ||
| change := ChangePasswordRequest{CurrentPassword: rotationPassword, NewPassword: rotationNewPassword} | ||
|
|
||
| t.Run("revocation-scan-fails", func(t *testing.T) { | ||
| user := createRotationUser(t, svc, store, "rotation-scan-fails@example.com") | ||
| apiKey, _, err := svc.CreateAPIKey(ctx, user.ID, "ci", rotationPassword, perms, &expiry) | ||
| require.NoError(t, err) | ||
| require.NoError(t, newRotationService(failingKeyListStore{store}).ChangePassword(ctx, user.ID, change)) | ||
|
|
||
| _, _, err = svc.ValidateUserAPIKey(ctx, apiKey) | ||
| require.ErrorIs(t, err, ErrAPIKeyPasswordRotated) | ||
| }) | ||
|
|
||
| t.Run("key-minted-across-rotation", func(t *testing.T) { | ||
| user := createRotationUser(t, svc, store, "rotation-mint-race@example.com") | ||
| racing := &userReadHook{PostgresStore: store, onRead: func() { | ||
| require.NoError(t, svc.ChangePassword(ctx, user.ID, change)) | ||
| }} | ||
| apiKey, _, err := newRotationService(racing).CreateAPIKey(ctx, user.ID, "ci", rotationPassword, perms, &expiry) | ||
| require.NoError(t, err, "the mint verified the password it read before the rotation") | ||
|
|
||
| _, _, err = svc.ValidateUserAPIKey(ctx, apiKey) | ||
| require.ErrorIs(t, err, ErrAPIKeyPasswordRotated) | ||
| }) | ||
|
|
||
| t.Run("key-minted-after-rotation", func(t *testing.T) { | ||
| user := createRotationUser(t, svc, store, "rotation-fresh-key@example.com") | ||
| before, _, err := svc.CreateAPIKey(ctx, user.ID, "old", rotationPassword, perms, &expiry) | ||
| require.NoError(t, err) | ||
| require.NoError(t, svc.UpdateUserProfile(ctx, user.ID, "rotation-fresh-key-renamed@example.com", rotationPassword, "")) | ||
| _, _, err = svc.ValidateUserAPIKey(ctx, before) | ||
| require.NoError(t, err, "a write that keeps the password must not invalidate keys") | ||
|
|
||
| require.NoError(t, svc.ChangePassword(ctx, user.ID, change)) | ||
| after, _, err := svc.CreateAPIKey(ctx, user.ID, "new", rotationNewPassword, perms, &expiry) | ||
| require.NoError(t, err) | ||
| key, owner, err := svc.ValidateUserAPIKey(ctx, after) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, user.ID, owner.ID) | ||
| assert.Equal(t, "new", key.Name) | ||
| }) | ||
| } |
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
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
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
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
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
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
5 changes: 5 additions & 0 deletions
5
internal/database/postgres/migrations/000103_api_key_password_version.down.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| -- 000103 down: drop the password-version binding of API keys. | ||
| DROP TRIGGER IF EXISTS users_bump_password_version ON users; | ||
| DROP FUNCTION IF EXISTS bump_user_password_version(); | ||
| ALTER TABLE api_keys DROP COLUMN IF EXISTS password_version; | ||
| ALTER TABLE users DROP COLUMN IF EXISTS password_version; | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Deactivate API keys before removing the password versions.
If a password rotation's revocation scan failed, an older key can remain active. This down migration discards the version mismatch. If migration 000103 is applied again, both versions default to zero, so
ValidateUserAPIKeyaccepts the older key. Deactivate API keys in the down migration before dropping the columns so a rollback cannot restore access to rotated credentials.🧰 Tools
🪛 Squawk (2.65.0)
[warning] 4-4: Dropping a column may break existing clients.
(ban-drop-column)
[warning] 5-5: Dropping a column may break existing clients.
(ban-drop-column)
🤖 Prompt for AI Agents