Repository navigation
fix(auth): spend the MFA enrollment code and throttle Login's MFA leg per account (#442) #1000
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
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,116 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-lambda-go/events" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func loginReqFrom(ip, email, code string) *events.LambdaFunctionURLRequest { | ||
| body := fmt.Sprintf(`{"email":%q,"password":%q,"mfa_code":%q}`, email, b64("pw"), code) | ||
| if code == "" { | ||
| body = fmt.Sprintf(`{"email":%q,"password":%q}`, email, b64("pw")) | ||
| } | ||
| req := &events.LambdaFunctionURLRequest{Body: body} | ||
| req.RequestContext.HTTP.SourceIP = ip | ||
| return req | ||
| } | ||
|
|
||
| func requireClientCode(t *testing.T, err error, code int, msgAndArgs ...any) { | ||
| t.Helper() | ||
| ce, ok := IsClientError(err) | ||
| require.True(t, ok, "expected a client error, got %v", err) | ||
| assert.Equal(t, code, ce.code, msgAndArgs...) | ||
| } | ||
|
|
||
| // Issue #442: Login's TOTP / recovery-code leg is throttled per account, so | ||
| // rotating source IPs does not reset the code-guessing budget. Email variants | ||
| // that Login resolves to the same address share one budget. | ||
| func TestHandler_login_MFALegRateLimitedPerEmail(t *testing.T) { | ||
| limit := getDefaultRateLimits()["login_mfa"].MaxAttempts | ||
| require.Less(t, limit, 100) | ||
| emails := []string{"victim@example.com", "Victim@Example.com", `"Someone" <victim@example.com>`} | ||
|
|
||
| mockAuth := new(MockAuthService) | ||
| t.Cleanup(func() { mockAuth.AssertExpectations(t) }) | ||
| mockAuth.On("Login", mock.Anything, mock.Anything). | ||
| Return((*LoginResponse)(nil), ErrInvalidMFACode_test()).Times(limit) | ||
| h := &Handler{auth: mockAuth, rateLimiter: NewInMemoryRateLimiter()} | ||
|
|
||
| for i := 0; i <= limit; i++ { | ||
| ip := fmt.Sprintf("198.51.100.%d", i+1) // fresh IP each time: the per-IP bucket never trips | ||
| _, err := h.login(context.Background(), loginReqFrom(ip, emails[i%len(emails)], "000000")) | ||
| if i < limit { | ||
| requireClientCode(t, err, 401, "attempt %d", i+1) | ||
| } else { | ||
| requireClientCode(t, err, 429, "attempt %d must be throttled", i+1) | ||
| } | ||
| } | ||
|
|
||
| // Another account keeps its own budget. | ||
| mockAuth.On("Login", mock.Anything, mock.Anything). | ||
| Return((*LoginResponse)(nil), ErrInvalidMFACode_test()).Once() | ||
| _, err := h.login(context.Background(), loginReqFrom("203.0.113.1", "other@example.com", "000000")) | ||
| requireClientCode(t, err, 401) | ||
| } | ||
|
|
||
| // The password-only first leg never touches the per-email bucket, so users | ||
| // without MFA and the mfa_required round trip are unaffected. | ||
| func TestHandler_login_NoMFACodeSkipsPerEmailBucket(t *testing.T) { | ||
| mockAuth := new(MockAuthService) | ||
| t.Cleanup(func() { mockAuth.AssertExpectations(t) }) | ||
| mockAuth.On("Login", mock.Anything, mock.Anything). | ||
| Return((*LoginResponse)(nil), ErrMFARequired_test()).Once() | ||
| rl := new(MockRateLimiter) | ||
| t.Cleanup(func() { rl.AssertExpectations(t) }) | ||
| rl.On("AllowWithIP", mock.Anything, "198.51.100.1", "login").Return(true, nil).Once() | ||
| h := &Handler{auth: mockAuth, rateLimiter: rl} | ||
|
|
||
| _, err := h.login(context.Background(), loginReqFrom("198.51.100.1", "u@example.com", "")) | ||
| requireClientCode(t, err, 401) | ||
| rl.AssertNotCalled(t, "AllowWithEmail", mock.Anything, mock.Anything, mock.Anything) | ||
| } | ||
|
|
||
| func TestHandler_login_MFALegRateLimiterErrorFailsClosed(t *testing.T) { | ||
| mockAuth := new(MockAuthService) | ||
| rl := new(MockRateLimiter) | ||
| t.Cleanup(func() { rl.AssertExpectations(t) }) | ||
| rl.On("AllowWithIP", mock.Anything, mock.Anything, "login").Return(true, nil).Once() | ||
| rl.On("AllowWithEmail", mock.Anything, "u@example.com", "login_mfa").Return(false, errors.New("db down")).Once() | ||
| h := &Handler{auth: mockAuth, rateLimiter: rl} | ||
|
|
||
| _, err := h.login(context.Background(), loginReqFrom("198.51.100.1", "U@example.com", "000000")) | ||
| requireClientCode(t, err, 503) | ||
| mockAuth.AssertNotCalled(t, "Login", mock.Anything, mock.Anything) | ||
| } | ||
|
|
||
| // Issue #442: requireMFASession checks the session before the per-IP "mfa" | ||
| // bucket, so requests without a valid session cannot spend the budget of | ||
| // users behind the same IP. | ||
| func TestHandler_mfaEndpoints_InvalidSessionDoesNotSpendIPBudget(t *testing.T) { | ||
| limit := getDefaultRateLimits()["mfa"].MaxAttempts | ||
| mockAuth := new(MockAuthService) | ||
| t.Cleanup(func() { mockAuth.AssertExpectations(t) }) | ||
| mockAuth.On("ValidateSession", mock.Anything, "bad").Return((*Session)(nil), errors.New("invalid")).Times(limit + 1) | ||
| mockAuth.On("ValidateSession", mock.Anything, "tok").Return(&Session{UserID: "user-1"}, nil).Once() | ||
| mockAuth.On("MFAEnableAPI", mock.Anything, "user-1", "000000").Return([]string(nil), nil).Once() | ||
| h := &Handler{auth: mockAuth, rateLimiter: NewInMemoryRateLimiter()} | ||
|
|
||
| for i := 0; i <= limit; i++ { | ||
| req := authedReq("bad", `{"code":"000000"}`) | ||
| req.RequestContext.HTTP.SourceIP = "198.51.100.1" | ||
| _, err := h.mfaEnable(context.Background(), req) | ||
| requireClientCode(t, err, 401, "attempt %d", i+1) | ||
| } | ||
|
|
||
| req := authedReq("tok", `{"code":"000000"}`) | ||
| req.RequestContext.HTTP.SourceIP = "198.51.100.1" | ||
| _, err := h.mfaEnable(context.Background(), req) | ||
| require.NoError(t, err, "a valid session on the same IP must still have its budget") | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // currentTOTPStep matches the counter of a code generated for the current step, | ||
| // allowing the clock to cross one step boundary while the test runs. | ||
| func currentTOTPStep() any { | ||
| return mock.MatchedBy(func(counter int64) bool { | ||
| now := time.Now().Unix() / 30 | ||
| return counter == now || counter == now-1 | ||
| }) | ||
| } | ||
|
|
||
| // Issue #442: MFAEnable hands the enrollment code's counter to EnableMFA, so the | ||
| // code that turned MFA on cannot be spent again at Login. | ||
| func TestMFAEnable_RecordsEnrollmentCodeCounter(t *testing.T) { | ||
| ctx := context.Background() | ||
| mockStore := new(MockStore) | ||
| service := createTestService(mockStore, new(MockEmailSender)) | ||
|
|
||
| secret := "JBSWY3DPEHPK3PXP" | ||
| expiresAt := time.Now().Add(mfaPendingExpiry) | ||
| user := createTestUser(t, "SecurePass@123") | ||
| user.MFAPendingSecret = secret | ||
| user.MFAPendingSecretExpiresAt = &expiresAt | ||
| step := time.Now().Unix()/30 - 1 | ||
|
|
||
| mockStore.On("GetUserByID", ctx, user.ID).Return(user, nil) | ||
| mockStore.On("EnableMFA", ctx, user.ID, secret, step, mock.AnythingOfType("[]string")).Return(nil).Once() | ||
|
|
||
| _, err := service.MFAEnable(ctx, user.ID, generateTOTP(secret, step)) | ||
| require.NoError(t, err) | ||
| mockStore.AssertExpectations(t) | ||
| mockStore.AssertNotCalled(t, "ClaimTOTPCounter", mock.Anything, mock.Anything, mock.Anything) | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.