fix(api): prevent IP rate-limit bypass via unvalidated x-api-key header without breaking /v1/* per-key throttling
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 52/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- TypeScript
- スター
- 2
- フォーク
- 1
- 平均マージ
- 6分
- マージ済み PR(30日)
- 27
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
sumamakhan761/tryinstance のほかの issue
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
sumamakhan761/tryinstance#65 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
sumamakhan761/tryinstance#64 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
sumamakhan761/tryinstance#63 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
sumamakhan761/tryinstance#62 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
sumamakhan761/tryinstance#60 ·
メンテナーはふだん 1 日以内に返信
sumamakhan761/tryinstance の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
makecindy/cindy#15342 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
kwitsch/claude-code-plugin-tooling#219 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信
-
no-repo-link tool-submission
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
BraveOPotato/NoSignups#2618 ·
-
enhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100