Skip to content

vault: opt-in paid email recovery for the personal vault ($5 lifetime) - #298

Merged
ralyodio merged 3 commits into
masterfrom
riotcoder/vault-email-recovery
Nov 30, 2026
Merged

ralyodio merged 3 commits into
masterfrom
riotcoder/vault-email-recovery

Conversation

@ralyodio

Copy link
Copy Markdown
Contributor

What

Opt-in, paid ($5 lifetime) email recovery for the logicsrc personal vault (OpenCreds): if the master password and the offline recovery key are both lost, the user gets back in through their account email. The server never holds anything that alone, even with the DB and server env, opens an enrolled vault.

How

Enroll (logicsrc vault recovery enable, vault unlocked, purchase required):

  • CLI draws E (32 random bytes), escrowBlob = AES-256-GCM(K) under E with AAD <ns>:vault:email-escrow:v1|<createdAt>.
  • E is split into S (random) and U = E XOR S. CLI posts {escrowBlob, escrowIv, serverShare: S, userShare: U, vaultCreatedAt} to POST /api/opencreds/recovery/enroll (person lsk_ key only).
  • Server checks the entitlement (402 + checkout URL otherwise) and seals S under VAULT_ESCROW_KEY (versioned, AAD binds the user id). It stores the row as pending, renders U as a recovery code (Crockford base32, 11 data groups + 1 check group) and emails it. Only after the email is sent does it activate the row and revoke the prior one. If the email fails, it deletes the pending row and returns 502. U is never stored; there is no column for it. The response is {enrolled, email: masked}.

Recover (logicsrc vault recover --email, works locked):

  • The CLI checks the recovery code's check group first, so a typo costs nothing. Then start emails an 8-digit one-time code (HMAC at rest, 15 min, 5 tries, single use, 3 starts/hour). claim returns the blob and S decrypted, is audited, and triggers a "your vault email recovery was used" email.
  • CLI: E = S XOR U, decrypts K, proves it against an item, prompts for the new password twice, then rewrapUserKey + resetRecoveryKey (prints the new offline key once) and saves meta (autosync pushes it). It then re-enrolls with fresh E/S/U so the used code is dead; if that fails it warns loudly and keeps the recovered vault.
  • A wrong U gives the clear error "recovery code doesn't match" (exit 3). The one-time code is consumed; the purchase and enrollment stay.

Manage: logicsrc vault recovery status | buy | enable | disable. Disable revokes the enrollment and NULLs the escrow material.

Payment: POST /api/opencreds/recovery/checkout creates a CoinPay checkout with metadata {purpose: "vault_recovery", user_id, payment_ref} on property app.logicsrc.com. Ledger vault_recovery_payments (session id PK, status, settled_at); entitlement = any settled row. POST /api/coinpay/webhook now verifies the signature once and dispatches: vault_recovery goes to the new idempotent settler, everything else goes to the auto-rotation settler unchanged (existing tests green). Settings has a "Vault email recovery — $5 lifetime" card with status, a Buy button, the CLI hint and a switch-off.

Migrations: 014_vault_email_recovery.sql in both migrations/ and migrations-pg/ (enrollments, codes, payments, audit). Verified with migrate.mjs on SQLite and on a throwaway Postgres 17.

Docs: security.md and crypto.md gain an "Email recovery (opt-in, paid)" section that states the trade-off plainly. A stolen DB plus server env cannot open a vault without the enrollment email. A live server compromise during enroll/recover, or the mailbox plus a live server (or the mailbox plus the account sign-in), can. Vaults that are never enrolled stay zero-knowledge. cli.md documents the commands.

Tests

  • bun run --cwd packages/opencreds test: 264 passed (247 before; +16 email-recovery.test.ts, +1 standalone-binary CLI case)
  • bun run --cwd apps/pwa test: 137 passed (126 before; +11 vault-recovery.test.mjs); the new file and auto-rotation.test.mjs also pass against Postgres (PWA_TEST_DATABASE_URL)
  • bun run --cwd packages/cli test: 210 passed (+2 client tests)
  • tsc --noEmit clean for packages/opencreds and packages/cli

Before deploying (prod env)

  • VAULT_ESCROW_KEY: 32 random bytes, base64. Generate with node -e "console.log(require('crypto').randomBytes(32).toString('base64'))" and keep it in the logicsrc vault. Without it the endpoints answer 503 and the Settings card says "not available". Rotation: move the old key to VAULT_ESCROW_KEY_V<n>, set the new one, bump VAULT_ESCROW_KEY_VERSION.
  • VAULT_RECOVERY_PRICE_USD (optional, default 5).
  • Already required: RESEND_API_KEY, COINPAY_API_KEY, COINPAY_WEBHOOK_SECRET.
  • If the prod app role does not get DML on new tables by default privileges, grant SELECT/INSERT/UPDATE/DELETE on the four vault_recovery_* tables (INSERT/SELECT only on vault_recovery_audit).

🤖 Generated with Claude Code

A second way back into a logicsrc personal vault when the master password and
the offline recovery key are both gone, without the server holding anything
that alone (even with the DB and server env) opens the vault.

Client (packages/opencreds): random escrow key E encrypts the user key K
(AES-256-GCM, AAD "<ns>:vault:email-escrow:v1|<createdAt>"); E is split into
S (random, to the server) and U = E XOR S (emailed once as a checksummed
Crockford recovery code, never stored). `recover --email` checks the code's
check group first, asks the server for a one-time code, claims S, recombines
E, proves K against an item, sets a new password, issues a new offline
recovery key and re-enrolls with fresh shares (warns loudly if that fails).
New `recovery status|buy|enable|disable` commands; the CLI client lives in
packages/cli/src/vault-sync.ts.

Server (apps/pwa): /api/opencreds/recovery/{checkout,enroll,start,claim} +
status/disable. S is sealed under VAULT_ESCROW_KEY (versioned for rotation,
AAD binds the user). Enrollment is pending until the email with U is sent,
else rolled back. One-time codes: 8 digits, HMAC at rest, 15 min, 5 tries,
single use, 3 starts/hour; claims are audited and announced by email.
Purchase via CoinPay checkout (purpose vault_recovery); /api/coinpay/webhook
now verifies once and dispatches by purpose, auto_rotation unchanged.
Settings card with status and a Buy button. Migration 014 in both dialects.

Docs: security.md and crypto.md "Email recovery (opt-in, paid)" sections with
the honest trade-off; cli.md command reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

ThreatCrush Security Scan

2 finding(s) in the 20 file(s) this pull request changes.

LOW: 2

Severity Rule Location
LOW sql-template-interpolation apps/pwa/test/vault-recovery.test.mjs:234
LOW insecure-randomness-for-secret packages/opencreds/src/email-recovery.test.ts:168
58 pre-existing finding(s) elsewhere in the repository — **HIGH**: 3 | **MEDIUM**: 31 | **LOW**: 24

Not introduced by this pull request.

Severity Rule Location
HIGH js-host-header-trust apps/commandboard-web/server.js:27
HIGH secret-generic-high-entropy apps/pwa/.env.example:11
HIGH js-ssrf-outbound-request apps/pwa/public/sw.js:45
MEDIUM gha-ssh-key-secret .github/workflows/deploy-dev2-app-logicsrc-com.yml:39
MEDIUM gha-ssh-key-secret .github/workflows/deploy-dev2-logicsrc-com.yml:39
MEDIUM js-unescaped-html-sink apps/commandboard-web/src/main.ts:19
MEDIUM sh-remote-script-execution apps/logicsrc-web/public/install.sh:71
MEDIUM js-unescaped-html-sink apps/logicsrc-web/src/app/[[...slug]]/page.tsx:59
MEDIUM js-unescaped-html-sink apps/logicsrc-web/src/app/layout.tsx:88
MEDIUM js-unescaped-html-sink apps/logicsrc-web/src/app/pricing/page.tsx:42
MEDIUM js-unescaped-html-sink apps/logicsrc-web/src/components/breadcrumbs.tsx:43
MEDIUM js-unescaped-html-sink apps/logicsrc-web/src/components/pit-footer.tsx:16
MEDIUM js-unescaped-html-sink apps/logicsrc-web/src/components/site-shell.tsx:36
MEDIUM js-unescaped-html-sink apps/pwa/public/keyring.js:51
MEDIUM sql-template-interpolation apps/pwa/src/routes/rotation.mjs:79
MEDIUM redos-nested-quantifier packages/cli/src/rotation-catalog.ts:253
MEDIUM redos-nested-quantifier packages/cli/src/rotation-catalog.ts:297
MEDIUM redos-nested-quantifier packages/cli/src/rotation-catalog.ts:299
MEDIUM redos-nested-quantifier packages/cli/src/rotation-catalog.ts:303
MEDIUM redos-nested-quantifier packages/cli/src/rotation-catalog.ts:307

…and 38 more; the full set is in the SARIF artifact.

Snippets are redacted; ThreatCrush never prints matched credential material.

const tables = ["vault_recovery_enrollments", "vault_recovery_codes", "vault_recovery_payments", "vault_recovery_audit", "users", "api_keys"];
const needles = [payload.userShare, Buffer.from(U).toString("hex"), Buffer.from(U).toString("base64url"), code, code.replace(/-/g, "")];
for (const table of tables) {
for (const row of await all(`SELECT * FROM ${table}`)) {
async start() {
if (!enrollment?.active) throw new EmailRecoveryError("not enrolled", 404, "not_enrolled");
state.starts++;
otp = { code: String(Math.floor(Math.random() * 1e8)).padStart(8, "0"), used: false };
The account password resets by email, so mailbox control = sign-in + one-time
code + the enrollment code U. Say so in security.md and stop the one-time-code
email from implying the vault stays safe if someone else reads the mailbox.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ralyodio
ralyodio marked this pull request as ready for review November 30, 2026 15:22
Touching it made the credential gate re-scan the existing SESSION_SECRET
placeholder (false positive). VAULT_ESCROW_KEY / _VERSION / VAULT_RECOVERY_PRICE_USD
are documented in docs/opencreds/security.md and crypto.md; prod values live in
vault app-logicsrc-com--prod.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ralyodio
ralyodio merged commit 41ad9f0 into master Nov 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants