Charlie Roffe

Principal Architect & Engineering Director · Cloud & AI

Madrid, Spain · LinkedIn · GitHub

ADR-019: Layered API Abuse Protection — Rate Limits, App Check & Turnstile

Context

The API is publicly reachable and fronts real money: demo requests fan out to Vertex AI, the certificate parser is CPU-intensive, share links write Firestore documents, and the CV form triggers DNS lookups plus an email dispatch per request. A security review found the protection uneven:
1. Rate limiting covered AI/cert routes but not the CV form or share endpoints.
2. App Check existed but was theatre — the flag defaulted off everywhere and the client sent a hardcoded mock token, so any curl could drive the LLM endpoints.
3. Nothing distinguished humans from bots on the CV form, a lead-capture target for scrapers.
No single mechanism covers these: each control answers a different question and fails differently.

Decision

Adopt a three-layer, individually flagged defense stack — cheapest check first, each layer answering one question:
1. Rate limiting ("too much?"): per-IP 15-minute windows — AI + cert 30, share 60, CV form 5 — with draft-7 RateLimit headers and problem+json 429s (ADR-018).
2. Firebase App Check ("the real web app?"): invisible reCAPTCHA v3 attestation on AI/cert/CV routes, verified by the Admin SDK; 401 problem otherwise.
3. Cloudflare Turnstile ("a human driving?"): Managed-mode widget on the CV form only, token verified server-side against siteverify.
Cross-cutting: invisible-first UX (no challenges for legitimate visitors); independent feature flags flowing through Terraform with dev-first, two-sided rollouts (client capability ships before server enforcement); asymmetric failure postures — client failures fail closed, our failures on the Turnstile path fail open with loud logs so a Cloudflare outage cannot kill CV lead capture, and client-side attestation stalls are capped at 10s; attestation origins are part of all three CSP copies; secrets follow ADR-009 (Secret Manager, bootstrap-placeholder pattern).

Amended 2026-10-16: rate limiting now also covers /api/terraform (sharing the AI/cert limiter of 30), the contact send endpoint (5), the telemetry beacon (100), and POST /mcp (120), and a site-wide cap of 300 AI calls per UTC day (always on, counted in Firestore ai_usage, set by the Terraform variable ai_daily_request_cap) runs after App Check on /api/ai/* (except zero-LLM /copilot/resume). App Check also guards /api/terraform, /api/contact and /api/share, and Turnstile also guards POST /api/contact/send.

Consequences

Benefits:
* Each abuse vector has a proportionate, named control, and hostile traffic is rejected before any DNS, Firestore, or LLM cost is incurred.
* Legitimate visitors see nothing — both attestation layers verified invisible end-to-end on dev and prod.
* Every layer is independently observable (headers, structured logs, Cloudflare analytics) and independently reversible via a Terraform variable.

Trade-offs:
* Three external dependencies sit in the request path; the fail-open posture and the 10s cap bound the blast radius at the cost of a brief unprotected window during a Cloudflare outage.
* A misconfigured (unset) Turnstile secret silently disables that layer — it logs at ERROR (an unreachable or non-OK siteverify logs at WARNING), and a Terraform-managed email alert now fires on every fail-open path, alongside a runbook validation step.
* Privacy-hardened browsers may be escalated or rejected by the attestation layers — accepted for low-stakes protected actions.

Alternatives considered

1. A single CAPTCHA on everything: Rejected — taxes every legitimate visitor to solve a problem only the CV form has, and answers none of the app-attestation or volume questions.
2. API keys / user accounts: Rejected — a portfolio must stay frictionless and anonymous; account systems add attack surface and kill demo conversion.
3. Cloud Armor / WAF in front of Cloud Run: Deferred — adds fixed monthly cost against the zero-budget standby goal; the application-layer stack covers the realistic threat model, and a WAF remains the escalation path under real attack.
4. IP allow/deny lists: Rejected — public demos must be reachable by anyone once; lists are operationally brittle and trivially evaded.

Architecture Decision Records