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

@ -537,9 +537,6 @@ export function registerCredsCommands(parent: Command, options: RegisterOptions
async function recoverByEmail(store: VaultStore, client: EmailRecoveryClient): Promise<void> {
requireMeta(store);
process.stderr.write(
'Email recovery needs the recovery code from the "Your LogicSRC vault email-recovery code" email.\n',
);
let result: EmailRecoveryResult;
try {
result = await runEmailRecovery({
@ -615,6 +612,15 @@ vault stays zero-knowledge. See docs/opencreds/security.md for the trade-off.`))
` Enrolled ${status.enrolled ? `yes, since ${status.enrolledAt ?? "?"}` : `no${status.purchased ? ` (\`${cli} recovery enable\`)` : ""}`}\n`,
);
if (status.email) process.stdout.write(` Email ${status.email}\n`);
if (status.enrolled) {
const m = status.stepupMethods;
const have = [m?.passkey ? "passkey" : "", m?.coinpay ? "CoinPay" : ""].filter(Boolean).join(" or ");
process.stdout.write(
status.assisted
? ` Second proof a ${have || "passkey or CoinPay"} sign-in in the browser (assisted recovery: the server can open this vault)\n`
: ` Second proof the recovery code in the enrollment email${have ? ` (or switch to ${have}: \`${cli} recovery enable --assisted\`)` : ""}\n`,
);
}
});
});
@ -637,7 +643,11 @@ vault stays zero-knowledge. See docs/opencreds/security.md for the trade-off.`))
recovery
.command("enable")
.description("enroll this vault in email recovery; running it again re-keys and emails a new code")
.action(async function (this: Command) {
.option(
"--assisted",
"no old email needed: recover with the emailed one-time code plus a passkey or CoinPay sign-in. The server then keeps both halves of the recovery key and is able to open this vault",
)
.action(async function (this: Command, opts: { assisted?: boolean }) {
await run(async () => {
const store = storeFor(this);
const client = recoveryClient(store);
@ -652,13 +662,30 @@ vault stays zero-knowledge. See docs/opencreds/security.md for the trade-off.`))
);
}
const userKey = await unlock(store);
let result: { enrolled: boolean; email: string };
if (opts.assisted && status.stepupMethods && !status.stepupMethods.passkey && !status.stepupMethods.coinpay) {
fail(
"Assisted recovery confirms with a passkey or a connected CoinPay account, and this account has neither. " +
"Add a passkey or sign in with CoinPay on the account's Settings page, then run this again.",
EXIT.USAGE,
);
}
let result: { enrolled: boolean; email: string; assisted?: boolean };
try {
result = await enrollEmailRecovery(client, meta, userKey);
result = await enrollEmailRecovery(client, meta, userKey, { assisted: Boolean(opts.assisted) });
} catch (err) {
const e = err as EmailRecoveryError;
fail(`${e.message}${e.checkoutUrl ? `\n\n ${e.checkoutUrl}\n` : ""}`, EXIT.USAGE);
}
if (opts.assisted) {
process.stdout.write(
"Assisted recovery is on. To get back in you need two things: the one-time code we email at that moment, " +
"and a passkey or CoinPay sign-in in the browser. No old email is needed.\n" +
"The trade: the server now keeps both halves of your recovery key (sealed) and is able to open this vault. " +
`Go back to the stricter setup any time with \`${cli} recovery enable\`.\n` +
(status.enrolled ? "The previous recovery code no longer works.\n" : ""),
);
return;
}
process.stdout.write(
`Email recovery is on. Your recovery code was emailed to ${result.email}.\n` +
"Keep that email: it is half of the way back in, and it is not stored anywhere else.\n" +

View file

@ -70,6 +70,12 @@ export interface EscrowEnrollment {
serverShare: string;
userShare: string;
vaultCreatedAt: string;
/**
* Assisted recovery (opt-in): ask the server to keep U too, sealed, and to release it
* after the one-time code plus a passkey or CoinPay sign-in. Custodial: the server then
* holds everything that opens the vault.
*/
assisted?: boolean;
}
/** What the server releases on a successful claim. */
@ -78,6 +84,8 @@ export interface EscrowMaterial {
escrowIv: string;
serverShare: string;
vaultCreatedAt: string;
/** Only on an assisted claim (a confirmed step-up): U, base64. */
userShare?: string;
}
export interface EmailRecoveryStatus {
@ -87,6 +95,18 @@ export interface EmailRecoveryStatus {
email: string | null;
purchased: boolean;
priceUsd: number;
/** Assisted recovery is on: no enrollment-email code needed, a browser confirmation instead. */
assisted?: boolean;
/** What the account can confirm with. */
stepupMethods?: { passkey: boolean; coinpay: boolean };
}
/** A browser confirmation of an assisted recovery. */
export interface RecoveryStepup {
id: string;
url: string;
/** Seconds until the link dies. */
expiresIn: number;
}
/** The account's email-recovery endpoints. The logicsrc CLI provides one. */
@ -97,7 +117,11 @@ export interface EmailRecoveryClient {
/** Throws EmailRecoveryError with status 402 and a checkoutUrl when not purchased. */
enroll(payload: EscrowEnrollment): Promise<{ enrolled: boolean; email: string }>;
start(): Promise<{ sent: boolean; email?: string }>;
claim(code: string): Promise<EscrowMaterial>;
/** With `stepupId` (assisted recovery) the material carries U as well. */
claim(code: string, stepupId?: string): Promise<EscrowMaterial>;
/** Assisted recovery: open a confirmation and read its status. Absent on a client that cannot. */
stepup?(): Promise<RecoveryStepup>;
stepupStatus?(id: string): Promise<{ status: string; method?: string | null }>;
disable(): Promise<{ disabled: boolean }>;
}
@ -195,8 +219,10 @@ export async function enrollEmailRecovery(
client: EmailRecoveryClient,
meta: VaultMeta,
userKey: Uint8Array,
): Promise<{ enrolled: boolean; email: string }> {
opts: { assisted?: boolean } = {},
): Promise<{ enrolled: boolean; email: string; assisted?: boolean }> {
const prepared = await prepareEnrollment(meta, userKey);
if (opts.assisted) prepared.payload.assisted = true;
try {
return await client.enroll(prepared.payload);
} finally {
@ -338,6 +364,73 @@ export interface EmailRecoveryPrompts {
oneTimeCode(email: string | undefined): Promise<string>;
/** The new master password, already confirmed. */
newPassword(): Promise<string>;
/** Test seam for the wait between confirmation checks. */
sleep?(ms: number): Promise<void>;
}
/**
* The vault's user key through email recovery, by whichever second proof the
* account uses. Nothing on disk changes here.
*
* - Assisted recovery on: a link to confirm in a browser (passkey or CoinPay),
* then the emailed one-time code. No enrollment-email code is asked for.
* - Otherwise: the recovery code from the enrollment email (checked first, so a
* typo costs nothing), then the emailed one-time code.
*/
export async function obtainUserKeyByEmail(opts: {
store: VaultStore;
meta: VaultMeta;
client: EmailRecoveryClient;
prompts: Pick<EmailRecoveryPrompts, "recoveryCode" | "oneTimeCode" | "sleep">;
say?: (line: string) => void;
}): Promise<{ userKey: Uint8Array; assisted: boolean }> {
const { store, meta, client, prompts } = opts;
const say = opts.say ?? (() => {});
const status = await client.status().catch(() => undefined);
if (status?.assisted && client.stepup && client.stepupStatus) {
const stepup = await client.stepup();
say("Confirm this recovery in a browser, with your passkey or your CoinPay account:");
say(`\n ${stepup.url}\n`);
say(`Waiting for the confirmation (the link works for ${Math.max(1, Math.round(stepup.expiresIn / 60))} minutes)...`);
const sleep = prompts.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
const deadline = Date.now() + stepup.expiresIn * 1000;
for (;;) {
const s = await client.stepupStatus(stepup.id);
if (s.status === "approved") break;
if (s.status !== "pending" || Date.now() > deadline) {
throw new EmailRecoveryError("The confirmation link expired before it was confirmed. Run this again.", undefined, "stepup_expired");
}
await sleep(2000);
}
say("Confirmed.");
const started = await client.start();
say(`A one-time code was emailed to ${started.email ?? "your account email"}. It expires in 15 minutes.`);
const code = (await prompts.oneTimeCode(started.email)).replace(/\s+/g, "");
const material = await client.claim(code, stepup.id);
if (!material.userShare) {
throw new EmailRecoveryError("The account did not release the recovery key. Run this again.", undefined, "stepup_required");
}
const userShare = fromBase64(material.userShare);
material.userShare = "";
try {
return { userKey: await recoverUserKeyByEmail({ meta, material, userShare, envelopes: store.listEnvelopes() }), assisted: true };
} finally {
wipe(userShare);
}
}
say('This needs the recovery code from your newest "Your LogicSRC vault email-recovery code" email.');
const userShare = await parseRecoveryCode(await prompts.recoveryCode());
try {
const started = await client.start();
say(`A one-time code was emailed to ${started.email ?? "your account email"}. It expires in 15 minutes.`);
const code = (await prompts.oneTimeCode(started.email)).replace(/\s+/g, "");
const material = await client.claim(code);
return { userKey: await recoverUserKeyByEmail({ meta, material, userShare, envelopes: store.listEnvelopes() }), assisted: false };
} finally {
wipe(userShare);
}
}
export interface EmailRecoveryResult {
@ -346,6 +439,8 @@ export interface EmailRecoveryResult {
/** The new offline recovery key, to print once. */
recoveryKey: string;
reenrolled: boolean;
/** Assisted recovery was on, and stays on after the re-arm. */
assisted?: boolean;
reenrolledEmail?: string;
reenrollError?: string;
}
@ -370,18 +465,7 @@ export async function runEmailRecovery(opts: {
if (!meta) throw new EmailRecoveryError(`No vault at ${store.baseDir}`, undefined, "no_vault");
assertProfile(meta, ["user"]);
const userShare = await parseRecoveryCode(await prompts.recoveryCode());
const userKey = await (async () => {
try {
const started = await client.start();
say(`A one-time code was emailed to ${started.email ?? "your account email"}. It expires in 15 minutes.`);
const code = (await prompts.oneTimeCode(started.email)).replace(/\s+/g, "");
const material = await client.claim(code);
return await recoverUserKeyByEmail({ meta, material, userShare, envelopes: store.listEnvelopes() });
} finally {
wipe(userShare);
}
})();
const { userKey, assisted } = await obtainUserKeyByEmail({ store, meta, client, prompts, say });
const password = await prompts.newPassword();
if (!password) throw new EmailRecoveryError("A master password is required", undefined, "no_password");
@ -389,9 +473,9 @@ export async function runEmailRecovery(opts: {
const reset = await resetRecoveryKey(rewrapped, userKey);
store.writeMeta(reset.meta);
const result: EmailRecoveryResult = { meta: reset.meta, userKey, recoveryKey: reset.recoveryKey, reenrolled: false };
const result: EmailRecoveryResult = { meta: reset.meta, userKey, recoveryKey: reset.recoveryKey, reenrolled: false, assisted };
try {
const enrolled = await enrollEmailRecovery(client, reset.meta, userKey);
const enrolled = await enrollEmailRecovery(client, reset.meta, userKey, { assisted });
result.reenrolled = Boolean(enrolled.enrolled);
result.reenrolledEmail = enrolled.email;
} catch (err) {

View file

@ -193,6 +193,7 @@ export {
parseRecoveryCode,
prepareEnrollment,
recoverUserKeyByEmail,
obtainUserKeyByEmail,
runEmailRecovery,
sealEscrow,
splitEscrowKey,
@ -201,6 +202,7 @@ export {
type EmailRecoveryPrompts,
type EmailRecoveryResult,
type EmailRecoveryStatus,
type RecoveryStepup,
type EscrowEnrollment,
type EscrowMaterial,
} from "./email-recovery.js";