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

@ -3,7 +3,7 @@ import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, w
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { VaultMeta } from "@logicsrc/opencreds";
import { rewrapUserKey, type VaultMeta } from "@logicsrc/opencreds";
import {
KeyringStore,
authorizedKeysLines,
@ -197,6 +197,61 @@ describe.skipIf(!hasSshKeygen)("sync", () => {
expect(a.find("id_ed25519")).toBeUndefined();
});
it("takes a password changed on another machine without a flag (same keyring, newer wrap)", async () => {
const key = throwawayKey(dir);
const account = fakeAccount();
const a = new KeyringStore(join(dir, "pw-a"));
const created = await createKeyring("old");
a.writeMeta(created.meta);
a.write(await sealEntry(created.userKey, { name: "id_ed25519", privateKey: key.privateKey, publicKey: key.publicKey }));
await syncKeyring(a, account);
const b = new KeyringStore(join(dir, "pw-b"));
await syncKeyring(b, account);
// `logicsrc passwd` on a: re-wrap, then replace the account's copy.
const rewrapped = await rewrapUserKey(created.meta, created.userKey, "new");
a.writeMeta({ ...rewrapped, updatedAt: new Date(Date.now() + 2000).toISOString() });
await syncKeyring(a, account, "use-local");
const r = await syncKeyring(b, account);
expect(r.adoptedPassword).toBe(true);
expect(r.backup).toBeUndefined();
const bKey = await unlockKeyring(b.readMeta()!, { passphrase: "new" });
expect((await openEntry(bKey, b.get("id_ed25519"))).privateKey).toBe(key.privateKey);
});
it("takes a keyring reset on another machine when it holds every key this one has, and keeps a backup", async () => {
const key = throwawayKey(dir);
const account = fakeAccount();
const b = new KeyringStore(join(dir, "reset-b"));
const old = await createKeyring("forgotten");
b.writeMeta({ ...old.meta, createdAt: "2026-01-01T00:00:00.000Z" });
b.write(await sealEntry(old.userKey, { name: "id_ed25519", privateKey: key.privateKey, publicKey: key.publicKey }));
// `logicsrc passwd --email` on another machine: a new keyring with the same key in it.
const fresh = await createKeyring("one password");
await account.putMeta(fresh.meta);
await account.putEntry(await sealEntry(fresh.userKey, { name: "id_ed25519", privateKey: key.privateKey, publicKey: key.publicKey }));
const r = await syncKeyring(b, account);
expect(r.adoptedReset).toBe(true);
expect(r.backup && existsSync(r.backup)).toBe(true);
const bKey = await unlockKeyring(b.readMeta()!, { passphrase: "one password" });
expect((await openEntry(bKey, b.get("id_ed25519"))).privateKey).toBe(key.privateKey);
});
it("still asks when the newer account keyring would lose a key this machine has", async () => {
const key = throwawayKey(dir);
const account = fakeAccount();
const b = new KeyringStore(join(dir, "keep-b"));
const old = await createKeyring("forgotten");
b.writeMeta({ ...old.meta, createdAt: "2026-01-01T00:00:00.000Z" });
b.write(await sealEntry(old.userKey, { name: "id_ed25519", privateKey: key.privateKey, publicKey: key.publicKey }));
await account.putMeta((await createKeyring("one password")).meta);
await expect(syncKeyring(b, account)).rejects.toBeInstanceOf(KeyringDivergedError);
expect(b.find("id_ed25519")).toBeTruthy();
});
it("refuses to merge two different keyrings unless told which wins", async () => {
const account = fakeAccount();
const first = await createKeyring("one");