fix(api): prevent IP rate-limit bypass via unvalidated x-api-key header without breaking /v1/* per-key throttling

Aperta
#31 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
api, backend, security

Direzione di ricerca

Read apps/api/src/common/guards/api-key-throttler.guard.ts and the guard order in apps/api/src/app.module.ts, then trace how PublicApiKeyGuard sets request.apiKey and is applied in the /v1/* controllers. Check the existing throttler configuration and tests before implementing the IP limit and post-auth per-key limit described here. Done means rotating invalid headers cannot bypass IP throttling, while distinct validated keys retain separate /v1/* buckets; add tests for both cases.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

backend bug
Problem Description

In apps/api/src/common/guards/api-key-throttler.guard.ts (getTracker), throttling keys off req.headers['x-api-key'] or Authorization: Bearer inst_... before the API key is validated:

const rawKey = req.headers['x-api-key'];
if (rawKey && typeof rawKey === 'string') {
  return `api_key:${rawKey.trim().slice(-12)}`;
}

Because ApiKeyThrottlerGuard is registered globally as the first APP_GUARD in apps/api/src/app.module.ts, an attacker hitting any @Public() route can send a rotating dummy x-api-key header on every request to get a fresh rate-limit bucket and completely bypass IP-based throttling.

Note on Guard Ordering:

  • ApiKeyThrottlerGuard (APP_GUARD) runs before controller-level @UseGuards(PublicApiKeyGuard) on /v1/* controllers.
  • PublicApiKeyGuard (apps/api/src/public-api/guards/public-api-key.guard.ts) validates the key and attaches request.apiKey and request.companyId (not req.user?.apiKeyId).
  • Simply falling back to IP everywhere would break per-API-key throttling for /v1/* users behind shared IPs (Zapier, Make, n8n, serverless, or proxies).

Expected Behavior
  1. Global throttling (ApiKeyThrottlerGuard) must never trust raw x-api-key or Bearer inst_... headers; non-/v1/* and @Public() routes must throttle strictly by client IP.
  2. /v1/* public API routes must still throttle per validated API key (request.apiKey.id) after PublicApiKeyGuard runs, while keeping a reasonable IP safety-net against invalid-key spam.

Where to Look
  • apps/api/src/common/guards/api-key-throttler.guard.ts
  • apps/api/src/app.module.ts
  • apps/api/src/public-api/guards/public-api-key.guard.ts
  • apps/api/src/public-api/controllers/*.ts (e.g., v1-analytics.controller.ts)

Solution / Implementation Guide
  1. Use IP-only tracking in ApiKeyThrottlerGuard:
    • Remove the unvalidated req.headers['x-api-key'] and Bearer inst_... branches from ApiKeyThrottlerGuard.getTracker, so the global guard always tracks by client IP (req.ips?.length ? req.ips[0] : req.ip).
  2. Add a post-auth per-key throttler for /v1/*:
    • Configure named throttlers in ThrottlerModule (e.g., default for global IP limits and apiKey for /v1/*) so @SkipThrottle({ default: true }) (or a higher IP safety-net limit) does not accidentally disable per-key throttling.
    • Create a ValidatedApiKeyThrottlerGuard that keys off req.apiKey?.id (falling back to IP if absent) and apply it after PublicApiKeyGuard: @UseGuards(PublicApiKeyGuard, ValidatedApiKeyThrottlerGuard).
  3. Add tests:
    • Verify rotating dummy x-api-key headers on @Public() routes still hits 429 by IP.
    • Verify valid /v1/* requests from the same IP with different API keys get separate per-key buckets.
Lingua principale
TypeScript
Stelle
2
Fork
1
Merge medio
6m
PR unite (30g)
27

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sumamakhan761/tryinstance

Tutte le issue di sumamakhan761/tryinstance

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.