Files
redefined-designs/frontend/tests/e2e/support/db.ts
T
synAdminandClaude Opus 5 36dbf18916
Linting / lint (pull_request) Canceled after 0s
SonarQube Analysis / sonarqube (pull_request) Canceled after 0s
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>
2026-09-09 16:03:39 -05:00

94 lines
3.8 KiB
TypeScript

import { Client } from 'pg';
/**
* Direct database access for the tests, for the one thing the API deliberately
* will not give them.
*
* The password-reset token is only ever delivered by email, which these tests
* cannot read. It is read from the database rather than through a helper
* endpoint because an endpoint that returns a reset token for an arbitrary
* address is account takeover for every customer if it is ever reachable, and
* an environment gate is a thin thing to stand between that and production.
* Doing it here keeps the capability entirely inside the test process.
*
* That reasoning is unchanged from when it lived inline in
* password-reset.spec.ts. What has changed is that it is no longer sitting in a
* spec where it can be copied into the next one that wants a shortcut.
*/
/**
* The default port is 55500, matching scripts/start-local.ps1.
*
* It used to be 55432, which is the integration suite's disposable Postgres —
* a different database, with different credentials, that the app under test is
* not connected to. Worse, 55432 is reserved by Hyper-V on at least one machine
* here, so the spec failed with a bare ECONNREFUSED naming a port nobody had
* chosen. TEST_PGPORT still overrides, for CI and for anyone running the stack
* somewhere else.
*/
function connectionSettings() {
return {
host: process.env.TEST_PGHOST || 'localhost',
port: parseInt(process.env.TEST_PGPORT || '55500', 10),
user: process.env.TEST_PGUSER || 'redefined_local',
password: process.env.TEST_PGPASSWORD || 'redefined_local',
database: process.env.TEST_PGDATABASE || 'redefined_local'
};
}
/** Opens a connection, runs the query, and closes it whatever happens. */
async function withClient<T>(run: (client: Client) => Promise<T>): Promise<T> {
const client = new Client(connectionSettings());
await client.connect();
try {
return await run(client);
} finally {
await client.end();
}
}
/**
* The most recent password-reset token issued to an address.
*
* Throws rather than returning null: every caller is about to build a URL from
* it, and a missing token means the request under test did not do what it said,
* which is worth failing loudly at the point it happened.
*/
export async function readPasswordResetToken(email: string): Promise<string> {
return withClient(async (client) => {
const { rows } = await client.query(
`SELECT t.token FROM customer_tokens t
JOIN customers c ON c.id = t.customer_id
WHERE c.email = $1 AND t.kind = 'password_reset'
ORDER BY t.created_at DESC LIMIT 1`,
[email]
);
if (!rows.length) throw new Error(`no password_reset token issued for ${email}`);
return rows[0].token as string;
});
}
/**
* Puts a passkey on an account, for the tests that are about what happens to it
* (#42).
*
* Registering one for real needs an authenticator, which Playwright does not
* have and which no fixture can supply. The credential is a row, and a password
* reset acts on the row rather than on the hardware behind it, so a row is
* enough to test the interaction — nothing here ever tries to sign in with it.
*
* Here rather than in a spec for the same reason as the token read above: the
* next spec that wants a shortcut should have to come and ask for one.
*/
export async function giveCustomerAPasskey(email: string, name = 'Test key'): Promise<void> {
await withClient(async (client) => {
const { rowCount } = await client.query(
`INSERT INTO customer_credentials (customer_id, credential_id, public_key, name)
SELECT id, 'e2e-' || id || '-' || floor(random() * 1e9)::text, 'not-a-real-key', $2
FROM customers WHERE email = $1`,
[email, name]
);
if (rowCount === 0) throw new Error(`no customer to give a passkey to: ${email}`);
});
}