-
Notifications
You must be signed in to change notification settings - Fork 0
🛡️ Sentinel: implement secure dual rate-limiting #118
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
3
commits into
main
Choose a base branch
from
jules-1277239486621716037-75649645
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| packages: | ||
| - "." | ||
|
|
||
| onlyBuiltDependencies: | ||
| - "@prisma/client" | ||
| - "@prisma/engines" | ||
|
|
||
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,147 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { rateLimit, rateLimitDual, resetRateLimits } from '../rate-limit'; | ||
| import { headers } from 'next/headers'; | ||
|
|
||
| vi.mock('next/headers', () => ({ | ||
| headers: vi.fn(), | ||
| })); | ||
|
|
||
| describe('rate-limit.ts', () => { | ||
| beforeEach(() => { | ||
| resetRateLimits(); | ||
| vi.useFakeTimers(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe('rateLimit', () => { | ||
| it('allows requests up to the limit and blocks exceeding ones', () => { | ||
| const key = 'test-key'; | ||
|
|
||
| // Limit of 3 | ||
| expect(rateLimit(key, 3, 60_000).allowed).toBe(true); | ||
| expect(rateLimit(key, 3, 60_000).allowed).toBe(true); | ||
| expect(rateLimit(key, 3, 60_000).allowed).toBe(true); | ||
|
|
||
| // 4th request within the same window should be blocked | ||
| const blocked = rateLimit(key, 3, 60_000); | ||
| expect(blocked.allowed).toBe(false); | ||
| expect(blocked.retryAfterSeconds).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it('sliding window lets requests through after the window expires', () => { | ||
| const key = 'sliding-key'; | ||
|
|
||
| rateLimit(key, 2, 10_000); | ||
| vi.advanceTimersByTime(4000); | ||
| rateLimit(key, 2, 10_000); | ||
|
|
||
| // 3rd request is blocked | ||
| expect(rateLimit(key, 2, 10_000).allowed).toBe(false); | ||
|
|
||
| // Advance by 6001 ms (total 10,001 ms from first hit) - first hit falls out | ||
| vi.advanceTimersByTime(6001); | ||
|
|
||
| // Now we should be allowed again | ||
| expect(rateLimit(key, 2, 10_000).allowed).toBe(true); | ||
| }); | ||
|
|
||
| it('performs cleanup if the buckets size exceeds 10,000', () => { | ||
| // Create over 10,000 stale entries | ||
| for (let i = 0; i < 10005; i++) { | ||
| rateLimit(`key-${i}`, 1, 10_000); | ||
| } | ||
|
|
||
| // Advance time so they all expire | ||
| vi.advanceTimersByTime(11_000); | ||
|
|
||
| // Run another rateLimit to trigger opportunistic cleanup | ||
| rateLimit('new-key', 5, 10_000); | ||
|
|
||
| // Ensure behavior remains correct for the new key | ||
| expect(rateLimit('new-key', 5, 10_000).allowed).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('rateLimitDual', () => { | ||
| it('extracts IP safely and limits requests', async () => { | ||
| const mockHeaders = { | ||
| get: (h: string) => { | ||
| if (h === 'x-forwarded-for') return '203.0.113.195, 198.51.100.1'; | ||
| return null; | ||
| } | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| // Under standard limits (target = 2, ip = 4) | ||
| const res1 = await rateLimitDual('action', 'user@example.com', 2, 60_000, 4, 60_000); | ||
| expect(res1.allowed).toBe(true); | ||
|
|
||
| const res2 = await rateLimitDual('action', 'user@example.com', 2, 60_000, 4, 60_000); | ||
| expect(res2.allowed).toBe(true); | ||
|
|
||
| // 3rd target attempt should block | ||
| const res3 = await rateLimitDual('action', 'user@example.com', 2, 60_000, 4, 60_000); | ||
| expect(res3.allowed).toBe(false); | ||
| }); | ||
|
|
||
| it('uses x-real-ip if x-forwarded-for is missing', async () => { | ||
| const mockHeaders = { | ||
| get: (h: string) => { | ||
| if (h === 'x-real-ip') return '198.51.100.5'; | ||
| return null; | ||
| } | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| const res = await rateLimitDual('action', 'user2@example.com', 2, 60_000, 2, 60_000); | ||
| expect(res.allowed).toBe(true); | ||
| }); | ||
|
|
||
| it('falls back to unknown-ip if all headers are missing', async () => { | ||
| const mockHeaders = { | ||
| get: () => null | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| const res = await rateLimitDual('action', 'user3@example.com', 2, 60_000, 2, 60_000); | ||
| expect(res.allowed).toBe(true); | ||
| }); | ||
|
|
||
| it('blocks by IP first, preventing target lockout (bucket pollution protection)', async () => { | ||
| const mockHeaders = { | ||
| get: (h: string) => { | ||
| if (h === 'x-forwarded-for') return '1.2.3.4'; | ||
| return null; | ||
| } | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeaders); | ||
|
|
||
| // Target limit: 3, IP limit: 2 | ||
| // Execute 2 requests | ||
| await rateLimitDual('login', 'legit@example.com', 3, 60_000, 2, 60_000); | ||
| await rateLimitDual('login', 'legit@example.com', 3, 60_000, 2, 60_000); | ||
|
|
||
| // 3rd request should hit the IP limit first | ||
| const resBlock = await rateLimitDual('login', 'legit@example.com', 3, 60_000, 2, 60_000); | ||
| expect(resBlock.allowed).toBe(false); | ||
|
|
||
| // Change the IP to simulate a different client IP trying to log in as the same legitimate user | ||
| const mockHeadersNewIP = { | ||
| get: (h: string) => { | ||
| if (h === 'x-forwarded-for') return '5.6.7.8'; | ||
| return null; | ||
| } | ||
| }; | ||
| (headers as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockHeadersNewIP); | ||
|
|
||
| // The new IP should be able to attempt login for 'legit@example.com' since the target limit (3) wasn't breached | ||
| // and target bucket wasn't polluted by the IP-blocked requests | ||
| const resAllowed = await rateLimitDual('login', 'legit@example.com', 3, 60_000, 2, 60_000); | ||
| expect(resAllowed.allowed).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove the restating comment.
// 2. Target check secondrepeats the following code. Remove it. The preceding comment already explains why the order matters.🤖 Prompt for AI Agents
Source: Coding guidelines