-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(auth): invalidate email OTP on failed guess, not just a match (#2221) #2269
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
mini0n-ai
wants to merge
1
commit into
CapSoftware:main
Choose a base branch
from
mini0n-ai:fix/auth-otp-invalidation-2221
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.
+241
−23
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import type { MySql2Database } from "drizzle-orm/mysql2"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { DrizzleAdapter } from "../../../../packages/database/auth/drizzle-adapter"; | ||
|
|
||
| interface VerificationTokenRow { | ||
| identifier: string; | ||
| token: string; | ||
| expires: Date; | ||
| } | ||
|
|
||
| function createMockDb(initialRows: VerificationTokenRow[]) { | ||
| let table = [...initialRows]; | ||
| let deletePredicate: unknown = null; | ||
|
|
||
| const db = { | ||
| select: () => ({ | ||
| from: () => ({ | ||
| where: () => ({ | ||
| limit: async () => table.slice(0, 1), | ||
| }), | ||
| }), | ||
| }), | ||
| delete: () => ({ | ||
| where: (pred: unknown) => { | ||
| deletePredicate = pred; | ||
| const initialCount = table.length; | ||
| table = table.filter( | ||
| (row) => | ||
| !( | ||
| row.identifier.toLowerCase() === "user@example.com" && | ||
| row.token === "123456" | ||
| ), | ||
| ); | ||
| const rowsAffected = initialCount - table.length; | ||
| return Promise.resolve({ rowsAffected }); | ||
| }, | ||
| }), | ||
| transaction: async (cb: (tx: unknown) => Promise<unknown>) => cb(db), | ||
| getTable: () => table, | ||
| getDeletePredicate: () => deletePredicate, | ||
| }; | ||
|
|
||
| return db; | ||
| } | ||
|
|
||
| describe("useVerificationToken", () => { | ||
| it("burns the token on wrong guess and returns null", async () => { | ||
| const mockDb = createMockDb([ | ||
| { | ||
| identifier: "user@example.com", | ||
| token: "123456", | ||
| expires: new Date(Date.now() + 600000), | ||
| }, | ||
| ]); | ||
|
|
||
| const adapter = DrizzleAdapter(mockDb as unknown as MySql2Database); | ||
| const result = await adapter.useVerificationToken?.({ | ||
| identifier: "USER@example.com", | ||
| token: "999999", | ||
| }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(mockDb.getDeletePredicate()).not.toBeNull(); | ||
| expect(mockDb.getTable()).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("returns token and invalidates it on correct guess", async () => { | ||
| const mockDb = createMockDb([ | ||
| { | ||
| identifier: "user@example.com", | ||
| token: "123456", | ||
| expires: new Date(Date.now() + 600000), | ||
| }, | ||
| ]); | ||
|
|
||
| const adapter = DrizzleAdapter(mockDb as unknown as MySql2Database); | ||
| const result = await adapter.useVerificationToken?.({ | ||
| identifier: "USER@example.com", | ||
| token: "123456", | ||
| }); | ||
|
|
||
| expect(result).not.toBeNull(); | ||
| expect(result?.identifier).toBe("user@example.com"); | ||
| expect(result?.token).toBe("123456"); | ||
| expect(mockDb.getDeletePredicate()).not.toBeNull(); | ||
| expect(mockDb.getTable()).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("returns null if token does not exist", async () => { | ||
| const mockDb = createMockDb([]); | ||
|
|
||
| const adapter = DrizzleAdapter(mockDb as unknown as MySql2Database); | ||
| const result = await adapter.useVerificationToken?.({ | ||
| identifier: "nonexistent@example.com", | ||
| token: "123456", | ||
| }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(mockDb.getDeletePredicate()).toBeNull(); | ||
| }); | ||
|
|
||
| it("prevents race condition by checking rowsAffected on token consumption", async () => { | ||
| let table = [ | ||
| { | ||
| identifier: "user@example.com", | ||
| token: "123456", | ||
| expires: new Date(Date.now() + 600000), | ||
| }, | ||
| ]; | ||
|
|
||
| const mockDb = { | ||
| select: () => ({ | ||
| from: () => ({ | ||
| where: () => ({ | ||
| limit: async () => table.slice(0, 1), | ||
| }), | ||
| }), | ||
| }), | ||
| delete: () => ({ | ||
| where: () => { | ||
| const initialCount = table.length; | ||
| table = []; | ||
| const rowsAffected = initialCount; | ||
| return Promise.resolve({ rowsAffected }); | ||
| }, | ||
| }), | ||
| transaction: async (cb: (tx: unknown) => Promise<unknown>) => cb(mockDb), | ||
| } as unknown as MySql2Database; | ||
|
|
||
| const adapter = DrizzleAdapter(mockDb); | ||
|
|
||
| const firstResult = await adapter.useVerificationToken?.({ | ||
| identifier: "USER@example.com", | ||
| token: "123456", | ||
| }); | ||
|
|
||
| expect(firstResult).not.toBeNull(); | ||
| expect(firstResult?.token).toBe("123456"); | ||
|
|
||
| const secondResult = await adapter.useVerificationToken?.({ | ||
| identifier: "USER@example.com", | ||
| token: "123456", | ||
| }); | ||
|
|
||
| expect(secondResult).toBeNull(); | ||
| }); | ||
|
|
||
| it("deletes only the selected token instance and preserves replacement tokens for the same user", async () => { | ||
| let table = [ | ||
| { | ||
| identifier: "user@example.com", | ||
| token: "123456", | ||
| expires: new Date(Date.now() + 600000), | ||
| }, | ||
| { | ||
| identifier: "user@example.com", | ||
| token: "replacement_token", | ||
| expires: new Date(Date.now() + 600000), | ||
| }, | ||
| ]; | ||
|
|
||
| const mockDb = { | ||
| select: () => ({ | ||
| from: () => ({ | ||
| where: () => ({ | ||
| limit: async () => [table[0]], | ||
| }), | ||
| }), | ||
| }), | ||
| delete: () => ({ | ||
| where: () => { | ||
| const initialCount = table.length; | ||
| table = table.filter( | ||
| (row) => | ||
| !( | ||
| row.identifier === "user@example.com" && row.token === "123456" | ||
| ), | ||
| ); | ||
| const rowsAffected = initialCount - table.length; | ||
| return Promise.resolve({ rowsAffected }); | ||
| }, | ||
| }), | ||
| transaction: async (cb: (tx: unknown) => Promise<unknown>) => cb(mockDb), | ||
| } as unknown as MySql2Database; | ||
|
|
||
| const adapter = DrizzleAdapter(mockDb); | ||
| const result = await adapter.useVerificationToken?.({ | ||
| identifier: "USER@example.com", | ||
| token: "999999", | ||
| }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(table.some((r) => r.token === "123456")).toBe(false); | ||
| expect(table.some((r) => r.token === "replacement_token")).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.
P2: The OTP deletion is scoped only by identifier and can invalidate a different token during rotation
DELETE filters only by email, so OTP rotation or concurrent requests can consume the wrong row or replay one OTP.
Atomically consume the selected token by its unique key and verify the affected-row count; add race tests.
AI prompt