Email verification and account pre-hijacking
In short
- The threat
An unverified address is a claim: someone can register yours first, keep a session, and still be inside when you arrive.
- The rule
Prove the mailbox with a code typed in the account’s own session, bind it to that address, and end every other session when it succeeds.
- In the package
flows/email-verification.ts, configured underemailVerification.
What goes wrong
Section titled “What goes wrong”An unverified address is a claim. If the app trusts it (sends invoices there, lets an invitation attach to it, links a social login to it), someone can claim an address they do not own.
The sharper version is account pre-hijacking. The attacker registers the victim’s address before the victim does, with a password the attacker knows, and stays signed in. Later the real owner arrives, proves the mailbox through a reset or a verification, and starts using the account, while the attacker’s old session is still live inside it.
A squatter registers the owner's address first, with a password they know, and stays signed in. Later the owner proves the mailbox, by typing the verification code in their own session or by completing a password reset. At that moment every session of the account is revoked: the squatter's next request is refused, while the owner's own session is re-issued after a code, or the owner signs in with the new password after a reset. A proof meant for another account, or mailed to an address the account no longer has, is refused at the same point and verifies nothing. By default a code: 8 digits, lives 15 min, 5 wrong guesses, then it is deleted. Time runs left to right, not to scale.
Text version
The rule
Section titled “The rule”Make the proof belong to one account and one address, and let it clear everyone else out.
A code, typed by the signed-in user
Section titled “A code, typed by the signed-in user”By default verification mails an 8-digit code instead of a link. The code is entered on the device that started the flow, by a user who is signed in, so the proof is tied to that account’s session. A code sent to one account cannot verify another, even if both are open in the same browser.
Bound to the address it was sent to
Section titled “Bound to the address it was sent to”The address is stored with the code, and verification only succeeds if the account still has that address, so a code mailed before an address change proves nothing about the new one. The store’s write is guarded the same way (A014).
Short-lived, hashed, counted
Section titled “Short-lived, hashed, counted”A code lives 15 minutes, is stored as an HMAC under a server secret, allows 5 wrong guesses (counted atomically before each comparison) and is deleted on success, expiry or exhaustion (A013). Only the code that was submitted is deleted, and a guess is counted on the code it is compared with: a new code sent while an old one was being checked stays valid and keeps every attempt, and the old one answers as spent. Resending is limited to 3 mails an hour per account.
Verifying ends every other session
Section titled “Verifying ends every other session”When the address is verified, every session of the account is revoked and the caller’s own session is re-issued. A squatter who registered first is signed out the moment the real owner proves the mailbox (A012).
A completed password reset does the same, and also marks the address verified, since the reset link reached it.
Links when you need them
Section titled “Links when you need them”emailVerification.kind: 'link' mails a single-use link instead (32 random bytes, stored as SHA-256, 24 hours).
The link only verifies when it is opened in a signed-in session of the same account; opened anywhere else, it is refused and left unspent, so the user can sign in and open it again. That keeps the pre-hijacking defence: a squatter’s session can never be the one that completes verification.
Links without a session, if you opt in
Section titled “Links without a session, if you opt in”Users often open the mail on another device, or after their session expired. emailVerification.linkWithoutSession: true also accepts a valid link opened with no session (A083).
What stays the same. The token is single-use, expires, and is bound to the account and to the address it was sent to, so a link for an address the account has since changed is refused. A link opened inside a session of another account is still refused and left unspent.
What it never does is sign anyone in. The address is verified, every session of the account is revoked (the epoch is bumped, as on any verification, so a squatter’s session ends) and no session is issued, so the user signs in afterwards. A user who opens the link signed in on the same account still gets their session re-issued.
Verify on a click, never on a fetch
Section titled “Verify on a click, never on a fetch”With the option on, any request that carries the token verifies the address, and mail security scanners fetch links in mail before the reader sees them. If the page verifies on load, or the link points straight at an API route, the scanner verifies the account with nobody clicking. So:
- put the token in the URL fragment (
https://app.example/verify#<token>), which a fetch never sends to the server; - serve a page that shows what is being confirmed and a button, and do nothing on load;
- on the click, POST the token from the fragment to your route, which calls
verifyEmailLinkwith the request’s session (ornull); never verify on a GET.
The NestJS and tRPC guides show such a route.
Word the mail so the reader knows what they are confirming (“if you did not create an account, ignore this mail”), verify only on a POST from a click, and keep trust that matters (invitations, linking a social login) behind something the registrant cannot pass, such as a password reset. Leave the option off when that risk outweighs the convenience.
In code
Section titled “In code”The defaults written out, and a link route that verifies only on a click.
import type { AuthConfig, AuthUser } from '@noctcore/auth';
type AppConfig = AuthConfig<AuthUser, unknown, []>;type EmailVerificationOptions = NonNullable<AppConfig['emailVerification']>;
// The defaults, written out. Pass them as `emailVerification` to createAuth,// or leave the option out for the same values.export const emailVerification = { // Typed by the signed-in user, bound to the account and the address. kind: 'code', ttl: '15m', digits: 8, maxAttempts: 5, // Link mode only: accept a link opened with no session. linkWithoutSession: false,} satisfies EmailVerificationOptions;import { toPublicError, type RequestMeta, type StoredSession } from '@noctcore/auth';import type { AppAuth } from '../build/node-server/auth';
type Session = StoredSession<Record<string, never>>;
/** * POST only, from the click on a page that read the token from its URL * fragment and did nothing on load. A mail scanner that fetches the link * never reaches this route. */export async function confirmEmail( auth: AppAuth, input: { token: string; session: Session | null; meta: RequestMeta },) { const result = await auth.verifyEmailLink(input);
// Opened in another account's session: refused, and left unspent. if (!result.ok) return { error: toPublicError(result) };
// Signed in as this account: every session was revoked and this one // re-issued. With linkWithoutSession and no session, nobody is signed in. return { verified: true, session: result.value.session ?? null };}Where the package and the guide differ
Section titled “Where the package and the guide differ”- Codes. The guide prefers codes, suggests 8 digits, and says a code at that rate limit need not be hashed or attempt-counted. The package follows the shape and is stricter on storage: codes are HMACed and capped at 5 attempts, which costs nothing and survives a leaked token table.
- Rate limits. The guide keys its rate limit on the email address; the package limits guesses per code (the attempt cap) and per IP, and resends per account.
Attack tests
Section titled “Attack tests”| Test | Attack | Stopped by |
|---|---|---|
| A012 | Account pre-hijacking | flows/password-reset.ts, flows/email-verification.ts, flows/register.ts |
| A013 | Guessing a short code | tokens/one-time-tokens.ts, flows/password-reset.ts |
| A014 | Verifying an address with a proof meant for another account or address | flows/email-verification.ts |
| A083 | Using a verification link opened outside a session (linkWithoutSession) |
flows/email-verification.ts, flows/resolve-settings.ts |
Further reading
Section titled “Further reading”- Email address verification codes and Email addresses in Pilcrow’s auth guide.
- Pre-hijacked accounts, the study that named the attack.