Skip to content

[PM-6631] Handle Fido2VerificationException during passkey attestation and assertion#59

Open
lizard-boy wants to merge 8 commits into
mainfrom
auth/pm-6631/handle-webauthn-creation-exception
Open

[PM-6631] Handle Fido2VerificationException during passkey attestation and assertion#59
lizard-boy wants to merge 8 commits into
mainfrom
auth/pm-6631/handle-webauthn-creation-exception

Conversation

@lizard-boy

@lizard-boy lizard-boy commented Oct 19, 2024

Copy link
Copy Markdown

Type of change

- [X] Bug fix
- [ ] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

In bitwarden#3615 we handled the Fido2VerificationException when asserting a WebAuthn credential for 2FA.

In this PR, we address the MakeNewCredentialAsync methods similarly, as well as the MakeAssertionAsync when asserting a WebAuthn credential for login, which was missed in bitwarden#3615 .

📓 We have https://bitwarden.atlassian.net/browse/PM-4172 in the backlog to consolidate the implementations, at which point we should consider an abstraction.

Code changes

  • AssertWebAuthnLoginCredentialCommand: Added try/catch around assertion that returns a BadRequestException instead of the unhandled exception returned previously. This will be handled on the client, as it is the pattern already established in the class for communicating assertion errors.
  • CreateWebAuthnLoginCredentialCommand: Added try/catch around attestation that returns false along with a log message. I did this instead of throwing a BadRequestException as this is the pattern already established in this command for handling invalid data. I added a log here as returning false gives no indication of the root cause.
  • UserService: Added try/catch around attestation that returns false along with a log message. I did this instead of throwing a BadRequestException as this is the pattern already established in this command for handling invalid data. I added a log here as returning false gives no indication of the root cause.

Before you submit

  • Please check for formatting errors (dotnet format --verify-no-changes) (required)
  • If making database changes - make sure you also update Entity Framework queries and/or migrations
  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team

Greptile Summary

This pull request addresses exception handling for WebAuthn credential creation and assertion, focusing on improving error management in three key files:

  • Added try/catch block in AssertWebAuthnLoginCredentialCommand.cs to handle Fido2VerificationException during login credential assertion
  • Implemented error handling in CreateWebAuthnLoginCredentialCommand.cs for WebAuthn credential creation, returning false and logging errors on exception
  • Enhanced UserService.cs to catch Fido2VerificationException during WebAuthn registration, logging the error and returning false on failure
  • Centralized BadRequestException throwing in AssertWebAuthnLoginCredentialCommand.cs for consistent error handling

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +50 to +59
Fido2NetLib.Objects.AssertionVerificationResult assertionVerificationResult = null;
try
{
assertionVerificationResult = await _fido2.MakeAssertionAsync(
assertionResponse, options, credentialPublicKey, (uint)credential.Counter, callback);
}
catch (Fido2VerificationException)
{
ThrowInvalidCredentialException();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: The catch block swallows the specific exception details. Consider logging the exception message or type for better diagnostics.

Comment on lines 62 to 63
credential.Counter = (int)assertionVerificationResult.Counter;
await _webAuthnCredentialRepository.ReplaceAsync(credential);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Ensure that assertionVerificationResult is not null before accessing its properties.

Comment on lines +73 to +76
private void ThrowInvalidCredentialException()
{
throw new BadRequestException("Invalid credential.");
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider making this method more flexible by allowing custom error messages or including more context in the exception.

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.

2 participants