feat(passwd): the reset reaches every copy the account owns (#303)

An account has one personal vault and one keyring, plus a local copy on each
machine. `logicsrc passwd` (and `--email`) changed this machine and left the
rest to manual commands: `vault sync`, then `keyring sync --use-remote` on
every other machine.

- passwd pulls the account's vault first and pushes it after. Without the
  pull, a password set on another machine since the last sync won on the next
  pull ("the account's wins") and silently replaced the one just chosen. It
  also lets a freshly logged-in machine with no local vault reset by email.
- `keyring sync` takes the account's keyring without a flag in the two cases
  where nothing can be lost: the same keyring under a newer password, or a
  newer keyring that holds every key this machine has (the old one is still
  backed up). Anything else stays a question, as before.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-12-02 21:50:07 -08:00 • committed by GitHub
parent ef965b0aaa
commit f1944ac101
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 269 additions and 7 deletions

View file

@ -11,12 +11,18 @@ import {
enrollEmailRecovery,
formatRecoveryCode,
fromBase64,
pullVault,
pushVault,
rewrapUserKey,
unlockVault,
unlockWithRecoveryKey,
type EmailRecoveryClient,
type EscrowEnrollment,
type Envelope,
type EscrowMaterial,
type RemoteItem,
type RemoteVault,
type SyncRemote,
type VaultStore
} from "@logicsrc/opencreds";
import { KeyringStore, createKeyring, openEntry, sealEntry, unlockKeyring } from "./keyring.js";
@ -259,6 +265,21 @@ describe("logicsrc passwd --email", () => {
expect(JSON.stringify(vault.readMeta())).toBe(before);
}, 60_000);
it("a failed push to the account does not undo the reset; it says what to run", async () => {
const { server } = await enrolled("a", "b");
const said: string[] = [];
const broken = () => ({ label: "down" }) as never;
const r = await changeAllPasswords(
{ email: true },
{ vault, keyring, sshDir: join(dir, "nowhere"), recovery: () => server.client, vaultRemote: broken, remote: () => undefined, ask: answers(server.inbox[0]!, "12345678", "", ""), askNew: async () => "n", say: (l) => said.push(l) }
);
expect(r.vault).toBe("changed");
expect(r.vaultSynced).toBe(false);
expect(said.join("\n")).toMatch(/Could not read the account's vault first/);
expect(said.join("\n")).toMatch(/Vault sync skipped\. Run: logicsrc vault sync/);
await expect(unlockVault(vault.readMeta()!, "n")).resolves.toBeInstanceOf(Uint8Array);
}, 60_000);
it("needs a signed-in account", async () => {
await seed("a", "b");
await expect(
@ -266,3 +287,107 @@ describe("logicsrc passwd --email", () => {
).rejects.toThrow(/logicsrc login/);
}, 60_000);
});
/** The account's copy of the vault, in memory (the shape of opencreds' own sync test remote). */
function fakeVaultAccount() {
let held: RemoteVault | null = null;
const rows = new Map<string, { envelope: string | null; revision: number; seq: number }>();
let seq = 0;
const snapshot = (): RemoteVault | null => (held ? JSON.parse(JSON.stringify(held)) : null);
const item = (id: string): RemoteItem => {
const r = rows.get(id);
if (!r) return { id, envelope: null, revision: 0, seq: 0 };
return { id, envelope: r.envelope ? { ...JSON.parse(r.envelope), revision: r.revision } : null, revision: r.revision, seq: r.seq };
};
const remote: SyncRemote = {
label: "test-account",
async getVault() {
return snapshot();
},
async putMeta(meta, baseRevision) {
const current = held?.metaRevision ?? 0;
if (current !== baseRevision) return { ok: false, vault: snapshot() };
held = { meta: JSON.parse(JSON.stringify(meta)), metaRevision: current + 1, folders: held?.folders ?? null };
return { ok: true, revision: current + 1 };
},
async putFolders(blob, baseRevision) {
if (!held) return { ok: false, vault: null };
const current = held.folders?.revision ?? 0;
if (current !== baseRevision) return { ok: false, vault: snapshot() };
held.folders = { ...blob, revision: current + 1 };
return { ok: true, revision: current + 1 };
},
async listItems(since) {
const items = [...rows.keys()].map(item).filter((i) => i.seq >= since).sort((a, b) => a.seq - b.seq);
return { items, cursor: items.reduce((m, i) => Math.max(m, i.seq), since) };
},
async putItems(changes) {
const applied: Array<{ id: string; revision: number; seq: number }> = [];
const conflicts: RemoteItem[] = [];
for (const c of changes) {
const current = rows.get(c.id)?.revision ?? 0;
if (current !== c.baseRevision || (c.baseRevision > 0 && !rows.has(c.id))) {
conflicts.push(item(c.id));
continue;
}
const { revision: _r, ...rest } = c.envelope ?? ({} as Envelope);
rows.set(c.id, { envelope: c.envelope ? JSON.stringify(rest) : null, revision: current + 1, seq: ++seq });
applied.push({ id: c.id, revision: current + 1, seq });
}
return { applied, conflicts };
},
async reset() {
held = null;
rows.clear();
}
};
return { remote, meta: () => held?.meta, has: (id: string) => Boolean(rows.get(id)?.envelope) };
}
describe("logicsrc passwd across machines", () => {
it("an email reset on a machine that is behind the account still wins everywhere", async () => {
const account = fakeVaultAccount();
const recovery = fakeRecovery();
// Machine A makes the vault, turns email recovery on, and syncs.
const created = await createVault("original");
vault.writeMeta(created.meta);
await enrollEmailRecovery(recovery.client, created.meta, created.userKey);
await pushVault(vault, account.remote, created.userKey);
// Machine B gets it, then falls behind: A changes the password again.
const b = createVaultStore(join(dir, "vault-b"));
const bKeyring = new KeyringStore(join(dir, "keyring-b"));
await pullVault(b, account.remote);
await changeAllPasswords(
{},
{ vault, keyring, vaultRemote: () => account.remote, remote: () => undefined, ask: answers("original"), askNew: async () => "set on A", say: () => {} }
);
await expect(unlockVault(account.meta()!, "set on A")).resolves.toBeInstanceOf(Uint8Array);
// B knows no password at all and resets by email.
const r = await changeAllPasswords(
{ email: true },
{
vault: b,
keyring: bKeyring,
sshDir: join(dir, "nowhere"),
recovery: () => recovery.client,
vaultRemote: () => account.remote,
remote: () => undefined,
ask: answers(recovery.inbox[0]!, "12345678"),
askNew: async () => "set on B",
say: () => {}
}
);
expect(r).toMatchObject({ vault: "changed", vaultSynced: true, keyring: "created", keyringEscrowed: true });
// B, the account, and then A after its next sync all open with B's password.
await expect(unlockVault(b.readMeta()!, "set on B")).resolves.toBeInstanceOf(Uint8Array);
await expect(unlockVault(account.meta()!, "set on B")).resolves.toBeInstanceOf(Uint8Array);
await expect(unlockVault(account.meta()!, "set on A")).rejects.toThrow();
await pullVault(vault, account.remote);
await expect(unlockVault(vault.readMeta()!, "set on B")).resolves.toBeInstanceOf(Uint8Array);
// The keyring's key went to the account inside the vault, as ciphertext.
expect(account.has(keyringEscrowId(bKeyring.readMeta()!))).toBe(true);
}, 90_000);
});