Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contracts/public-surface.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@
"profile check",
"profile freeze",
"profile show",
"profile sign",
"profile verify",
"review",
"run",
"sbom",
Expand Down
6 changes: 5 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ const COMMAND_REGISTRY: Record<string, CommandDescriptor> = {
profile: {
handler: cmdProfile,
stability: "experimental",
help: "Versioned, traveling project profile — declare {skills, mcp, workflows, plugins, vault, gates, scope} and audit declared-vs-discovered drift (show|freeze|check; --json, --gate)",
help: "Versioned, traveling project profile — declare {skills, mcp, workflows, plugins, vault, gates, scope}, audit declared-vs-discovered drift, and sign the scope/RoE (show|freeze|check|sign|verify; --json, --gate, --key)",
},
pkg: {
handler: cmdPkg,
Expand Down Expand Up @@ -718,6 +718,10 @@ const SUBCOMMAND_HELP: Record<string, string> = {
"Snapshot the discovered toolchain into .kit-profile.toml (preserves operator-authored workflows/plugins/scope/gates)",
"profile check":
"Report declared-vs-discovered drift (--gate fails CI on any drift; honest skip when no profile declared)",
"profile sign":
"Sign the profile (scope/RoE) into .kit-profile.sig via your identity/keystore — offline-verifiable",
"profile verify":
"Verify .kit-profile.sig offline (--key pin → local identity → org .kit-policy.signers)",
"memory index": "Index ~/.claude transcripts into the SQLite memory store",
"memory search": "Full-text search memory (current project; --global for all)",
"memory stats": "Show what the local memory store contains",
Expand Down
66 changes: 65 additions & 1 deletion src/commands/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
type ProfileDrift,
type DriftEntry,
} from "../profile/reconcile.js";
import { signProfile, verifyProfileSignature } from "../profile/sign.js";
import { flagValue } from "../utils/flags.js";

function noProfileNotice(jsonMode: boolean): boolean {
if (jsonMode) {
Expand Down Expand Up @@ -199,12 +201,74 @@ async function profileFreeze(jsonMode: boolean): Promise<boolean> {
return true;
}

async function profileSign(jsonMode: boolean): Promise<boolean> {
const res = await signProfile();
if (!res.ok) {
if (jsonMode) {
console.log(JSON.stringify({ signed: false, error: res.error }, null, 2));
} else {
console.error(`${c.red}✗ ${res.error}${c.reset}`);
}
return false;
}
if (jsonMode) {
console.log(
JSON.stringify({ signed: true, kid: res.kid, fingerprint: res.fingerprint }, null, 2),
);
return true;
}
const rootedNote = res.rooted ? ` ${c.dim}(hardware-rooted)${c.reset}` : "";
console.log(
`${c.green}✓${c.reset} signed ${c.bold}${res.fingerprint}${c.reset} as ${c.bold}${res.kid}${c.reset}${rootedNote} ${c.dim}→ ${PROFILE_FILE}.sig${c.reset}`,
);
console.log(
`${c.dim}commit ${PROFILE_FILE} + ${PROFILE_FILE}.sig; verifiers check it offline against .kit-policy.signers${c.reset}`,
);
return true;
}

async function profileVerify(jsonMode: boolean): Promise<boolean> {
const r = await verifyProfileSignature(process.cwd(), { key: flagValue(process.argv, "--key") });
if (jsonMode) {
console.log(JSON.stringify(r, null, 2));
// trust-absence (unverifiable) is not a forge — mirror kit policy verify's fail-open there.
return r.status === "valid" || r.status === "unverifiable";
}
switch (r.status) {
case "valid":
console.log(
`${c.green}✓ profile signature valid${c.reset} ${c.dim}${r.fingerprint} ${r.detail}${c.reset}`,
);
return true;
case "unverifiable":
console.warn(`${c.yellow}! ${r.detail}${c.reset}`);
return true;
case "unsigned":
console.error(`${c.red}${r.detail}${c.reset}`);
return false;
case "invalid":
console.error(
`${c.red}✗ profile signature INVALID${c.reset} ${c.dim}(${r.detail})${c.reset}`,
);
return false;
case "revoked":
console.error(
`${c.red}✗ profile signed by a REVOKED key${c.reset} ${c.dim}(${r.detail})${c.reset}`,
);
return false;
}
}

export async function cmdProfile(): Promise<boolean> {
const sub = process.argv[3] ?? "show";
const jsonMode = hasFlag(process.argv, "--json");
if (sub === "show") return await profileShow(jsonMode);
if (sub === "freeze") return await profileFreeze(jsonMode);
if (sub === "check") return await profileCheck(jsonMode, hasFlag(process.argv, "--gate"));
console.error(`${c.red}usage: kit profile <show|freeze|check> [--json] [--gate]${c.reset}`);
if (sub === "sign") return await profileSign(jsonMode);
if (sub === "verify") return await profileVerify(jsonMode);
console.error(
`${c.red}usage: kit profile <show|freeze|check|sign|verify> [--json] [--gate] [--key <pem>]${c.reset}`,
);
return false;
}
108 changes: 108 additions & 0 deletions src/profile/sign.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { describe, it, beforeEach, afterEach } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, writeFileSync, existsSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { loadOrCreateIdentity } from "../identity.js";
import { PROFILE_FILE } from "./schema.js";
import { signProfile, verifyProfileSignature, verifiedScope, getProfileSigPath } from "./sign.js";

let idDir: string;
let proj: string;
let savedIdEnv: string | undefined;

const SCOPED = `version = 1
name = "acme"
[scope]
egress = ["api.acme.com"]
fs = ["."]
secrets = ["DATABASE_URL"]
`;

beforeEach(() => {
idDir = mkdtempSync(join(tmpdir(), "kit-id-"));
proj = mkdtempSync(join(tmpdir(), "kit-profsign-"));
savedIdEnv = process.env.KIT_IDENTITY_DIR;
process.env.KIT_IDENTITY_DIR = idDir;
loadOrCreateIdentity(); // mint a file identity under idDir
});

afterEach(() => {
if (savedIdEnv === undefined) delete process.env.KIT_IDENTITY_DIR;
else process.env.KIT_IDENTITY_DIR = savedIdEnv;
rmSync(idDir, { recursive: true, force: true });
rmSync(proj, { recursive: true, force: true });
});

describe("signProfile / verifyProfileSignature", () => {
it("signs a profile and verifies it via the local identity", async () => {
writeFileSync(join(proj, PROFILE_FILE), SCOPED);
const res = await signProfile(proj);
assert.equal(res.ok, true);
assert.ok(existsSync(getProfileSigPath(proj)));
assert.match(res.fingerprint ?? "", /^sha256:/);

const v = await verifyProfileSignature(proj);
assert.equal(v.status, "valid");
assert.equal(v.via, "local");
});

it("refuses to sign when no profile is declared", async () => {
const res = await signProfile(proj);
assert.equal(res.ok, false);
assert.match(res.error ?? "", /no profile/);
});

it("reports unsigned when there is no .kit-profile.sig", async () => {
writeFileSync(join(proj, PROFILE_FILE), SCOPED);
const v = await verifyProfileSignature(proj);
assert.equal(v.status, "unsigned");
});

it("detects tampering — a changed profile invalidates the signature", async () => {
writeFileSync(join(proj, PROFILE_FILE), SCOPED);
await signProfile(proj);
// Change a signed field (name is part of the canonical bytes).
writeFileSync(join(proj, PROFILE_FILE), SCOPED.replace("acme", "evil-corp"));
const v = await verifyProfileSignature(proj);
assert.equal(v.status, "invalid");
});

it("re-signing after an edit restores validity (generated is excluded from the signed bytes)", async () => {
writeFileSync(join(proj, PROFILE_FILE), SCOPED);
await signProfile(proj);
writeFileSync(join(proj, PROFILE_FILE), SCOPED.replace("acme", "acme-web"));
assert.equal((await verifyProfileSignature(proj)).status, "invalid");
await signProfile(proj);
assert.equal((await verifyProfileSignature(proj)).status, "valid");
});
});

describe("verifiedScope (Pillar 3 hook — fail-closed)", () => {
it("returns the scope only when the signature is valid", async () => {
writeFileSync(join(proj, PROFILE_FILE), SCOPED);
await signProfile(proj);
const scope = await verifiedScope(proj);
assert.deepEqual(scope?.egress, ["api.acme.com"]);
assert.deepEqual(scope?.fs, ["."]);
});

it("returns null when the profile is unsigned (unsigned scope grants nothing)", async () => {
writeFileSync(join(proj, PROFILE_FILE), SCOPED);
assert.equal(await verifiedScope(proj), null);
});

it("returns null when the profile was tampered after signing", async () => {
writeFileSync(join(proj, PROFILE_FILE), SCOPED);
await signProfile(proj);
writeFileSync(join(proj, PROFILE_FILE), SCOPED.replace("api.acme.com", "evil.example.com"));
assert.equal(await verifiedScope(proj), null);
});

it("returns null when a signed profile declares no scope", async () => {
writeFileSync(join(proj, PROFILE_FILE), `version = 1\nname = "no-scope"\n`);
await signProfile(proj);
assert.equal((await verifyProfileSignature(proj)).status, "valid");
assert.equal(await verifiedScope(proj), null);
});
});
Loading
Loading