Skip to content

feat: add rate limit on /api/proxy - #1122

Merged
greatest0fallt1me merged 1 commit into
CalloraOrg:mainfrom
Kaycee276:fix-b085
Jul 29, 2026
Merged

feat: add rate limit on /api/proxy#1122
greatest0fallt1me merged 1 commit into
CalloraOrg:mainfrom
Kaycee276:fix-b085

Conversation

@Kaycee276

Copy link
Copy Markdown
Contributor

Title

feat: Add per-user token-bucket rate limit on /api/proxy

Description

This PR implements a per-user token-bucket rate limiting mechanism for the /api/proxy routes as outlined for the GrantFox FWC26 campaign. This enhancement ensures secure and controlled access to proxy endpoints by throwing a 429 Too Many Requests error and supplying a Retry-After header when clients exceed their quota.

Key Changes

  • Rate Limit Middleware (src/middleware/rateLimit.ts):
    • Created createProxyRateLimitMiddleware leveraging the TokenBucketRateLimiter.
    • Determines the limiting identity based on a fallback cascade: API Key userId -> Request userId -> Client IP.
    • Returns a standard TooManyRequestsError envelope along with calculated Retry-After response headers.
    • Includes structured warning logs enriched with requestId and retryAfterMs.
  • Route Integration (src/routes/proxyRoutes.ts):
    • Injected proxyRateLimit across all proxy route handlers (GET, POST, PATCH, DELETE, etc.).
    • Positioned the rate limiter immediately before the idempotency layer to protect downstream processing.
  • Types Update (src/types/gateway.ts):
    • Added optional proxyRateLimit request handler to ProxyDeps allowing for injected mocks during tests.
  • Tests (src/middleware/rateLimit.test.ts):
    • (Previously authored) unit tests covering key extraction fallbacks, API Key record scenarios, token bucket capacities, and the 429 status code outputs.

Quality Assurance & Security

  • Algorithm: Implements the efficient token-bucket algorithm allowing for controlled bursts while strictly capping overages.
  • Input Boundaries: Rate limiting identities are carefully extracted securely from validated tokens/API keys preventing limit bypasses.
  • Logging: Integrated standard logger.warn for tracking rate-limit thresholds with correlation IDs attached.

Closes #950

@drips-wave

drips-wave Bot commented Jul 29, 2026

Copy link
Copy Markdown

@Kaycee276 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me
greatest0fallt1me merged commit e178e7d into CalloraOrg:main Jul 29, 2026
1 check passed
@greatest0fallt1me

Copy link
Copy Markdown
Contributor

LGTM 🙌

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add rate limit on /api/proxy [b#085]

2 participants