Contact: captcha-regio (global/eu) in de token-response en frame-src voor de StrictSite-CSP (CMS-U9 vervolg)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 23:11:07 +01:00
co-authored by Claude Sonnet 5.5
parent 3275092401
commit 5378c21799
15 changed files with 291 additions and 34 deletions
@@ -74,7 +74,7 @@ public class FriendlyCaptchaVerifierTests
{
var handler = FakeCaptchaHandler.Always(HttpStatusCode.OK, """{"success":true}""");
await Verifier(handler, c => c.VerifyUrl = "https://eu.frcapi.com/api/v2/captcha/siteverify")
await Verifier(handler, c => c.Region = "eu")
.VerifyAsync(Token, CancellationToken.None);
handler.Calls.Single().Request.RequestUri!.Host.Should().Be("eu.frcapi.com");
@@ -224,6 +224,63 @@ public class ContactCaptchaOptionsTests
problems.Single().Should().NotContain("key").And.NotContain("insecure");
}
[Fact]
public void TheRegionDefaultsToGlobal_AndDerivesTheVerifyUrl()
{
var o = Valid();
o.ApiEndpoint.Should().Be("global");
o.EffectiveVerifyUrl.Should().Be("https://global.frcapi.com/api/v2/captcha/siteverify");
o.Problems().Should().BeEmpty();
}
[Fact]
public void TheEuRegion_DerivesTheEuVerifyUrl_AndAnnouncesEu()
{
var o = Valid();
o.Region = "EU";
o.ApiEndpoint.Should().Be("eu");
o.EffectiveVerifyUrl.Should().Be("https://eu.frcapi.com/api/v2/captcha/siteverify");
o.Problems().Should().BeEmpty();
}
[Theory]
[InlineData("global", "https://eu.frcapi.com/api/v2/captcha/siteverify")]
[InlineData("eu", "https://global.frcapi.com/api/v2/captcha/siteverify")]
[InlineData("global", "https://evil.example/api/v2/captcha/siteverify")]
public void AVerifyUrlThatDoesNotMatchTheRegion_RefusesToStart(string region, string url)
{
var o = Valid();
o.Region = region;
o.VerifyUrl = url;
o.Problems().Should().ContainSingle().Which.Should().Contain("does not match Contact:Captcha:Region");
}
[Fact]
public void AMatchingVerifyUrl_IsAccepted_AndUsed()
{
var o = Valid();
o.Region = "eu";
o.VerifyUrl = "https://eu.frcapi.com/api/v2/captcha/siteverify";
o.Problems().Should().BeEmpty();
o.EffectiveVerifyUrl.Should().Be("https://eu.frcapi.com/api/v2/captcha/siteverify");
}
[Theory]
[InlineData("us")]
[InlineData("")]
[InlineData("europe")]
public void AnUnknownRegion_RefusesToStart(string region)
{
var o = Valid();
o.Region = region;
o.Problems().Should().ContainSingle().Which.Should().Contain("Contact:Captcha:Region");
}
[Fact]
public void TheProviderNameIsCaseInsensitive()
{
@@ -20,7 +20,7 @@ public sealed class CaptchaContactHostFactory() : ContactHostFactory(new()
["Contact:Captcha:Provider"] = "friendlycaptcha",
["Contact:Captcha:SiteKey"] = "FCMPIPELINEKEY",
["Contact:Captcha:Secret"] = "pipeline-secret-ZXQ",
["Contact:Captcha:VerifyUrl"] = "https://eu.frcapi.com/api/v2/captcha/siteverify",
["Contact:Captcha:Region"] = "eu",
["RateLimiting:ContactForm:PermitLimit"] = "1000",
["RateLimiting:ContactToken:PermitLimit"] = "1000",
})
@@ -72,7 +72,8 @@ public class ContactCaptchaPipelineTests(CaptchaContactHostFactory factory) : IC
var captcha = json.RootElement.GetProperty("captcha");
captcha.GetProperty("provider").GetString().Should().Be("friendlycaptcha");
captcha.GetProperty("siteKey").GetString().Should().Be("FCMPIPELINEKEY");
captcha.EnumerateObject().Should().HaveCount(2);
captcha.GetProperty("apiEndpoint").GetString().Should().Be("eu");
captcha.EnumerateObject().Should().HaveCount(3);
text.Should().NotContain("pipeline-secret-ZXQ").And.NotContain("frcapi.com");
}
@@ -158,6 +159,71 @@ public class ContactCaptchaPipelineTests(CaptchaContactHostFactory factory) : IC
}
}
/// <summary>No Region configured: the default, global.</summary>
public sealed class GlobalCaptchaContactHostFactory() : ContactHostFactory(new()
{
["Contact:Enabled"] = "true",
["Contact:SiteId"] = "pipeline-site",
["Contact:MinSeconds"] = "0",
["Contact:Captcha:Enabled"] = "true",
["Contact:Captcha:Provider"] = "friendlycaptcha",
["Contact:Captcha:SiteKey"] = "FCMGLOBALKEY",
["Contact:Captcha:Secret"] = "pipeline-secret-ZXQ",
})
{
protected override bool OpenCmsSwitch => true;
}
public class GlobalCaptchaRegionPipelineTests(GlobalCaptchaContactHostFactory factory) : IClassFixture<GlobalCaptchaContactHostFactory>
{
[Fact]
public async Task WithoutARegion_TheTokenResponseAnnouncesTheGlobalEndpoint()
{
var response = await factory.CreateClient().GetAsync($"{Http.Route}/token");
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
var captcha = json.RootElement.GetProperty("captcha");
captcha.GetProperty("apiEndpoint").GetString().Should().Be("global");
captcha.GetProperty("siteKey").GetString().Should().Be("FCMGLOBALKEY");
}
}
public class CaptchaOffTokenTests(EnabledContactHostFactory factory) : IClassFixture<EnabledContactHostFactory>
{
[Fact]
public async Task WithTheCaptchaOff_TheTokenResponseHasNoCaptcha()
{
var response = await factory.CreateClient().GetAsync($"{Http.Route}/token");
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
json.RootElement.GetProperty("captcha").ValueKind.Should().Be(JsonValueKind.Null);
}
}
public class CaptchaInconsistentRegionStartupTests
{
private sealed class InconsistentFactory() : ContactHostFactory(new()
{
["Contact:Enabled"] = "true",
["Contact:Captcha:Enabled"] = "true",
["Contact:Captcha:Provider"] = "friendlycaptcha",
["Contact:Captcha:SiteKey"] = "FCMKEY",
["Contact:Captcha:Secret"] = "pipeline-secret-ZXQ",
["Contact:Captcha:Region"] = "global",
["Contact:Captcha:VerifyUrl"] = "https://eu.frcapi.com/api/v2/captcha/siteverify",
});
[Fact]
public void ARegionThatContradictsTheVerifyUrl_RefusesToStart()
{
using var factory = new InconsistentFactory();
var act = () => factory.CreateClient();
act.Should().Throw<Exception>().Where(e => e.ToString().Contains("does not match Contact:Captcha:Region"));
}
}
public class CaptchaStartupTests
{
private sealed class MissingSecretFactory() : ContactHostFactory(new()