-
Notifications
You must be signed in to change notification settings - Fork 2k
[Browser MFA] Split validation out of Finish function #63978
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
Open
danielashare
wants to merge
2
commits into
master
Choose a base branch
from
danielashare/browser-mfa-webauthn-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−24
Open
Changes from all commits
Commits
Show all changes
2 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
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,7 @@ import ( | |||||||||
| "context" | ||||||||||
| "crypto/x509" | ||||||||||
| "fmt" | ||||||||||
| "maps" | ||||||||||
| "slices" | ||||||||||
| "testing" | ||||||||||
| "time" | ||||||||||
|
|
@@ -110,22 +111,25 @@ func TestLoginFlow_BeginFinish(t *testing.T) { | |||||||||
| }, | ||||||||||
| } | ||||||||||
| for _, test := range tests { | ||||||||||
| t.Run(test.name, func(t *testing.T) { | ||||||||||
| identity := test.identity | ||||||||||
| runTest := func(t *testing.T, validate bool) { | ||||||||||
| t.Parallel() | ||||||||||
|
|
||||||||||
| identity := test.identity.clone() | ||||||||||
| user := test.user | ||||||||||
|
|
||||||||||
| webLogin := &wanlib.LoginFlow{ | ||||||||||
| U2F: u2fConfig, | ||||||||||
| Webauthn: webConfig, | ||||||||||
| Identity: test.identity, | ||||||||||
| Identity: identity, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // 1st step of the login ceremony. | ||||||||||
| chalExts := &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| } | ||||||||||
| assertion, err := webLogin.Begin(ctx, wanlib.BeginParams{ | ||||||||||
| User: test.user, | ||||||||||
| ChallengeExtensions: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
| User: test.user, | ||||||||||
| ChallengeExtensions: chalExts, | ||||||||||
| }) | ||||||||||
| require.NoError(t, err) | ||||||||||
| // We care about a few specific settings, for everything else defaults are | ||||||||||
|
|
@@ -152,11 +156,25 @@ func TestLoginFlow_BeginFinish(t *testing.T) { | |||||||||
| assertionResp, err := test.key.SignAssertion(test.origin, assertion) | ||||||||||
| require.NoError(t, err) | ||||||||||
|
|
||||||||||
| if validate { | ||||||||||
| // Capture state before validation | ||||||||||
| initialCounter := getSignatureCounter(identity.User.GetLocalAuth().MFA[0]) | ||||||||||
| initialSessionDataCount := len(identity.SessionData) | ||||||||||
| initialUpdatedDevicesCount := len(identity.UpdatedDevices) | ||||||||||
|
|
||||||||||
| // Finish(validateOnly=true) (shouldn't consume) | ||||||||||
| _, err = webLogin.Finish(ctx, user, assertionResp, chalExts, true) | ||||||||||
| require.NoError(t, err) | ||||||||||
|
|
||||||||||
| // Assert state unchanged | ||||||||||
| assert.Equal(t, initialCounter, getSignatureCounter(identity.User.GetLocalAuth().MFA[0])) | ||||||||||
| assert.Len(t, identity.SessionData, initialSessionDataCount) | ||||||||||
| assert.Len(t, identity.UpdatedDevices, initialUpdatedDevicesCount) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // 2nd and last step of the login ceremony. | ||||||||||
| beforeLastUsed := time.Now().Add(-1 * time.Second) | ||||||||||
| loginData, err := webLogin.Finish(ctx, user, assertionResp, &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }) | ||||||||||
| loginData, err := webLogin.Finish(ctx, user, assertionResp, chalExts, false /* validateOnly */) | ||||||||||
| require.NoError(t, err) | ||||||||||
| // Last used time and counter are updated. | ||||||||||
| require.True(t, beforeLastUsed.Before(loginData.Device.LastUsed)) | ||||||||||
|
|
@@ -169,7 +187,10 @@ func TestLoginFlow_BeginFinish(t *testing.T) { | |||||||||
| } | ||||||||||
| // Did we delete the challenge? | ||||||||||
| require.Empty(t, identity.SessionData) | ||||||||||
| }) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| t.Run(test.name+"/Finish", func(t *testing.T) { runTest(t, false) }) | ||||||||||
| t.Run(test.name+"/ValidateAndFinish", func(t *testing.T) { runTest(t, true) }) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -287,21 +308,37 @@ func TestLoginFlow_Finish_errors(t *testing.T) { | |||||||||
| name string | ||||||||||
| user string | ||||||||||
| createResp func() *wantypes.CredentialAssertionResponse | ||||||||||
| exts *mfav1.ChallengeExtensions | ||||||||||
| }{ | ||||||||||
| { | ||||||||||
| name: "NOK empty user", | ||||||||||
| user: "", | ||||||||||
| createResp: func() *wantypes.CredentialAssertionResponse { return okResp }, | ||||||||||
| exts: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
|
Comment on lines
+317
to
+319
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull into
Suggested change
|
||||||||||
| }, | ||||||||||
| { | ||||||||||
| name: "NOK nil resp", | ||||||||||
| user: user, | ||||||||||
| createResp: func() *wantypes.CredentialAssertionResponse { return nil }, | ||||||||||
| exts: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| name: "NOK empty resp", | ||||||||||
| user: user, | ||||||||||
| createResp: func() *wantypes.CredentialAssertionResponse { return &wantypes.CredentialAssertionResponse{} }, | ||||||||||
| exts: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| name: "NOK nil required extensions", | ||||||||||
| user: user, | ||||||||||
| createResp: func() *wantypes.CredentialAssertionResponse { return okResp }, | ||||||||||
| exts: nil, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| name: "NOK assertion with bad origin", | ||||||||||
|
|
@@ -318,6 +355,9 @@ func TestLoginFlow_Finish_errors(t *testing.T) { | |||||||||
| require.NoError(t, err) | ||||||||||
| return resp | ||||||||||
| }, | ||||||||||
| exts: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| name: "NOK assertion with bad RPID", | ||||||||||
|
|
@@ -336,6 +376,9 @@ func TestLoginFlow_Finish_errors(t *testing.T) { | |||||||||
| require.NoError(t, err) | ||||||||||
| return resp | ||||||||||
| }, | ||||||||||
| exts: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| name: "NOK assertion signed by unknown device", | ||||||||||
|
|
@@ -358,6 +401,9 @@ func TestLoginFlow_Finish_errors(t *testing.T) { | |||||||||
| require.NoError(t, err) | ||||||||||
| return resp | ||||||||||
| }, | ||||||||||
| exts: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| name: "NOK assertion with invalid signature", | ||||||||||
|
|
@@ -378,15 +424,19 @@ func TestLoginFlow_Finish_errors(t *testing.T) { | |||||||||
| require.NoError(t, err) | ||||||||||
| return resp | ||||||||||
| }, | ||||||||||
| exts: &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| for _, test := range tests { | ||||||||||
| t.Run(test.name, func(t *testing.T) { | ||||||||||
| _, err := webLogin.Finish(ctx, test.user, test.createResp(), &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }) | ||||||||||
| runTest := func(t *testing.T, validateOnly bool) { | ||||||||||
| _, err := webLogin.Finish(ctx, test.user, test.createResp(), test.exts, validateOnly) | ||||||||||
| require.Error(t, err) | ||||||||||
| }) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| t.Run(test.name+"/Finish", func(t *testing.T) { runTest(t, false) }) | ||||||||||
| t.Run(test.name+"/ValidateOnly", func(t *testing.T) { runTest(t, true) }) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -633,7 +683,7 @@ func TestCredentialRPID(t *testing.T) { | |||||||||
|
|
||||||||||
| loginData, err := webLogin.Finish(ctx, user, car, &mfav1.ChallengeExtensions{ | ||||||||||
| Scope: mfav1.ChallengeScope_CHALLENGE_SCOPE_LOGIN, | ||||||||||
| }) | ||||||||||
| }, false) | ||||||||||
| require.NoError(t, err, "Finish failed") | ||||||||||
| assert.Equal(t, rpID, loginData.Device.GetWebauthn().CredentialRpId, "CredentialRpId mismatch") | ||||||||||
| }) | ||||||||||
|
|
@@ -893,7 +943,7 @@ func TestLoginFlow_scopeAndReuse(t *testing.T) { | |||||||||
| assertionResp, err := webKey.SignAssertion(webOrigin, assertion) | ||||||||||
| require.NoError(t, err) | ||||||||||
|
|
||||||||||
| loginData, err := webLogin.Finish(ctx, user, assertionResp, test.requiredExt) | ||||||||||
| loginData, err := webLogin.Finish(ctx, user, assertionResp, test.requiredExt, false) | ||||||||||
| if test.assertErr != nil { | ||||||||||
| test.assertErr(t, err) | ||||||||||
| return | ||||||||||
|
|
@@ -1061,7 +1111,7 @@ func TestLoginFlow_userVerification(t *testing.T) { | |||||||||
| assertionResp, err := test.dev.SignAssertion(origin, assertion) | ||||||||||
| require.NoError(t, err, "dev.SignAssertion") | ||||||||||
|
|
||||||||||
| _, err = lf.Finish(ctx, user, assertionResp, test.requiredExts) | ||||||||||
| _, err = lf.Finish(ctx, user, assertionResp, test.requiredExts, false) | ||||||||||
| if test.wantErr != "" { | ||||||||||
| assert.ErrorContains(t, err, test.wantErr, "lf.Finish error mismatch") | ||||||||||
| } else { | ||||||||||
|
|
@@ -1258,6 +1308,30 @@ func newFakeIdentity(user string, devices ...*types.MFADevice) *fakeIdentity { | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (f *fakeIdentity) clone() *fakeIdentity { | ||||||||||
| // Deep copy the User | ||||||||||
| userCopy := &types.UserV2{ | ||||||||||
| Metadata: f.User.Metadata, | ||||||||||
| Spec: types.UserSpecV2{ | ||||||||||
| LocalAuth: &types.LocalAuthSecrets{ | ||||||||||
| MFA: slices.Clone(f.User.GetLocalAuth().MFA), | ||||||||||
| Webauthn: f.User.GetLocalAuth().Webauthn, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Copy SessionData map | ||||||||||
| sessionDataCopy := make(map[string]*wantypes.SessionData, len(f.SessionData)) | ||||||||||
| maps.Copy(sessionDataCopy, f.SessionData) | ||||||||||
|
|
||||||||||
| return &fakeIdentity{ | ||||||||||
| User: userCopy, | ||||||||||
| MappedUser: f.MappedUser, | ||||||||||
| UpdatedDevices: slices.Clone(f.UpdatedDevices), | ||||||||||
| SessionData: sessionDataCopy, | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (f *fakeIdentity) GetMFADevices(ctx context.Context, user string, withSecrets bool) ([]*types.MFADevice, error) { | ||||||||||
| // Return a defensive copy of the slice, the caller might modify it. | ||||||||||
| return slices.Clone(f.User.GetLocalAuth().MFA), nil | ||||||||||
|
|
||||||||||
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.
Just to clarify, I was OK keeping the public variants of Finish and Validate. The refactor was more about having them both delegate to the private
finishfunc.