Skip to content

feat: rate limit and cross-site write check on express, fastify and nestjs adapters - #219

Merged
thegdsks merged 1 commit into
mainfrom
feat/adapter-rate-limit-csrf
Oct 13, 2026
Merged

thegdsks merged 1 commit into
mainfrom
feat/adapter-rate-limit-csrf

Conversation

@thegdsks

Copy link
Copy Markdown
Member

The management and OAuth routes on the Express, Fastify and NestJS adapters had no limiter (the open CodeQL js/missing-rate-limiting alerts) and no cross-site check for cookie sessions. Only nextjs-auth wired CSRF.

What changed

  • New guard.admit(request, relativePath) in the shared adapter guard, called first by all three adapters.
  • Rate limits per client in three tiers: token, registration and authorize routes 30/min, other writes 120/min, reads 300/min. 429 with Retry-After.
  • Cookie-session writes (unsafe method, Cookie header, no Authorization) must come from the request's own origin or trustedOrigins. Sec-Fetch-Site same-origin or none is accepted. Bearer callers are untouched.
  • New options on every adapter: rateLimit, trustedOrigins, csrf. nestjs passes them through (its key list is type-checked, which caught the missing keys).

Behavior change

Both checks are on by default, so a minor bump. Anything above 300 reads/min per client, or a cookie-session write from a different origin than the adapter's Host, now gets 429 or 403. Opt out with rateLimit: false or csrf: false; trustedOrigins covers split frontend and API origins.

Verified

  • core: adapter-admit (8) and adapter-guard (21) tests pass; tsc clean.
  • express 56, fastify 20, nestjs 19 adapter tests pass, plus new express tests for 429 and 403 through a real router.
  • Adapters were run against a freshly built core dist.

Not verified

  • Whether CodeQL stops flagging the routes. It may only recognise express-rate-limit style sanitizers; if the alerts remain I will dismiss them with a reason pointing at admit.
  • Counters are in process memory, so multi-instance deployments get per-instance limits. Documented in docs/adapters/index.mdx.
  • Without trustedProxy the client IP is unknown, so callers are split by credential hash, and anonymous callers share one bucket.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your first pull request. CI will run shortly. If the change touches package source, please add a changeset with pnpm changeset.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
8.6% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@thegdsks
thegdsks merged commit 2c6f86d into main Oct 13, 2026
12 of 13 checks passed
@thegdsks
thegdsks deleted the feat/adapter-rate-limit-csrf branch October 13, 2026 16:35
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.22222% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/core/src/adapter-admit.ts 92.85% 4 Missing and 2 partials ⚠️
packages/core/src/adapter-guard.ts 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant