feat(recovery): assisted recovery, no old email: one-time code + passkey or CoinPay (CLI 0.18.0) (#305)

Email recovery needs the recovery code from the enrollment email. People who
delete their mail lose it, and with it the only way back in.

`logicsrc vault recovery enable --assisted` (opt-in) replaces that code with a
fresh sign-in: recovery asks for the one-time code emailed at that moment and a
confirmation in the browser with a passkey of the account or its connected
CoinPay identity (bound by subject, never by email, never creating an account,
no session made).

- Server: U is kept sealed under VAULT_ESCROW_KEY in vault_recovery_assisted
  (migration 015) only for accounts that ask. A step-up
  (vault_recovery_stepups) is single use, lives ten minutes, names no account
  and is approved only by a proof belonging to the account being recovered.
  The second proof is checked before the one-time code, so a missing
  confirmation never spends it. A claim without a confirmed step-up never
  returns U. disable and a plain re-enroll delete the stored U.
- CLI: `passwd --email` and `vault recover --email` print the link, wait for
  the confirmation, then ask for the one-time code. The re-arm keeps the mode.
- This mode is custodial: the server holds everything that opens the vault.
  docs/opencreds/security.md says so; accounts that do not opt in keep the
  split-key scheme unchanged.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-12-02 22:17:56 -08:00 • committed by GitHub
parent 896eba3c07
commit 6ff3089088
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 799 additions and 62 deletions

View file

@ -43,7 +43,7 @@ if (isPostgres) {
await db.execute("DROP SCHEMA public CASCADE");
await db.execute("CREATE SCHEMA public");
}
for (const file of ["001_auth.sql", "002_credshare.sql", "005_machine_keys.sql", "007_hardening.sql", "010_rotation_runs.sql", "011_rotation_policy.sql", "012_auto_rotation_billing.sql", "014_vault_email_recovery.sql"]) {
for (const file of ["001_auth.sql", "002_credshare.sql", "005_machine_keys.sql", "007_hardening.sql", "010_rotation_runs.sql", "011_rotation_policy.sql", "012_auto_rotation_billing.sql", "014_vault_email_recovery.sql", "015_vault_assisted_recovery.sql"]) {
const sql = readFileSync(join(here, "..", "src", isPostgres ? "migrations-pg" : "migrations", file), "utf8");
for (const statement of sql.split(/;\s*$/m).map((s) => s.trim()).filter(Boolean)) await db.execute(statement);
}
@ -144,7 +144,7 @@ test("enroll without the purchase: 402 with a checkout link, and nothing stored"
assert.match(r.body.checkoutUrl, /^https:\/\/coinpay\.test\/pay\/cs_vr\d+$/);
assert.equal(Number((await get(`SELECT COUNT(*) AS n FROM vault_recovery_enrollments WHERE user_id = ?`, ["u_a"])).n), 0);
const status = await call(keys.u_a, "GET", "/api/opencreds/recovery");
assert.deepEqual({ ...status.body, enrolledAt: null }, { enrolled: false, enrolledAt: null, email: "u***@example.com", purchased: false, priceUsd: 5, configured: true });
assert.deepEqual({ ...status.body, enrolledAt: null }, { enrolled: false, assisted: false, stepupMethods: { passkey: false, coinpay: false }, enrolledAt: null, email: "u***@example.com", purchased: false, priceUsd: 5, configured: true });
});
test("webhook: settles vault_recovery once (replay is a no-op) and still settles auto_rotation", async () => {
@ -380,3 +380,111 @@ test("an enroll right after a claim sends the finished wording; a later one send
assert.equal((await call(keys.u_a, "POST", "/api/opencreds/recovery/enroll", await enrollPayload())).status, 201);
assert.equal(mail.at(-1).subject, "Your LogicSRC vault email-recovery code — keep this email");
});
// ---- assisted recovery (opt-in): no old email, a passkey or CoinPay step-up instead ----
async function assistedUser(uid, { coinpay = true } = {}) {
await run(`INSERT INTO users (id, email, coinpay_sub, created_at) VALUES (?,?,?,?)`, [uid, `${uid}@example.com`, coinpay ? `sub_${uid}` : null, Date.now()]).catch(() => {});
await vr.settleVaultRecovery({ checkoutSessionId: `cs_${uid}`, paymentId: null, userId: uid, amountUsd: 5, method: "comp" });
return get(`SELECT id, email FROM users WHERE id = ?`, [uid]);
}
test("assisted: a plain enrollment stores no user share, and cannot be claimed with a step-up", async () => {
const user = await assistedUser("u_as1");
const payload = await enrollPayload();
assert.equal((await vr.enroll(user, vr.parseEnrollment(payload))).assisted, false);
assert.equal((await all(`SELECT 1 FROM vault_recovery_assisted WHERE user_id = ?`, [user.id])).length, 0);
assert.equal((await vr.recoveryStatus(user)).assisted, false);
await assert.rejects(vr.createStepup(user), (e) => e.code === "not_assisted");
});
test("assisted: needs a passkey or a connected CoinPay account before it can be turned on", async () => {
const user = await assistedUser("u_as2", { coinpay: false });
await assert.rejects(vr.enroll(user, vr.parseEnrollment({ ...(await enrollPayload()), assisted: true })), (e) => e.code === "no_stepup_method");
assert.equal((await all(`SELECT 1 FROM vault_recovery_enrollments WHERE user_id = ?`, [user.id])).length, 0);
});
test("assisted: one-time code + an approved step-up release both shares, once; the code alone releases only S", async () => {
const user = await assistedUser("u_as3");
const payload = await enrollPayload();
const r = await vr.enroll(user, vr.parseEnrollment({ ...payload, assisted: true }));
assert.equal(r.assisted, true);
assert.equal((await vr.recoveryStatus(user)).assisted, true);
// U is at rest only sealed: no table holds it in the clear.
const row = await get(`SELECT * FROM vault_recovery_assisted WHERE user_id = ?`, [user.id]);
assert.ok(row.user_share_ct && !row.user_share_ct.includes(payload.userShare));
const stepup = await vr.createStepup(user);
assert.match(stepup.url, /\/recover\/confirm\/[A-Za-z0-9_-]{20,}$/);
assert.deepEqual(await vr.stepupStatus(user, stepup.id), { status: "pending", method: null });
// Not approved yet: refused, and the one-time code is NOT spent by the refusal.
await vr.startRecovery(user);
const otp = /One-time code: (\d{8})/.exec(mail.at(-1).text)[1];
await assert.rejects(vr.claimRecovery(user, otp, {}, Date.now(), stepup.id), (e) => e.code === "stepup_required");
// A proof for some other account approves nothing.
await assert.rejects(vr.approveStepup(stepup.id, "u_a", "coinpay"), (e) => e.code === "stepup_wrong_account");
await assert.rejects(vr.approveStepup(stepup.id, null, "passkey"), (e) => e.code === "stepup_wrong_account");
assert.equal((await vr.stepupStatus(user, stepup.id)).status, "pending");
await vr.approveStepup(stepup.id, user.id, "coinpay");
assert.deepEqual(await vr.stepupStatus(user, stepup.id), { status: "approved", method: "coinpay" });
const material = await vr.claimRecovery(user, otp, {}, Date.now(), stepup.id);
assert.equal(material.userShare, payload.userShare);
assert.equal(material.serverShare, payload.serverShare);
// Single use: the step-up and the code are both spent.
assert.equal((await vr.stepupStatus(user, stepup.id)).status, "used");
await assert.rejects(vr.claimRecovery(user, otp, {}, Date.now(), stepup.id), (e) => e.code === "stepup_required");
// Without a step-up the server never hands out U, assisted or not.
await vr.startRecovery(user);
const otp2 = /One-time code: (\d{8})/.exec(mail.at(-1).text)[1];
const plain = await vr.claimRecovery(user, otp2);
assert.equal(plain.userShare, undefined);
});
test("assisted: a step-up expires, another account cannot read its status, and disable deletes the stored share", async () => {
const user = await assistedUser("u_as4");
await vr.enroll(user, vr.parseEnrollment({ ...(await enrollPayload()), assisted: true }));
const stepup = await vr.createStepup(user);
assert.deepEqual(await vr.stepupStatus({ id: "u_a" }, stepup.id), { status: "expired" });
const later = Date.now() + vr.STEPUP_TTL_MS + 1000;
await assert.rejects(vr.approveStepup(stepup.id, user.id, "passkey", {}, later), (e) => e.code === "stepup_expired");
await vr.disable(user.id);
assert.equal((await all(`SELECT 1 FROM vault_recovery_assisted WHERE user_id = ?`, [user.id])).length, 0);
});
test("assisted: the routes exist, and the confirmation page for an unknown link is gone, not an error", async () => {
const r = await call(keys.u_a, "POST", "/api/opencreds/recovery/stepup");
assert.equal(r.status, 409);
assert.equal(r.body.code, "not_assisted");
const s = await call(keys.u_a, "GET", `/api/opencreds/recovery/stepup/${"x".repeat(32)}`);
assert.deepEqual(s.body, { status: "expired" });
});
test("assisted: the confirmation page shows for a live link, is gone for a dead one, and a passkey of nobody approves nothing", async () => {
const user = await assistedUser("u_as5");
await vr.enroll(user, vr.parseEnrollment({ ...(await enrollPayload()), assisted: true }));
const stepup = await vr.createStepup(user);
const live = await realFetch(`${base}/recover/confirm/${stepup.id}`);
assert.equal(live.status, 200);
const html = await live.text();
assert.match(html, /Confirm vault recovery/);
assert.doesNotMatch(html, /u_as5@example\.com/, "the page names no account");
const dead = await realFetch(`${base}/recover/confirm/${"z".repeat(32)}`);
assert.equal(dead.status, 410);
// This account has no passkey: there is nothing to assert with.
const options = await realFetch(`${base}/recover/confirm/${stepup.id}/passkey/options`, { method: "POST" });
assert.equal(options.status, 409);
assert.equal((await vr.stepupStatus(user, stepup.id)).status, "pending");
// Once approved, the page says to go back to the terminal.
await vr.approveStepup(stepup.id, user.id, "coinpay");
assert.match(await (await realFetch(`${base}/recover/confirm/${stepup.id}`)).text(), /Go back to your terminal/);
});