Send a test event to the tenant's webhook
C# · ASP.NET Core · intermediate · modification
Backend for the new "Send test event" button in Settings > Webhooks, plus stricter checks when the URL is saved. `Webhooks/WebhookAddress.cs` holds the URL rules used on save and again before a test send: `https` only, no `localhost`, and no `localhost` names and no IP address from the ranges in the save rules as the host (`IsPublic` holds that range list, IPv4-mapped forms included). `Webhooks/WebhookTestSender.cs` builds a signed `webhook.test` event, posts it through the named `webhooks` client under a 5-second deadline that also covers reading the answer, and keeps at most 2 KB of the body. `Settings/SettingsEndpoints.cs` validates on save and adds `POST /api/settings/webhook/test`; `Program.cs` registers the named client and the sender. `WebhookTestEventTests.cs` covers the save rules and the test route against a fake receiver handler.
ASP.NET Core 10 minimal API on .NET 10 (C# 14, nullable reference types enabled, implicit usings), xUnit for tests. The API runs as containers in the company VPC (10.20.0.0/16, IPv4 only) next to internal services such as the billing admin service at `https://billing-admin.corp.internal` (10.20.4.17); the VPC's DNS resolves `*.corp.internal` names to their 10.20.x addresses, and internal services serve HTTPS with certificates from the company's internal CA, which is in the API containers' trust store. The cloud instance metadata service answers at 169.254.169.254. Outbound internet egress is allowed directly: there is no proxy and no proxy environment variable is set. Event deliveries are sent by the existing delivery service, which runs outside the VPC and is not part of this PR; before this PR the API itself never sent requests to tenant URLs. Existing and unchanged: `ITenantSettingsStore` (EF Core on Postgres) with `GetWebhookAsync(tenantId, ct)`, returning `WebhookSettings(Url, SigningSecret)` (`Url` is `null` until one is saved, every tenant has a signing secret), and `SaveWebhookUrlAsync(tenantId, url, ct)`; `WebhookSigner.Sign(secret, payload)` returns the `X-Portal-Signature` header value (`sha256=` plus the hex HMAC of the payload). Authentication is a JWT bearer token in the `Authorization` header (no cookies); the `TenantAdmin` policy requires the `admin` role and a `tenant_id` claim, which `User.GetTenantId()` (`Portal.Api.Auth`) reads. `IHttpClientFactory` runs with its default options. The settings page shows the returned body as plain text and ships separately. Tests use the existing `PortalApiFactory` (`WebApplicationFactory<Program>` with a test authentication scheme) and its `CreateClientFor(userId, tenantId, role)` extension; in tests the store is an in-memory fake and the signing secret of tenant `t-1` is `whsec_t-1`.
Requirements
- Settings > Webhooks (existing) stores one outgoing webhook URL per tenant, saved with `PUT /api/settings/webhook` and `{ "url" }`. Saving now answers 400 `{ "error" }` for a URL that is not an absolute `https` URL or whose host is `localhost`, a name ending in `.localhost`, or an IP address in one of these ranges, so the admin sees the mistake when saving: IPv4 `0.0.0.0/8`, `10.0.0.0/8`, `100.64.0.0/10`, `127.0.0.0/8`, `169.254.0.0/16`, `172.16.0.0/12`, `192.0.0.0/24`, `192.0.2.0/24`, `192.168.0.0/16`, `198.18.0.0/15`, `198.51.100.0/24`, `203.0.113.0/24`, `224.0.0.0/3`; IPv6 `::/96`, `2001:db8::/32`, `fc00::/7`, `fe80::/10`, `fec0::/10`, `ff00::/8`, and IPv4-mapped addresses (`::ffff:a.b.c.d`) whose IPv4 address is in one of the IPv4 ranges.
- New `POST /api/settings/webhook/test` sends a sample event (`type: "webhook.test"`, signed with the tenant's signing secret like every delivery) from the API to the saved URL and answers 200 `{ status, body, truncated }`: the receiver's HTTP status code and the first 2,048 bytes of its response body as UTF-8 text (bytes that do not decode show as U+FFFD), so the admin can debug their receiver. No saved URL answers 404; a URL saved before this PR that does not meet the save rules answers 422; a receiver that cannot be reached, or does not answer with a status and the first 2,048 bytes of its body (or the whole body, if shorter) within 5 seconds, answers 502 `{ "error" }`.
- Webhook URLs must be public https endpoints on the internet. The API runs inside our network, so it must not send requests to addresses inside our network or to the cloud metadata service, whatever URL a tenant admin saves.
- Unchanged: every `/api/settings` route requires a signed-in tenant admin (401 without a token, 403 for other roles) and is limited to 60 requests a minute per tenant.
Files touched
- src/Portal.Api/Program.cs
- src/Portal.Api/Settings/SettingsEndpoints.cs
- src/Portal.Api/Webhooks/WebhookAddress.cs
- src/Portal.Api/Webhooks/WebhookTestSender.cs
- tests/Portal.Api.Tests/WebhookTestEventTests.cs