mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 17:24:48 +00:00
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>
175 lines
7.2 KiB
JavaScript
175 lines
7.2 KiB
JavaScript
// Integration tests for GET /api/credshare/teams/:slug/vaults.
|
|
//
|
|
// This endpoint used to issue two queries per vault inside a loop. libSQL is a
|
|
// remote database, so each one is a network round trip: a real team with 176
|
|
// vaults spent ~10s in here, and `logicsrc teams pull` resolves the vault id
|
|
// twice, which is where the ~24s pull came from.
|
|
//
|
|
// The behavioural assertions below are the ones the loop already satisfied. The
|
|
// one that matters for the regression is `issues the same number of queries`:
|
|
// it pins the cost as INDEPENDENT of vault count rather than at some magic
|
|
// number, so any future rewrite that reintroduces per-vault I/O fails here
|
|
// regardless of how many statements the constant part happens to use.
|
|
//
|
|
// Runs against an in-memory libSQL database, so DATABASE_URL must be set before
|
|
// anything imports db.mjs.
|
|
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";
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const { db, run, isPostgres } = await import("../src/db.mjs");
|
|
const { credshareRouter } = await import("../src/routes/credshare.mjs");
|
|
|
|
async function migrate() {
|
|
// Against a real Postgres (PWA_TEST_DATABASE_URL) start from an empty schema;
|
|
// run with --test-concurrency=1 then, the files share one database.
|
|
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(actingUserId) {
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
req.user = { id: actingUserId };
|
|
next();
|
|
});
|
|
app.use(credshareRouter);
|
|
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}`);
|
|
return { status: res.status, body: await res.json() };
|
|
},
|
|
close: () => new Promise((resolve) => server.close(resolve))
|
|
};
|
|
}
|
|
|
|
const now = Date.now();
|
|
|
|
/** Count db.execute() calls while fn runs -- one call is one network round trip. */
|
|
async function countQueries(fn) {
|
|
const original = db.execute.bind(db);
|
|
let calls = 0;
|
|
db.execute = (...args) => {
|
|
calls += 1;
|
|
return original(...args);
|
|
};
|
|
try {
|
|
await fn();
|
|
} finally {
|
|
db.execute = original;
|
|
}
|
|
return calls;
|
|
}
|
|
|
|
/**
|
|
* A team with `vaultCount` vaults. Every vault gets `secretsPerVault` secrets;
|
|
* the caller is granted access to all but the last, so hasAccess has both
|
|
* values in play.
|
|
*/
|
|
async function seedTeam({ teamId, slug, vaultCount, secretsPerVault = 2 }) {
|
|
await run(`INSERT INTO credshare_teams (id, slug, name, created_by, created_at) VALUES (?,?,?,?,?)`,
|
|
[teamId, slug, slug, "u_me", now]);
|
|
await run(`INSERT INTO credshare_members (id, team_id, user_id, email, role, status, created_at) VALUES (?,?,?,?,?,?,?)`,
|
|
[`m_${teamId}`, teamId, "u_me", "me@example.com", "owner", "active", now]);
|
|
|
|
for (let i = 0; i < vaultCount; i += 1) {
|
|
// Zero-padded so lexical name order matches creation order for the ORDER BY assertion.
|
|
const vid = `${teamId}_v${String(i).padStart(3, "0")}`;
|
|
await run(`INSERT INTO credshare_vaults (id, team_id, name, created_by, created_at) VALUES (?,?,?,?,?)`,
|
|
[vid, teamId, `app${String(i).padStart(3, "0")}--prod`, "u_me", now]);
|
|
if (i < vaultCount - 1) {
|
|
await run(`INSERT INTO credshare_vault_grants (vault_id, user_id, wrapped_dek, granted_by, created_at) VALUES (?,?,?,?,?)`,
|
|
[vid, "u_me", `wrapped-${vid}`, "u_me", now]);
|
|
}
|
|
for (let s = 0; s < secretsPerVault; s += 1) {
|
|
await run(`INSERT INTO credshare_secrets (vault_id, name, nonce, ciphertext, fingerprint, version, updated_by, updated_at) VALUES (?,?,?,?,?,?,?,?)`,
|
|
[vid, `KEY_${s}`, `n${s}`, `c${s}`, `fp${s}`, 1, "u_me", now]);
|
|
}
|
|
}
|
|
}
|
|
|
|
await migrate();
|
|
await run(`INSERT INTO users (id, email, created_at) VALUES (?,?,?)`, ["u_me", "me@example.com", now]);
|
|
await run(`INSERT INTO users (id, email, created_at) VALUES (?,?,?)`, ["u_outsider", "outsider@example.com", now]);
|
|
await seedTeam({ teamId: "t_small", slug: "small", vaultCount: 3, secretsPerVault: 2 });
|
|
await seedTeam({ teamId: "t_big", slug: "big", vaultCount: 30, secretsPerVault: 4 });
|
|
|
|
test("reports each vault's secret count and whether the caller holds a grant", async (t) => {
|
|
const app = await serve("u_me");
|
|
t.after(() => app.close());
|
|
|
|
const res = await app.get("/api/credshare/teams/small/vaults");
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.vaults.length, 3);
|
|
|
|
// Ordered by name, counts per vault, and the last vault is the ungranted one.
|
|
assert.deepEqual(res.body.vaults.map((v) => v.name), ["app000--prod", "app001--prod", "app002--prod"]);
|
|
assert.deepEqual(res.body.vaults.map((v) => v.secretCount), [2, 2, 2]);
|
|
assert.deepEqual(res.body.vaults.map((v) => v.hasAccess), [true, true, false]);
|
|
|
|
// hasAccess must be a real boolean, not SQLite's 0/1 leaking through: clients
|
|
// branch on it, and `if (0)` and `if ("0")` disagree.
|
|
for (const v of res.body.vaults) {
|
|
assert.equal(typeof v.hasAccess, "boolean");
|
|
assert.equal(typeof v.secretCount, "number");
|
|
}
|
|
});
|
|
|
|
test("counts secrets per vault rather than across the team", async (t) => {
|
|
const app = await serve("u_me");
|
|
t.after(() => app.close());
|
|
|
|
const res = await app.get("/api/credshare/teams/big/vaults");
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.vaults.length, 30);
|
|
// 30 vaults x 4 secrets: a join that multiplied rows would report 120 here.
|
|
assert.ok(res.body.vaults.every((v) => v.secretCount === 4),
|
|
`expected every vault to report 4 secrets, got ${JSON.stringify(res.body.vaults.map((v) => v.secretCount))}`);
|
|
});
|
|
|
|
test("issues the same number of queries for 3 vaults as for 30", async (t) => {
|
|
const app = await serve("u_me");
|
|
t.after(() => app.close());
|
|
|
|
const small = await countQueries(() => app.get("/api/credshare/teams/small/vaults"));
|
|
const big = await countQueries(() => app.get("/api/credshare/teams/big/vaults"));
|
|
|
|
assert.equal(big, small,
|
|
`vault listing must not scale with vault count: 3 vaults took ${small} queries, 30 took ${big}`);
|
|
// And the constant is small -- membership check plus the listing itself.
|
|
assert.ok(small <= 4, `expected a handful of queries, got ${small}`);
|
|
});
|
|
|
|
test("refuses a caller who is not a member", async (t) => {
|
|
const app = await serve("u_outsider");
|
|
t.after(() => app.close());
|
|
|
|
const res = await app.get("/api/credshare/teams/small/vaults");
|
|
assert.equal(res.status, 403);
|
|
});
|
|
|
|
test("404s an unknown team", async (t) => {
|
|
const app = await serve("u_me");
|
|
t.after(() => app.close());
|
|
|
|
const res = await app.get("/api/credshare/teams/nope/vaults");
|
|
assert.equal(res.status, 404);
|
|
});
|