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

@ -344,6 +344,116 @@ function fakeVaultAccount() {
return { remote, meta: () => held?.meta, has: (id: string) => Boolean(rows.get(id)?.envelope) };
}
/** An account with assisted recovery: it keeps U too and releases it for a confirmed step-up. */
function fakeAssistedRecovery() {
let enrollment: (EscrowMaterial & { userShare?: string; assisted: boolean }) | null = null;
let otp: { code: string; used: boolean } | null = null;
const stepups = new Map<string, string>();
const state = { enrolls: 0, polls: 0, approveAfterPolls: 2 };
const client: EmailRecoveryClient = {
label: "fake.test",
async status() {
return { enrolled: Boolean(enrollment), enrolledAt: null, email: "m***@example.com", purchased: true, priceUsd: 5, assisted: Boolean(enrollment?.assisted), stepupMethods: { passkey: true, coinpay: false } };
},
async checkout() {
return { url: "https://pay.test/1", priceUsd: 5 };
},
async enroll(p: EscrowEnrollment) {
state.enrolls++;
enrollment = { escrowBlob: p.escrowBlob, escrowIv: p.escrowIv, serverShare: p.serverShare, vaultCreatedAt: p.vaultCreatedAt, assisted: p.assisted === true, userShare: p.assisted ? p.userShare : undefined };
otp = null;
return { enrolled: true, email: "m***@example.com", assisted: p.assisted === true };
},
async start() {
otp = { code: "12345678", used: false };
return { sent: true, email: "m***@example.com" };
},
async claim(code: string, stepupId?: string) {
if (!stepupId || stepups.get(stepupId) !== "approved") throw new EmailRecoveryError("Confirm the recovery in your browser first", 403, "stepup_required");
if (!otp || otp.used || otp.code !== code) throw new EmailRecoveryError("Wrong one-time code", 401, "wrong_code");
otp.used = true;
stepups.set(stepupId, "used");
const { assisted: _a, ...material } = enrollment!;
return { ...material };
},
async stepup() {
stepups.set("su_1", "pending");
return { id: "su_1", url: "https://fake.test/recover/confirm/su_1", expiresIn: 600 };
},
async stepupStatus(id: string) {
// The person confirms in the browser a moment later.
if (++state.polls >= state.approveAfterPolls && stepups.get(id) === "pending") stepups.set(id, "approved");
return { status: stepups.get(id) ?? "expired" };
},
async disable() {
enrollment = null;
return { disabled: true };
}
};
return { client, state, assisted: () => Boolean(enrollment?.assisted) };
}
describe("logicsrc passwd --email with assisted recovery", () => {
it("asks for no enrollment-email code: a browser confirmation and the one-time code are enough", async () => {
await seed("lost", "also lost");
const server = fakeAssistedRecovery();
await enrollEmailRecovery(server.client, vault.readMeta()!, await unlockVault(vault.readMeta()!, "lost"), { assisted: true });
const said: string[] = [];
const r = await changeAllPasswords(
{ noSync: true, email: true },
{
vault,
keyring,
sshDir: join(dir, "nowhere"),
recovery: () => server.client,
sleep: async () => {},
// Only the one-time code, then Enter twice for the lost keyring. No recovery code prompt.
ask: answers("12345678", "", ""),
askNew: async () => "one password",
say: (l) => said.push(l)
}
);
expect(r).toMatchObject({ vault: "changed", keyring: "created", emailRearmed: true, assistedRecovery: true });
await expect(unlockVault(vault.readMeta()!, "one password")).resolves.toBeInstanceOf(Uint8Array);
expect(said.join("\n")).toMatch(/https:\/\/fake\.test\/recover\/confirm\/su_1/);
expect(said.join("\n")).not.toMatch(/enrollment email|email-recovery code/);
// Re-armed the same way: still assisted.
expect(server.state.enrolls).toBe(2);
expect(server.assisted()).toBe(true);
}, 60_000);
it("gives up cleanly, changing nothing, when the link is never confirmed", async () => {
await seed("lost", "also lost");
const server = fakeAssistedRecovery();
await enrollEmailRecovery(server.client, vault.readMeta()!, await unlockVault(vault.readMeta()!, "lost"), { assisted: true });
server.state.approveAfterPolls = Number.POSITIVE_INFINITY;
const before = JSON.stringify(vault.readMeta());
let polls = 0;
const realNow = Date.now;
await expect(
changeAllPasswords(
{ noSync: true, email: true },
{
vault,
keyring,
recovery: () => server.client,
// Each wait jumps the clock past the link's lifetime.
sleep: async () => {
if (++polls > 1) Date.now = () => realNow() + 11 * 60 * 1000;
},
ask: answers(),
askNew: async () => "n",
say: () => {}
}
).finally(() => {
Date.now = realNow;
})
).rejects.toThrow(/expired before it was confirmed/);
expect(JSON.stringify(vault.readMeta())).toBe(before);
}, 60_000);
});
describe("logicsrc passwd across machines", () => {
it("an email reset on a machine that is behind the account still wins everywhere", async () => {
const account = fakeVaultAccount();