feat(passkeys): a password reset takes the passkeys with it (#42)
The last issue in the passkeys project, and the only one that adds no capability. It is the safety net, and it exists because this is the piece most likely to be skipped and most expensive to discover missing. A reset is the recovery path, and recovery has to be complete. The reset already deletes every session on the account, on the reasoning that a reset prompted by a compromise must not leave an intruder signed in for the remaining thirty days of their cookie. A passkey an intruder registered has no expiry at all. Leaving those behind would mean a customer can recover their password and still not have their account back. The obvious objection is that this hands whoever controls the mailbox a way to strip a customer's passkeys. It does, and it costs nothing: anyone who can complete a reset already controls the email address and therefore already controls the account. The passkeys were not protecting anything by that point. Anything in flight goes too. An intruder who pressed add a passkey moments before the reset could otherwise finish that ceremony afterwards and put a credential straight back onto the account the reset had just cleared. Changing a password deliberately does not do this, and the asymmetry is the point. A change requires the current password from someone already signed in, so nothing about it suggests a lockout or a compromise, and it already spares the current session for the same reason. A customer who suspects one particular device revokes that device by name from the account page, which is a better tool than deleting everything. A reset has no idea which credential is the problem, so it takes all of them. The customer is told twice. Before, in the reset email and on the reset form, unconditionally — that form has no session and is never told whether the account has passkeys, because answering that would make the reset page an oracle for it, so the wording has to read the same to someone who has none. After, with a count, and only when the count is not zero. That moment is the only one where the count can be reported: the rows are gone by the time anyone could go and look. A customer told two were removed who only remembers registering one has just learned something they could not otherwise find out. That notice is a panel that waits to be dismissed rather than a toast, because a toast dismisses itself and this is the message a customer needs to still be looking at while they decide what to do about it. The other question this issue asks — whether a reset ends a session established by a passkey — turns out to need no code, because #39 made both paths call one createSession. But "it falls out for free" is a claim, so there is now a test that establishes a session through that exact function and watches the reset end it. docs/ops/account-recovery.md records the whole policy, including the two answers that are not code. Losing an authenticator is not a lockout: the customer signs in with their password and revokes the lost credential themselves, which is why this change implements nothing for it. Losing the email address is a lockout, and there is deliberately no self-service route out — this shop holds no second proof of identity, and anything invented to fill that gap would be a weaker credential than the one it replaced. The manual route runs through the shop owner verifying against order history, and its third step, changing the address from the admin screen, does not exist yet. That is written down as a gap with its own notification and audit questions rather than smuggled in here. Verified: backend tsc clean for src and tests, 521 unit tests pass, lint clean apart from warnings that predate this branch; frontend tsc, lint and build clean. The integration and end-to-end suites need a database this machine has no Docker for, so CI is what proves those. Closes #42 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
a0238ea4ed
commit
36dbf18916
@@ -68,6 +68,12 @@ export const TEMPLATES: Record<TemplateKey, TemplateDefinition> = {
|
||||
defaultBody:
|
||||
'Someone asked to reset the password for this account.\n\n' +
|
||||
'[Choose a new password]({{resetUrl}}). This link expires in {{expiresIn}}.\n\n' +
|
||||
// Said before the customer follows the link rather than after they have
|
||||
// used it, because it is the one consequence of a reset they cannot undo
|
||||
// and might have chosen differently about (#42). Worded so it reads the
|
||||
// same to someone who has never registered one.
|
||||
'Resetting your password also removes any passkeys saved on this account, ' +
|
||||
'and signs you out everywhere. You can add your passkeys again afterwards.\n\n' +
|
||||
"If this wasn't you, you can ignore this email — your password has not changed."
|
||||
},
|
||||
|
||||
|
||||
@@ -374,6 +374,11 @@ router.post('/reset-password', asyncRoute(async (req: Request, res: Response) =>
|
||||
|
||||
const passwordHash = await bcrypt.hash(String(password), PASSWORD_HASH_ROUNDS);
|
||||
|
||||
// Reported back so the customer is told, rather than finding an empty list
|
||||
// the next time they look. Declared out here because it is decided inside the
|
||||
// transaction and read after it.
|
||||
let passkeysRemoved = 0;
|
||||
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
@@ -389,6 +394,37 @@ router.post('/reset-password', asyncRoute(async (req: Request, res: Response) =>
|
||||
// up to 30 days.
|
||||
await client.query(`DELETE FROM customer_sessions WHERE customer_id = $1`, [customerId]);
|
||||
await client.query(`DELETE FROM customer_tokens WHERE customer_id = $1 AND kind = 'password_reset'`, [customerId]);
|
||||
|
||||
// Passkeys go with the sessions, for the same reason and more of it (#42).
|
||||
//
|
||||
// A reset is the recovery path, and recovery has to be complete. The line
|
||||
// above already takes the position that a reset must evict anyone else
|
||||
// holding the account — a session an intruder holds lasts up to 30 days, and
|
||||
// a passkey an intruder registered lasts forever. Leaving those behind would
|
||||
// mean a customer can recover their password and still not have their
|
||||
// account back.
|
||||
//
|
||||
// The obvious objection is that this lets whoever controls the mailbox strip
|
||||
// a customer's passkeys. It does, and it costs nothing: anyone who can
|
||||
// complete a reset already controls the email address, and therefore already
|
||||
// controls the account. The passkeys were not protecting anything at that
|
||||
// point.
|
||||
//
|
||||
// Deliberately NOT the same rule as change-password, which leaves passkeys
|
||||
// alone. That one requires the current password from someone already signed
|
||||
// in — no part of it suggests a lockout or a compromise, and a customer who
|
||||
// suspects one device can revoke that device by name on the account page
|
||||
// (#40). This path has no idea which credential is the problem, so it takes
|
||||
// all of them.
|
||||
const removed = await client.query(`DELETE FROM customer_credentials WHERE customer_id = $1`, [customerId]);
|
||||
passkeysRemoved = removed.rowCount ?? 0;
|
||||
|
||||
// Including anything in flight. A registration challenge issued to an
|
||||
// intruder moments before the reset would otherwise still be completable
|
||||
// afterwards, which would put a passkey back on the account the reset just
|
||||
// cleared.
|
||||
await client.query(`DELETE FROM webauthn_challenges WHERE customer_id = $1`, [customerId]);
|
||||
|
||||
await client.query('COMMIT');
|
||||
} catch (err) {
|
||||
await client.query('ROLLBACK');
|
||||
@@ -400,7 +436,15 @@ router.post('/reset-password', asyncRoute(async (req: Request, res: Response) =>
|
||||
const { rows: fresh } = await pool.query<CustomerRecord>(`SELECT * FROM customers WHERE id = $1`, [customerId]);
|
||||
const sessionToken = await createSession(customerId);
|
||||
setSessionCookie(res, sessionToken);
|
||||
res.json(publicCustomer(requireRow(fresh, 'the customer whose password was just reset')));
|
||||
// The count rides along with the customer rather than being left for the
|
||||
// account page to imply. A customer who never registered a passkey sees zero
|
||||
// and is told nothing; one who is told two were removed and only remembers
|
||||
// registering one has just learned something they could not otherwise find
|
||||
// out — the row is already gone by the time they could go looking.
|
||||
res.json({
|
||||
...publicCustomer(requireRow(fresh, 'the customer whose password was just reset')),
|
||||
passkeysRemoved
|
||||
});
|
||||
}));
|
||||
|
||||
router.post('/login', asyncRoute(async (req: Request, res: Response) => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import request from 'supertest';
|
||||
import app from '../../src/app';
|
||||
import { pool } from '../../src/db';
|
||||
import { pool, requireRow } from '../../src/db';
|
||||
import { createSession } from '../../src/customerSession';
|
||||
import { resetDb, closeDb } from './setup/testDb';
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -204,4 +205,131 @@ describe('POST /api/customers/reset-password', () => {
|
||||
const retry = await request(app).post('/api/customers/reset-password').send({ token, password: 'long-enough-password' });
|
||||
expect(retry.status).toBe(200);
|
||||
});
|
||||
|
||||
// #42. A reset is the recovery path, so it has to leave the account with no
|
||||
// way in that the customer did not just establish. Sessions were already
|
||||
// covered above; a passkey outlives a session without bound.
|
||||
describe('and the passkeys on the account', () => {
|
||||
async function customerId(email: string): Promise<number> {
|
||||
const { rows } = await pool.query<{ id: number }>(`SELECT id FROM customers WHERE email = $1`, [email]);
|
||||
return requireRow(rows, 'the customer this test just registered').id;
|
||||
}
|
||||
|
||||
// Registering one for real needs an authenticator, which no test has. The
|
||||
// row is what the reset acts on, so the row is what these insert.
|
||||
async function giveAPasskey(id: number, credentialId: string): Promise<void> {
|
||||
await pool.query(
|
||||
`INSERT INTO customer_credentials (customer_id, credential_id, public_key, name)
|
||||
VALUES ($1, $2, 'not-a-real-key', 'Test key')`,
|
||||
[id, credentialId]
|
||||
);
|
||||
}
|
||||
|
||||
async function passkeyCount(id: number): Promise<number> {
|
||||
const { rows } = await pool.query<{ n: number }>(
|
||||
`SELECT count(*)::int AS n FROM customer_credentials WHERE customer_id = $1`,
|
||||
[id]
|
||||
);
|
||||
return requireRow(rows, 'a count of credentials').n;
|
||||
}
|
||||
|
||||
it('removes every passkey, so one an intruder registered does not survive it', async () => {
|
||||
await register('haskeys@example.com');
|
||||
const id = await customerId('haskeys@example.com');
|
||||
await giveAPasskey(id, 'credential-one');
|
||||
await giveAPasskey(id, 'credential-two');
|
||||
|
||||
const token = await requestReset('haskeys@example.com');
|
||||
const res = await request(app)
|
||||
.post('/api/customers/reset-password')
|
||||
.send({ token, password: 'a-brand-new-password' });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(await passkeyCount(id)).toBe(0);
|
||||
});
|
||||
|
||||
it('says how many it removed, because nothing else can report it afterwards', async () => {
|
||||
await register('counted@example.com');
|
||||
const id = await customerId('counted@example.com');
|
||||
await giveAPasskey(id, 'credential-counted');
|
||||
|
||||
const token = await requestReset('counted@example.com');
|
||||
const res = await request(app)
|
||||
.post('/api/customers/reset-password')
|
||||
.send({ token, password: 'a-brand-new-password' });
|
||||
|
||||
// The rows are gone by the time the customer could go and look, so a
|
||||
// reset that removed something and said nothing would hide exactly the
|
||||
// case worth knowing about.
|
||||
expect(res.body.passkeysRemoved).toBe(1);
|
||||
});
|
||||
|
||||
it('reports zero for a customer who never registered one', async () => {
|
||||
await register('nokeys@example.com');
|
||||
|
||||
const token = await requestReset('nokeys@example.com');
|
||||
const res = await request(app)
|
||||
.post('/api/customers/reset-password')
|
||||
.send({ token, password: 'a-brand-new-password' });
|
||||
|
||||
// The notice on the reset form is shown on this number, so zero has to
|
||||
// mean zero rather than undefined.
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.passkeysRemoved).toBe(0);
|
||||
});
|
||||
|
||||
it('leaves another customer’s passkeys alone', async () => {
|
||||
await register('mine@example.com');
|
||||
await register('theirs@example.com');
|
||||
const mine = await customerId('mine@example.com');
|
||||
const theirs = await customerId('theirs@example.com');
|
||||
await giveAPasskey(mine, 'credential-mine');
|
||||
await giveAPasskey(theirs, 'credential-theirs');
|
||||
|
||||
const token = await requestReset('mine@example.com');
|
||||
await request(app).post('/api/customers/reset-password').send({ token, password: 'a-brand-new-password' });
|
||||
|
||||
expect(await passkeyCount(theirs)).toBe(1);
|
||||
});
|
||||
|
||||
it('clears a challenge in flight, so a registration cannot land after the reset', async () => {
|
||||
await register('inflight@example.com');
|
||||
const id = await customerId('inflight@example.com');
|
||||
await pool.query(
|
||||
`INSERT INTO webauthn_challenges (challenge, customer_id, kind, expires_at)
|
||||
VALUES ('challenge-in-flight', $1, 'registration', now() + interval '5 minutes')`,
|
||||
[id]
|
||||
);
|
||||
|
||||
const token = await requestReset('inflight@example.com');
|
||||
await request(app).post('/api/customers/reset-password').send({ token, password: 'a-brand-new-password' });
|
||||
|
||||
// Otherwise an intruder who pressed "add a passkey" moments earlier could
|
||||
// finish the ceremony afterwards and put a credential back on the account
|
||||
// the reset had just cleared.
|
||||
const { rows } = await pool.query<{ n: number }>(
|
||||
`SELECT count(*)::int AS n FROM webauthn_challenges WHERE customer_id = $1`,
|
||||
[id]
|
||||
);
|
||||
expect(requireRow(rows, 'a count of challenges').n).toBe(0);
|
||||
});
|
||||
|
||||
it('terminates a session established by a passkey, not only one from a password', async () => {
|
||||
await register('passkeysession@example.com');
|
||||
const id = await customerId('passkeysession@example.com');
|
||||
|
||||
// The call the passkey login route makes. Not an imitation of it — the
|
||||
// same function, so this asserts the shared session path rather than
|
||||
// asserting that two paths happen to agree today.
|
||||
const passkeySession = await createSession(id);
|
||||
const asPasskeyHolder = () =>
|
||||
request(app).get('/api/customers/me').set('Cookie', `rd_session=${passkeySession}`);
|
||||
expect((await asPasskeyHolder()).status).toBe(200);
|
||||
|
||||
const token = await requestReset('passkeysession@example.com');
|
||||
await request(app).post('/api/customers/reset-password').send({ token, password: 'a-brand-new-password' });
|
||||
|
||||
expect((await asPasskeyHolder()).status).toBe(401);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user