Skip to content

Add empty string check for recovery code #61926

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

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ await Assert.ThrowsAsync<ArgumentNullException>("user",
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetTwoFactorEnabledAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user",
async () => await store.SetTwoFactorEnabledAsync(null, true));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.RedeemCodeAsync(user: null, code: "fake", default));
await Assert.ThrowsAsync<ArgumentNullException>("code", async () => await store.RedeemCodeAsync(new IdentityUser("fake"), code: null, default));
await Assert.ThrowsAsync<ArgumentException>("code", async () => await store.RedeemCodeAsync(new IdentityUser("fake"), code: "", default));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetAccessFailedCountAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.GetLockoutEnabledAsync(null));
await Assert.ThrowsAsync<ArgumentNullException>("user", async () => await store.SetLockoutEnabledAsync(null, false));
Expand Down
2 changes: 1 addition & 1 deletion src/Identity/Extensions.Stores/src/UserStoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ public virtual async Task<bool> RedeemCodeAsync(TUser user, string code, Cancell
ThrowIfDisposed();

ArgumentNullThrowHelper.ThrowIfNull(user);
ArgumentNullThrowHelper.ThrowIfNull(code);
ArgumentNullThrowHelper.ThrowIfNullOrEmpty(code);

var mergedCodes = await GetTokenAsync(user, InternalLoginProvider, RecoveryCodeTokenName, cancellationToken).ConfigureAwait(false) ?? "";
var splitCodes = mergedCodes.Split(';');
Expand Down
23 changes: 23 additions & 0 deletions src/Shared/ThrowHelpers/ArgumentNullThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ public static void ThrowIfNull(
#endif
}

/// <summary>Throws an <see cref="ArgumentException"/> if <paramref name="argument"/> is null or empty.</summary>
/// <param name="argument">The <see cref="string"/> argument to validate as non-null and non-empty.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
public static void ThrowIfNullOrEmpty(
#if INTERNAL_NULLABLE_ATTRIBUTES || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
[NotNull]
#endif
string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if !NET7_0_OR_GREATER || NETSTANDARD || NETFRAMEWORK
if (argument is null)
{
Throw(paramName);
}
else if (argument.Length == 0)
{
throw new ArgumentException("Must not be null or empty", paramName);
}
#else
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
#endif
}

#if !NET7_0_OR_GREATER || NETSTANDARD || NETFRAMEWORK
#if INTERNAL_NULLABLE_ATTRIBUTES || NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
[DoesNotReturn]
Expand Down
Loading