mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-11-29 15:51:52 +00:00
security: keep signing keys out of deployable team vaults (CLI 0.13.0) (#264)
Some checks are pending
CI / build (push) Waiting to run
Deploy to dev2 / deploy (push) Waiting to run
test / test (push) Waiting to run
Some checks are pending
CI / build (push) Waiting to run
Deploy to dev2 / deploy (push) Waiting to run
test / test (push) Waiting to run
* security: keep signing keys out of deployable team vaults (CLI 0.13.0) After the 2026-10-27 dev2 incident, root on the box drained every wallet whose key sat in an app.env, and deploys write app.env from team vaults. Wallet keys, seed phrases, hsm secrets and SSH/deploy keys now stay out of that path: - server: a machine (deploy) key never receives a signing key from GET /vaults/:id/secrets (listed as `withheld`, audited as secrets:withheld:signing) and cannot write or delete one (403 signing_key_machine_write). People still see them, so they can move them. - CLI `teams push` refuses a .env holding signing keys, by name or by value shape (PEM private key, 0x-hex, Solana base58/byte array, mnemonic), unless --allow-signing-keys. - CLI `teams pull` withholds signing keys by default; --include-signing-keys needs a person at a terminal, so deploy-app.sh's fallback to the logged-in account withholds them too. - One name rule in packages/opencreds/src/signing-keys.ts, mirrored in apps/pwa/src/lib/signing-keys.mjs, both held to one fixture file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: bound the wallet-name regex, build PEM fixtures at runtime Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Anthony Ettinger <anthony@profullstack.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1bff3e975c
commit
b5f875837a
12 changed files with 297 additions and 12 deletions
24
apps/pwa/src/lib/signing-keys.mjs
Normal file
24
apps/pwa/src/lib/signing-keys.mjs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copy of the NAME rule in packages/opencreds/src/signing-keys.ts (read the
|
||||
// reasoning there). The server only sees secret names, so this is what it
|
||||
// enforces: a machine key never receives or writes a signing key. Both copies
|
||||
// are held to packages/opencreds/src/signing-keys.fixtures.json.
|
||||
|
||||
const norm = (name) => `_${name.toUpperCase().replace(/[^A-Z0-9]+/g, "_")}_`;
|
||||
|
||||
/** Seed phrases and raw key material, whatever service they belong to. */
|
||||
const ALWAYS = /_(MNEMONIC|SEED_PHRASE|SEEDPHRASE|SEED_WORDS|RECOVERY_PHRASE|XPRV|XPRIV|HSM_SECRET|KEYPAIR|KEY_PAIR)_/;
|
||||
/** "Private key" in any spelling, and SSH / deploy keys that open a box. */
|
||||
const PRIVATE = /_(PRIVATE_KEY|PRIV_KEY|PRIVKEY|PRIVATEKEY|SIGNING_KEY|SIGNER_KEY|SSH_KEY|DEPLOY_KEY)(_|$)/;
|
||||
/** App keys that sign tokens or pushes, not funds; needed by a running service. */
|
||||
const SERVICE = /_(VAPID|GITHUB_APP|GH_APP|JWT|JWK|DKIM|APNS|APPLE|FIREBASE|GOOGLE|GCP|SERVICE_ACCOUNT|SAML|OIDC|WEBHOOK|TLS|SSL|CERT|CLERK|SUPABASE)_/;
|
||||
/** A wallet word next to KEY / SECRET / SEED: SOLANA_SECRET_KEY, RELAYER_KEY, WALLET_SEED. */
|
||||
const WALLET = /_(SOLANA|SOL|EVM|ETH|ETHEREUM|BTC|BITCOIN|WALLET|HOT_WALLET|RELAYER|TREASURY|SIGNER|OPERATOR|DEPLOYER|PAYER|FEE_PAYER)_(?:[A-Z0-9]{1,24}_){0,4}(KEY|SECRET|SEED)_/;
|
||||
/** ...unless it is plainly an address, an RPC or an API credential. */
|
||||
const NOT_A_KEY = /_(RPC|API|URL|ADDRESS|ADDR|PUBLIC|PUBKEY|PUB|ID|NAME)_/;
|
||||
|
||||
export function isSigningKeyName(name) {
|
||||
const n = norm(name);
|
||||
if (ALWAYS.test(n)) return true;
|
||||
if (PRIVATE.test(n)) return !SERVICE.test(n);
|
||||
return WALLET.test(n) && !NOT_A_KEY.test(n);
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ import { id, sha256, token } from "../lib/crypto.mjs";
|
|||
import { keyFingerprint, requestOriginInfo } from "../lib/keyfp.mjs";
|
||||
import sodium from "libsodium-wrappers";
|
||||
import { bearer, keyForBearer, keyCoversVault, normalizeKey } from "../lib/apikey.mjs";
|
||||
import { isSigningKeyName } from "../lib/signing-keys.mjs";
|
||||
import {
|
||||
TeamMemberError,
|
||||
normEmail,
|
||||
|
|
@ -709,9 +710,14 @@ credshareRouter.delete("/api/credshare/vaults/:id/key-grants/:keyId", api(async
|
|||
|
||||
credshareRouter.get("/api/credshare/vaults/:id/secrets", api(async (req, res, user, key) => {
|
||||
const vault = await vaultCtx(res, req.params.id, user.id, key); if (!vault) return;
|
||||
const rows = await all(`SELECT * FROM credshare_secrets WHERE vault_id = ? ORDER BY name`, [vault.id]);
|
||||
const all_ = await all(`SELECT * FROM credshare_secrets WHERE vault_id = ? ORDER BY name`, [vault.id]);
|
||||
// A deploy box never receives a signing key (wallet key, seed, SSH key): root
|
||||
// on a box running 24/7 must not mean the keys that move money.
|
||||
const withheld = isMachine(key) ? all_.filter((s) => isSigningKeyName(s.name)).map((s) => s.name) : [];
|
||||
const rows = withheld.length ? all_.filter((s) => !withheld.includes(s.name)) : all_;
|
||||
await audit({ req, teamId: vault.team_id, vaultId: vault.id, actorUserId: user.id, action: "secrets:fetch", keyName: key?.name ?? (req.user ? "browser session" : null) });
|
||||
res.json({ vaultId: vault.id, keyVersion: Number(vault.key_version ?? 1), secrets: rows.map((s) => ({ name: s.name, nonce: s.nonce, ciphertext: s.ciphertext, fingerprint: s.fingerprint, version: s.version, updatedAt: s.updated_at })) });
|
||||
if (withheld.length) await audit({ req, teamId: vault.team_id, vaultId: vault.id, actorUserId: user.id, actorKeyId: key.id, action: "secrets:withheld:signing", keyName: withheld.join(",") });
|
||||
res.json({ vaultId: vault.id, keyVersion: Number(vault.key_version ?? 1), withheld, secrets: rows.map((s) => ({ name: s.name, nonce: s.nonce, ciphertext: s.ciphertext, fingerprint: s.fingerprint, version: s.version, updatedAt: s.updated_at })) });
|
||||
}, { machine: "read" }));
|
||||
|
||||
credshareRouter.put("/api/credshare/vaults/:id/secrets", api(async (req, res, user, key) => {
|
||||
|
|
@ -728,6 +734,10 @@ credshareRouter.put("/api/credshare/vaults/:id/secrets", api(async (req, res, us
|
|||
}
|
||||
const upserts = Array.isArray(req.body?.upserts) ? req.body.upserts : [];
|
||||
const deletes = Array.isArray(req.body?.deletes) ? req.body.deletes : [];
|
||||
if (isMachine(key)) {
|
||||
const signing = [...upserts.map((u) => u?.name), ...deletes].filter((n) => typeof n === "string" && isSigningKeyName(n));
|
||||
if (signing.length) return res.status(403).json({ error: `A machine key cannot write signing keys (${signing.join(", ")}). Keep them in \`logicsrc keyring\` and unlock them where they are used.`, code: "signing_key_machine_write" });
|
||||
}
|
||||
const applied = [];
|
||||
for (const u of upserts) {
|
||||
if (!u || typeof u.name !== "string" || typeof u.nonce !== "string" || typeof u.ciphertext !== "string" || typeof u.fingerprint !== "string") {
|
||||
|
|
|
|||
|
|
@ -270,6 +270,36 @@ test("a read-only machine key is refused every write; a read-write one may write
|
|||
await run(`DELETE FROM credshare_secrets WHERE vault_id = ? AND name = 'NEW'`, ["v_web"]);
|
||||
});
|
||||
|
||||
test("a machine key never receives or writes a signing key; a person still sees it", async () => {
|
||||
await run(`INSERT INTO credshare_secrets (vault_id, name, nonce, ciphertext, fingerprint, version, updated_by, updated_at) VALUES (?,?,?,?,?,?,?,?)`,
|
||||
["v_web", "SOLANA_PRIVATE_KEY", "n", "c", "fp", 1, "u_ann", now]);
|
||||
const m = await machineKey({ readOnly: false });
|
||||
await http.post("/api/credshare/keys", m.token, { publicKey: "pk-signing" });
|
||||
await http.post("/api/credshare/vaults/v_web/key-grants", annKey, { keyId: m.id, wrappedDek: "sealed-signing" });
|
||||
|
||||
const got = await http.get("/api/credshare/vaults/v_web/secrets", m.token);
|
||||
assert.equal(got.status, 200);
|
||||
assert.deepEqual(got.body.secrets.map((s) => s.name), ["API_KEY"]);
|
||||
assert.deepEqual(got.body.withheld, ["SOLANA_PRIVATE_KEY"]);
|
||||
const logged = await get(`SELECT * FROM credshare_audit WHERE vault_id = ? AND action = 'secrets:withheld:signing' AND actor_key_id = ?`, ["v_web", m.id]);
|
||||
assert.equal(logged.key_name, "SOLANA_PRIVATE_KEY");
|
||||
|
||||
const person = await http.get("/api/credshare/vaults/v_web/secrets", annKey);
|
||||
assert.deepEqual(person.body.secrets.map((s) => s.name).sort(), ["API_KEY", "SOLANA_PRIVATE_KEY"]);
|
||||
assert.deepEqual(person.body.withheld, []);
|
||||
|
||||
for (const body of [
|
||||
{ upserts: [{ name: "EVM_PRIVATE_KEY", nonce: "n", ciphertext: "c", fingerprint: "f" }], deletes: [] },
|
||||
{ upserts: [], deletes: ["SOLANA_PRIVATE_KEY"] }
|
||||
]) {
|
||||
const put = await http.put("/api/credshare/vaults/v_web/secrets", m.token, body);
|
||||
assert.equal(put.status, 403);
|
||||
assert.equal(put.body.code, "signing_key_machine_write");
|
||||
}
|
||||
assert.ok(await get(`SELECT 1 FROM credshare_secrets WHERE vault_id = ? AND name = 'SOLANA_PRIVATE_KEY'`, ["v_web"]));
|
||||
await run(`DELETE FROM credshare_secrets WHERE vault_id = ? AND name = 'SOLANA_PRIVATE_KEY'`, ["v_web"]);
|
||||
});
|
||||
|
||||
test("expired and revoked keys are 401, and revoking drops the key's grants", async () => {
|
||||
const expired = await machineKey({ expiresAt: Date.now() - 1000 });
|
||||
assert.equal((await http.get("/api/credshare/me", expired.token)).status, 401);
|
||||
|
|
|
|||
16
apps/pwa/test/signing-keys.test.mjs
Normal file
16
apps/pwa/test/signing-keys.test.mjs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// The server's copy of the signing-key NAME rule must agree with the CLI's
|
||||
// (packages/opencreds/src/signing-keys.ts): both are held to one fixture list.
|
||||
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 { isSigningKeyName } from "../src/lib/signing-keys.mjs";
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const fx = JSON.parse(readFileSync(join(here, "../../../packages/opencreds/src/signing-keys.fixtures.json"), "utf8"));
|
||||
|
||||
test("signing key names match the shared fixtures", () => {
|
||||
for (const n of fx.signing) assert.equal(isSigningKeyName(n), true, n);
|
||||
for (const n of fx.not_signing) assert.equal(isSigningKeyName(n), false, n);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue