Harden the contact API and add tested failure recovery

RN
2mo ago
+754-429

The contact API had no throttle despite the README describing one, buffered the entire request before checking its size, and classified upstream outages as client errors. The browser could clear a message and launch another send after an uncertain network failure.

This change adds byte-bounded streamed reads, read/provider deadlines, strict origin/media-type/body validation, a bounded per-isolate IP throttle, request IDs, and explicit 429/502/504 handling without logging message content. The form preserves entered text on uncertain failures. Ten endpoint regression tests cover success, invalid origins/input, chunked and multibyte limits, throttling, honeypot, missing configuration, upstream failures, and method handling.

The complete dependency audit now includes build tools; the lockfile resolves the 11 reported package advisories, with compatible Cloudflare worker types. CI has read-only check permissions, timeouts, concurrency control, and pinned actions. Dependabot and an operations guide cover updates, edge rate limiting, deployment verification, and rollback.

Validation:

  • Fresh npm ci --ignore-scripts and npm run check passed: format, lint, TypeScript, 10 API tests, build, and full dependency audit with zero reported advisories.
  • Playwright/Chromium 143 at 1280×844 and 390×844: rendered page identity, no framework overlay or uncaught JS errors, no horizontal overflow, failed requests preserve the message and successful responses reset the form. Screenshots inspected after reveal animations completed. Contact calls were mocked; no email was sent.
  • Browser plugin unavailable; a temporary browser outside the repository replaced invalid Playwright CDN downloads.
  • Local documentation links and git diff --check passed.

Production limits: the local throttle is not a distributed quota. Cloudflare edge rate limits, production secrets, sender-domain verification, monitoring, and real email delivery still require environment verification. The operations guide states these boundaries explicitly.


Summary by cubic

The contact API previously buffered the entire request before rejecting oversized bodies, had no documented IP throttle, and treated upstream outages as client errors; the form could clear a message and open a mailto fallback after an uncertain network failure.

  • The API now streams reads with byte and time limits, validates origin, media type, and field types strictly, and returns explicit 413/415/429/502/504 responses with a request ID and no message-content logging.
  • The form preserves entered text on uncertain failures and no longer automatically opens mailto, preventing duplicate sends during outages.
  • Ten endpoint regression tests cover success, invalid input, chunked and multibyte body limits, throttling, honeypot, missing service config, provider failures, and method handling.
  • CI pins actions, restricts permissions, adds timeouts and concurrency control, and audits build tooling too; the lockfile resolves 11 reported advisories.
  • Dependabot and a new operations guide document release steps, edge-rate-limit boundaries, monitoring, and rollback.