From 8be604feac126fa76177d68dbe09bc42410dfc35 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Fri, 26 Jan 2024 10:44:39 +0100 Subject: [PATCH] [PM-5731] feat: add unknown error handling --- .../Services/Fido2AuthenticatorService.cs | 15 +++++--- .../Fido2AuthenticatorMakeCredentialTests.cs | 37 +++++++++++++------ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/Core/Services/Fido2AuthenticatorService.cs b/src/Core/Services/Fido2AuthenticatorService.cs index a48f0cfd7..15a5a23cd 100644 --- a/src/Core/Services/Fido2AuthenticatorService.cs +++ b/src/Core/Services/Fido2AuthenticatorService.cs @@ -83,9 +83,14 @@ namespace Bit.Core.Services var reencrypted = await _cipherService.EncryptAsync(cipher); await _cipherService.SaveWithServerAsync(reencrypted); credentialId = fido2Credential.CredentialId; - } catch { + } catch (NotAllowedError) { throw; - // throw new NotImplementedException(); + } catch (Exception e) { + _logService.Error( + $"[Fido2Authenticator] Unknown error occured during attestation: {e.Message}" + ); + + throw new UnknownError(); } return new Fido2AuthenticatorMakeCredentialResult @@ -182,9 +187,9 @@ namespace Bit.Core.Services AuthenticatorData = authenticatorData, Signature = signature }; - } catch { - _logService.Info( - "[Fido2Authenticator] Aborting because no matching credentials were found in the vault." + } catch (Exception e) { + _logService.Error( + $"[Fido2Authenticator] Unknown error occured during assertion: {e.Message}" ); throw new UnknownError(); diff --git a/test/Core.Test/Services/Fido2AuthenticatorMakeCredentialTests.cs b/test/Core.Test/Services/Fido2AuthenticatorMakeCredentialTests.cs index 5a86cad2a..8d8a20f32 100644 --- a/test/Core.Test/Services/Fido2AuthenticatorMakeCredentialTests.cs +++ b/test/Core.Test/Services/Fido2AuthenticatorMakeCredentialTests.cs @@ -365,20 +365,33 @@ namespace Bit.Core.Test.Services await Assert.ThrowsAsync(() => sutProvider.Sut.MakeCredentialAsync(mParams)); } - // /** Spec: If any error occurred while creating the new credential object, return an error code equivalent to "UnknownError" and terminate the operation. */ - // it("should throw unkown error if creation fails", async () => { - // const _encryptedCipher = Symbol(); - // userInterfaceSession.confirmNewCredential.mockResolvedValue({ - // cipherId: existingCipher.id, - // userVerified: false, - // }); - // cipherService.encrypt.mockResolvedValue(_encryptedCipher as unknown as Cipher); - // cipherService.updateWithServer.mockRejectedValue(new Error("Internal error")); + [Theory] + [InlineCustomAutoData(new[] { typeof(SutProviderCustomization) })] + public async Task MakeCredentialAsync_ThrowsUnknownError_SavingCipherFails(SutProvider sutProvider, Fido2AuthenticatorMakeCredentialParams mParams) + { + // Common Arrange + mParams.CredTypesAndPubKeyAlgs = [ + new PublicKeyCredentialAlgorithmDescriptor { + Type = "public-key", + Algorithm = -7 // ES256 + } + ]; + mParams.RpEntity = new PublicKeyCredentialRpEntity { Id = "bitwarden.com" }; + mParams.RequireUserVerification = false; + sutProvider.GetDependency().EcdsaGenerateKeyPairAsync(Arg.Any()) + .Returns((RandomBytes(32), RandomBytes(32))); - // const result = async () => await authenticator.makeCredential(params, tab); + // Arrange + sutProvider.GetDependency().GetAsync(Arg.Is(_encryptedCipher.Id)).Returns(_encryptedCipher); + sutProvider.GetDependency().ConfirmNewCredentialAsync(Arg.Any()).Returns(new Fido2ConfirmNewCredentialResult { + CipherId = _encryptedCipher.Id, + UserVerified = false + }); + sutProvider.GetDependency().SaveWithServerAsync(Arg.Any()).Throws(new Exception("Error")); - // await expect(result).rejects.toThrowError(Fido2AuthenticatorErrorCode.Unknown); - // }); + // Act & Assert + await Assert.ThrowsAsync(() => sutProvider.Sut.MakeCredentialAsync(mParams)); + } #endregion