diff --git a/src/server/api/services/accountDeletion.ts b/src/server/api/services/accountDeletion.ts index a8eb410..cb51131 100644 --- a/src/server/api/services/accountDeletion.ts +++ b/src/server/api/services/accountDeletion.ts @@ -6,6 +6,7 @@ import { env } from '~/env'; import { authOptions, generateEmailToken } from '~/server/auth'; import { db } from '~/server/db'; import { sendAccountDeletedEmail, sendAccountDeletionCodeEmail } from '~/server/mailer'; +import { isReviewLoginEmail, reviewLoginCode } from '~/server/mobileAuth'; import { HANDOFF_IDENTIFIER_PREFIX, hashVerificationToken, @@ -159,7 +160,9 @@ export const requestAccountDeletionCode = async (userId: number) => { throw rateLimited(result.retryAfterSeconds); } - const code = generateEmailToken(); + // Like at sign-in, the review account's code is the fixed one and nothing is emailed + const isReviewAccount = isReviewLoginEmail(user.email); + const code = isReviewAccount ? reviewLoginCode() : generateEmailToken(); const identifier = deleteCodeIdentifier(userId); await db.$transaction([ @@ -174,11 +177,13 @@ export const requestAccountDeletionCode = async (userId: number) => { ]); await resetRateLimit(ACCOUNT_DELETION_LIMITS.verifyPerCode, key); - const sent = await sendAccountDeletionCodeEmail( - user.email, - code, - DELETE_ACCOUNT_CODE_MAX_AGE_SECONDS / MINUTE, - ); + const sent = + isReviewAccount || + (await sendAccountDeletionCodeEmail( + user.email, + code, + DELETE_ACCOUNT_CODE_MAX_AGE_SECONDS / MINUTE, + )); if (!sent) { throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Failed to send email' }); @@ -455,7 +460,7 @@ export const deleteAccount = async ( ).catch(console.error); }); - if (email) { + if (email && !isReviewLoginEmail(email)) { sendAccountDeletedEmail(email).catch(console.error); } }; diff --git a/src/server/mobileAuth.ts b/src/server/mobileAuth.ts index c6b13e2..f0ada76 100644 --- a/src/server/mobileAuth.ts +++ b/src/server/mobileAuth.ts @@ -239,13 +239,19 @@ export const deleteMobileSession = async (sessionToken: string, pushToken?: stri }; /** - * Sends the same sign-in code as the web email provider, stored the way next-auth stores it. + * App store reviewers can't receive our emails: one configured address gets a fixed code instead, + * for signing in and for deleting the account. */ -const isReviewLoginEmail = (email: string) => - !!env.REVIEW_LOGIN_EMAIL && - !!env.REVIEW_LOGIN_CODE && - email === tryNormalizeEmail(env.REVIEW_LOGIN_EMAIL); +export const isReviewLoginEmail = (email: string) => { + const reviewEmail = env.REVIEW_LOGIN_EMAIL && tryNormalizeEmail(env.REVIEW_LOGIN_EMAIL); + return !!reviewEmail && !!env.REVIEW_LOGIN_CODE && reviewEmail === tryNormalizeEmail(email); +}; + +export const reviewLoginCode = () => env.REVIEW_LOGIN_CODE!.trim().toUpperCase(); +/** + * Sends the same sign-in code as the web email provider, stored the way next-auth stores it. + */ export const sendEmailCode = async (rawEmail: string, ip: string) => { const email = tryNormalizeEmail(rawEmail); @@ -273,9 +279,8 @@ export const sendEmailCode = async (rawEmail: string, ip: string) => { ); } - // App store reviewers can't receive our emails: one configured address gets a fixed code instead const isReviewLogin = isReviewLoginEmail(email); - const token = isReviewLogin ? env.REVIEW_LOGIN_CODE!.trim().toUpperCase() : generateEmailToken(); + const token = isReviewLogin ? reviewLoginCode() : generateEmailToken(); await adapter.createVerificationToken({ identifier: email, token: hashVerificationToken(token, getAuthSecret()), diff --git a/src/tests/accountDeletion.integration.test.ts b/src/tests/accountDeletion.integration.test.ts index 5683fbe..0f29302 100644 --- a/src/tests/accountDeletion.integration.test.ts +++ b/src/tests/accountDeletion.integration.test.ts @@ -723,3 +723,76 @@ void test( } }, ); + +void test( + 'account deletion: the review account uses the fixed review code and can sign up again', + { skip: !databaseUrl }, + async () => { + const t = await setup(); + const reviewEmail = t.emailOf('Review'); + const frank = await t.createUser('Frank'); + const userIds = [frank.id]; + const originalEnv = { ...t.env }; + t.env.REVIEW_LOGIN_EMAIL = reviewEmail.toUpperCase(); + t.env.REVIEW_LOGIN_CODE = 'rv7k2m'; + t.env.INVITE_ONLY = false; + + /** Runs `action`, returning the first argument of everything it logged. */ + const logsOf = async (action: () => Promise) => { + const logged: unknown[] = []; + const original = console.log; + console.log = (...args: unknown[]) => logged.push(args[0]); + try { + await action(); + } finally { + console.log = original; + } + return logged; + }; + const signInAsReviewer = async () => { + await t.sendEmailCode(reviewEmail, `review-${reviewEmail}`); + const { user } = await t.verifyEmailCode(reviewEmail, 'RV7K2M', `review-${reviewEmail}`); + userIds.push(user.id); + return t.db.user.findUniqueOrThrow({ where: { id: user.id } }); + }; + + try { + // Other accounts still need the emailed code: the review code doesn't work for them + const frankCaller = t.callerFor(frank); + const frankCode = await t.requestCode(frank); + assert.notEqual(frankCode, 'RV7K2M'); + await assert.rejects( + frankCaller.user.deleteAccount({ code: 'RV7K2M' }), + withCode('BAD_REQUEST'), + ); + await frankCaller.user.deleteAccount({ code: frankCode }); + assert.ok((await t.db.user.findUniqueOrThrow({ where: { id: frank.id } })).deletedAt); + + const reviewer = await signInAsReviewer(); + const caller = t.callerFor(reviewer); + + // The review code only works once a code has been requested + await assert.rejects(caller.user.deleteAccount({ code: 'RV7K2M' }), withCode('BAD_REQUEST')); + + // Requesting sends nothing, and the fixed code deletes the account, also without an email + await t.resetRateLimit(t.ACCOUNT_DELETION_LIMITS.verifyPerCode, String(reviewer.id)); + const requestLogs = await logsOf(() => caller.user.requestDeleteAccountCode()); + assert.ok(!requestLogs.includes('Account deletion code : ')); + const deleteLogs = await logsOf(() => caller.user.deleteAccount({ code: ' rv7k2m ' })); + assert.ok(!deleteLogs.includes('Account deleted email : ')); + const deleted = await t.db.user.findUniqueOrThrow({ where: { id: reviewer.id } }); + assert.ok(deleted.deletedAt); + assert.equal(deleted.email, null); + + // The reviewer can sign in again, as a fresh account + const again = await signInAsReviewer(); + assert.notEqual(again.id, reviewer.id); + assert.equal(again.email, reviewEmail); + assert.equal(again.deletedAt, null); + } finally { + Object.assign(t.env, originalEnv); + await t.db.verificationToken.deleteMany({ where: { identifier: reviewEmail } }); + await t.db.user.deleteMany({ where: { id: { in: userIds } } }); + } + }, +);