-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[PM-33417] Enforce single use Webauthn assertion #7439
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
Draft
ike-kottlowski
wants to merge
17
commits into
main
Choose a base branch
from
auth/pm-33417/enforce-single-use-webauthn-assertion
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f57ace2
feat: create cache for webauthn login
ike-kottlowski 4ea9549
feat: implement cache in options and assertion flows
ike-kottlowski 3dcf006
test: add tests for Webauthn Challenge cache
ike-kottlowski 3b7e30e
test: add tests for cache implementation
ike-kottlowski 3456478
chore: dotnet format
ike-kottlowski c207613
Merge branch 'main' into auth/pm-33417/enforce-single-use-webauthn-asβ¦
ike-kottlowski 337646d
Merge branch 'main' into auth/pm-33417/enforce-single-use-webauthn-asβ¦
ike-kottlowski 7e463aa
chore: rename cache provider
ike-kottlowski 943b187
feat: extend to webauthn UpdateKeySet and tests
ike-kottlowski 648aaab
Merge branch 'auth/pm-33417/enforce-single-use-webauthn-assertion' ofβ¦
ike-kottlowski 7ab33ad
feat: read and write from commands rather than command call sites
ike-kottlowski 456668d
test: fix tests
ike-kottlowski dbc6234
chore: dotnet format
ike-kottlowski 2783104
Merge branch 'main' into auth/pm-33417/enforce-single-use-webauthn-asβ¦
ike-kottlowski e892c37
feat: change caching provider to store used challenges to better enfoβ¦
ike-kottlowski e599255
feat: change call sites to match new caching pattern
ike-kottlowski 7dc1f27
Merge branch 'auth/pm-33417/enforce-single-use-webauthn-assertion' ofβ¦
ike-kottlowski 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
17 changes: 17 additions & 0 deletions
17
src/Core/Auth/UserFeatures/WebAuthnLogin/IWebAuthnChallengeCacheProvider.cs
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,17 @@ | ||
| namespace Bit.Core.Auth.UserFeatures.WebAuthnLogin; | ||
|
|
||
| /// <summary> | ||
| /// Enforces single-use semantics for WebAuthn assertion challenges per the W3C WebAuthn | ||
| /// specification (Β§13.4.3). Tracks used challenges so that each challenge can only be | ||
| /// validated once. | ||
| /// </summary> | ||
| public interface IWebAuthnChallengeCacheProvider | ||
| { | ||
| /// <summary> | ||
| /// Attempts to mark a challenge as used. Returns <c>true</c> if this is the first use | ||
| /// (challenge was not in cache and has now been saved). Returns <c>false</c> if the | ||
| /// challenge was already marked as used (found in cache). | ||
| /// </summary> | ||
| /// <param name="challenge">The challenge bytes from <see cref="Fido2NetLib.AssertionOptions.Challenge"/>.</param> | ||
| Task<bool> TryMarkChallengeAsUsedAsync(byte[] challenge); | ||
| } |
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
2 changes: 1 addition & 1 deletion
2
...atures/WebAuthnLogin/Implementations/GetWebAuthnLoginCredentialAssertionOptionsCommand.cs
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
33 changes: 33 additions & 0 deletions
33
src/Core/Auth/UserFeatures/WebAuthnLogin/Implementations/WebAuthnChallengeCacheProvider.cs
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,33 @@ | ||
| using Bit.Core.Utilities; | ||
| using Microsoft.Extensions.Caching.Distributed; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Bit.Core.Auth.UserFeatures.WebAuthnLogin.Implementations; | ||
|
|
||
| internal class WebAuthnChallengeCacheProvider( | ||
| [FromKeyedServices("persistent")] IDistributedCache distributedCache) : IWebAuthnChallengeCacheProvider | ||
| { | ||
| private const string _cacheKeyPrefix = "WebAuthnLoginAssertion_"; | ||
| private static readonly DistributedCacheEntryOptions _cacheOptions = new() | ||
| { | ||
| AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(17) | ||
| }; | ||
|
|
||
| private readonly IDistributedCache _distributedCache = distributedCache; | ||
|
|
||
| public async Task<bool> TryMarkChallengeAsUsedAsync(byte[] challenge) | ||
| { | ||
| var cacheKey = BuildCacheKey(challenge); | ||
| var cached = await _distributedCache.GetAsync(cacheKey); | ||
| if (cached != null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| await _distributedCache.SetAsync(cacheKey, [1], _cacheOptions); | ||
| return true; | ||
| } | ||
|
|
||
| private static string BuildCacheKey(byte[] challenge) | ||
| => $"{_cacheKeyPrefix}{CoreHelpers.Base64UrlEncode(challenge)}"; | ||
| } | ||
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
66 changes: 66 additions & 0 deletions
66
test/Core.Test/Auth/UserFeatures/WebAuthnLogin/WebAuthnChallengeCacheProviderTests.cs
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,66 @@ | ||
| using Bit.Core.Auth.UserFeatures.WebAuthnLogin.Implementations; | ||
| using Bit.Core.Utilities; | ||
| using Bit.Test.Common.AutoFixture; | ||
| using Bit.Test.Common.AutoFixture.Attributes; | ||
| using Microsoft.Extensions.Caching.Distributed; | ||
| using NSubstitute; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Core.Test.Auth.UserFeatures.WebAuthnLogin; | ||
|
|
||
| [SutProviderCustomize] | ||
| public class WebAuthnChallengeCacheProviderTests | ||
| { | ||
| [Theory, BitAutoData] | ||
| internal async Task TryMarkChallengeAsUsedAsync_FirstUse_SavesAndReturnsTrue( | ||
| SutProvider<WebAuthnChallengeCacheProvider> sutProvider) | ||
| { | ||
| // Arrange | ||
| var challenge = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; | ||
| var expectedKey = $"WebAuthnLoginAssertion_{CoreHelpers.Base64UrlEncode(challenge)}"; | ||
|
|
||
| sutProvider.GetDependency<IDistributedCache>() | ||
| .GetAsync(expectedKey, Arg.Any<CancellationToken>()) | ||
| .Returns((byte[])null); | ||
|
|
||
| // Act | ||
| var result = await sutProvider.Sut.TryMarkChallengeAsUsedAsync(challenge); | ||
|
|
||
| // Assert | ||
| Assert.True(result); | ||
| await sutProvider.GetDependency<IDistributedCache>() | ||
| .Received(1) | ||
| .SetAsync( | ||
| expectedKey, | ||
| Arg.Is<byte[]>(b => b.Length == 1 && b[0] == 1), | ||
| Arg.Is<DistributedCacheEntryOptions>(o => | ||
| o.AbsoluteExpirationRelativeToNow == TimeSpan.FromMinutes(17)), | ||
| Arg.Any<CancellationToken>()); | ||
| } | ||
|
|
||
| [Theory, BitAutoData] | ||
| internal async Task TryMarkChallengeAsUsedAsync_AlreadyUsed_ReturnsFalseAndDoesNotSave( | ||
| SutProvider<WebAuthnChallengeCacheProvider> sutProvider) | ||
| { | ||
| // Arrange | ||
| var challenge = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; | ||
| var expectedKey = $"WebAuthnLoginAssertion_{CoreHelpers.Base64UrlEncode(challenge)}"; | ||
|
|
||
| sutProvider.GetDependency<IDistributedCache>() | ||
| .GetAsync(expectedKey, Arg.Any<CancellationToken>()) | ||
| .Returns(new byte[] { 1 }); | ||
|
|
||
| // Act | ||
| var result = await sutProvider.Sut.TryMarkChallengeAsUsedAsync(challenge); | ||
|
|
||
| // Assert | ||
| Assert.False(result); | ||
| await sutProvider.GetDependency<IDistributedCache>() | ||
| .DidNotReceive() | ||
| .SetAsync( | ||
| Arg.Any<string>(), | ||
| Arg.Any<byte[]>(), | ||
| Arg.Any<DistributedCacheEntryOptions>(), | ||
| Arg.Any<CancellationToken>()); | ||
| } | ||
| } |
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.
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.
Details and fix
The
GetAsyncfollowed byRemoveAsyncis not atomic. Two concurrent requests presenting the same challenge token can both read the cache entry before either removes it, allowing the challenge to be used twice. This is a classic TOCTOU (time-of-check-time-of-use) issue that directly undermines the W3C section 13.4.3 single-use requirement this PR aims to enforce.The
IDistributedCacheinterface does not expose an atomic get-and-delete operation. Consider either:Use the cache value as a lock: Call
SetAsyncwith a "consumed" sentinel value (and a short TTL) before returningtrue. The check becomes: read the value, and only proceed if it equals the "available" sentinel (not "consumed"). This shrinks the race window but does not eliminate it.Use a lower-level API with atomic semantics: If the backing store is Redis, use
GETDELor a Lua script viaIConnectionMultiplexerdirectly. If CosmosDB, use an optimistic-concurrency conditional delete (ETag-based). This fully eliminates the race.Accept and document the residual risk: If the data-protector token that wraps the challenge already limits replay (short TTL, HMAC-bound), document that the cache is a defense-in-depth layer with a known non-atomic window, and note that a future iteration should close it.