From 32750924017aedc30b4da7af26b777f0e82abdcc Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Tue, 10 Nov 2026 23:06:23 +0100 Subject: [PATCH] Contact: Friendly Captcha v2, opstartvalidatie, ASF-host met Contact-module en hosttests (CMS-U9) Co-Authored-By: Claude Sonnet 5.5 --- .../contact-forms-module/aidlc-state.md | 2 +- .../features/contact-forms-module/audit.md | 13 + .../code/summary.md | 8 + .../construction/phase-1-handoff.md | 31 ++ ...ptcha-and-asf-host-code-generation-plan.md | 20 +- .../contact/pages/MessagesPage.test.tsx | 30 ++ openapi/slpsoftware.v1.json | 2 + .../ContactHostTests.cs | 264 +++++++++++++++++ .../HostConfigurationTests.cs | 13 + ...ModularCms.Api.AndreaSlopFotografie.csproj | 10 +- .../appsettings.json | 30 ++ .../ContactMailTests.cs | 14 + .../FriendlyCaptchaTests.cs | 276 ++++++++++++++++++ .../Pipeline/ContactCaptchaPipelineTests.cs | 181 ++++++++++++ .../Config/ContactOptions.cs | 45 ++- .../ContactModule.cs | 14 +- .../Controllers/ContactMessagesController.cs | 5 +- .../ContactOpenApiDocumentTransformer.cs | 2 + .../Services/ContactSubmissionService.cs | 4 +- .../Services/FriendlyCaptchaVerifier.cs | 109 +++++++ 20 files changed, 1052 insertions(+), 21 deletions(-) create mode 100644 aidlc-docs/features/contact-forms-module/construction/cms-u9-friendly-captcha-and-asf-host/code/summary.md create mode 100644 src/SlpModularCms.Api.AndreaSlopFotografie.Tests/ContactHostTests.cs create mode 100644 src/SlpModularCms.Modules.Contact.Tests/FriendlyCaptchaTests.cs create mode 100644 src/SlpModularCms.Modules.Contact.Tests/Pipeline/ContactCaptchaPipelineTests.cs create mode 100644 src/SlpModularCms.Modules.Contact/Services/FriendlyCaptchaVerifier.cs diff --git a/aidlc-docs/features/contact-forms-module/aidlc-state.md b/aidlc-docs/features/contact-forms-module/aidlc-state.md index 6322392..0a4e92d 100644 --- a/aidlc-docs/features/contact-forms-module/aidlc-state.md +++ b/aidlc-docs/features/contact-forms-module/aidlc-state.md @@ -5,7 +5,7 @@ - **Feature Slug**: contact-forms-module - **Project Type**: Brownfield - **Start Date**: 2026-11-09 -- **Current Stage**: PHASE 2 - CMS-U7, CMS-U5 and CMS-U6 built and committed; next plan CMS-U8 +- **Current Stage**: PHASE 2 - CMS-U7, U5, U6, U8 and U9 built - **Branch**: feature/contact-forms-module (worktree ..SlpModularCms-contact-forms, from master d9cc539; main checkout stays on feature/identity-email-notifications — never switch it) ## Workspace State diff --git a/aidlc-docs/features/contact-forms-module/audit.md b/aidlc-docs/features/contact-forms-module/audit.md index fa43bb4..2f72157 100644 --- a/aidlc-docs/features/contact-forms-module/audit.md +++ b/aidlc-docs/features/contact-forms-module/audit.md @@ -155,3 +155,16 @@ **Context**: Phase 2, one commit. --- + + +## CMS-U9 built + +**Timestamp**: 2026-11-10 + +**User Input**: Approval of plan CMS-U9 with the owner's answers (Friendly Captcha v2; missing token = spam 202; provider 401/sitekey_invalid = fail-open + error log; in-page widget) and the instruction to postpone the frame-src catalogue change until the owner decides (via SLP Software - SLS - Main). + +**AI Response**: Verifier, config, start-up validation, ASF host reference and tests built; docs checked against the provider's v2 documentation; found that the v2 widget needs frame-src (reported, step 6 postponed, patch parked). + +**Context**: Phase 2, one commit, nothing pushed. + +--- diff --git a/aidlc-docs/features/contact-forms-module/construction/cms-u9-friendly-captcha-and-asf-host/code/summary.md b/aidlc-docs/features/contact-forms-module/construction/cms-u9-friendly-captcha-and-asf-host/code/summary.md new file mode 100644 index 0000000..87afb22 --- /dev/null +++ b/aidlc-docs/features/contact-forms-module/construction/cms-u9-friendly-captcha-and-asf-host/code/summary.md @@ -0,0 +1,8 @@ +# CMS-U9 — Friendly Captcha v2 and the ASF host (summary) + +- `FriendlyCaptchaVerifier` (v2 siteverify, X-API-Key, fail-open rules, no secrets/tokens in logs), `ContactCaptchaOptions` (+ VerifyUrl, TimeoutSeconds, `Problems()`), start-up validation (`ContactOptionsValidator`, ValidateOnStart: the host refuses to start with an incomplete captcha setup), module wiring (`NullCaptchaVerifier` while off), token response only announces a complete setup, missing/empty `captchaToken` while on = spam (202). +- OpenAPI: descriptions for token and submit operations regenerated. +- ASF host: `ProjectReference` + `Contact` config section (off), rate-limit policies, release conditions in the file and in the handoff (section 8). +- Tests (new): verifier (request shape, EU URL, all answers, timeout, network error, cancellation, empty token, no secrets in logs), options validation, submission rule, pipeline with a fake provider (token response shape, spam vs genuine identical 202, missing token, fail-open 500/503/401, label with `& ' " < >`, start-up refusal), mail label encoding, ASF host tests (closed by default, public routes reachable while the gate is closed, per-client vs shared bucket, committed defaults), frontend label test. +- NOT included (instruction): the `frame-src` CSP catalogue change (`AllowedSiteFrameOrigins`), parked as a patch; see handoff section 8 for the provider's CSP requirement. +- Deviation found against the plan: the v2 widget needs `frame-src` (iframes); the "no frame-src" assumption was wrong. Also `sitekey_invalid` is HTTP 400 (not 401/403). diff --git a/aidlc-docs/features/contact-forms-module/construction/phase-1-handoff.md b/aidlc-docs/features/contact-forms-module/construction/phase-1-handoff.md index 4921fca..e5bead5 100644 --- a/aidlc-docs/features/contact-forms-module/construction/phase-1-handoff.md +++ b/aidlc-docs/features/contact-forms-module/construction/phase-1-handoff.md @@ -112,3 +112,34 @@ Rebased onto `origin/master` `e671078` (merge of PR #11: identity-email-notifica - Env var `Contact__RecipientEmail` (SLP: environment only, like ASF). Optional `Contact__Mail__SweepSeconds|MaxAttempts|ClaimMinutes|BatchSize`, `Contact__Mail__BackoffMinutes__0..n`. - Mail itself needs the host's normal `Email:*` configuration; without it messages stay `pending` with `mailError: "disabled"`. - Detail: `nextMailAttemptAt`; resend: `mailError`. New migration `ContactMailClaim`. + + +## 8. CMS-U9 additions (Friendly Captcha v2 and the ASF host) + +### Captcha contract (no wire change) +- `GET /contact-messages/token` -> `captcha: null` while off, otherwise `{ "provider": "friendlycaptcha", "siteKey": "" }`. Nothing else about the captcha (API key, verify URL) is ever returned. +- `POST /contact-messages`: `captchaToken` = the widget's response string (the widget also puts it in the form field `frc-captcha-response`). While the captcha is on, a **missing, empty or rejected** value is treated as spam: answer `202 {"status":"received"}` identical to a genuine message, stored with status `spam` (never a 400, so a bot learns nothing). While the captcha is off the field is ignored. +- Verify (server side, Friendly Captcha **v2**): `POST https://global.frcapi.com/api/v2/captcha/siteverify` (EU-only: `https://eu.frcapi.com/api/v2/captcha/siteverify`), header `X-API-Key: `, JSON `{ "response": "...", "sitekey": "..." }`; `success:true` passes, `success:false` (HTTP 200 `response_invalid|response_timeout|response_duplicate`, or 400 `response_missing|bad_request`) = spam. Checked against developer.friendlycaptcha.com on 2026-11-10. +- **Fail-open**: timeout (default 3 s), network error and 5xx let the visitor through (warning `captcha_unavailable`, kind only). Our own wrong API key or site key (HTTP 401, or 400 with `error_code: sitekey_invalid`) also lets the visitor through but logs an **error** `captcha_misconfigured`. The response token, the API key and the site key are never logged. + +### Configuration (per site) +| Key | Notes | +|---|---| +| `Contact:Captcha:Enabled` | default `false` | +| `Contact:Captcha:Provider` | `friendlycaptcha` (only supported value; case-insensitive) | +| `Contact:Captcha:SiteKey` | public; sent to the browser | +| `Contact__Captcha__Secret` | the API key; **environment variable / secrets manager only**, never committed | +| `Contact:Captcha:VerifyUrl` | default global v2 URL; use the EU URL above for EU-only processing; must be https | +| `Contact:Captcha:TimeoutSeconds` | default 3 (1-30) | + +With `Contact:Enabled=true` and `Captcha:Enabled=true` the host **refuses to start** when provider, site key or secret is missing/invalid (the message names the setting only, never a value). + +### Widget (website session) and CSP +- Widget: Friendly Captcha v2 SDK with `sitekey` = `siteKey` from the token response; for the EU endpoint the widget needs `apiEndpoint: "eu"` (`data-api-endpoint="eu"`). The response goes into `captchaToken`. Exact SDK package/events: to be verified by the website session against the SDK docs. +- **CSP**: the v2 widget renders in iframes. The provider's CSP guide says you need `frame-src https://*.frcapi.com` (global and EU). `StrictSite` currently has no `frame-src` (iframes fall back to `default-src 'self'` and are blocked). The additive catalogue change `SecurityHeaders:AllowedSiteFrameOrigins` (StrictSite only, absent = header byte-identical) is **prepared but NOT included in this unit**; waiting for the owner's decision on exact origins vs wildcard and on global vs EU. No `connect-src`/`script-src` additions are documented by the provider. +- Privacy (provider statements, v2 docs): "Friendly Captcha does not rely on user tracking or cookies for its protection service"; the dedicated EU endpoint is "hosted in Germany" and "personal information (i.e. visitor IP addresses) never leave the EU". The site's privacy page must name the captcha provider and the processing of the visitor's IP address for the challenge. + +### ASF host (`Api.AndreaSlopFotografie`) +- References `Modules.Contact`; section `Contact` in its `appsettings.json` (`Enabled:false`, `SiteId: andreaslop`), the three contact rate-limit policies. Capabilities now list `Contact`; the module does nothing and touches no database while `Contact:Enabled=false`. +- Release conditions (also in the comment of that section): `ForwardedHeaders__KnownProxies__0` = reverse proxy (IIS) address first (otherwise the whole site shares one bucket); `Contact__RecipientEmail` (env only); mail via `Email__Smtp__*`, `App__PublicBaseUrl`; sender = `Email:FromAddress` or the address chosen in the site settings from `Email:AllowedFromAddresses` (set by environment for ASF); captcha settings above; then `Contact__Enabled=true` and the admin switch (Berichten). +- Host tests (`Api.AndreaSlopFotografie.Tests/ContactHostTests.cs`, real MariaDB): closed by default; public routes stay reachable (200/202) while the availability gate is "not available" and the management routes answer 503; per-client buckets with a known proxy vs one shared bucket without. diff --git a/aidlc-docs/features/contact-forms-module/construction/plans/cms-u9-friendly-captcha-and-asf-host-code-generation-plan.md b/aidlc-docs/features/contact-forms-module/construction/plans/cms-u9-friendly-captcha-and-asf-host-code-generation-plan.md index b3fcb5c..563559c 100644 --- a/aidlc-docs/features/contact-forms-module/construction/plans/cms-u9-friendly-captcha-and-asf-host-code-generation-plan.md +++ b/aidlc-docs/features/contact-forms-module/construction/plans/cms-u9-friendly-captcha-and-asf-host-code-generation-plan.md @@ -1,6 +1,6 @@ # Code Generation Plan — CMS-U9 friendly-captcha-and-asf-host -Status: PLAN, waiting for approval by SLP Software - SLS - Main. Nothing built. No push, no deploy. +Status: BUILT except step 6 (frame-src catalogue change, postponed on instruction). No push, no deploy. ## Starting point (already in the code) - `ICaptchaVerifier` seam with `NullCaptchaVerifier` (always true); `ContactSubmissionService` calls it only when `Contact:Captcha:Enabled`; a `false` answer is classified as spam: **202 + status spam**, identical to today. @@ -15,18 +15,18 @@ Status: PLAN, waiting for approval by SLP Software - SLS - Main. Nothing built. 5. Privacy note (docs): Friendly Captcha does not use cookies or personal tracking; the visitor's IP address is processed for the challenge request by the provider (EU-hosted option exists; verify call from our server sends the response + sitekey only, **not** the visitor IP unless we choose to). Documented per provider/version in the contract doc; exact wording to be checked against the provider's own privacy statement at build time, and ASF's privacy page owner (Andrea) needs to mention it. ## Build steps -- [ ] **Step 1 — Verifier**: `FriendlyCaptchaVerifier : ICaptchaVerifier` using `IHttpClientFactory` (named client, timeout from options), returns true/false per the rules above; `CaptchaProviderOptionsValidator`; provider selection in `ContactModule` (`NullCaptchaVerifier` stays for provider-less/disabled). -- [ ] **Step 2 — Submission rule**: when enabled, an empty `captchaToken` = failed check (spam), without calling the provider. -- [ ] **Step 3 — Token response**: unchanged shape; only returns `captcha` when enabled **and** provider/sitekey valid; test that no secret/verify URL ever appears in any response. -- [ ] **Step 4 — Tests (no real network)**: fake `HttpMessageHandler`: success true/false, timeout, 5xx, 401/403 (misconfigured), malformed JSON, cancellation; empty token; request body/headers exact (`X-API-Key`, JSON fields); logs carry no token/secret/PII; pipeline test on the real host with the fake handler registered: spam => 202 + stored as spam, identical response body to genuine; fail-open => genuine stored; token response `captcha` shape; startup validation (missing secret/sitekey, unknown provider). -- [ ] **Step 5 — Subject label check (decision a)**: tests with labels containing `& ' " < > ` and line-break/control characters through validator, mail (subject stays fixed; HTML body encodes the label; text part indents it), Reply-To, admin list/detail JSON (React escapes on display; verified by a frontend test with `x & "q"`). -- [ ] **Step 6 — CSP proposal (not applied to ASF by me)**: add a catalogue option so a site can allow the captcha provider: for `StrictSite`: `connect-src 'self' https://global.frcapi.com` (and `https://eu.frcapi.com` when the EU host is used), `frame-src` is not needed for the v2 default widget (it runs in-page); if the widget is configured in iframe mode add `frame-src https://global.frcapi.com`; `script-src` additions only if the SDK is loaded from a CDN instead of the site bundle (avoid: bundle it). Delivered as: `SecurityHeaders:AllowedSiteConnectOrigins` entries (already supported by the catalogue) plus documented values; **no catalogue code change is needed for connect-src**, and I add a `frame-src` extension only if you want iframe mode. Documented in the contract doc. -- [ ] **Step 7 — ASF host** (coordinate with session `AndreaSlopFotografie`; the host project is theirs, I only add): +- [x] **Step 1 — Verifier**: `FriendlyCaptchaVerifier : ICaptchaVerifier` using `IHttpClientFactory` (named client, timeout from options), returns true/false per the rules above; `CaptchaProviderOptionsValidator`; provider selection in `ContactModule` (`NullCaptchaVerifier` stays for provider-less/disabled). +- [x] **Step 2 — Submission rule**: when enabled, an empty `captchaToken` = failed check (spam), without calling the provider. +- [x] **Step 3 — Token response**: unchanged shape; only returns `captcha` when enabled **and** provider/sitekey valid; test that no secret/verify URL ever appears in any response. +- [x] **Step 4 — Tests (no real network)**: fake `HttpMessageHandler`: success true/false, timeout, 5xx, 401/403 (misconfigured), malformed JSON, cancellation; empty token; request body/headers exact (`X-API-Key`, JSON fields); logs carry no token/secret/PII; pipeline test on the real host with the fake handler registered: spam => 202 + stored as spam, identical response body to genuine; fail-open => genuine stored; token response `captcha` shape; startup validation (missing secret/sitekey, unknown provider). +- [x] **Step 5 — Subject label check (decision a)**: tests with labels containing `& ' " < > ` and line-break/control characters through validator, mail (subject stays fixed; HTML body encodes the label; text part indents it), Reply-To, admin list/detail JSON (React escapes on display; verified by a frontend test with `x & "q"`). +- [ ] **Step 6 (POSTPONED on instruction until the frame-src decision; patch parked, not committed) — CSP proposal (not applied to ASF by me)**: add a catalogue option so a site can allow the captcha provider: for `StrictSite`: `connect-src 'self' https://global.frcapi.com` (and `https://eu.frcapi.com` when the EU host is used), `frame-src` is not needed for the v2 default widget (it runs in-page); if the widget is configured in iframe mode add `frame-src https://global.frcapi.com`; `script-src` additions only if the SDK is loaded from a CDN instead of the site bundle (avoid: bundle it). Delivered as: `SecurityHeaders:AllowedSiteConnectOrigins` entries (already supported by the catalogue) plus documented values; **no catalogue code change is needed for connect-src**, and I add a `frame-src` extension only if you want iframe mode. Documented in the contract doc. +- [x] **Step 7 — ASF host** (coordinate with session `AndreaSlopFotografie`; the host project is theirs, I only add): - `ProjectReference` to `Modules.Contact` + module registration in `Api.AndreaSlopFotografie`; per-site `Contact:SiteId` (e.g. `andreaslop`), own database/connection as already used there. - Host tests (new, in `Api.AndreaSlopFotografie.Tests`, real pipeline on MariaDB): (a) contact routes answer only when `Contact:Enabled` + CMS switch; (b) `[PublicSiteEndpoint]`: token/submit routes stay reachable (200/202/404-closed, never the gate's 503) while the availability gate is "not available"; admin routes remain gated; (c) rate limits are per client: with `ForwardedHeaders:KnownProxies` set, two client addresses get separate buckets; without it, one shared bucket (test documents the consequence). - Docs: release conditions for ASF: `ForwardedHeaders__KnownProxies__0=` before `Contact__Enabled=true`; `Contact__RecipientEmail` env only; From: `Email:FromAddress` is the site address; contact mail uses `GetEffectiveSenderAsync`, so it follows `Email:AllowedFromAddresses`/the site settings (to be set for ASF to a real ASF address via env, not in repo); captcha secret via env. -- [ ] **Step 8 — Docs**: contract doc (captcha shape, `captchaToken` rule, spam behaviour, fail-open, CSP values, privacy note), handoff section 8 (keys + env var names), OpenAPI regenerated (description of `captchaToken`/`captcha`), summary, state/audit. -- [ ] **Step 9 — Verification**: all test projects incl. pipeline on a temporary MariaDB; one commit; no push; container removed. +- [x] **Step 8 — Docs**: contract doc (captcha shape, `captchaToken` rule, spam behaviour, fail-open, CSP values, privacy note), handoff section 8 (keys + env var names), OpenAPI regenerated (description of `captchaToken`/`captcha`), summary, state/audit. +- [x] **Step 9 — Verification**: all test projects incl. pipeline on a temporary MariaDB; one commit; no push; container removed. ## Answers to the ASF-host facts (short) 1. ASF csproj/registration: **to build in U9** (step 7). diff --git a/frontend/src/features/contact/pages/MessagesPage.test.tsx b/frontend/src/features/contact/pages/MessagesPage.test.tsx index c40c733..844cf2e 100644 --- a/frontend/src/features/contact/pages/MessagesPage.test.tsx +++ b/frontend/src/features/contact/pages/MessagesPage.test.tsx @@ -234,6 +234,36 @@ describe('Message list', () => { ); }); + it('shows a subject label with special characters as plain text', async () => { + const label = `Fotoshoot & "bruiloft" 'zomer'`; + server.use( + http.get(`${API_BASE}/api/v1/contact-messages`, () => + HttpResponse.json({ + items: [ + { + id: MSG_NEW, + receivedAt: '2026-11-09T10:00:00Z', + name: 'Jan', + email: 'jan@example.nl', + subject: label, + status: 'read', + mailStatus: 'sent', + }, + ], + page: 1, + pageSize: 25, + totalCount: 1, + unreadCount: 0, + }), + ), + ); + await openPage(); + + const row = await screen.findByTestId(`contact-row-${MSG_NEW}`); + expect(row).toHaveTextContent(label); + expect(row.querySelector('b')).toBeNull(); + }); + it('gives the mail icons a text alternative', async () => { await openPage(); await screen.findByTestId(`contact-row-${MSG_NEW}`); diff --git a/openapi/slpsoftware.v1.json b/openapi/slpsoftware.v1.json index f4dbeb3..6ae0699 100644 --- a/openapi/slpsoftware.v1.json +++ b/openapi/slpsoftware.v1.json @@ -1753,6 +1753,7 @@ ] }, "post": { + "description": "Stores a message. While the captcha is on, 'captchaToken' (the widget response) is required: a missing, empty or rejected value is treated as spam and answered exactly like a genuine message (202, stored with status spam), never with a 400. A captcha provider that does not answer in time, or fails, lets the visitor through (fail-open).", "parameters": [ { "description": "A UUID chosen by the client per attempt series. A replay within 24 hours is answered with the same 202 without storing the message twice.", @@ -1806,6 +1807,7 @@ }, "/api/v1/contact-messages/token": { "get": { + "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.", "responses": { "200": { "content": { diff --git a/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/ContactHostTests.cs b/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/ContactHostTests.cs new file mode 100644 index 0000000..3d5af63 --- /dev/null +++ b/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/ContactHostTests.cs @@ -0,0 +1,264 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using FluentAssertions; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using SlpModularCms.Modules.Contact.Data; +using SlpModularCms.Modules.Contact.Data.Entities; + +namespace SlpModularCms.Api.AndreaSlopFotografie.Tests; + +/// +/// The real Andrea Slop Fotografie host with the Contact module, against a real MariaDB/MySQL (like the other +/// host tests: set ConnectionStrings__DefaultConnection to a server you may create databases on; every +/// factory uses its own throw-away database). +/// +public class AsfContactHostFactory : WebApplicationFactory +{ + private readonly Dictionary _settings; + private readonly string _databaseName = "AsfContactHost_" + Guid.NewGuid().ToString("N")[..12]; + + /// When set, the pipeline sees this as the TCP peer (the reverse proxy) of every request. + public string? PeerAddress { get; init; } + + public AsfContactHostFactory(Dictionary? settings = null) => _settings = settings ?? []; + + private static string BaseConnectionString => + Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection") + ?? throw new InvalidOperationException("Set ConnectionStrings__DefaultConnection to a MariaDB/MySQL server for the host tests."); + + private string ConnectionString => + Regex.IsMatch(BaseConnectionString, "Database=[^;]*", RegexOptions.IgnoreCase) + ? Regex.Replace(BaseConnectionString, "Database=[^;]*", $"Database={_databaseName}", RegexOptions.IgnoreCase) + : $"{BaseConnectionString};Database={_databaseName}"; + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.UseEnvironment("Development"); + builder.ConfigureAppConfiguration((_, config) => + { + var all = new Dictionary + { + ["ConnectionStrings:DefaultConnection"] = ConnectionString, + ["Availability:StatusCacheSeconds"] = "0", + }; + foreach (var (key, value) in _settings) all[key] = value; + config.AddInMemoryCollection(all); + }); + + if (PeerAddress is not null) + { + // Stands in for the reverse proxy: the TCP peer of every request. Runs before the forwarded-headers step. + builder.ConfigureServices(services => services.AddTransient(_ => new PeerFilter(PeerAddress))); + } + } + + private sealed class PeerFilter(string address) : IStartupFilter + { + public Action Configure(Action next) => app => + { + app.Use((context, nextMiddleware) => + { + context.Connection.RemoteIpAddress = IPAddress.Parse(address); + return nextMiddleware(); + }); + next(app); + }; + } + + public async Task WithDb(Func> action) + { + await using var scope = Services.CreateAsyncScope(); + return await action(scope.ServiceProvider.GetRequiredService()); + } + + public async Task OpenCmsSwitchAsync() + { + _ = CreateClient(); // starts the host (migrates the contact tables) + await WithDb(async db => + { + db.ContactSettings.Add(new ContactSettings { Enabled = true, UpdatedAt = DateTimeOffset.UtcNow, UpdatedByUserId = Guid.NewGuid() }); + await db.SaveChangesAsync(); + return 0; + }); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + try + { + using var scope = Services.CreateScope(); + scope.ServiceProvider.GetRequiredService().Database.EnsureDeleted(); + } + catch + { + // Best effort clean-up of the throw-away database. + } + } + + base.Dispose(disposing); + } +} + +/// The committed defaults, nothing overridden. +public sealed class AsfDefaultContactHostFactory() : AsfContactHostFactory(null); + +internal static class ContactHttp +{ + public const string Route = "/api/v1/contact-messages"; + + public static async Task GetTokenAsync(HttpClient client, string? forwardedFor = null) + { + var request = new HttpRequestMessage(HttpMethod.Get, $"{Route}/token"); + if (forwardedFor is not null) request.Headers.Add("X-Forwarded-For", forwardedFor); + var response = await client.SendAsync(request); + response.StatusCode.Should().Be(HttpStatusCode.OK); + using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + return json.RootElement.GetProperty("token").GetString()!; + } + + public static Task PostAsync(HttpClient client, string token, string name, string? forwardedFor = null) + { + var request = new HttpRequestMessage(HttpMethod.Post, Route) + { + Content = new StringContent(JsonSerializer.Serialize(new + { + name, email = "jan@example.nl", subject = "Portretfotografie", message = "Hallo", hpCheck = "", token, + }), Encoding.UTF8, "application/json"), + }; + request.Headers.Add("Idempotency-Key", Guid.NewGuid().ToString()); + if (forwardedFor is not null) request.Headers.Add("X-Forwarded-For", forwardedFor); + return client.SendAsync(request); + } +} + +public class AsfContactDisabledByDefaultTests : IClassFixture +{ + private readonly AsfDefaultContactHostFactory _factory; + + public AsfContactDisabledByDefaultTests(AsfDefaultContactHostFactory factory) => _factory = factory; + + [Fact] + public async Task TheCommittedDefaults_KeepTheContactModuleLoadedButClosed() + { + var client = _factory.CreateClient(); + + using var caps = JsonDocument.Parse(await client.GetStringAsync("/api/v1/System/capabilities")); + caps.RootElement.GetProperty("modules").EnumerateArray().Select(e => e.GetString()).Should().Contain("Contact"); + + var token = await client.GetAsync($"{ContactHttp.Route}/token"); + token.StatusCode.Should().Be(HttpStatusCode.NotFound); + (await token.Content.ReadAsStringAsync()).Should().BeEmpty(); + (await client.PostAsync(ContactHttp.Route, null)).StatusCode.Should().Be(HttpStatusCode.NotFound); + } +} + +public class AsfContactGateTests : IClassFixture +{ + private readonly AsfOpenContactHostFactory _factory; + + public AsfContactGateTests(AsfOpenContactHostFactory factory) => _factory = factory; + + [Fact] + public async Task ThePublicContactRoutes_StayReachable_WhileTheCmsIsNotAvailable_ButTheManagementRoutesAreGated() + { + await _factory.OpenCmsSwitchAsync(); + // Switch the CMS off through the availability state of the real database (status 1 = not available). + await _factory.WithDb(db => db.Database.ExecuteSqlRawAsync( + "INSERT INTO AvailabilityState (Id, Status, Message, LastUpdatedAt, UpdatedBy) VALUES (UUID(), 1, 'host test', NOW(), 'host test')")); + var client = _factory.CreateClient(); + + (await client.GetAsync($"{ContactHttp.Route}?pageSize=1")).StatusCode + .Should().Be(HttpStatusCode.ServiceUnavailable, "the gate really is closed for everything not marked public"); + + var token = await ContactHttp.GetTokenAsync(client); + var post = await ContactHttp.PostAsync(client, token, "ASF Gate"); + + post.StatusCode.Should().Be(HttpStatusCode.Accepted); + (await _factory.WithDb(db => db.ContactMessages.CountAsync(m => m.Name == "ASF Gate"))).Should().Be(1); + (await _factory.WithDb(db => db.ContactMessages.SingleAsync(m => m.Name == "ASF Gate"))).SiteId.Should().Be("andreaslop"); + } +} + +public sealed class AsfOpenContactHostFactory() : AsfContactHostFactory(new() +{ + ["Contact:Enabled"] = "true", + ["Contact:MinSeconds"] = "0", + ["RateLimiting:ContactForm:PermitLimit"] = "1000", + ["RateLimiting:ContactToken:PermitLimit"] = "1000", +}); + +/// One form submission allowed per window: the buckets show who shares one. +public sealed class AsfBehindProxyWithKnownProxyFactory() : AsfContactHostFactory(new() +{ + ["Contact:Enabled"] = "true", + ["Contact:MinSeconds"] = "0", + ["RateLimiting:ContactForm:PermitLimit"] = "1", + ["RateLimiting:ContactToken:PermitLimit"] = "1000", +}); + +public sealed class AsfBehindProxyWithoutKnownProxyFactory() : AsfContactHostFactory(new() +{ + ["Contact:Enabled"] = "true", + ["Contact:MinSeconds"] = "0", + ["RateLimiting:ContactForm:PermitLimit"] = "1", + ["RateLimiting:ContactToken:PermitLimit"] = "1000", +}); + +public class AsfRateLimitBehindIisTests +{ + [Fact] + public async Task WithTheProxyKnown_EachVisitorAddressHasItsOwnBucket() + { + // The proxy list is read while the host is being composed, before test configuration is applied, so (like in + // production) it comes from the environment: ForwardedHeaders__KnownProxies__0. + Environment.SetEnvironmentVariable("ForwardedHeaders__KnownProxies__0", "10.1.1.1"); + try + { + await RunWithKnownProxyAsync(); + } + finally + { + Environment.SetEnvironmentVariable("ForwardedHeaders__KnownProxies__0", null); + } + } + + private static async Task RunWithKnownProxyAsync() + { + using var factory = new AsfBehindProxyWithKnownProxyFactory { PeerAddress = "10.1.1.1" }; + await factory.OpenCmsSwitchAsync(); + var client = factory.CreateClient(); + + var a1 = await ContactHttp.PostAsync(client, await ContactHttp.GetTokenAsync(client, "203.0.113.10"), "ASF A1", "203.0.113.10"); + var a2 = await ContactHttp.PostAsync(client, await ContactHttp.GetTokenAsync(client, "203.0.113.10"), "ASF A2", "203.0.113.10"); + var b1 = await ContactHttp.PostAsync(client, await ContactHttp.GetTokenAsync(client, "203.0.113.20"), "ASF B1", "203.0.113.20"); + + a1.StatusCode.Should().Be(HttpStatusCode.Accepted); + a2.StatusCode.Should().Be(HttpStatusCode.TooManyRequests, "the same visitor is limited"); + b1.StatusCode.Should().Be(HttpStatusCode.Accepted, "another visitor has a bucket of their own"); + } + + [Fact] + public async Task WithoutKnownProxies_TheWholeSiteSharesOneBucket_WhichIsWhyItIsAReleaseCondition() + { + using var factory = new AsfBehindProxyWithoutKnownProxyFactory { PeerAddress = "10.1.1.1" }; + await factory.OpenCmsSwitchAsync(); + var client = factory.CreateClient(); + + var a = await ContactHttp.PostAsync(client, await ContactHttp.GetTokenAsync(client, "203.0.113.10"), "ASF Shared A", "203.0.113.10"); + var b = await ContactHttp.PostAsync(client, await ContactHttp.GetTokenAsync(client, "203.0.113.20"), "ASF Shared B", "203.0.113.20"); + + a.StatusCode.Should().Be(HttpStatusCode.Accepted); + b.StatusCode.Should().Be(HttpStatusCode.TooManyRequests, "forwarded addresses are ignored without KnownProxies: everybody is the proxy"); + } +} diff --git a/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs b/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs index 0063cf6..9b91f04 100644 --- a/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs +++ b/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs @@ -66,6 +66,19 @@ public class HostConfigurationTests config.GetSection("ForwardedHeaders:KnownProxies").Get().Should().BeNullOrEmpty(); } + [Fact] + public void Contact_IsOffByDefault_ForTheRightSite_WithNoRecipientOrCaptchaSecretCommitted() + { + var config = Load(); + + config.GetValue("Contact:Enabled").Should().BeFalse(); + config["Contact:SiteId"].Should().Be("andreaslop"); + config.GetValue("Contact:Captcha:Enabled").Should().BeFalse(); + config["Contact:RecipientEmail"].Should().BeNullOrEmpty("the address comes from the environment only"); + config["Contact:Captcha:Secret"].Should().BeNullOrEmpty(); + config["Contact:Captcha:SiteKey"].Should().BeNullOrEmpty(); + } + [Fact] public void Host_DoesNotReferenceMasterOrOfferingsModules() { diff --git a/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj b/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj index 431adb8..1b978b4 100644 --- a/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj +++ b/src/SlpModularCms.Api.AndreaSlopFotografie/SlpModularCms.Api.AndreaSlopFotografie.csproj @@ -18,14 +18,16 @@ + diff --git a/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json b/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json index 957375a..9bdf07c 100644 --- a/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json +++ b/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json @@ -77,6 +77,24 @@ "Cors": { "AllowedOrigins": [] }, + // Contact form of the website. OFF: no routes, no database access, until it is released on purpose. + // Release conditions, in this order (see aidlc-docs/features/contact-forms-module/construction/phase-1-handoff.md): + // 1. ForwardedHeaders__KnownProxies__0 = the IP address of the reverse proxy (IIS). Without it every visitor + // reaches the API from the proxy's address and the whole site shares ONE rate-limit bucket. + // 2. Contact__RecipientEmail (environment variable only; no address belongs in this file) and working mail + // (Email__Smtp__*, App__PublicBaseUrl); the sender is the site's From address (Email:FromAddress or the + // value chosen in the site settings from Email:AllowedFromAddresses). + // 3. Captcha (recommended): Contact__Captcha__Enabled=true, Contact__Captcha__Provider=friendlycaptcha, + // Contact__Captcha__SiteKey, and the API key ONLY as the environment variable Contact__Captcha__Secret. + // 4. Contact__Enabled=true, then switch the form on in the admin (Berichten). + "Contact": { + "Enabled": false, + "SiteId": "andreaslop", + "MinSeconds": 3, + "Captcha": { + "Enabled": false + } + }, "RateLimiting": { "Login": { "PermitLimit": 5, @@ -93,6 +111,18 @@ "OfferingsPublic": { "PermitLimit": 120, "WindowSeconds": 60 + }, + "ContactForm": { + "PermitLimit": 5, + "WindowSeconds": 600 + }, + "ContactToken": { + "PermitLimit": 20, + "WindowSeconds": 600 + }, + "ContactResend": { + "PermitLimit": 5, + "WindowSeconds": 600 } }, "SecurityHeaders": { diff --git a/src/SlpModularCms.Modules.Contact.Tests/ContactMailTests.cs b/src/SlpModularCms.Modules.Contact.Tests/ContactMailTests.cs index 61a98b3..8e01f68 100644 --- a/src/SlpModularCms.Modules.Contact.Tests/ContactMailTests.cs +++ b/src/SlpModularCms.Modules.Contact.Tests/ContactMailTests.cs @@ -107,6 +107,20 @@ public class ContactMailSenderTests rig.Sent.TextBody.Should().Contain("regel2").And.Contain("Naam:"); } + [Fact] + public async Task ASubjectLabelWithSpecialCharacters_IsEncodedInTheBody_AndNeverReachesTheSubjectOrAHeader() + { + var rig = new Rig(); + const string label = "Fotoshoot & \"bruiloft\" 'zomer'"; + + await rig.Build().SendAsync(Message(subject: label), CancellationToken.None); + + rig.Sent!.Subject.Should().Be("Nieuw bericht via De Site"); + rig.Sent.HtmlBody.Should().Contain("Fotoshoot & <b>").And.NotContain("\"bruiloft\""); + rig.Sent.TextBody.Should().Contain(" " + label); + rig.Sent.ReplyToAddress.Should().Be(Visitor); + } + [Fact] public async Task English_UsesEnglishLabelsAndSubject() { diff --git a/src/SlpModularCms.Modules.Contact.Tests/FriendlyCaptchaTests.cs b/src/SlpModularCms.Modules.Contact.Tests/FriendlyCaptchaTests.cs new file mode 100644 index 0000000..12a1527 --- /dev/null +++ b/src/SlpModularCms.Modules.Contact.Tests/FriendlyCaptchaTests.cs @@ -0,0 +1,276 @@ +using System.Net; +using System.Text; +using FluentAssertions; +using NSubstitute; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SlpModularCms.Modules.Contact.Config; +using SlpModularCms.Modules.Contact.Models; +using SlpModularCms.Modules.Contact.Services; + +namespace SlpModularCms.Modules.Contact.Tests; + +/// A stand-in for the provider: never touches the network. Records every request it receives. +public sealed class FakeCaptchaHandler(Func> respond) : HttpMessageHandler +{ + public List<(HttpRequestMessage Request, string Body)> Calls { get; } = []; + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var body = request.Content is null ? "" : await request.Content.ReadAsStringAsync(cancellationToken); + lock (Calls) Calls.Add((request, body)); + return await respond(request, cancellationToken); + } + + public static HttpResponseMessage Json(HttpStatusCode status, string json) => + new(status) { Content = new StringContent(json, Encoding.UTF8, "application/json") }; + + public static FakeCaptchaHandler Always(HttpStatusCode status, string json) => new((_, _) => Task.FromResult(Json(status, json))); +} + +public class FriendlyCaptchaVerifierTests +{ + private const string Secret = "ZXQ-secret-key"; + private const string SiteKey = "FCMZXQ-site"; + private const string Token = "ZXQ-captcha-response"; + + private sealed class Factory(HttpMessageHandler handler) : IHttpClientFactory + { + public HttpClient CreateClient(string name) => new(handler, disposeHandler: false); + } + + private readonly CapturingLoggerProvider _logs = new(); + + private FriendlyCaptchaVerifier Verifier(HttpMessageHandler handler, Action? configure = null) + { + var captcha = new ContactCaptchaOptions + { + Enabled = true, Provider = "friendlycaptcha", SiteKey = SiteKey, Secret = Secret, TimeoutSeconds = 1, + }; + configure?.Invoke(captcha); + return new FriendlyCaptchaVerifier( + new Factory(handler), + Options.Create(new ContactOptions { Enabled = true, Captcha = captcha }), + LoggerFactory.Create(b => b.AddProvider(_logs)).CreateLogger()); + } + + [Fact] + public async Task ASuccessfulAnswer_Passes_AndTheRequestIsExactlyWhatTheProviderExpects() + { + var handler = FakeCaptchaHandler.Always(HttpStatusCode.OK, """{"success":true,"data":{"event_id":"e"}}"""); + + (await Verifier(handler).VerifyAsync(Token, CancellationToken.None)).Should().BeTrue(); + + var (request, body) = handler.Calls.Should().ContainSingle().Subject; + request.Method.Should().Be(HttpMethod.Post); + request.RequestUri!.ToString().Should().Be("https://global.frcapi.com/api/v2/captcha/siteverify"); + request.Headers.GetValues("X-API-Key").Should().ContainSingle().Which.Should().Be(Secret); + request.Content!.Headers.ContentType!.MediaType.Should().Be("application/json"); + body.Should().Be($$"""{"response":"{{Token}}","sitekey":"{{SiteKey}}"}"""); + } + + [Fact] + public async Task TheEuEndpoint_IsUsedWhenConfigured() + { + var handler = FakeCaptchaHandler.Always(HttpStatusCode.OK, """{"success":true}"""); + + await Verifier(handler, c => c.VerifyUrl = "https://eu.frcapi.com/api/v2/captcha/siteverify") + .VerifyAsync(Token, CancellationToken.None); + + handler.Calls.Single().Request.RequestUri!.Host.Should().Be("eu.frcapi.com"); + } + + [Theory] + [InlineData(HttpStatusCode.OK, """{"success":false,"error":{"error_code":"response_invalid","detail":"x"}}""")] + [InlineData(HttpStatusCode.OK, """{"success":false,"error":{"error_code":"response_duplicate"}}""")] + [InlineData(HttpStatusCode.OK, """{"success":false,"error":{"error_code":"response_timeout"}}""")] + [InlineData(HttpStatusCode.BadRequest, """{"success":false,"error":{"error_code":"response_missing"}}""")] + [InlineData(HttpStatusCode.BadRequest, """{"success":false,"error":{"error_code":"bad_request"}}""")] + [InlineData(HttpStatusCode.OK, "not json at all")] + [InlineData(HttpStatusCode.OK, "{}")] + public async Task ADefiniteNo_IsSpam(HttpStatusCode status, string json) + { + var handler = FakeCaptchaHandler.Always(status, json); + + (await Verifier(handler).VerifyAsync(Token, CancellationToken.None)).Should().BeFalse(); + } + + [Theory] + [InlineData(HttpStatusCode.Unauthorized, """{"success":false,"error":{"error_code":"auth_invalid"}}""")] + [InlineData(HttpStatusCode.Forbidden, "")] + [InlineData(HttpStatusCode.BadRequest, """{"success":false,"error":{"error_code":"sitekey_invalid"}}""")] + public async Task OurOwnWrongKeyOrSiteKey_LetsTheVisitorThrough_AndLogsAnError(HttpStatusCode status, string json) + { + var handler = FakeCaptchaHandler.Always(status, json); + + (await Verifier(handler).VerifyAsync(Token, CancellationToken.None)).Should().BeTrue(); + + _logs.Messages.Should().Contain(m => m.Contains("captcha_misconfigured")); + } + + [Theory] + [InlineData(HttpStatusCode.InternalServerError)] + [InlineData(HttpStatusCode.BadGateway)] + [InlineData(HttpStatusCode.ServiceUnavailable)] + public async Task AProviderServerError_FailsOpen_WithAWarning(HttpStatusCode status) + { + var handler = FakeCaptchaHandler.Always(status, ""); + + (await Verifier(handler).VerifyAsync(Token, CancellationToken.None)).Should().BeTrue(); + + _logs.Messages.Should().Contain(m => m.Contains("captcha_unavailable")); + } + + [Fact] + public async Task ATimeout_FailsOpen() + { + var handler = new FakeCaptchaHandler(async (_, ct) => + { + await Task.Delay(TimeSpan.FromSeconds(30), ct); + return FakeCaptchaHandler.Json(HttpStatusCode.OK, """{"success":true}"""); + }); + + var verdict = await Verifier(handler).VerifyAsync(Token, CancellationToken.None); + + verdict.Should().BeTrue(); + _logs.Messages.Should().Contain(m => m.Contains("captcha_unavailable") && m.Contains("timeout")); + } + + [Fact] + public async Task ANetworkError_FailsOpen_AndTheLogHasNoUrlOrToken() + { + var handler = new FakeCaptchaHandler((_, _) => throw new HttpRequestException("cannot reach https://global.frcapi.com with " + Token)); + + (await Verifier(handler).VerifyAsync(Token, CancellationToken.None)).Should().BeTrue(); + + _logs.Messages.Should().NotContain(m => m.Contains(Token) || m.Contains("frcapi.com")); + } + + [Fact] + public async Task ACallerCancellation_IsNotSwallowed() + { + var handler = new FakeCaptchaHandler(async (_, ct) => + { + await Task.Delay(TimeSpan.FromSeconds(30), ct); + return FakeCaptchaHandler.Json(HttpStatusCode.OK, "{}"); + }); + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(50)); + + var act = async () => await Verifier(handler, c => c.TimeoutSeconds = 20).VerifyAsync(Token, cts.Token); + + await act.Should().ThrowAsync(); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public async Task AnEmptyToken_FailsWithoutCallingTheProvider(string? token) + { + var handler = FakeCaptchaHandler.Always(HttpStatusCode.OK, """{"success":true}"""); + + (await Verifier(handler).VerifyAsync(token, CancellationToken.None)).Should().BeFalse(); + + handler.Calls.Should().BeEmpty(); + } + + [Fact] + public async Task NeitherTheSecretNorTheTokenIsEverLogged() + { + foreach (var status in new[] { HttpStatusCode.OK, HttpStatusCode.Unauthorized, HttpStatusCode.InternalServerError }) + { + await Verifier(FakeCaptchaHandler.Always(status, """{"success":false}""")).VerifyAsync(Token, CancellationToken.None); + } + + _logs.Messages.Should().NotContain(m => m.Contains(Secret) || m.Contains(Token) || m.Contains(SiteKey)); + } +} + +public class ContactCaptchaOptionsTests +{ + private static ContactCaptchaOptions Valid() => new() + { + Enabled = true, Provider = "friendlycaptcha", SiteKey = "FCM-site", Secret = "key", + }; + + [Fact] + public void WhileOff_NothingIsRequired() => new ContactCaptchaOptions().Problems().Should().BeEmpty(); + + [Fact] + public void ACompleteSetup_HasNoProblems() => Valid().Problems().Should().BeEmpty(); + + [Theory] + [InlineData("provider")] + [InlineData("sitekey")] + [InlineData("secret")] + [InlineData("url")] + [InlineData("timeout")] + public void EachMissingOrInvalidSetting_IsReportedByName_NeverByValue(string which) + { + var o = Valid(); + switch (which) + { + case "provider": o.Provider = "other"; break; + case "sitekey": o.SiteKey = ""; break; + case "secret": o.Secret = null; break; + case "url": o.VerifyUrl = "http://insecure.example"; break; + default: o.TimeoutSeconds = 0; break; + } + + var problems = o.Problems(); + + problems.Should().ContainSingle(); + problems.Single().Should().Contain("Contact:Captcha:"); + problems.Single().Should().NotContain("key").And.NotContain("insecure"); + } + + [Fact] + public void TheProviderNameIsCaseInsensitive() + { + var o = Valid(); + o.Provider = "FriendlyCaptcha"; + o.IsFriendlyCaptcha.Should().BeTrue(); + } +} + +public class CaptchaSubmissionRuleTests +{ + [Fact] + public async Task WhileOn_AMissingToken_IsSpam_AndTheProviderIsNotAsked() + { + using var h = new SubmissionHarness(o => o.Captcha.Enabled = true); + + var result = await h.Submit(SubmissionHarness.Request(h.MatureToken(), captcha: null)); + + result.Should().BeOfType(); + (await h.Db.ContactMessages.ToListAsyncForTests()).Single().Status.Should().Be(Data.Entities.ContactMessageStatus.Spam); + await h.Captcha.DidNotReceiveWithAnyArgs().VerifyAsync(default, default); + } + + [Fact] + public async Task WhileOn_APresentToken_GoesToTheProvider() + { + using var h = new SubmissionHarness(o => o.Captcha.Enabled = true); + + await h.Submit(SubmissionHarness.Request(h.MatureToken(), captcha: "solved")); + + await h.Captcha.Received(1).VerifyAsync("solved", Arg.Any()); + (await h.Db.ContactMessages.ToListAsyncForTests()).Single().Status.Should().Be(Data.Entities.ContactMessageStatus.New); + } + + [Fact] + public async Task WhileOff_NoTokenIsNeeded() + { + using var h = new SubmissionHarness(); + + await h.Submit(SubmissionHarness.Request(h.MatureToken())); + + (await h.Db.ContactMessages.ToListAsyncForTests()).Single().Status.Should().Be(Data.Entities.ContactMessageStatus.New); + } +} + +internal static class QueryExtensions +{ + public static Task> ToListAsyncForTests(this IQueryable source) => + Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync(source); +} diff --git a/src/SlpModularCms.Modules.Contact.Tests/Pipeline/ContactCaptchaPipelineTests.cs b/src/SlpModularCms.Modules.Contact.Tests/Pipeline/ContactCaptchaPipelineTests.cs new file mode 100644 index 0000000..a9e76b1 --- /dev/null +++ b/src/SlpModularCms.Modules.Contact.Tests/Pipeline/ContactCaptchaPipelineTests.cs @@ -0,0 +1,181 @@ +using System.Net; +using System.Text.Json; +using FluentAssertions; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using SlpModularCms.Modules.Contact.Data.Entities; +using SlpModularCms.Modules.Contact.Services; + +namespace SlpModularCms.Modules.Contact.Tests.Pipeline; + +/// The real host with Friendly Captcha switched on and a fake provider (no network). +public sealed class CaptchaContactHostFactory() : ContactHostFactory(new() +{ + ["Contact:Enabled"] = "true", + ["Contact:SiteId"] = "pipeline-site", + ["Contact:MinSeconds"] = "0", + ["Contact:Captcha:Enabled"] = "true", + ["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", + ["RateLimiting:ContactForm:PermitLimit"] = "1000", + ["RateLimiting:ContactToken:PermitLimit"] = "1000", +}) +{ + /// What the fake provider answers next: status and JSON. + public volatile string Answer = """{"success":true}"""; + public volatile int AnswerStatus = 200; + + protected override bool OpenCmsSwitch => true; + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + base.ConfigureWebHost(builder); + var handler = new FakeCaptchaHandler((_, _) => + Task.FromResult(FakeCaptchaHandler.Json((HttpStatusCode)AnswerStatus, Answer))); + Handler = handler; + builder.ConfigureTestServices(services => + services.AddHttpClient(FriendlyCaptchaVerifier.HttpClientName) + .ConfigurePrimaryHttpMessageHandler(() => handler)); + } + + public FakeCaptchaHandler Handler { get; private set; } = null!; +} + +public class ContactCaptchaPipelineTests(CaptchaContactHostFactory factory) : IClassFixture +{ + private static string Body(string token, string name, string? captchaToken, string subject = "website", string message = "Ik wil graag een offerte.") => + JsonSerializer.Serialize(new + { + name, email = "jan@example.nl", subject, message, hpCheck = "", token, captchaToken, + }); + + private async Task<(HttpResponseMessage Response, ContactMessage Row)> SubmitAsync(string name, string? captchaToken, string subject = "website", string message = "Ik wil graag een offerte.") + { + var client = factory.CreateClient(); + var token = await Http.GetTokenAsync(client); + var response = await Http.PostAsync(client, Body(token, name, captchaToken, subject, message)); + var row = await factory.WithDb(db => db.ContactMessages.AsNoTracking().SingleAsync(m => m.Name == name)); + return (response, row); + } + + [Fact] + public async Task TheTokenResponse_AnnouncesTheProviderAndSiteKey_AndNothingElse() + { + var response = await factory.CreateClient().GetAsync($"{Http.Route}/token"); + + var text = await response.Content.ReadAsStringAsync(); + using var json = JsonDocument.Parse(text); + 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); + text.Should().NotContain("pipeline-secret-ZXQ").And.NotContain("frcapi.com"); + } + + [Fact] + public async Task AGoodAnswer_StoresAGenuineMessage_AndTheProviderGotTheKeyAndTheEuUrl() + { + factory.AnswerStatus = 200; + factory.Answer = """{"success":true}"""; + + var (response, row) = await SubmitAsync("Captcha Genuine", "solved-response"); + + response.StatusCode.Should().Be(HttpStatusCode.Accepted); + row.Status.Should().Be(ContactMessageStatus.New); + var (request, body) = factory.Handler.Calls.Last(c => c.Body.Contains("solved-response")); + request.RequestUri!.Host.Should().Be("eu.frcapi.com"); + request.Headers.GetValues("X-API-Key").Single().Should().Be("pipeline-secret-ZXQ"); + body.Should().Contain("FCMPIPELINEKEY"); + } + + [Fact] + public async Task ARejectedAnswer_IsSpam_WithTheSame202AndBodyAsAGenuineMessage() + { + factory.AnswerStatus = 200; + factory.Answer = """{"success":false,"error":{"error_code":"response_invalid"}}"""; + var (spamResponse, spamRow) = await SubmitAsync("Captcha Spam", "bad-response"); + factory.Answer = """{"success":true}"""; + var (genuineResponse, _) = await SubmitAsync("Captcha Genuine Two", "good-response"); + + spamRow.Status.Should().Be(ContactMessageStatus.Spam); + spamRow.MailStatus.Should().Be(ContactMailStatus.NotApplicable); + spamResponse.StatusCode.Should().Be(HttpStatusCode.Accepted); + (await spamResponse.Content.ReadAsStringAsync()).Should().Be(await genuineResponse.Content.ReadAsStringAsync()); + } + + [Fact] + public async Task AMissingToken_IsSpam_Not400_AndTheProviderIsNotAsked() + { + var before = factory.Handler.Calls.Count; + + var (response, row) = await SubmitAsync("Captcha Missing", null); + + response.StatusCode.Should().Be(HttpStatusCode.Accepted); + row.Status.Should().Be(ContactMessageStatus.Spam); + factory.Handler.Calls.Count.Should().Be(before); + } + + [Theory] + [InlineData(500)] + [InlineData(503)] + [InlineData(401)] + public async Task AProviderOutageOrOurWrongKey_FailsOpen_AndTheMessageIsGenuine(int status) + { + factory.AnswerStatus = status; + factory.Answer = ""; + try + { + var (response, row) = await SubmitAsync($"Captcha Open {status}", "some-response"); + + response.StatusCode.Should().Be(HttpStatusCode.Accepted); + row.Status.Should().Be(ContactMessageStatus.New); + } + finally + { + factory.AnswerStatus = 200; + factory.Answer = """{"success":true}"""; + } + } + + [Fact] + public async Task ALabelWithSpecialCharacters_IsStoredAndListedAsTyped() + { + const string label = "Fotoshoot & \"bruiloft\" 'zomer'"; + var (response, row) = await SubmitAsync("Captcha Label", "solved-response", subject: label, message: "Hallo daar & \"dag\""); + + response.StatusCode.Should().Be(HttpStatusCode.Accepted); + row.Subject.Should().Be(label); + var admin = await Jwt.ClientWithRoleAsync(factory, "Administrator"); + var list = await admin.GetStringAsync($"{Http.Route}?pageSize=100"); + using var json = JsonDocument.Parse(list); + json.RootElement.GetProperty("items").EnumerateArray() + .Single(i => i.GetProperty("name").GetString() == "Captcha Label") + .GetProperty("subject").GetString().Should().Be(label); + } +} + +public class CaptchaStartupTests +{ + private sealed class MissingSecretFactory() : ContactHostFactory(new() + { + ["Contact:Enabled"] = "true", + ["Contact:Captcha:Enabled"] = "true", + ["Contact:Captcha:Provider"] = "friendlycaptcha", + ["Contact:Captcha:SiteKey"] = "FCMPIPELINEKEY", + }); + + [Fact] + public void ASwitchedOnCaptchaWithoutSecret_RefusesToStart_NamingTheSetting() + { + using var factory = new MissingSecretFactory(); + + var act = () => factory.CreateClient(); + + act.Should().Throw() + .Where(e => e.ToString().Contains("Contact:Captcha:Secret") && !e.ToString().Contains("FCMPIPELINEKEY")); + } +} diff --git a/src/SlpModularCms.Modules.Contact/Config/ContactOptions.cs b/src/SlpModularCms.Modules.Contact/Config/ContactOptions.cs index 1d05b79..3186570 100644 --- a/src/SlpModularCms.Modules.Contact/Config/ContactOptions.cs +++ b/src/SlpModularCms.Modules.Contact/Config/ContactOptions.cs @@ -73,12 +73,55 @@ public class ContactMailOptions } } +/// Start-up validation: only checks anything while the module and the captcha are switched on. +public sealed class ContactOptionsValidator : Microsoft.Extensions.Options.IValidateOptions +{ + public Microsoft.Extensions.Options.ValidateOptionsResult Validate(string? name, ContactOptions options) + { + if (!options.Enabled) return Microsoft.Extensions.Options.ValidateOptionsResult.Success; + var problems = options.Captcha.Problems(); + return problems.Count == 0 + ? Microsoft.Extensions.Options.ValidateOptionsResult.Success + : Microsoft.Extensions.Options.ValidateOptionsResult.Fail(problems); + } +} + public class ContactCaptchaOptions { + public const string FriendlyCaptcha = "friendlycaptcha"; + public const string DefaultVerifyUrl = "https://global.frcapi.com/api/v2/captcha/siteverify"; + public bool Enabled { get; set; } + + /// The captcha provider; the only supported value is friendlycaptcha (Friendly Captcha v2). public string? Provider { get; set; } + + /// Public site key; it is also sent to the browser in the token response. public string? SiteKey { get; set; } - /// Server-side secret; supply it through the environment or a secrets manager, never committed. + /// Server-side API key; supply it through the environment (Contact__Captcha__Secret) or a secrets manager, never committed. public string? Secret { get; set; } + + /// Verify endpoint. Global by default; for the EU-only endpoint use https://eu.frcapi.com/api/v2/captcha/siteverify. + public string VerifyUrl { get; set; } = DefaultVerifyUrl; + + /// A provider that does not answer within this time counts as passed (fail-open). + public int TimeoutSeconds { get; set; } = 3; + + public bool IsFriendlyCaptcha => + string.Equals(Provider?.Trim(), FriendlyCaptcha, StringComparison.OrdinalIgnoreCase); + + /// The problems that stop the host from starting while the captcha is on (setting names only, never values). + public IReadOnlyList Problems() + { + if (!Enabled) return []; + var problems = new List(); + 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."); + if (TimeoutSeconds is < 1 or > 30) problems.Add("Contact:Captcha:TimeoutSeconds must be between 1 and 30."); + return problems; + } } diff --git a/src/SlpModularCms.Modules.Contact/ContactModule.cs b/src/SlpModularCms.Modules.Contact/ContactModule.cs index 6ddbe14..6a17539 100644 --- a/src/SlpModularCms.Modules.Contact/ContactModule.cs +++ b/src/SlpModularCms.Modules.Contact/ContactModule.cs @@ -30,7 +30,10 @@ public class ContactModule : IModule { services.AddOptions() .Configure((options, config) => - config.GetSection(ContactOptions.SectionName).Bind(options)); + config.GetSection(ContactOptions.SectionName).Bind(options)) + .ValidateOnStart(); + // A switched-on captcha with missing settings stops the host at start-up (setting names only, never a value). + services.AddSingleton, ContactOptionsValidator>(); services.AddDbContext((serviceProvider, options) => { @@ -43,7 +46,14 @@ public class ContactModule : IModule services.AddScoped(); services.AddScoped(); services.AddSingleton(); - services.AddSingleton(); + // Captcha: the provider is chosen by configuration; while it is off (or unset) everything passes. + services.AddHttpClient(FriendlyCaptchaVerifier.HttpClientName); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(sp => + sp.GetRequiredService>().Value.Captcha is { Enabled: true, IsFriendlyCaptcha: true } + ? sp.GetRequiredService() + : sp.GetRequiredService()); // Mail: the request only stores and signals; the background service sends (and retries) with the host's mail pieces. services.AddScoped(); services.AddSingleton(); diff --git a/src/SlpModularCms.Modules.Contact/Controllers/ContactMessagesController.cs b/src/SlpModularCms.Modules.Contact/Controllers/ContactMessagesController.cs index f0306bf..3d3ea35 100644 --- a/src/SlpModularCms.Modules.Contact/Controllers/ContactMessagesController.cs +++ b/src/SlpModularCms.Modules.Contact/Controllers/ContactMessagesController.cs @@ -37,8 +37,9 @@ public class ContactMessagesController( public IActionResult GetToken() { var settings = options.Value; - var captcha = settings.Captcha is { Enabled: true } c && !string.IsNullOrWhiteSpace(c.Provider) - ? new ContactCaptchaInfo(c.Provider!, c.SiteKey ?? string.Empty) + // 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!) : null; return Ok(new ContactTokenResponse(tokens.Issue(), settings.MinSeconds, captcha)); diff --git a/src/SlpModularCms.Modules.Contact/OpenApi/ContactOpenApiDocumentTransformer.cs b/src/SlpModularCms.Modules.Contact/OpenApi/ContactOpenApiDocumentTransformer.cs index 7b89045..c012c16 100644 --- a/src/SlpModularCms.Modules.Contact/OpenApi/ContactOpenApiDocumentTransformer.cs +++ b/src/SlpModularCms.Modules.Contact/OpenApi/ContactOpenApiDocumentTransformer.cs @@ -32,6 +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.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); @@ -126,6 +127,7 @@ public sealed class ContactOpenApiDocumentTransformer : IOpenApiDocumentTransfor Schema = new OpenApiSchema { Type = JsonSchemaType.String, Format = "uuid" }, }); + submit.Description = "Stores a message. While the captcha is on, 'captchaToken' (the widget response) is required: a missing, empty or rejected value is treated as spam and answered exactly like a genuine message (202, stored with status spam), never with a 400. A captcha provider that does not answer in time, or fails, lets the visitor through (fail-open)."; submit.Responses ??= []; submit.Responses["400"] = new OpenApiResponseReference("Validation", document); submit.Responses["404"] = new OpenApiResponse { Description = "The contact form is closed on this site (the website shows its mailto fallback)." }; diff --git a/src/SlpModularCms.Modules.Contact/Services/ContactSubmissionService.cs b/src/SlpModularCms.Modules.Contact/Services/ContactSubmissionService.cs index 97daa7f..5cced79 100644 --- a/src/SlpModularCms.Modules.Contact/Services/ContactSubmissionService.cs +++ b/src/SlpModularCms.Modules.Contact/Services/ContactSubmissionService.cs @@ -90,7 +90,9 @@ public sealed class ContactSubmissionService( // 4. Classify. A filled honeypot, a too-fast submission or a rejected captcha is spam. var isSpam = tokenCheck == TokenCheck.TooYoung || !string.IsNullOrEmpty(request.HpCheck) - || (o.Captcha.Enabled && !await captcha.VerifyAsync(request.CaptchaToken, cancellationToken)); + || (o.Captcha.Enabled + && (string.IsNullOrWhiteSpace(request.CaptchaToken) // required while on: missing = spam, never a 400 + || !await captcha.VerifyAsync(request.CaptchaToken, cancellationToken))); // 5. Store. Spam is kept for a short time (a real visitor may have tripped a check) but // capped per day so a flood cannot fill the table. diff --git a/src/SlpModularCms.Modules.Contact/Services/FriendlyCaptchaVerifier.cs b/src/SlpModularCms.Modules.Contact/Services/FriendlyCaptchaVerifier.cs new file mode 100644 index 0000000..5452f1b --- /dev/null +++ b/src/SlpModularCms.Modules.Contact/Services/FriendlyCaptchaVerifier.cs @@ -0,0 +1,109 @@ +using System.Net.Http.Json; +using System.Text.Json; +using System.Text.Json.Serialization; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SlpModularCms.Modules.Contact.Config; + +namespace SlpModularCms.Modules.Contact.Services; + +/// +/// Verifies a Friendly Captcha v2 response server-side (POST {VerifyUrl}, header X-API-Key, JSON +/// { response, sitekey }). Only a definite negative answer counts as failed. Everything that is not the +/// visitor's fault (timeout, network error, 5xx, our own wrong key or site key) lets the visitor through and is logged +/// without personal data: the response token, the key and the secret are never logged. +/// +public sealed class FriendlyCaptchaVerifier( + IHttpClientFactory httpClients, + IOptions options, + ILogger logger) : ICaptchaVerifier +{ + public const string HttpClientName = "contact-captcha"; + public const string ApiKeyHeader = "X-API-Key"; + + private sealed record VerifyRequest( + [property: JsonPropertyName("response")] string Response, + [property: JsonPropertyName("sitekey")] string SiteKey); + + private sealed class VerifyError + { + [JsonPropertyName("error_code")] public string? ErrorCode { get; set; } + } + + private sealed class VerifyResponse + { + [JsonPropertyName("success")] public bool Success { get; set; } + [JsonPropertyName("error")] public VerifyError? Error { get; set; } + } + + public async Task VerifyAsync(string? captchaToken, CancellationToken cancellationToken) + { + if (string.IsNullOrWhiteSpace(captchaToken)) + { + return false; + } + + var captcha = options.Value.Captcha; + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + timeout.CancelAfter(TimeSpan.FromSeconds(Math.Clamp(captcha.TimeoutSeconds, 1, 30))); + + try + { + using var request = new HttpRequestMessage(HttpMethod.Post, captcha.VerifyUrl) + { + Content = JsonContent.Create(new VerifyRequest(captchaToken, captcha.SiteKey ?? string.Empty)), + }; + request.Headers.TryAddWithoutValidation(ApiKeyHeader, captcha.Secret); + + using var response = await httpClients.CreateClient(HttpClientName).SendAsync(request, timeout.Token); + var status = (int)response.StatusCode; + + VerifyResponse? body = null; + if (status < 500) + { + try + { + body = await response.Content.ReadFromJsonAsync(timeout.Token); + } + catch (JsonException) + { + // Not JSON: judged by the status code below. + } + } + + if (status >= 500) + { + logger.LogWarning("captcha_unavailable status={Status}; letting the visitor through", status); + return true; + } + + // Our own configuration is wrong (API key or site key rejected): not the visitor's fault. + if (status is 401 or 403 || body?.Error?.ErrorCode == "sitekey_invalid") + { + logger.LogError("captcha_misconfigured status={Status} code={Code}; letting the visitor through", + status, body?.Error?.ErrorCode); + return true; + } + + if (response.IsSuccessStatusCode && body is { Success: true }) + { + return true; + } + + // A definite no (wrong, expired or reused response, or a missing/bad request): spam. + return false; + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + logger.LogWarning("captcha_unavailable reason=timeout; letting the visitor through"); + return true; + } + catch (HttpRequestException ex) + { + // Type only: the message could quote the URL. + logger.LogWarning("captcha_unavailable reason=network exception={ExceptionType}; letting the visitor through", + ex.GetType().Name); + return true; + } + } +}