-
Notifications
You must be signed in to change notification settings - Fork 8
Share captcha validation policy #1121
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
Open
BenjaminMichaelis
wants to merge
8
commits into
main
Choose a base branch
from
benjaminmichaelis/super-giggle
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dab18ce
Share captcha validation policy
BenjaminMichaelis 23b2eba
Fail closed on missing captcha config
BenjaminMichaelis d360c48
Harden captcha failure handling
BenjaminMichaelis 2a45ad2
Merge remote-tracking branch 'origin/main' into benjaminmichaelis/sup…
BenjaminMichaelis 4d81cbb
fix: Resolve compilation errors in AI integration and identity code
BenjaminMichaelis 780e082
Address PR review comments: deduplication, null safety, behavior docu…
BenjaminMichaelis ebae8b2
fix: address remaining captcha PR review feedback
BenjaminMichaelis d2f8cd8
fix: address remaining PR review feedback
BenjaminMichaelis 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
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
152 changes: 152 additions & 0 deletions
152
EssentialCSharp.Web.Tests/CaptchaValidationServiceTests.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,152 @@ | ||
| using EssentialCSharp.Web.Models; | ||
| using EssentialCSharp.Web.Services; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace EssentialCSharp.Web.Tests; | ||
|
|
||
| public class CaptchaValidationServiceTests | ||
| { | ||
| [Test] | ||
| public async Task ValidateAsync_MissingConfig_RejectsWithoutVerification() | ||
| { | ||
| StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called.")); | ||
| using ServiceProvider serviceProvider = CreateServiceProvider( | ||
| new CaptchaOptions { SecretKey = string.Empty, SiteKey = string.Empty }, | ||
| captchaService); | ||
|
|
||
| ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); | ||
|
|
||
| CaptchaValidationResult result = await validationService.ValidateAsync("token", "127.0.0.1"); | ||
|
|
||
| await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.Disabled); | ||
| await Assert.That(result.ShouldProceed).IsFalse(); | ||
| await Assert.That(captchaService.CallCount).IsEqualTo(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateAsync_MissingSiteKey_RejectsWithoutVerification() | ||
| { | ||
| StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called.")); | ||
| using ServiceProvider serviceProvider = CreateServiceProvider( | ||
| new CaptchaOptions { SecretKey = "secret", SiteKey = string.Empty }, | ||
| captchaService); | ||
|
|
||
| ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); | ||
|
|
||
| CaptchaValidationResult result = await validationService.ValidateAsync("token", "127.0.0.1"); | ||
|
|
||
| await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.Disabled); | ||
| await Assert.That(result.ShouldProceed).IsFalse(); | ||
| await Assert.That(captchaService.CallCount).IsEqualTo(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateAsync_MissingSecretKey_RejectsWithoutVerification() | ||
| { | ||
| StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called.")); | ||
| using ServiceProvider serviceProvider = CreateServiceProvider( | ||
| new CaptchaOptions { SecretKey = string.Empty, SiteKey = "sitekey" }, | ||
| captchaService); | ||
|
|
||
| ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); | ||
|
|
||
| CaptchaValidationResult result = await validationService.ValidateAsync("token", "127.0.0.1"); | ||
|
|
||
| await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.Disabled); | ||
| await Assert.That(result.ShouldProceed).IsFalse(); | ||
| await Assert.That(captchaService.CallCount).IsEqualTo(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateAsync_MissingToken_ReturnsMissingToken() | ||
| { | ||
| StubCaptchaService captchaService = new((_, _, _) => throw new InvalidOperationException("Verifier should not be called.")); | ||
| using ServiceProvider serviceProvider = CreateServiceProvider( | ||
| new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, | ||
| captchaService); | ||
|
|
||
| ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); | ||
|
|
||
| CaptchaValidationResult result = await validationService.ValidateAsync(string.Empty, "127.0.0.1"); | ||
|
|
||
| await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.MissingToken); | ||
| await Assert.That(result.ShouldProceed).IsFalse(); | ||
| await Assert.That(captchaService.CallCount).IsEqualTo(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateAsync_Unavailable_ReturnsUnavailable() | ||
| { | ||
| StubCaptchaService captchaService = new((_, _, _) => Task.FromResult<HCaptchaResult?>(null)); | ||
| using ServiceProvider serviceProvider = CreateServiceProvider( | ||
| new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, | ||
| captchaService); | ||
|
|
||
| ICaptchaValidationService validationService = serviceProvider.GetRequiredService<ICaptchaValidationService>(); | ||
|
|
||
| CaptchaValidationResult result = await validationService.ValidateAsync("token", "127.0.0.1"); | ||
|
|
||
| await Assert.That(result.Outcome).IsEqualTo(CaptchaValidationOutcome.Unavailable); | ||
| await Assert.That(result.ShouldProceed).IsFalse(); | ||
| await Assert.That(captchaService.CallCount).IsEqualTo(1); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ValidateAsync_InvalidAndValid_ReturnExpectedOutcome() | ||
| { | ||
| StubCaptchaService invalidCaptchaService = new((_, _, _) => Task.FromResult<HCaptchaResult?>(new HCaptchaResult | ||
| { | ||
| Success = false, | ||
| ErrorCodes = ["invalid-input-response"] | ||
| })); | ||
| using ServiceProvider invalidProvider = CreateServiceProvider( | ||
| new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, | ||
| invalidCaptchaService); | ||
|
|
||
| ICaptchaValidationService invalidValidationService = invalidProvider.GetRequiredService<ICaptchaValidationService>(); | ||
| CaptchaValidationResult invalidResult = await invalidValidationService.ValidateAsync("token", "127.0.0.1"); | ||
|
|
||
| await Assert.That(invalidResult.Outcome).IsEqualTo(CaptchaValidationOutcome.Invalid); | ||
| await Assert.That(invalidResult.Response).IsNotNull(); | ||
| await Assert.That(invalidResult.ShouldProceed).IsFalse(); | ||
|
|
||
| StubCaptchaService validCaptchaService = new((_, _, _) => Task.FromResult<HCaptchaResult?>(new HCaptchaResult | ||
| { | ||
| Success = true | ||
| })); | ||
| using ServiceProvider validProvider = CreateServiceProvider( | ||
| new CaptchaOptions { SecretKey = "secret", SiteKey = "sitekey" }, | ||
| validCaptchaService); | ||
|
|
||
| ICaptchaValidationService validValidationService = validProvider.GetRequiredService<ICaptchaValidationService>(); | ||
| CaptchaValidationResult validResult = await validValidationService.ValidateAsync("token", "127.0.0.1"); | ||
|
|
||
| await Assert.That(validResult.Outcome).IsEqualTo(CaptchaValidationOutcome.Valid); | ||
| await Assert.That(validResult.ShouldProceed).IsTrue(); | ||
| await Assert.That(validCaptchaService.CallCount).IsEqualTo(1); | ||
| } | ||
|
|
||
| private static ServiceProvider CreateServiceProvider(CaptchaOptions options, ICaptchaService captchaService) | ||
| { | ||
| ServiceCollection services = new(); | ||
| services.AddSingleton(Options.Create(options)); | ||
| services.AddSingleton(captchaService); | ||
| services.AddSingleton<ICaptchaValidationService, CaptchaValidationService>(); | ||
| return services.BuildServiceProvider(); | ||
| } | ||
|
|
||
| private sealed class StubCaptchaService(Func<string?, string?, CancellationToken, Task<HCaptchaResult?>> verifyAsync) : ICaptchaService | ||
| { | ||
| public int CallCount { get; private set; } | ||
|
|
||
| public Task<HCaptchaResult?> VerifyAsync(string? response, CancellationToken cancellationToken = default) | ||
| => VerifyAsync(response, remoteIp: null, cancellationToken); | ||
|
|
||
| public async Task<HCaptchaResult?> VerifyAsync(string? response, string? remoteIp, CancellationToken cancellationToken = default) | ||
| { | ||
| CallCount++; | ||
| return await verifyAsync(response, remoteIp, 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
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.
Uh oh!
There was an error while loading. Please reload this page.