-
Notifications
You must be signed in to change notification settings - Fork 139
fix: prevent account enumeration via resend-verification endpoint #1455
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
KaranUnique
merged 1 commit into
Canopus-Labs:security
from
ionfwsrijan:fix/1437-resend-verification-enumeration
Aug 8, 2026
+147
−8
Merged
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
142 changes: 142 additions & 0 deletions
142
backend/tests/resendVerification.enumeration.unit.test.js
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,142 @@ | ||
| import { Module } from "node:module"; | ||
| import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from "vitest"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // resend-verification enumeration fix (issue #1437): the endpoint must return | ||
| // the identical generic 200 for every input (not found / verified / unverified) | ||
| // so it cannot be used as an account oracle. | ||
| // | ||
| // authController.js is CommonJS, so we shim Node's module loader (same pattern | ||
| // as registerEnumeration.unit.test.js). | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const userMock = vi.hoisted(() => ({ | ||
| findOne: vi.fn(), | ||
| })); | ||
|
|
||
| const sendEmailMock = vi.hoisted(() => ({ | ||
| sendVerificationEmail: vi.fn(), | ||
| })); | ||
|
|
||
| const testDoubles = new Map(); | ||
| const originalLoad = Module._load; | ||
| Module._load = function (request, parent, isMain) { | ||
| if (testDoubles.has(request)) { | ||
| return testDoubles.get(request); | ||
| } | ||
| return originalLoad.call(this, request, parent, isMain); | ||
| }; | ||
|
Comment on lines
+21
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Restore This file replaces the process-global CommonJS loader. It never restores Proposed fix-import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from "vitest";
+import { describe, it, expect, beforeAll, beforeEach, afterEach, afterAll, vi } from "vitest";
+afterAll(() => {
+ Module._load = originalLoad;
+ testDoubles.clear();
+ clearRequireCache();
+});🤖 Prompt for AI Agents |
||
|
|
||
| const clearRequireCache = () => { | ||
| Object.keys(require.cache).forEach((key) => { | ||
| if ( | ||
| key.includes("controllers\\authController") || | ||
| key.includes("controllers/authController") || | ||
| key.includes("models\\User") || | ||
| key.includes("models/User") | ||
| ) { | ||
| delete require.cache[key]; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| let resendVerificationEmail; | ||
|
|
||
| beforeAll(async () => { | ||
| clearRequireCache(); | ||
| testDoubles.set("../models/User", { | ||
| findOne: userMock.findOne, | ||
| create: vi.fn(), | ||
| }); | ||
| testDoubles.set("../utils/sendEmail", sendEmailMock); | ||
|
|
||
| const mod = await import("../controllers/authController.js"); | ||
| resendVerificationEmail = mod.resendVerificationEmail; | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| userMock.findOne.mockReset(); | ||
| sendEmailMock.sendVerificationEmail.mockReset(); | ||
| sendEmailMock.sendVerificationEmail.mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| const mockRes = () => { | ||
| const res = { statusCode: 200, body: null }; | ||
| res.status = (code) => { | ||
| res.statusCode = code; | ||
| return res; | ||
| }; | ||
| res.json = (body) => { | ||
| res.body = body; | ||
| return res; | ||
| }; | ||
| return res; | ||
| }; | ||
|
|
||
| const req = (email) => ({ body: { email } }); | ||
|
|
||
| const GENERIC = { | ||
| success: true, | ||
| message: "If this email is registered, a verification link has been sent.", | ||
| }; | ||
|
|
||
| describe("resendVerificationEmail — account enumeration", () => { | ||
| it("returns the generic 200 and sends nothing when the email is not registered", async () => { | ||
| userMock.findOne.mockResolvedValue(null); | ||
|
|
||
| const res = mockRes(); | ||
| await resendVerificationEmail(req("not-registered@example.com"), res); | ||
|
|
||
| expect(res.statusCode).toBe(200); | ||
| expect(res.body).toEqual(GENERIC); | ||
| expect(sendEmailMock.sendVerificationEmail).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns the generic 200 (not a 400) for an already-verified email and sends nothing", async () => { | ||
| userMock.findOne.mockResolvedValue({ email: "verified@example.com", isEmailVerified: true }); | ||
|
|
||
| const res = mockRes(); | ||
| await resendVerificationEmail(req("verified@example.com"), res); | ||
|
|
||
| expect(res.statusCode).toBe(200); | ||
| expect(res.body).toEqual(GENERIC); | ||
| expect(sendEmailMock.sendVerificationEmail).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("sends the email and returns the same generic shape for an unverified user", async () => { | ||
| const unverified = { | ||
| email: "unverified@example.com", | ||
| isEmailVerified: false, | ||
| emailVerificationToken: null, | ||
| emailVerificationExpires: null, | ||
| save: vi.fn(async function () { | ||
| return this; | ||
| }), | ||
| }; | ||
| userMock.findOne.mockResolvedValue(unverified); | ||
| vi.stubEnv("FRONTEND_URL", "http://localhost:5173"); | ||
|
|
||
| const res = mockRes(); | ||
| await resendVerificationEmail(req("unverified@example.com"), res); | ||
|
|
||
| expect(res.statusCode).toBe(200); | ||
| expect(res.body).toEqual(GENERIC); | ||
| expect(sendEmailMock.sendVerificationEmail).toHaveBeenCalledTimes(1); | ||
| expect(sendEmailMock.sendVerificationEmail).toHaveBeenCalledWith( | ||
| "unverified@example.com", | ||
| expect.stringContaining("http://localhost:5173/verify-email") | ||
| ); | ||
| }); | ||
|
|
||
| it("returns 400 only when the email field itself is missing", async () => { | ||
| const res = mockRes(); | ||
| await resendVerificationEmail(req(undefined), res); | ||
|
|
||
| expect(res.statusCode).toBe(400); | ||
| expect(userMock.findOne).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
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.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Return the generic response when delivery operations fail.
Lines 369-375 require the same response for all account states. For an unverified user,
user.save()orsendVerificationEmail()can throw and the outer catch returns HTTP 500. Nonexistent and verified users return HTTP 200 before these operations. This can reveal unverified accounts during a database-write or email-provider failure.Catch and log failures after the user lookup, then return the generic HTTP 200 response.
Proposed fix
Also applies to: 384-384
🤖 Prompt for AI Agents