Commit Graph

14 Commits

Author SHA1 Message Date
2fd2a4a71a security: stop trusting the leftmost x-forwarded-for for rate limits and abuse quotas (FRE-371)
getClientIP (the rate-limit and audit key) took the LEFTMOST x-forwarded-for
hop and read x-real-ip unconditionally. In our topology — client → Cloudflare
→ same-host origin proxy → app — both proxies APPEND, so the leftmost hop is
whatever the sender typed: a rotating X-Forwarded-For minted a fresh rate-limit
bucket per request, and unlimited bug-report quota via the same pattern in
routes/api/the-nook/report.ts. x-real-ip is client-settable too; nothing in
the chain overwrites it. It is no longer read at all.

New trust order (src/server/trusted-client-ip.ts, resolveTrustedClientIp):

  1. a non-loopback socket peer IS the client — its headers are ignored;
  2. cf-connecting-ip (Cloudflare overwrites it at the edge; parsed as an
     address or rejected);
  3. x-forwarded-for counted from the RIGHT, discounting one hop for the
     Cloudflare edge address the origin proxy appended (the topology has
     exactly one trusted proxy below Cloudflare);
  4. "unknown" — callers key stricter shared limits on it, so resolution
     fails closed toward a tighter bucket, never a looser one.

Addresses are never truncated: a /24-truncated key would pool attackers with
the innocents behind them.

Share-vs-copy, stated deliberately: this is a port of the parsing primitives
 FRE-366 reviewed in request-client.ts, NOT an import. resolveClientIp carries
a standing rule (1d9287a) that it never feeds a security decision; lifting
that rule for these callers would couple an access-control change to an
analytics branch under review, and the analytics helper only exposes
truncated prefixes while rate limiting needs the full address. If the two
drift, trusted-client-ip.ts is the one security paths follow. The deploy-side
precondition (origin reachable only via Cloudflare + the same-host proxy) is
documented in the file header; if the app is ever directly internet-reachable
no in-app read can recover the true client.

Tests updated deliberately, not to go green: rate-limit.test.ts now pins the
rightmost-minus-one order, cf-connecting-ip precedence, peer-over-headers,
x-real-ip rejection, and the spoof case (a rotating leftmost hop does not
change the resolved client); report.test.ts pins the quota key stability
under a rotating header. New trusted-client-ip.test.ts covers the helper's
own edges: APIEvent wrapper, loopback forms, unparseable hops failing closed.
2026-10-30 05:06:54 +00:00
e446eb1775 fix(p8-010): move rate-limit store to a shared distributed DB store
Replace the per-Vercel-instance in-memory Map rate-limit cache with an
atomic shared store backed by the existing Turso RateLimit table, so limits
hold across all instances/redeploys and cannot be bypassed by distributing
brute-force attempts across instances (audit finding p8-010, MEDIUM).

- checkRateLimit now performs a single atomic round-trip:
  INSERT ... ON CONFLICT(identifier) DO UPDATE ... RETURNING count, reset_at
  with window-reset semantics (CASE WHEN reset_at < now THEN 1 ELSE count+1).
- The DB is now the primary source of truth (no longer a fire-and-forget
  fallback). The per-instance Map is reduced to a short-TTL local cache used
  ONLY to fast-fail already-blocked identifiers (cuts DB load during brute-
  force storms); it can never let a request bypass the limit.
- ensureRateLimitSchema() creates the table + a UNIQUE identifier index so
  ON CONFLICT upserts are well-defined; added RateLimit to db/create.ts.
- resetLoginRateLimits / clearRateLimitStore invalidate the local cache.
- getClientIP now trusts proxy headers in non-development environments
  (production + test); local dev stays strict against header spoofing.
- bunfig.toml defines import.meta.env.SSR=true so the server-only env guard
  loads under 'bun test'.
- Tests: await clearRateLimitStore in beforeEach (fixes a race where an
  un-awaited clear let leftover rows corrupt the next upsert); unique test
  identifiers; realistic remote-shared-store perf bounds; new p8-010
  distributed-store tests (restart-survival, multi-instance aggregation,
  no bypass by alternating instances).
2026-07-22 18:10:28 -04:00
30b2d03c68 cleanup 2026-05-28 20:22:30 -04:00
d48bbc0fc3 security cleanup, fix turnstile 2026-05-28 16:48:06 -04:00
9fc0a73fea general: hot path cooldown 2026-01-11 14:24:28 -05:00
41b8a5416e fix: reverse check 2026-01-11 11:28:43 -05:00
1fb8f45705 fix: session validation fixes 2026-01-11 11:23:32 -05:00
041b2f8dc2 oof 2026-01-07 14:37:40 -05:00
7e89e6dda2 removed excess comments 2026-01-04 11:14:54 -05:00
0fb071a5d7 password validation meter 2026-01-01 14:51:23 -05:00
8e77727148 config centralized 2026-01-01 02:22:33 -05:00
6e31246060 lockout 2025-12-30 09:50:04 -05:00
f5abf77454 idk h3 at all 2025-12-28 21:56:34 -05:00
1ba20339a8 security hardening 2025-12-28 20:04:29 -05:00