-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(security): harden public auth rate limits #6997
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
40d573d
fix(security): harden public auth rate limits
TheodoreSpeaks 46c4a14
fix(security): fail closed without client IP
TheodoreSpeaks 00761e3
fix(security): backstop public OTP requests
TheodoreSpeaks 2fd9f2c
fix(security): preserve independent rate-limit backstops
TheodoreSpeaks 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
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,92 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { queueTableRows, requestUtilsMockFns, resetDbChainMock, schemaMock } from '@sim/testing' | ||
| import { NextRequest } from 'next/server' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockIsEmailAllowed, mockCheckRateLimitDirect } = vi.hoisted(() => ({ | ||
| mockIsEmailAllowed: vi.fn(), | ||
| mockCheckRateLimitDirect: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/security/deployment', () => ({ isEmailAllowed: mockIsEmailAllowed })) | ||
| vi.mock('@/lib/core/rate-limiter', () => ({ | ||
| RateLimiter: class { | ||
| checkRateLimitDirect = mockCheckRateLimitDirect | ||
| }, | ||
| })) | ||
|
|
||
| import { POST } from '@/app/api/chat/[identifier]/sso/route' | ||
|
|
||
| const deployment = { | ||
| id: 'chat-1', | ||
| authType: 'sso', | ||
| allowedEmails: ['@acme.com'], | ||
| isActive: true, | ||
| } | ||
|
|
||
| function post(email: string): NextRequest { | ||
| return new NextRequest('http://localhost/api/chat/support/sso', { | ||
| method: 'POST', | ||
| headers: { 'content-type': 'application/json' }, | ||
| body: JSON.stringify({ email }), | ||
| }) | ||
| } | ||
|
|
||
| const context = { params: Promise.resolve({ identifier: 'support' }) } | ||
|
|
||
| describe('POST /api/chat/[identifier]/sso', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| resetDbChainMock() | ||
| queueTableRows(schemaMock.chat, [deployment]) | ||
| requestUtilsMockFns.mockGetClientIp.mockReturnValue('127.0.0.1') | ||
| mockCheckRateLimitDirect.mockResolvedValue({ allowed: true }) | ||
| mockIsEmailAllowed.mockReturnValue(true) | ||
| }) | ||
|
|
||
| it('applies both client-IP and chat-resource limits', async () => { | ||
| const response = await POST(post('user@acme.com'), context) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| expect(await response.json()).toEqual({ eligible: true }) | ||
| expect(mockCheckRateLimitDirect).toHaveBeenNthCalledWith( | ||
| 1, | ||
| 'chat-sso:ip:127.0.0.1', | ||
| expect.objectContaining({ maxTokens: 20 }), | ||
| { failClosed: true } | ||
| ) | ||
| expect(mockCheckRateLimitDirect).toHaveBeenNthCalledWith( | ||
| 2, | ||
| 'chat-sso:resource:chat-1', | ||
| expect.objectContaining({ maxTokens: 100 }), | ||
| { failClosed: true } | ||
| ) | ||
| }) | ||
|
|
||
| it('returns 429 when the chat-resource limit is exceeded', async () => { | ||
| mockCheckRateLimitDirect | ||
| .mockResolvedValueOnce({ allowed: true }) | ||
| .mockResolvedValueOnce({ allowed: false, retryAfterMs: 3000 }) | ||
|
|
||
| const response = await POST(post('user@acme.com'), context) | ||
|
|
||
| expect(response.status).toBe(429) | ||
| expect(response.headers.get('Retry-After')).toBe('3') | ||
| }) | ||
|
|
||
| it('retains the chat-resource limit when the client IP cannot be resolved', async () => { | ||
| requestUtilsMockFns.mockGetClientIp.mockReturnValueOnce(null) | ||
|
|
||
| const response = await POST(post('user@acme.com'), context) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| expect(mockCheckRateLimitDirect).toHaveBeenCalledTimes(1) | ||
| expect(mockCheckRateLimitDirect).toHaveBeenCalledWith( | ||
| 'chat-sso:resource:chat-1', | ||
| expect.objectContaining({ maxTokens: 100 }), | ||
| { failClosed: 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.