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:
@@ -89,7 +89,9 @@ public sealed class ContactOptionsValidator : Microsoft.Extensions.Options.IVali
|
||||
public class ContactCaptchaOptions
|
||||
{
|
||||
public const string FriendlyCaptcha = "friendlycaptcha";
|
||||
public const string DefaultVerifyUrl = "https://global.frcapi.com/api/v2/captcha/siteverify";
|
||||
public const string Global = "global";
|
||||
public const string Eu = "eu";
|
||||
private const string VerifyPath = "/api/v2/captcha/siteverify";
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
@@ -102,8 +104,24 @@ public class ContactCaptchaOptions
|
||||
/// <summary>Server-side API key; supply it through the environment (<c>Contact__Captcha__Secret</c>) or a secrets manager, never committed.</summary>
|
||||
public string? Secret { get; set; }
|
||||
|
||||
/// <summary>Verify endpoint. Global by default; for the EU-only endpoint use <c>https://eu.frcapi.com/api/v2/captcha/siteverify</c>.</summary>
|
||||
public string VerifyUrl { get; set; } = DefaultVerifyUrl;
|
||||
/// <summary>
|
||||
/// Which Friendly Captcha endpoint this site uses: <c>global</c> (default) or <c>eu</c> (the dedicated EU endpoint,
|
||||
/// a paid plan). It decides the verify host here and is announced to the widget as <c>apiEndpoint</c>, so the
|
||||
/// website needs no rebuild to switch.
|
||||
/// </summary>
|
||||
public string Region { get; set; } = Global;
|
||||
|
||||
/// <summary>Optional override of the verify URL; empty = derived from <see cref="Region"/>. If set it must match the region.</summary>
|
||||
public string? VerifyUrl { get; set; }
|
||||
|
||||
/// <summary><c>global</c> or <c>eu</c>, normalised (an unknown value falls back to <c>global</c>; start-up validation rejects it first).</summary>
|
||||
public string ApiEndpoint =>
|
||||
string.Equals(Region?.Trim(), Eu, StringComparison.OrdinalIgnoreCase) ? Eu : Global;
|
||||
|
||||
private string RegionHost => ApiEndpoint == Eu ? "eu.frcapi.com" : "global.frcapi.com";
|
||||
|
||||
public string EffectiveVerifyUrl =>
|
||||
string.IsNullOrWhiteSpace(VerifyUrl) ? $"https://{RegionHost}{VerifyPath}" : VerifyUrl.Trim();
|
||||
|
||||
/// <summary>A provider that does not answer within this time counts as passed (fail-open).</summary>
|
||||
public int TimeoutSeconds { get; set; } = 3;
|
||||
@@ -119,8 +137,16 @@ public class ContactCaptchaOptions
|
||||
if (!IsFriendlyCaptcha) problems.Add("Contact:Captcha:Provider must be 'friendlycaptcha'.");
|
||||
if (string.IsNullOrWhiteSpace(SiteKey)) problems.Add("Contact:Captcha:SiteKey is required while the captcha is enabled.");
|
||||
if (string.IsNullOrWhiteSpace(Secret)) problems.Add("Contact:Captcha:Secret is required while the captcha is enabled (environment variable Contact__Captcha__Secret).");
|
||||
if (!Uri.TryCreate(VerifyUrl, UriKind.Absolute, out var uri) || uri.Scheme != Uri.UriSchemeHttps)
|
||||
problems.Add("Contact:Captcha:VerifyUrl must be an absolute https URL.");
|
||||
var region = Region?.Trim();
|
||||
if (!string.Equals(region, Global, StringComparison.OrdinalIgnoreCase) && !string.Equals(region, Eu, StringComparison.OrdinalIgnoreCase))
|
||||
problems.Add("Contact:Captcha:Region must be 'global' or 'eu'.");
|
||||
if (!string.IsNullOrWhiteSpace(VerifyUrl))
|
||||
{
|
||||
if (!Uri.TryCreate(VerifyUrl.Trim(), UriKind.Absolute, out var uri) || uri.Scheme != Uri.UriSchemeHttps)
|
||||
problems.Add("Contact:Captcha:VerifyUrl must be an absolute https URL.");
|
||||
else if (!string.Equals(uri.Host, RegionHost, StringComparison.OrdinalIgnoreCase) && problems.All(p => !p.Contains(":Region")))
|
||||
problems.Add("Contact:Captcha:VerifyUrl does not match Contact:Captcha:Region (leave VerifyUrl empty to derive it).");
|
||||
}
|
||||
if (TimeoutSeconds is < 1 or > 30) problems.Add("Contact:Captcha:TimeoutSeconds must be between 1 and 30.");
|
||||
return problems;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class ContactMessagesController(
|
||||
var settings = options.Value;
|
||||
// Only a complete, supported captcha setup is announced; the secret and the verify URL are never part of a response.
|
||||
var captcha = settings.Captcha is { Enabled: true, IsFriendlyCaptcha: true } c && !string.IsNullOrWhiteSpace(c.SiteKey)
|
||||
? new ContactCaptchaInfo(ContactCaptchaOptions.FriendlyCaptcha, c.SiteKey!)
|
||||
? new ContactCaptchaInfo(ContactCaptchaOptions.FriendlyCaptcha, c.SiteKey!, c.ApiEndpoint)
|
||||
: null;
|
||||
|
||||
return Ok(new ContactTokenResponse(tokens.Issue(), settings.MinSeconds, captcha));
|
||||
|
||||
@@ -17,7 +17,8 @@ public record ContactMessageRequest(
|
||||
string? CaptchaToken);
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public record ContactCaptchaInfo(string Provider, string SiteKey);
|
||||
/// <summary>What the website widget needs. <c>ApiEndpoint</c> is <c>global</c> or <c>eu</c> (Friendly Captcha data region).</summary>
|
||||
public record ContactCaptchaInfo(string Provider, string SiteKey, string ApiEndpoint = "global");
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public record ContactTokenResponse(string Token, int MinSeconds, ContactCaptchaInfo? Captcha);
|
||||
|
||||
@@ -32,7 +32,7 @@ public sealed class ContactOpenApiDocumentTransformer : IOpenApiDocumentTransfor
|
||||
if (document.Paths.TryGetValue(TokenPath, out var token) && token.Operations is not null
|
||||
&& token.Operations.TryGetValue(HttpMethod.Get, out var getToken))
|
||||
{
|
||||
getToken.Description = "Returns a signed form token and the minimum number of seconds before a submission counts as human. 'captcha' is null while the captcha is off; otherwise it is { provider: \"friendlycaptcha\", siteKey } (Friendly Captcha v2), which the website widget needs. Nothing else about the captcha (secret, verify URL) is ever returned.";
|
||||
getToken.Description = "Returns a signed form token and the minimum number of seconds before a submission counts as human. 'captcha' is null while the captcha is off; otherwise it is { provider: \"friendlycaptcha\", siteKey, apiEndpoint: \"global\"|\"eu\" } (Friendly Captcha v2; apiEndpoint is the widget's data region and follows the server configuration), which the website widget needs. Nothing else about the captcha (secret, verify URL) is ever returned.";
|
||||
getToken.Responses ??= [];
|
||||
getToken.Responses["404"] = new OpenApiResponse { Description = "The contact form is closed on this site (the website shows its mailto fallback)." };
|
||||
getToken.Responses["429"] = new OpenApiResponseReference("RateLimited", document);
|
||||
|
||||
@@ -49,7 +49,7 @@ public sealed class FriendlyCaptchaVerifier(
|
||||
|
||||
try
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, captcha.VerifyUrl)
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, captcha.EffectiveVerifyUrl)
|
||||
{
|
||||
Content = JsonContent.Create(new VerifyRequest(captchaToken, captcha.SiteKey ?? string.Empty)),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user