Repository navigation
fix(otp): reserve a verification attempt atomically before comparing the code #9774
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
5daef70
fix(otp): reserve a verification attempt atomically before comparing …
waleedlatif1 9615cb6
improvement(otp): centralize compare-and-consume in verifyOTP, tighte…
waleedlatif1 5c7a2ba
fix(otp): keep sub-second code lifetimes and drop unused deleteOTP ex…
waleedlatif1 5a003bb
test(otp): prove rejected verify requests never spend the code agains…
waleedlatif1 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
193 changes: 193 additions & 0 deletions
193
apps/sim/app/api/chat/[identifier]/otp/route.integration.ts
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,193 @@ | ||
| import { db } from '@sim/db' | ||
| import { chat, user, verification, workflow, workspace } from '@sim/db/schema' | ||
| import { readTestRedisUrl } from '@sim/db/testing/test-infrastructure' | ||
| import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' | ||
| import { createRouteContext } from '@sim/testing/helpers/http' | ||
| import { createMockRequest } from '@sim/testing/mocks/request.mock' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { eq, like } from 'drizzle-orm' | ||
| import Redis from 'ioredis' | ||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { storage, redisClient } = vi.hoisted(() => ({ | ||
| storage: { backend: 'database' as 'database' | 'redis' }, | ||
| redisClient: { current: undefined as Redis | undefined }, | ||
| })) | ||
| vi.mock('@/lib/core/storage', () => ({ getStorageMethod: () => storage.backend })) | ||
| vi.mock('@/lib/core/config/redis', async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import('@/lib/core/config/redis')>()), | ||
| getRedisClient: () => redisClient.current, | ||
| })) | ||
|
|
||
| import { MAX_OTP_ATTEMPTS, storeOTP } from '@/lib/core/security/otp' | ||
| import { PUT } from '@/app/api/chat/[identifier]/otp/route' | ||
|
|
||
| const redisUrl = readTestRedisUrl() | ||
| const ownerId = generateId() | ||
| const workspaceId = generateId() | ||
| const workflowId = generateId() | ||
| const chatId = generateId() | ||
| const identifier = `otp-${chatId}` | ||
| const email = `${generateId()}@otp.invalid` | ||
| const CODE = '246810' | ||
| const NO_CODE = 'No verification code found, request a new one' | ||
| const INVALID = 'Invalid verification code' | ||
| const LOCKED = 'Too many failed attempts. Please request a new code.' | ||
|
|
||
| async function verify(otp: string) { | ||
| const request = createMockRequest({ | ||
| method: 'PUT', | ||
| url: `/api/chat/${identifier}/otp`, | ||
| body: { email, otp }, | ||
| }) | ||
| const response = await PUT(request, createRouteContext({ identifier })) | ||
| const body = (await response.json()) as { error?: string; message?: string } | ||
| return { | ||
| status: response.status, | ||
| error: body.error ?? body.message, | ||
| cookie: response.headers.get('set-cookie'), | ||
| } | ||
| } | ||
|
|
||
| /** Distinct wrong six-digit guesses that never equal {@link CODE}. */ | ||
| function wrongGuesses(count: number) { | ||
| return Array.from({ length: count }, (_, index) => String(100000 + index)) | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| await db.insert(user).values({ | ||
| id: ownerId, | ||
| name: 'OTP fixture', | ||
| email: `${ownerId}@otp.invalid`, | ||
| emailVerified: true, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date(), | ||
| }) | ||
| await insertWorkspaceFixture(db, { | ||
| id: workspaceId, | ||
| name: 'OTP fixture', | ||
| ownerId, | ||
| billedAccountUserId: ownerId, | ||
| }) | ||
| await db.insert(workflow).values({ | ||
| id: workflowId, | ||
| userId: ownerId, | ||
| workspaceId, | ||
| name: 'OTP fixture', | ||
| lastSynced: new Date(), | ||
| createdAt: new Date(), | ||
| updatedAt: new Date(), | ||
| }) | ||
| await db.insert(chat).values({ | ||
| id: chatId, | ||
| workflowId, | ||
| userId: ownerId, | ||
| identifier, | ||
| title: 'OTP fixture', | ||
| authType: 'email', | ||
| allowedEmails: [email], | ||
| }) | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await db.delete(verification).where(like(verification.identifier, `chat-otp:${chatId}:%`)) | ||
| await deleteWorkspaceFixture(db, eq(workspace.id, workspaceId)) | ||
| await db.delete(user).where(eq(user.id, ownerId)) | ||
| await redisClient.current?.quit() | ||
| }) | ||
|
|
||
| describe.each(['database', 'redis'] as const)('%s chat OTP verification', (backend) => { | ||
| describe.runIf(backend === 'database' || Boolean(redisUrl))('real storage', () => { | ||
| beforeAll(async () => { | ||
| if (backend === 'redis' && redisUrl && !redisClient.current) { | ||
| redisClient.current = new Redis(redisUrl, { lazyConnect: true, maxRetriesPerRequest: 0 }) | ||
| await redisClient.current.connect() | ||
| } | ||
| }) | ||
| beforeEach(async () => { | ||
| storage.backend = backend | ||
| await storeOTP('chat', chatId, email, CODE) | ||
| }) | ||
| afterEach(async () => { | ||
| await db | ||
| .update(chat) | ||
| .set({ authType: 'email', allowedEmails: [email] }) | ||
| .where(eq(chat.id, chatId)) | ||
| }) | ||
|
|
||
| it.each([ | ||
| { | ||
| rejection: 'the chat no longer uses email auth', | ||
| change: { authType: 'public' }, | ||
| response: { status: 400, error: 'This chat does not use email authentication' }, | ||
| }, | ||
| { | ||
| rejection: 'the email is no longer allowed', | ||
| change: { allowedEmails: ['someone-else@otp.invalid'] }, | ||
| response: { status: 403, error: 'Email not authorized' }, | ||
| }, | ||
| ])('rejects without spending the code when $rejection', async ({ change, response }) => { | ||
| await db.update(chat).set(change).where(eq(chat.id, chatId)) | ||
| for (let attempt = 0; attempt < MAX_OTP_ATTEMPTS; attempt++) { | ||
| expect(await verify(CODE)).toMatchObject({ ...response, cookie: null }) | ||
| } | ||
| await db | ||
| .update(chat) | ||
| .set({ authType: 'email', allowedEmails: [email] }) | ||
| .where(eq(chat.id, chatId)) | ||
| expect(await verify(CODE)).toMatchObject({ status: 200 }) | ||
| }) | ||
|
|
||
| it('compares at most the attempt limit of guesses however many arrive concurrently', async () => { | ||
| const results = await Promise.all(wrongGuesses(30).map(verify)) | ||
|
|
||
| const compared = results.filter((result) => result.error !== NO_CODE) | ||
| expect(compared).toHaveLength(MAX_OTP_ATTEMPTS) | ||
| expect(compared.filter((result) => result.error === INVALID)).toHaveLength( | ||
| MAX_OTP_ATTEMPTS - 1 | ||
| ) | ||
| expect(compared.filter((result) => result.error === LOCKED)).toHaveLength(1) | ||
| expect(await verify(CODE)).toMatchObject({ status: 400, error: NO_CODE, cookie: null }) | ||
| }) | ||
|
|
||
| it('accepts the correct code on the last remaining attempt and mints the auth cookie', async () => { | ||
| for (const guess of wrongGuesses(MAX_OTP_ATTEMPTS - 1)) { | ||
| expect(await verify(guess)).toMatchObject({ status: 400, error: INVALID }) | ||
| } | ||
| const accepted = await verify(CODE) | ||
| expect(accepted.status).toBe(200) | ||
| expect(accepted.cookie).toContain(`chat_auth_${chatId}=`) | ||
| expect(await verify(CODE)).toMatchObject({ status: 400, error: NO_CODE }) | ||
| }) | ||
|
|
||
| it('keeps the code expiry when a failed attempt is recorded', async () => { | ||
| if (backend === 'redis') { | ||
| const key = `otp:${email}:${chatId}` | ||
| await redisClient.current?.pexpire(key, 900) | ||
| expect(await verify('999999')).toMatchObject({ status: 400, error: INVALID }) | ||
| const ttl = await redisClient.current?.pttl(key) | ||
| expect(ttl).toBeGreaterThan(0) | ||
| expect(ttl).toBeLessThanOrEqual(900) | ||
| return | ||
| } | ||
| const readExpiry = async () => | ||
| ( | ||
| await db | ||
| .select({ expiresAt: verification.expiresAt }) | ||
| .from(verification) | ||
| .where(eq(verification.identifier, `chat-otp:${chatId}:${email}`)) | ||
| )[0]?.expiresAt.getTime() | ||
| const before = await readExpiry() | ||
| expect(await verify('999999')).toMatchObject({ status: 400, error: INVALID }) | ||
| expect(await readExpiry()).toBe(before) | ||
| }) | ||
|
|
||
| it.runIf(backend === 'database')('rejects an expired code', async () => { | ||
| await db | ||
| .update(verification) | ||
| .set({ expiresAt: new Date(Date.now() - 1000) }) | ||
| .where(eq(verification.identifier, `chat-otp:${chatId}:${email}`)) | ||
| expect(await verify(CODE)).toMatchObject({ status: 400, error: NO_CODE, cookie: null }) | ||
| }) | ||
| }) | ||
| }) | ||
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.