Repository navigation
fix(auth): invalidate email OTP on a failed guess, not just a match #2224
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
richiemcilroy
merged 7 commits into
CapSoftware:main
from
addyCooks:fix/otp-verification-token-brute-force
Oct 22, 2026
+232
−10
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2562bc1
fix(auth): invalidate email OTP on a failed guess, not just a match
addyCooks 1dd7b68
fix(auth): make OTP consumption atomic to close concurrent-guess race
addyCooks 6473cc9
test(auth): cover OTP concurrent-guess atomicity against real MySQL
addyCooks 54e0bd5
test(auth): cover mixed correct/incorrect OTP guesses racing on real …
addyCooks 88cae4f
Merge main into fix/otp-burn
richiemcilroy 93cbaaa
test(auth): bind the adapter's useVerificationToken so the narrowing …
richiemcilroy 4f5d0a1
Merge branch 'main' into fix/otp-verification-token-brute-force
richiemcilroy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import type { SQL } from "drizzle-orm"; | ||
| import { MySqlDialect } from "drizzle-orm/mysql-core"; | ||
| import type { MySql2Database } from "drizzle-orm/mysql2"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { DrizzleAdapter } from "../../../../packages/database/auth/drizzle-adapter"; | ||
|
|
||
| type TokenRow = { identifier: string; token: string; expires: Date }; | ||
|
|
||
| function matchesPredicate(condition: SQL, row: TokenRow) { | ||
| const query = new MySqlDialect().sqlToQuery(condition); | ||
| const columns = [ | ||
| ...query.sql.matchAll( | ||
| /`verification_tokens`\.`(identifier|token)`\s*=\s*\?/g, | ||
| ), | ||
| ].map((match) => match[1] as "identifier" | "token"); | ||
| if (columns.length === 0) { | ||
| throw new Error(`Unexpected predicate: ${query.sql}`); | ||
| } | ||
| return columns.every((column, i) => row[column] === query.params[i]); | ||
| } | ||
|
|
||
| function fakeDatabase( | ||
| initialRow: TokenRow, | ||
| options?: { afterSelect?: () => void }, | ||
| ) { | ||
| let row: TokenRow | undefined = initialRow; | ||
| const db = { | ||
| select: () => ({ | ||
| from: () => ({ | ||
| where: (condition: SQL) => ({ | ||
| limit: async () => { | ||
| const result = row && matchesPredicate(condition, row) ? [row] : []; | ||
| options?.afterSelect?.(); | ||
| return result; | ||
| }, | ||
| }), | ||
| }), | ||
| }), | ||
| delete: () => ({ | ||
| where: async (condition: SQL) => { | ||
| if (row && matchesPredicate(condition, row)) { | ||
| row = undefined; | ||
| return [{ affectedRows: 1 }]; | ||
| } | ||
| return [{ affectedRows: 0 }]; | ||
| }, | ||
| }), | ||
| }; | ||
| return { | ||
| db: db as unknown as MySql2Database, | ||
| getRow: () => row, | ||
| setRow: (next: TokenRow) => { | ||
| row = next; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("useVerificationToken", () => { | ||
| const identifier = "person@example.com"; | ||
| const validRow: TokenRow = { | ||
| identifier, | ||
| token: "111111", | ||
| expires: new Date(Date.now() + 60_000), | ||
| }; | ||
|
|
||
| it("burns the code on a wrong guess instead of leaving it guessable", async () => { | ||
| const { db, getRow } = fakeDatabase({ ...validRow }); | ||
| const adapter = DrizzleAdapter(db); | ||
|
|
||
| const wrongGuess = await adapter.useVerificationToken?.({ | ||
| identifier, | ||
| token: "000000", | ||
| }); | ||
|
|
||
| expect(wrongGuess).toBeNull(); | ||
| expect(getRow()).toBeUndefined(); | ||
|
|
||
| const correctGuessAfterward = await adapter.useVerificationToken?.({ | ||
| identifier, | ||
| token: "111111", | ||
| }); | ||
| expect(correctGuessAfterward).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns the row and deletes it on a correct guess", async () => { | ||
| const { db, getRow } = fakeDatabase({ ...validRow }); | ||
| const adapter = DrizzleAdapter(db); | ||
|
|
||
| const result = await adapter.useVerificationToken?.({ | ||
| identifier, | ||
| token: "111111", | ||
| }); | ||
|
|
||
| expect(result).toMatchObject({ identifier, token: "111111" }); | ||
| expect(getRow()).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns null when no code was ever requested for the identifier", async () => { | ||
| const { db } = fakeDatabase({ ...validRow }); | ||
| const adapter = DrizzleAdapter(db); | ||
|
|
||
| const result = await adapter.useVerificationToken?.({ | ||
| identifier: "nobody@example.com", | ||
| token: "111111", | ||
| }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it("lets only one of two concurrent correct guesses succeed", async () => { | ||
| const { db, getRow } = fakeDatabase({ ...validRow }); | ||
| const adapter = DrizzleAdapter(db); | ||
|
|
||
| const [first, second] = await Promise.all([ | ||
| adapter.useVerificationToken?.({ identifier, token: "111111" }), | ||
| adapter.useVerificationToken?.({ identifier, token: "111111" }), | ||
| ]); | ||
|
|
||
| const successes = [first, second].filter((result) => result !== null); | ||
| expect(successes).toHaveLength(1); | ||
| expect(getRow()).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("does not delete a resent code that replaced the row a guess read", async () => { | ||
| const resentRow: TokenRow = { | ||
| identifier, | ||
| token: "222222", | ||
| expires: new Date(Date.now() + 60_000), | ||
| }; | ||
| const { db, getRow, setRow } = fakeDatabase( | ||
| { ...validRow }, | ||
| { | ||
| afterSelect: () => setRow({ ...resentRow }), | ||
| }, | ||
| ); | ||
| const adapter = DrizzleAdapter(db); | ||
|
|
||
| const wrongGuessAgainstStaleCode = await adapter.useVerificationToken?.({ | ||
| identifier, | ||
| token: "000000", | ||
| }); | ||
|
|
||
| expect(wrongGuessAgainstStaleCode).toBeNull(); | ||
| expect(getRow()).toEqual(resentRow); | ||
| }); | ||
| }); |
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.
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 mixed-guess test does not check which guess succeeded.
useVerificationTokenreturns the stored row, so checking itstokenagainst the stored token would also pass if a wrong guess incorrectly returned that row. This leaves a gap in the test, not a defect in the current sign-in code. Keep each result paired with its guess and assert that every wrong guess returnsnull.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!