# Association contact Worker

Plain JavaScript module Worker with no runtime dependencies. Wrangler is the only development dependency; its lockfile is included. The `DB` binding is a local D1 database named `contact`, with a placeholder production ID. Nothing is deployed by these instructions.

## Design

- `POST /api/contact` accepts an `application/json` object. All four fields must be strings and are trimmed. Name and message lengths count Unicode code points. Email uses a practical address-shape check rather than attempting full RFC mailbox parsing.
- The body limit is 10 KB = 10,240 UTF-8 bytes, inclusive. Both declared length and streamed actual bytes are checked, so chunked requests cannot evade it. Unknown JSON fields are ignored but still count toward the byte limit.
- The Worker sends the token, secret and `CF-Connecting-IP` to Cloudflare Siteverify. Only a successful HTTP response with `success: true` permits insertion. Verification errors and a 10-second timeout return 403 without writing. A missing configured secret returns 500.
- D1 uses bound SQL parameters and an autoincrement integer ID. `created_at` is an ISO 8601 UTC timestamp. Client IP is stored as `client_ip` (null when unavailable), but is omitted from the admin response. Tokens are never stored.
- Administrator credentials are compared using timing-safe SHA-256 digest comparison. Missing configuration fails closed. Internal exceptions return generic JSON errors without exposing personal data or secrets.
- CORS permits POST from any origin and the Content-Type request header. OPTIONS on `/api/contact` returns an empty 204 as HTTP requires. JSON responses, including errors, carry CORS headers. Admin access is intended for same-origin or server-side tools; cross-origin Authorization preflights are not enabled. Unknown paths return JSON 404. Known routes with unsupported methods return JSON 405 plus Allow.

## Local use

Requires a current Node.js version supported by Wrangler. The integration test additionally uses the built-in `node:sqlite` module (Node 22.13+).

```sh
npm ci
npx wrangler d1 migrations apply DB --local
```

Create a local `.dev.vars` file (ignored by Git) with `TURNSTILE_SECRET` and `ADMIN_TOKEN`. For manual local development, Cloudflare's public always-pass test secret is `1x0000000000000000000000000000000AA`; the always-fail secret is `2x0000000000000000000000000000000AA`. Use a disposable local admin token. Then:

```sh
npx wrangler dev --local
```

The Worker reads secrets from the environment bindings supplied by Cloudflare/Wrangler. No secret values are in the source or Wrangler config.

## Repeatable verification

After applying the migration, run `npm test`. It starts `npx wrangler dev --local` on loopback port 18987 (override with `TEST_PORT`), exercises HTTP routes against real Siteverify using both public test secrets, and inspects the local D1 database read-only. It checks trimming, validation, exact and oversized bodies, multibyte/chunked sizes, auth, missing IDs, CORS, parameterized storage, timestamps, client IP and unchanged row counts after rejected requests.

The test temporarily supplies public test credentials in `.dev.vars`, restores any previous file in `finally`, and stops its own development server. Run it with no other dev server using this project. Successful test submissions remain in the local test database. Logs are in `.wrangler/integration-wrangler.log`. Internet access to Cloudflare Siteverify is necessary for integration tests. No login, remote database, or browser is used.

References: [Siteverify](https://developers.cloudflare.com/turnstile/get-started/server-side-validation/), [D1 prepared statements](https://developers.cloudflare.com/d1/worker-api/prepared-statements/), [Workers best practices](https://developers.cloudflare.com/workers/best-practices/workers-best-practices/).

## Verified result

On 2026-11-23 with Node 26.8.2 and Wrangler 4.166.0, the local migration applied successfully and `npm test` passed all 57 HTTP checks plus database assertions. Both real Cloudflare Turnstile test-secret outcomes were verified. Local generated runtime types confirmed the D1 and timing-safe comparison APIs. The host's memory pressure made initial Wrangler startup slow, but verification completed. Development servers were stopped and temporary test credentials removed. No deployment or Cloudflare login occurred.
