security: add rate limiting to auth endpoints#662
Open
mmcintosh wants to merge 1 commit intoSonicJs-Org:mainfrom
Open
security: add rate limiting to auth endpoints#662mmcintosh wants to merge 1 commit intoSonicJs-Org:mainfrom
mmcintosh wants to merge 1 commit intoSonicJs-Org:mainfrom
Conversation
b0028ac to
efdc6bd
Compare
| } | ||
|
|
||
| it('valid key with matching scope — calls next()', async () => { | ||
| const hash = await hashApiKey(TOKEN) |
Check notice
Code scanning / CodeQL
Unused variable, import, function or class
| }) | ||
|
|
||
| const mw = requireApiKey('search:read') | ||
| const result = await mw(ctx, mockNext) |
Check notice
Code scanning / CodeQL
Unused variable, import, function or class
- Create KV-based sliding window rate limiter middleware - Apply rate limits to auth endpoints: - Login (JSON + form): 5 requests/minute - Register (JSON + form): 3 requests/minute - Password reset: 3 requests/15 minutes - Seed admin: 2 requests/minute - Includes Retry-After and X-RateLimit-* response headers - Graceful degradation: skips rate limiting if CACHE_KV not available - Export rateLimit from middleware/index.ts Fixes VULN-004
efdc6bd to
0a6ffb2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Security: Add Rate Limiting to Auth Endpoints
Summary
Adds KV-based rate limiting middleware to all authentication endpoints, preventing brute-force login attempts and registration abuse. Gracefully degrades when KV binding is unavailable.
Changes
1. Rate Limiting Middleware
CACHE_KVbindingratelimit:<prefix>:<ip>with auto-expiring TTLcf-connecting-ipheader (Cloudflare),x-forwarded-forfallbackX-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-Reset429 Too Many RequestswithRetry-Afterheader when exceeded2. Auth Endpoints Protected
POST /auth/loginand/auth/login/form-- 5 requests/minute per IPPOST /auth/registerand/auth/register/form-- 3 requests/minute per IPPOST /auth/seed-admin-- 2 requests/minute per IPPOST /auth/request-password-reset-- 3 requests/15 minutes per IP3. Graceful Degradation
CACHE_KVbinding is not availableTechnical Details
New File:
packages/core/src/middleware/rate-limit.ts--rateLimit({ max, windowMs, keyPrefix })middleware factoryUpdated Files:
packages/core/src/middleware/index.ts-- ExportrateLimitpackages/core/src/routes/auth.ts-- AppliedrateLimit()to 6 route handlersRate Limit Configuration:
POST /auth/loginloginPOST /auth/login/formloginPOST /auth/registerregisterPOST /auth/register/formregisterPOST /auth/seed-adminseed-adminPOST /auth/request-password-resetpassword-resetTesting
Performance Impact
Breaking Changes
maxvalues if needed for CI/testing.Migration Notes
CACHE_KVbinding for rate limiting to be active. If the binding is missing, all requests pass through (no rate limiting).Known Issues
None.
Demo / Screenshots
N/A -- no UI changes.
Related Issues
(Security hardening -- no linked issue)
Checklist