fix(api): prevent IP rate-limit bypass via unvalidated x-api-key header without breaking /v1/* per-key throttling
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
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
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 attachesrequest.apiKeyandrequest.companyId(notreq.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
- Global throttling (
ApiKeyThrottlerGuard) must never trust rawx-api-keyorBearer inst_...headers; non-/v1/*and@Public()routes must throttle strictly by client IP. /v1/*public API routes must still throttle per validated API key (request.apiKey.id) afterPublicApiKeyGuardruns, while keeping a reasonable IP safety-net against invalid-key spam.
Where to Look
apps/api/src/common/guards/api-key-throttler.guard.tsapps/api/src/app.module.tsapps/api/src/public-api/guards/public-api-key.guard.tsapps/api/src/public-api/controllers/*.ts(e.g.,v1-analytics.controller.ts)
Solution / Implementation Guide
- Use IP-only tracking in
ApiKeyThrottlerGuard:- Remove the unvalidated
req.headers['x-api-key']andBearer inst_...branches fromApiKeyThrottlerGuard.getTracker, so the global guard always tracks by client IP (req.ips?.length ? req.ips[0] : req.ip).
- Remove the unvalidated
- Add a post-auth per-key throttler for
/v1/*:- Configure named throttlers in
ThrottlerModule(e.g.,defaultfor global IP limits andapiKeyfor/v1/*) so@SkipThrottle({ default: true })(or a higher IP safety-net limit) does not accidentally disable per-key throttling. - Create a
ValidatedApiKeyThrottlerGuardthat keys offreq.apiKey?.id(falling back to IP if absent) and apply it afterPublicApiKeyGuard:@UseGuards(PublicApiKeyGuard, ValidatedApiKeyThrottlerGuard).
- Configure named throttlers in
- Add tests:
- Verify rotating dummy
x-api-keyheaders on@Public()routes still hits429by IP. - Verify valid
/v1/*requests from the same IP with different API keys get separate per-key buckets.
- Verify rotating dummy
- Lingua principale
- TypeScript
- Stelle
- 2
- Fork
- 1
- Merge medio
- 6m
- PR unite (30g)
- 27
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di sumamakhan761/tryinstance
-
backend good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
sumamakhan761/tryinstance#65 ·
I maintainer di solito rispondono entro 1 giorno
-
backend good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
sumamakhan761/tryinstance#64 ·
I maintainer di solito rispondono entro 1 giorno
-
backend good first issue
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
sumamakhan761/tryinstance#63 ·
I maintainer di solito rispondono entro 1 giorno
-
backend good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
sumamakhan761/tryinstance#62 ·
I maintainer di solito rispondono entro 1 giorno
-
backend good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
sumamakhan761/tryinstance#60 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di sumamakhan761/tryinstance
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
makecindy/cindy#15342 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
kwitsch/claude-code-plugin-tooling#219 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
no-repo-link tool-submission
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 74/100
BraveOPotato/NoSignups#2618 ·
-
enhancement good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100