fix(persistence): use ThrowsAny in tamper test; keep AesGcm exception subclass visible

This commit is contained in:
Claude
2026-08-28 12:08:37 +00:00
parent 62efc3260f
commit caa1e6e834
2 changed files with 2 additions and 11 deletions

View File

@@ -39,16 +39,7 @@ public sealed class EncryptionService
var cipher = wire.AsSpan(NonceSize, wire.Length - NonceSize - TagSize);
var plain = new byte[cipher.Length];
using var aes = new AesGcm(_key, TagSize);
try
{
aes.Decrypt(nonce, cipher, tag, plain);
}
catch (CryptographicException ex) when (ex.GetType() != typeof(CryptographicException))
{
// .NET throws the more specific AuthenticationTagMismatchException on tamper;
// normalize to the base type so callers can catch CryptographicException.
throw new CryptographicException("Decryption failed: authentication tag mismatch.", ex);
}
return System.Text.Encoding.UTF8.GetString(plain);
}
}

View File

@@ -31,7 +31,7 @@ public class EncryptionServiceTests
var bytes = Convert.FromBase64String(wire);
bytes[^1] ^= 0x01; // flip a bit in the tag
var tampered = Convert.ToBase64String(bytes);
Assert.Throws<CryptographicException>(() => svc.Decrypt(tampered));
Assert.ThrowsAny<CryptographicException>(() => svc.Decrypt(tampered));
}
[Fact]