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

オープン
#31 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
api, backend, security

調査の方向性

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 の本文から書いたものです。

説明

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.
主要言語
TypeScript
スター
2
フォーク
1
平均マージ
6分
マージ済み PR(30日)
27

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sumamakhan761/tryinstance のほかの issue

sumamakhan761/tryinstance の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。