logicsrc/apps/pwa/test/vault-page.test.mjs
Anthony Ettinger ae6298851b security: harden credentials app after the 2026-10-27 dev2 incident (CLI 0.10.0)
An attacker with root on dev2 read identity.json (identity secret key + a user
lsk_ token) and used the token against app.logicsrc.com. This closes the gaps
that made that worse than it had to be, and adds the tools to recover.

Identity keys
- A registered identity public key is never replaced by a login any more
  ("last login wins" let a stolen token swap in an attacker key). Changing it
  needs `logicsrc teams rotate-identity`: the server seals a random answer to
  the CURRENT key (single use, 5 min); the CLI proves possession and re-seals
  every vault grant to the new key in one transaction (POST /keys/rotate).
- Lost key: reset from a signed-in browser (Settings), which drops every grant
  sealed to the old key. `logicsrc teams key --import` moves a key to a new box.

Vault re-key
- `logicsrc teams rekey <team> <project> <env>` and `--all`: new vault key,
  values re-encrypted client side, re-sealed to every current member and
  machine key. Server bumps credshare_vaults.key_version; writes that name a
  retired version get 409, and the rekey UPDATE only touches rows whose value
  did not change mid-flight. A verify pass (per vault and a final sweep)
  re-seals any value a racing older client wrote under the retired key.

Key pinning (TOFU)
- CLI pins teammates' and machine keys' fingerprints in known-keys.json (0600)
  and refuses to seal (grant, rekey) to a changed key without
  --accept-key-change. `teams members` shows fingerprints and pin status;
  the dashboard shows fingerprints and public/pins.js flags changes.

Audit
- Every person read of a vault key (/grant) and of ciphertext (/secrets) is
  audited with IP and user agent (also denied reads and identity events).
  GET /teams/:slug/audit, `logicsrc teams audit`, and tables on the dashboard
  and vault pages.

At rest / transport
- Session tokens stored as sha256 (rows hashed in migration 007); CLI auth
  codes hashed. New API keys no longer travel in the /settings URL.
- CSP (script-src 'self', no inline script or handlers, frame-ancestors none),
  HSTS, X-Frame-Options, Referrer-Policy no-referrer, COOP/CORP, no-store on
  authenticated pages. Inline scripts moved to /app.js and
  /vendor/profile-enhance.js.
- The shared footer renders from the bundled template of a pinned
  @profullstack/footer 0.5.0 (allowlist sanitizer); no runtime CDN fetch.
- Migrations can run as a separate owner role (MIGRATE_DATABASE_URL) so the
  app can connect as a DML-only role with an append-only audit table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 16:04:22 +00:00

159 lines
6.7 KiB
JavaScript

// The vault page shows secret names to any active team member and carries
// what public/vault.js needs to decrypt values in the browser: the vault id,
// the member's registered public key (to reject a wrong pasted key before
// trying it) and whether a grant exists. It must never carry ciphertext or a
// wrapped key — those are fetched by the script from the session-authed API.
process.env.DATABASE_URL = process.env.PWA_TEST_DATABASE_URL || ":memory:";
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import express from "express";
import { splitVaultName, vaultPageBody, KEY_HELP } from "../src/lib/vault-page.mjs";
const here = dirname(fileURLToPath(import.meta.url));
const { db, run, isPostgres } = await import("../src/db.mjs");
const { pagesRouter } = await import("../src/routes/pages.mjs");
async function migrate() {
if (isPostgres) {
await db.execute("DROP SCHEMA public CASCADE");
await db.execute("CREATE SCHEMA public");
}
for (const file of ["001_auth.sql", "002_credshare.sql", "005_machine_keys.sql", "007_hardening.sql"]) {
const sql = readFileSync(join(here, "..", "src", isPostgres ? "migrations-pg" : "migrations", file), "utf8");
for (const statement of sql.split(/;\s*$/m).map((s) => s.trim()).filter(Boolean)) {
await db.execute(statement);
}
}
}
async function serve(user) {
const app = express();
app.use((req, _res, next) => { req.user = user; req.csrfToken = "t"; next(); });
app.use(pagesRouter);
const server = app.listen(0);
await new Promise((resolve) => server.once("listening", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
return {
async get(path) {
const res = await fetch(`${base}${path}`, { redirect: "manual" });
return { status: res.status, headers: res.headers, text: await res.text() };
},
close: () => new Promise((resolve) => server.close(resolve))
};
}
const now = Date.now();
await migrate();
for (const [uid, email] of [["u-ann", "ann@example.com"], ["u-bob", "bob@example.com"], ["u-eve", "eve@example.com"]]) {
await run(`INSERT INTO users (id, email, created_at) VALUES (?,?,?)`, [uid, email, now]);
}
await run(`INSERT INTO credshare_teams (id, slug, name, created_by, created_at) VALUES ('t1','acme','acme','u-ann',?)`, [now]);
for (const [mid, uid, email] of [["m1", "u-ann", "ann@example.com"], ["m2", "u-bob", "bob@example.com"]]) {
await run(`INSERT INTO credshare_members (id, team_id, user_id, email, role, status, joined_at, created_at) VALUES (?,?,?,?,?,?,?,?)`,
[mid, "t1", uid, email, "member", "active", now, now]);
}
await run(`INSERT INTO credshare_keys (user_id, public_key, updated_at) VALUES ('u-ann','ANN_PUBLIC_KEY',?)`, [now]);
await run(`INSERT INTO credshare_vaults (id, team_id, name, created_by, created_at) VALUES ('v1','t1','api--prod','u-ann',?)`, [now]);
await run(`INSERT INTO credshare_vault_grants (vault_id, user_id, wrapped_dek, granted_by, created_at) VALUES ('v1','u-ann','WRAPPED_DEK_SECRET','u-ann',?)`, [now]);
for (const name of ["DATABASE_URL", "STRIPE_KEY"]) {
await run(`INSERT INTO credshare_secrets (vault_id, name, nonce, ciphertext, fingerprint, version, updated_by, updated_at) VALUES (?,?,?,?,?,?,?,?)`,
["v1", name, "NONCE_" + name, "CIPHERTEXT_" + name, "fp", 2, "u-ann", now]);
}
test("a member with a grant sees names, their public key, and the unlock form", async () => {
const app = await serve({ id: "u-ann", email: "ann@example.com" });
try {
const res = await app.get("/teams/acme/vaults/v1");
assert.equal(res.status, 200);
assert.match(res.headers.get("cache-control") || "", /no-store/);
assert.match(res.text, /data-key-name="DATABASE_URL"/);
assert.match(res.text, /data-key-name="STRIPE_KEY"/);
assert.match(res.text, /data-public-key="ANN_PUBLIC_KEY"/);
assert.match(res.text, /data-has-grant="1"/);
assert.match(res.text, /data-role="unlock"/);
assert.match(res.text, /logicsrc teams key/);
assert.match(res.text, /src="\/vendor\/libsodium\.js"/);
assert.match(res.text, /src="\/vault\.js"/);
} finally {
await app.close();
}
});
test("the page never embeds ciphertext, nonces or the wrapped key", async () => {
const app = await serve({ id: "u-ann", email: "ann@example.com" });
try {
const { text } = await app.get("/teams/acme/vaults/v1");
assert.doesNotMatch(text, /CIPHERTEXT_|NONCE_|WRAPPED_DEK_SECRET/);
} finally {
await app.close();
}
});
test("a member with no key is offered a browser-made key, not a paste box", async () => {
const app = await serve({ id: "u-bob", email: "bob@example.com" });
try {
const { status, text } = await app.get("/teams/acme/vaults/v1");
assert.equal(status, 200);
assert.match(text, /data-public-key=""/);
assert.match(text, /data-has-grant="0"/);
assert.match(text, /data-action="generate"/);
assert.doesNotMatch(text, /data-role="unlock"/);
assert.match(text, /logicsrc teams grant acme api prod bob@example\.com/);
} finally {
await app.close();
}
});
test("a non-member and an unknown vault both fall through to 404", async () => {
const eve = await serve({ id: "u-eve", email: "eve@example.com" });
try {
assert.equal((await eve.get("/teams/acme/vaults/v1")).status, 404);
} finally {
await eve.close();
}
const ann = await serve({ id: "u-ann", email: "ann@example.com" });
try {
assert.equal((await ann.get("/teams/acme/vaults/nope")).status, 404);
} finally {
await ann.close();
}
});
test("the dashboard links each vault to its page", async () => {
const app = await serve({ id: "u-ann", email: "ann@example.com" });
try {
const { text } = await app.get("/dashboard");
assert.match(text, /href="\/teams\/acme\/vaults\/v1"/);
} finally {
await app.close();
}
});
test("vault names split the way the CLI splits them", () => {
assert.deepEqual(splitVaultName("api--prod"), { project: "api", env: "prod" });
assert.deepEqual(splitVaultName("my-app--staging"), { project: "my-app", env: "staging" });
assert.equal(splitVaultName("legacy"), null);
assert.equal(splitVaultName("a--b--c"), null);
});
test("names are escaped", () => {
const html = vaultPageBody({
team: { slug: "acme" },
vault: { id: "v", name: "x--y" },
secrets: [{ name: `<img src=x onerror=alert(1)>`, version: 1, updated_at: now }],
hasGrant: true,
publicKey: "pk",
email: "a@b.c"
});
assert.doesNotMatch(html, /<img src=x/);
});
test("the key help names the CLI command and a fallback for older CLIs", () => {
assert.match(KEY_HELP, /logicsrc teams key/);
assert.match(KEY_HELP, /jq -r \.keys\.secretKey ~\/\.config\/logicsrc\/identity\.json/);
});