-
Notifications
You must be signed in to change notification settings - Fork 0
🛡️ Sentinel: implement dual rate-limiting for auth actions #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
projectamazonph
wants to merge
1
commit into
main
Choose a base branch
from
jules-2331528734594301326-ee5087e9
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
|
|
||
| vi.mock('next/headers', () => ({ | ||
| headers: vi.fn(), | ||
| })); | ||
|
|
||
| import { rateLimit, rateLimitDual } from '../rate-limit'; | ||
| import { headers } from 'next/headers'; | ||
|
|
||
| describe('rate-limit.ts', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe('rateLimit', () => { | ||
| it('allows hits up to limit within window', () => { | ||
| const key = 'test-key-1'; | ||
| // Limit 3 | ||
| expect(rateLimit(key, 3, 60_000)).toEqual({ allowed: true, retryAfterSeconds: 0 }); | ||
| expect(rateLimit(key, 3, 60_000)).toEqual({ allowed: true, retryAfterSeconds: 0 }); | ||
| expect(rateLimit(key, 3, 60_000)).toEqual({ allowed: true, retryAfterSeconds: 0 }); | ||
| // 4th hit blocked | ||
| const res = rateLimit(key, 3, 60_000); | ||
| expect(res.allowed).toBe(false); | ||
| expect(res.retryAfterSeconds).toBe(60); | ||
| }); | ||
|
|
||
| it('sliding window lets older hits fall out and allows new hits', () => { | ||
| const key = 'test-key-2'; | ||
| // 3 hits at t=0 | ||
| rateLimit(key, 3, 60_000); | ||
| rateLimit(key, 3, 60_000); | ||
| rateLimit(key, 3, 60_000); | ||
|
|
||
| expect(rateLimit(key, 3, 60_000).allowed).toBe(false); | ||
|
|
||
| // Advance time by 30 seconds | ||
| vi.advanceTimersByTime(30_000); | ||
| expect(rateLimit(key, 3, 60_000).allowed).toBe(false); | ||
|
|
||
| // Advance by another 31 seconds (total 61s from start) | ||
| vi.advanceTimersByTime(31_000); | ||
| // Older hits should have fallen out | ||
| expect(rateLimit(key, 3, 60_000)).toEqual({ allowed: true, retryAfterSeconds: 0 }); | ||
| }); | ||
|
|
||
| it('opportunistically cleans up map when it grows unbounded', () => { | ||
| // Create over 10,000 buckets | ||
| // Fill the Map buckets with old timestamps | ||
| const now = Date.now(); | ||
| const cutoff = now - 60_000; | ||
|
|
||
| // We can trigger cleanup by exceeding 10,000 bucket size. | ||
| // Let's call rateLimit with 10,005 unique keys. | ||
| for (let i = 0; i < 10005; i++) { | ||
| rateLimit(`cleanup-key-${i}`, 5, 60_000); | ||
| } | ||
|
|
||
| // Now let's advance time by 61 seconds so that all of them are considered expired. | ||
| vi.advanceTimersByTime(61_000); | ||
|
|
||
| // Call rateLimit one more time to trigger cleanup. | ||
| // This should clean up all the older keys because all of them are <= cutoff. | ||
| rateLimit('trigger-cleanup', 5, 60_000); | ||
|
|
||
| // To verify cleanup happened, if we check again, it shouldn't hit memory limits or map bounds. | ||
| // The map size has been significantly reduced internally. | ||
| // Let's assert that a new key is allowed. | ||
| expect(rateLimit('new-key', 5, 60_000)).toEqual({ allowed: true, retryAfterSeconds: 0 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('rateLimitDual', () => { | ||
| it('extracts IP from x-forwarded-for first IP and limits requests', async () => { | ||
| const mockHeaders = { | ||
| get: vi.fn((name: string) => { | ||
| if (name === 'x-forwarded-for') return '192.168.1.100, 10.0.0.1'; | ||
| return null; | ||
| }), | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| // Call it 3 times with same email and IP | ||
| // IP limit = 2, Email limit = 3 | ||
| const email = 'user1@example.com'; | ||
| const r1 = await rateLimitDual(email, 2, 3, 60_000); | ||
| expect(r1.allowed).toBe(true); | ||
|
|
||
| const r2 = await rateLimitDual(email, 2, 3, 60_000); | ||
| expect(r2.allowed).toBe(true); | ||
|
|
||
| // Third call should be blocked by IP limit | ||
| const r3 = await rateLimitDual(email, 2, 3, 60_000); | ||
| expect(r3.allowed).toBe(false); | ||
| expect(r3.retryAfterSeconds).toBe(60); | ||
| }); | ||
|
|
||
| it('extracts IP from x-real-ip if x-forwarded-for is missing', async () => { | ||
| const mockHeaders = { | ||
| get: vi.fn((name: string) => { | ||
| if (name === 'x-real-ip') return '203.0.113.1'; | ||
| return null; | ||
| }), | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| // IP limit = 1, Email limit = 2 | ||
| const email = 'user2@example.com'; | ||
| const r1 = await rateLimitDual(email, 1, 2, 60_000); | ||
| expect(r1.allowed).toBe(true); | ||
|
|
||
| const r2 = await rateLimitDual(email, 1, 2, 60_000); | ||
| expect(r2.allowed).toBe(false); | ||
| }); | ||
|
|
||
| it('defaults to unknown-ip if both headers are missing', async () => { | ||
| const mockHeaders = { | ||
| get: vi.fn(() => null), | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| // IP limit = 1, Email limit = 2 | ||
| const email = 'user3@example.com'; | ||
| const r1 = await rateLimitDual(email, 1, 2, 60_000); | ||
| expect(r1.allowed).toBe(true); | ||
|
|
||
| const r2 = await rateLimitDual(email, 1, 2, 60_000); | ||
| expect(r2.allowed).toBe(false); | ||
| }); | ||
|
|
||
| it('blocks on email rate limit even if IP limit is not reached', async () => { | ||
| // Let's mock a scenario where same email is targeted from different IPs (credential stuffing) | ||
| let currentIp = '1.1.1.1'; | ||
| const mockHeaders = { | ||
| get: vi.fn((name: string) => { | ||
| if (name === 'x-real-ip') return currentIp; | ||
| return null; | ||
| }), | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| const email = 'target@example.com'; | ||
| // IP limit = 5, Email limit = 2 | ||
| // Hit 1 from IP 1.1.1.1 | ||
| currentIp = '1.1.1.1'; | ||
| expect((await rateLimitDual(email, 5, 2, 60_000)).allowed).toBe(true); | ||
|
|
||
| // Hit 2 from IP 2.2.2.2 | ||
| currentIp = '2.2.2.2'; | ||
| expect((await rateLimitDual(email, 5, 2, 60_000)).allowed).toBe(true); | ||
|
|
||
| // Hit 3 from IP 3.3.3.3 - should block on email limit (2) | ||
| currentIp = '3.3.3.3'; | ||
| const res = await rateLimitDual(email, 5, 2, 60_000); | ||
| expect(res.allowed).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: projectamazonph/amph-v2
Length of output: 27794
🏁 Script executed:
Repository: projectamazonph/amph-v2
Length of output: 17651
Do not use client-controlled forwarded headers as the IP key.
In this deployment path,
x-forwarded-forandx-real-ipare not rewritten by middleware, sosignUpActionmaps each spoofed firstx-forwarded-forvalue to a differentip:*bucket and can bypass the 10-request IP limit beforehashPasswordruns. Derive the key from a trusted proxy/platform signal and add a spoofed-header regression test.🤖 Prompt for AI Agents