-
-
Notifications
You must be signed in to change notification settings - Fork 2
🛡️ Sentinel: [CRITICAL] Fix Auth Bypass - Enforce Invite Code Validation #411
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
criptogus
wants to merge
1
commit into
main
Choose a base branch
from
sentinel-auth-invite-bypass-fix-6239222094704057239
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,184 @@ | ||
| import { jest } from '@jest/globals'; | ||
|
|
||
| // Mock database service BEFORE importing routes | ||
| jest.unstable_mockModule('@/services/database', () => ({ | ||
| query: jest.fn(), | ||
| queryOne: jest.fn(), | ||
| transaction: jest.fn(), | ||
| pool: { connect: jest.fn() }, | ||
| })); | ||
|
|
||
| // Mock bcryptjs to avoid CPU usage | ||
| jest.unstable_mockModule('bcryptjs', () => ({ | ||
| default: { | ||
| hash: jest.fn().mockResolvedValue('hashed_password'), | ||
| compare: jest.fn().mockResolvedValue(true), | ||
| }, | ||
| })); | ||
|
|
||
| // Mock login attempts service to avoid side effects | ||
| jest.unstable_mockModule('@/services/login-attempts.service', () => ({ | ||
| isAccountLocked: jest.fn().mockReturnValue({ locked: false }), | ||
| recordFailedAttempt: jest.fn(), | ||
| recordSuccessfulLogin: jest.fn(), | ||
| })); | ||
|
|
||
| const { queryOne, transaction } = await import('@/services/database'); | ||
| const request = (await import('supertest')).default; | ||
| const express = (await import('express')).default; | ||
| // Import the router | ||
| const { authRoutes } = await import('@/routes/auth'); | ||
| // Import error handler | ||
| const { errorHandler } = await import('@/middleware/errorHandler'); | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()); | ||
| app.use('/auth', authRoutes); | ||
| // Use error handler middleware | ||
| app.use(errorHandler); | ||
|
|
||
| describe('Auth Registration Invite Code Protection', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should FAIL registration WITHOUT invite code', async () => { | ||
| // Mock email check (returns null = user does not exist) | ||
| (queryOne as jest.Mock).mockResolvedValueOnce(null); | ||
|
|
||
| // Attempt registration without inviteCode | ||
| const res = await request(app) | ||
| .post('/auth/register') | ||
| .send({ | ||
| email: 'test@example.com', | ||
| password: 'password12345678', | ||
| full_name: 'Test User' | ||
| }); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toMatch(/convite/i); | ||
| }); | ||
|
|
||
| it('should FAIL registration with INVALID invite code', async () => { | ||
| // Mock email check (returns null = user does not exist) | ||
| (queryOne as jest.Mock).mockResolvedValueOnce(null); | ||
|
|
||
| // Mock invite check (returns null = invalid code) | ||
| (queryOne as jest.Mock).mockResolvedValueOnce(null); | ||
|
|
||
| const res = await request(app) | ||
| .post('/auth/register') | ||
| .send({ | ||
| email: 'test@example.com', | ||
| password: 'password12345678', | ||
| full_name: 'Test User', | ||
| inviteCode: 'INVALID' | ||
| }); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toMatch(/inválido/i); | ||
| }); | ||
|
|
||
| it('should FAIL registration with EXPIRED invite code', async () => { | ||
| // Mock email check | ||
| (queryOne as jest.Mock).mockResolvedValueOnce(null); | ||
|
|
||
| // Mock invite check (expired) | ||
| (queryOne as jest.Mock).mockResolvedValueOnce({ | ||
| id: 'inv-1', | ||
| code: 'EXPIRED', | ||
| expires_at: new Date(Date.now() - 10000).toISOString(), // Expired | ||
| max_uses: 10, | ||
| current_uses: 0 | ||
| }); | ||
|
|
||
| const res = await request(app) | ||
| .post('/auth/register') | ||
| .send({ | ||
| email: 'test@example.com', | ||
| password: 'password12345678', | ||
| full_name: 'Test User', | ||
| inviteCode: 'EXPIRED' | ||
| }); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toMatch(/expirado/i); | ||
| }); | ||
|
|
||
| it('should FAIL registration with EXHAUSTED invite code', async () => { | ||
| // Mock email check | ||
| (queryOne as jest.Mock).mockResolvedValueOnce(null); | ||
|
|
||
| // Mock invite check (exhausted) | ||
| (queryOne as jest.Mock).mockResolvedValueOnce({ | ||
| id: 'inv-1', | ||
| code: 'FULL', | ||
| expires_at: null, | ||
| max_uses: 5, | ||
| current_uses: 5 // Full | ||
| }); | ||
|
|
||
| const res = await request(app) | ||
| .post('/auth/register') | ||
| .send({ | ||
| email: 'test@example.com', | ||
| password: 'password12345678', | ||
| full_name: 'Test User', | ||
| inviteCode: 'FULL' | ||
| }); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(res.body.error).toMatch(/esgotado/i); | ||
| }); | ||
|
|
||
| it('should SUCCEED registration with VALID invite code and increment usage', async () => { | ||
| // 1. Mock email check (null = ok) | ||
| (queryOne as jest.Mock).mockResolvedValueOnce(null); | ||
|
|
||
| // 2. Mock invite check (valid) | ||
| const validInvite = { | ||
| id: 'inv-123', | ||
| code: 'VALID', | ||
| expires_at: null, | ||
| max_uses: 10, | ||
| current_uses: 0 | ||
| }; | ||
| (queryOne as jest.Mock).mockResolvedValueOnce(validInvite); | ||
|
|
||
| // 3. Mock Transaction | ||
| const mockClient = { | ||
| query: jest.fn(), | ||
| release: jest.fn(), | ||
| }; | ||
| (transaction as jest.Mock).mockImplementation(async (handler: any) => handler(mockClient)); | ||
|
|
||
| // 4. Mock Client Queries | ||
| // Call 1: Insert User | ||
| mockClient.query.mockResolvedValueOnce({ | ||
| rows: [{ | ||
| id: 'user-123', | ||
| email: 'test@example.com', | ||
| full_name: 'Test User' | ||
| }] | ||
| }); | ||
| // Call 2: Update Invite | ||
| mockClient.query.mockResolvedValueOnce({ rows: [] }); | ||
|
|
||
| const res = await request(app) | ||
| .post('/auth/register') | ||
| .send({ | ||
| email: 'test@example.com', | ||
| password: 'password12345678', | ||
| full_name: 'Test User', | ||
| inviteCode: 'VALID' | ||
| }); | ||
|
|
||
| expect(res.status).toBe(201); | ||
| expect(res.body).toHaveProperty('token'); | ||
|
|
||
| // Verify invitation usage was updated via client | ||
| expect(mockClient.query).toHaveBeenCalledTimes(2); | ||
| expect(mockClient.query).toHaveBeenNthCalledWith(1, expect.stringMatching(/INSERT INTO users/i), expect.any(Array)); | ||
| expect(mockClient.query).toHaveBeenNthCalledWith(2, expect.stringMatching(/UPDATE invitation_codes/i), ['inv-123']); | ||
| }); | ||
| }); |
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.
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.
The invite limit is validated before the transaction and the update here is unconditional, so two concurrent registrations can both pass the pre-check and then increment
current_uses, allowing more users thanmax_uses. This shows up when an invite has only one remaining use (or is at its limit) and two requests arrive close together. Consider moving the invite lookup into the same transaction withSELECT ... FOR UPDATE, or make thisUPDATEconditional oncurrent_uses < max_usesand check the affected row count before completing registration.Useful? React with 👍 / 👎.