mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 09:24:46 +00:00
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>
This commit is contained in:
parent
18477340e7
commit
ae6298851b
34 changed files with 1633 additions and 112 deletions
183
packages/cli/src/hardening.test.ts
Normal file
183
packages/cli/src/hardening.test.ts
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import type { Server } from "node:http";
|
||||
|
||||
/**
|
||||
* The post-incident flow end to end against the real credentials-app routes on
|
||||
* an in-memory database:
|
||||
*
|
||||
* rotate-identity new keypair, every grant re-sealed, proof of the old key
|
||||
* rekey new vault key, values unchanged, members + machine keys re-sealed
|
||||
* race a value written under the retired key is re-sealed by the verify pass
|
||||
* pins a teammate's changed key is refused until accepted
|
||||
* login a different local key is refused instead of replacing the registered one
|
||||
*/
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const pwa = (rel: string) => pathToFileURL(join(here, "..", "..", "..", "apps", "pwa", "src", rel)).href;
|
||||
|
||||
type Db = { execute(stmt: string | { sql: string; args: unknown[] }): Promise<{ rows: Array<Record<string, unknown>> }> };
|
||||
let db: Db;
|
||||
let server: Server;
|
||||
let base = "";
|
||||
let annToken = "";
|
||||
let bobToken = "";
|
||||
const scratch = mkdtempSync(join(process.env.LOGICSRC_TEST_TMP || tmpdir(), "logicsrc-hardening-"));
|
||||
const annHome = join(scratch, "ann");
|
||||
const bobHome = join(scratch, "bob");
|
||||
const otherHome = join(scratch, "ann-other-machine");
|
||||
const saved = { ...process.env };
|
||||
|
||||
const as = (home: string) => {
|
||||
process.env.LOGICSRC_HOME = home;
|
||||
delete process.env.LOGICSRC_API_KEY;
|
||||
};
|
||||
const identityOf = (home: string) => JSON.parse(readFileSync(join(home, "identity.json"), "utf8"));
|
||||
const envFile = (name: string, body: string) => {
|
||||
const p = join(scratch, name);
|
||||
writeFileSync(p, body);
|
||||
return p;
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
delete process.env.LOGICSRC_IDENTITY_FILE;
|
||||
({ db } = (await import(pwa("db.mjs"))) as { db: Db });
|
||||
for (const file of ["001_auth.sql", "002_credshare.sql", "005_machine_keys.sql", "007_hardening.sql"]) {
|
||||
const sql = readFileSync(fileURLToPath(pwa(join("migrations", file))), "utf8");
|
||||
for (const statement of sql.split(/;\s*$/m).map((s) => s.trim()).filter(Boolean)) await db.execute(statement);
|
||||
}
|
||||
for (const [id, email] of [["u_ann", "ann@example.com"], ["u_bob", "bob@example.com"]]) {
|
||||
await db.execute({ sql: "INSERT INTO users (id, email, created_at) VALUES (?,?,?)", args: [id, email, Date.now()] });
|
||||
}
|
||||
const expressModule = "express";
|
||||
const { default: express } = (await import(expressModule)) as { default: any };
|
||||
const { credshareRouter } = await import(pwa("routes/credshare.mjs"));
|
||||
const { apiKeysRouter } = await import(pwa("routes/apikeys.mjs"));
|
||||
const { createApiKey } = await import(pwa("lib/apikey.mjs"));
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use(credshareRouter);
|
||||
app.use(apiKeysRouter);
|
||||
server = app.listen(0);
|
||||
await new Promise((resolve) => server.once("listening", resolve));
|
||||
base = `http://127.0.0.1:${(server.address() as { port: number }).port}`;
|
||||
process.env.LOGICSRC_API = base;
|
||||
annToken = (await createApiKey("u_ann", "ann-laptop")).plaintext;
|
||||
bobToken = (await createApiKey("u_bob", "bob-laptop")).plaintext;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await new Promise((resolve) => server?.close(resolve));
|
||||
for (const key of Object.keys(process.env)) if (!(key in saved)) delete process.env[key];
|
||||
Object.assign(process.env, saved);
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe("identity rotation, re-key and key pinning", () => {
|
||||
it("sets up: ann and bob log in, ann pushes two vaults and grants bob one", async () => {
|
||||
const { loginAction, teamsPushAction, teamsGrantAction } = await import("./teams.js");
|
||||
const { TeamClient } = await import("@logicsrc/plugin-credential-sharing");
|
||||
as(bobHome);
|
||||
await loginAction({ token: bobToken });
|
||||
as(annHome);
|
||||
await loginAction({ token: annToken });
|
||||
const ann = new TeamClient({ apiUrl: base, token: annToken });
|
||||
await ann.createTeam("acme");
|
||||
const invite = await ann.invite("acme", "bob@example.com");
|
||||
await new TeamClient({ apiUrl: base, token: bobToken }).acceptInvite(invite.token!);
|
||||
await teamsPushAction("acme", "web", "prod", { env: envFile("web.env", "A=1\nB=two\n"), format: "json" });
|
||||
await teamsPushAction("acme", "api", "prod", { env: envFile("api.env", "C=3\n"), format: "json" });
|
||||
await teamsGrantAction("acme", "web", "prod", "bob@example.com", "json");
|
||||
const pins = JSON.parse(readFileSync(join(annHome, "known-keys.json"), "utf8"));
|
||||
expect(Object.keys(pins.keys).some((k) => k.endsWith("|bob@example.com"))).toBe(true);
|
||||
expect(statSync(join(annHome, "known-keys.json")).mode & 0o777).toBe(0o600);
|
||||
});
|
||||
|
||||
it("a login on another machine cannot replace ann's registered key", async () => {
|
||||
const { loginAction } = await import("./teams.js");
|
||||
const before = identityOf(annHome).keys.publicKey;
|
||||
as(otherHome);
|
||||
await expect(loginAction({ token: annToken })).rejects.toThrow(/not the registered one/);
|
||||
const r = await db.execute({ sql: "SELECT public_key FROM credshare_keys WHERE user_id = ?", args: ["u_ann"] });
|
||||
expect(r.rows[0]?.public_key).toBe(before);
|
||||
});
|
||||
|
||||
it("rotate-identity swaps ann's key and every grant, and she can still pull", async () => {
|
||||
const { teamsRotateIdentityAction } = await import("./hardening.js");
|
||||
const { teamsPullAction } = await import("./teams.js");
|
||||
as(annHome);
|
||||
const before = identityOf(annHome).keys.publicKey;
|
||||
await teamsRotateIdentityAction({ format: "json" });
|
||||
const after = identityOf(annHome).keys.publicKey;
|
||||
expect(after).not.toBe(before);
|
||||
expect(statSync(join(annHome, "identity.json")).mode & 0o777).toBe(0o600);
|
||||
const r = await db.execute({ sql: "SELECT public_key FROM credshare_keys WHERE user_id = ?", args: ["u_ann"] });
|
||||
expect(r.rows[0]?.public_key).toBe(after);
|
||||
const out = join(scratch, "web-after-rotate.env");
|
||||
await teamsPullAction("acme", "web", "prod", { env: out, format: "json" });
|
||||
expect(readFileSync(out, "utf8")).toMatch(/B=two/);
|
||||
});
|
||||
|
||||
it("bob's CLI refuses to seal to ann's changed key until he accepts it", async () => {
|
||||
const { teamsGrantAction } = await import("./teams.js");
|
||||
as(bobHome);
|
||||
// bob pins ann's (old) key the first time he sees it...
|
||||
const { checkAndPinKey } = await import("@logicsrc/plugin-credential-sharing");
|
||||
checkAndPinKey(base, "ann@example.com", "B".repeat(43) + "=");
|
||||
// ...so the rotated key is a change.
|
||||
await expect(teamsGrantAction("acme", "web", "prod", "ann@example.com", "json")).rejects.toThrow(/CHANGED/);
|
||||
await teamsGrantAction("acme", "web", "prod", "ann@example.com", "json", { acceptKeyChange: ["ann@example.com"] });
|
||||
});
|
||||
|
||||
it("rekey --all changes every vault key, keeps the values, and keeps bob", async () => {
|
||||
const { teamsRekeyAction } = await import("./hardening.js");
|
||||
const { teamsPullAction } = await import("./teams.js");
|
||||
as(annHome);
|
||||
const grantsBefore = await db.execute({ sql: "SELECT vault_id, user_id, wrapped_dek FROM credshare_vault_grants ORDER BY vault_id, user_id", args: [] });
|
||||
await teamsRekeyAction("acme", undefined, undefined, { all: true, format: "json" });
|
||||
const grantsAfter = await db.execute({ sql: "SELECT vault_id, user_id, wrapped_dek FROM credshare_vault_grants ORDER BY vault_id, user_id", args: [] });
|
||||
expect(grantsAfter.rows.length).toBe(grantsBefore.rows.length);
|
||||
for (let i = 0; i < grantsAfter.rows.length; i++) expect(grantsAfter.rows[i]!.wrapped_dek).not.toBe(grantsBefore.rows[i]!.wrapped_dek);
|
||||
const versions = await db.execute({ sql: "SELECT key_version FROM credshare_vaults", args: [] });
|
||||
expect(versions.rows.every((r) => Number(r.key_version) === 2)).toBe(true);
|
||||
as(bobHome);
|
||||
const out = join(scratch, "bob-web.env");
|
||||
await teamsPullAction("acme", "web", "prod", { env: out, format: "json" });
|
||||
expect(readFileSync(out, "utf8")).toMatch(/A=1/);
|
||||
});
|
||||
|
||||
it("a value written under the retired key during a re-key is re-sealed, not lost", async () => {
|
||||
const { teamsRekeyAction, verifyAndRepair } = await import("./hardening.js");
|
||||
const { teamsPullAction } = await import("./teams.js");
|
||||
const { TeamClient, unwrapVaultKey, encryptValue, fingerprintValue } = await import("@logicsrc/plugin-credential-sharing");
|
||||
as(annHome);
|
||||
const ann = identityOf(annHome);
|
||||
const client = new TeamClient({ apiUrl: base, token: annToken });
|
||||
const { vaults } = await client.listVaults("acme");
|
||||
const web = vaults.find((v) => v.name === "web--prod")!;
|
||||
// An old client: read the key now, write after the re-key, sending no key version.
|
||||
const staleDek = await unwrapVaultKey((await client.getMyGrant(web.id)).wrappedDek, ann.keys);
|
||||
await teamsRekeyAction("acme", "web", "prod", { format: "json" });
|
||||
const sealed = await encryptValue("late-value", staleDek);
|
||||
await client.putSecrets(web.id, [{ name: "LATE", ...sealed, fingerprint: fingerprintValue("late-value") }], []);
|
||||
// The verify pass a re-key run ends with re-seals it under the current key.
|
||||
const fixed = await verifyAndRepair(client, ann.keys, { vaultId: web.id, vault: web.name, oldDeks: [staleDek] });
|
||||
expect(fixed.repaired).toBe(1);
|
||||
expect(fixed.unreadable).toEqual([]);
|
||||
const out = join(scratch, "web-late.env");
|
||||
await teamsPullAction("acme", "web", "prod", { env: out, format: "json" });
|
||||
expect(readFileSync(out, "utf8")).toMatch(/LATE=late-value/);
|
||||
});
|
||||
|
||||
it("the audit trail records the reads with the acting user", async () => {
|
||||
const { TeamClient } = await import("@logicsrc/plugin-credential-sharing");
|
||||
const { audit } = await new TeamClient({ apiUrl: base, token: annToken }).teamAudit("acme", { action: "vault:read" });
|
||||
expect(audit.some((a) => a.actor === "bob@example.com" && a.vault === "web--prod")).toBe(true);
|
||||
const rot = await new TeamClient({ apiUrl: base, token: annToken }).teamAudit("acme", { action: "identity:rotate" });
|
||||
expect(rot.audit.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue