@hasna/hooks 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +31 -11
  2. package/bin/hooks/stop-review.js +17 -9
  3. package/bin/hooks-mcp.js +741 -311
  4. package/bin/index.js +1673 -634
  5. package/bin/native-safety-entry.js +2 -2
  6. package/bin/native-safety-worker.cjs +36 -34
  7. package/bin/release-manifest.json +297 -0
  8. package/bin/serve.js +577 -151
  9. package/dist/index.js +1813 -1178
  10. package/dist/lib/codex-safety-check.d.ts +1 -1
  11. package/dist/lib/error-events.d.ts +59 -0
  12. package/dist/lib/install-record.d.ts +136 -0
  13. package/dist/lib/manifest.d.ts +6 -0
  14. package/dist/lib/release-manifest.d.ts +162 -0
  15. package/dist/lib/store.d.ts +25 -2
  16. package/dist/openapi.d.ts +147 -0
  17. package/dist/registry-auth.d.ts +6 -1
  18. package/dist/registry-http.d.ts +2 -1
  19. package/dist/sdk/generated.d.ts +26 -0
  20. package/dist/sdk/index.js +14 -0
  21. package/hooks/codewith-native-common.ts +190 -6
  22. package/hooks/hook-signed-link-guard/README.md +46 -0
  23. package/hooks/hook-signed-link-guard/src/classify.ts +7 -3
  24. package/hooks/hook-signed-link-guard/src/jq.ts +205 -34
  25. package/hooks/hook-stop-review/README.md +5 -2
  26. package/hooks/hook-stop-review/src/jev.ts +9 -4
  27. package/hooks/hook-stop-review/src/policy.ts +7 -3
  28. package/hooks/hook-stop-review/src/runtime.ts +5 -4
  29. package/hooks/hook-trash-guard/README.md +9 -4
  30. package/hooks/hook-trash-guard/src/hook.ts +127 -10
  31. package/hooks/hook-workspace-repos-guard/src/hook.ts +27 -12
  32. package/hooks/pre-bash/README.md +47 -1
  33. package/hooks/pre-bash/src/hook.ts +168 -12
  34. package/hooks/shell-words.ts +40 -2
  35. package/package.json +3 -2
  36. package/scripts/build-release-manifest.ts +27 -0
  37. package/scripts/validate-package.ts +5 -1
  38. package/scripts/validate-release-manifest.ts +67 -0
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * Write bin/release-manifest.json: every bundled hook's catalogue version, the
4
+ * package-relative script `hooks run` executes and that script's sha256.
5
+ *
6
+ * It runs straight after `build:bundled-hooks`, once the build-owned hook
7
+ * bundles exist and before any later step (none of which writes a hook
8
+ * script), so the hashes are the hashes of the files the tarball ships. The
9
+ * manifest is what makes a bundled hook trusted by its own release
10
+ * (src/lib/release-manifest.ts).
11
+ */
12
+ import { mkdirSync, writeFileSync } from "node:fs";
13
+ import { dirname, join, resolve } from "node:path";
14
+ import { buildReleaseManifest, RELEASE_MANIFEST_RELATIVE_PATH, serializeReleaseManifest } from "../src/lib/release-manifest";
15
+
16
+ export function writeReleaseManifest(packageDir = resolve(import.meta.dir, "..")): { path: string; hooks: number } {
17
+ const manifest = buildReleaseManifest(packageDir);
18
+ const path = join(packageDir, RELEASE_MANIFEST_RELATIVE_PATH);
19
+ mkdirSync(dirname(path), { recursive: true });
20
+ writeFileSync(path, serializeReleaseManifest(manifest), { mode: 0o644 });
21
+ return { path, hooks: Object.keys(manifest.hooks).length };
22
+ }
23
+
24
+ if (import.meta.main) {
25
+ const { hooks } = writeReleaseManifest();
26
+ console.log(`Wrote the release manifest for ${hooks} bundled hooks`);
27
+ }
@@ -3,6 +3,7 @@ import { existsSync } from "node:fs";
3
3
  import { chmod, copyFile, mkdtemp, mkdir, readFile, realpath, rm, symlink, writeFile } from "node:fs/promises";
4
4
  import { dirname, join } from "node:path";
5
5
  import { validatePackedBundledHooks } from "./validate-bundled-hooks";
6
+ import { validatePackedReleaseManifest } from "./validate-release-manifest";
6
7
  import { ENTRY_ARGUMENT_LIMIT, nativeSafetyEntryWorker } from "./build-native-entry";
7
8
 
8
9
  type PackageJson = {
@@ -507,7 +508,7 @@ async function validateExtractedSmoke(pkg: PackageJson): Promise<void> {
507
508
  await validatePackedSkillsPolicy(pkg, packageDir, smokeDir);
508
509
 
509
510
  // 5. One bundled-hook run from the packed artifact (isolated data dir;
510
- // first run self-trusts, then executes).
511
+ // the release manifest names its bytes, then it executes).
511
512
  const runRes = spawnSync("bun", ["run", binIndex, "run", "gitguard"], {
512
513
  cwd: smokeDir,
513
514
  env,
@@ -519,6 +520,9 @@ async function validateExtractedSmoke(pkg: PackageJson): Promise<void> {
519
520
  if (!/decision/.test(runRes.stdout)) throw new Error(`packed bundled-hook run produced no hook decision: ${runRes.stdout.slice(0, 200)}`);
520
521
 
521
522
  await validatePackedEntryIsolation({ binIndex, binMcp, workspace, env, failClosedEnv: fcEnv });
523
+ // 6. The release manifest is true of the packed scripts, decides a bundled
524
+ // hook with nothing pinned, and refuses a script changed after the release.
525
+ validatePackedReleaseManifest(packageDir, workspace, fcEnv, env);
522
526
  } finally {
523
527
  await rm(workspace, { force: true, recursive: true });
524
528
  }
@@ -0,0 +1,67 @@
1
+ /** The packed package must carry a release manifest that is true of its own files. */
2
+ import { spawnSync } from "node:child_process";
3
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
4
+ import { join } from "node:path";
5
+ import { HOOKS } from "../src/lib/registry";
6
+ import { buildReleaseManifest, bundledScriptRelativePath, RELEASE_MANIFEST_RELATIVE_PATH, serializeReleaseManifest } from "../src/lib/release-manifest";
7
+
8
+ /**
9
+ * Three proofs on the extracted tarball, none of which needs a credential:
10
+ * 1. the manifest is there and is exactly what hashing the packed scripts
11
+ * produces, for every hook in the catalogue;
12
+ * 2. `hooks verify` run from the packed CLI reports a bundled hook as
13
+ * governed by this release, with nothing pinned anywhere;
14
+ * 3. a packed script changed by one byte refuses on its FIRST run, where the
15
+ * package used to pin whatever it found.
16
+ */
17
+ export function validatePackedReleaseManifest(
18
+ packageDir: string,
19
+ workspace: string,
20
+ /** An environment with no credential and no local opt-in: `hooks verify` needs neither. */
21
+ unconfiguredEnv: Record<string, string | undefined>,
22
+ /** The explicit local route, which `hooks run` needs to run at all. */
23
+ localEnv: Record<string, string | undefined>,
24
+ ): void {
25
+ const path = join(packageDir, RELEASE_MANIFEST_RELATIVE_PATH);
26
+ if (!existsSync(path)) throw new Error(`packed tarball is missing ${RELEASE_MANIFEST_RELATIVE_PATH}`);
27
+ const shipped = readFileSync(path, "utf8");
28
+ const expected = serializeReleaseManifest(buildReleaseManifest(packageDir));
29
+ if (shipped !== expected) throw new Error("the packed release manifest does not equal the hashes of the packed hook scripts");
30
+ // Every hook `hooks run` can resolve in this package is listed: one left
31
+ // out would refuse in the installed package.
32
+ const listed = Object.keys(JSON.parse(shipped).hooks as Record<string, unknown>);
33
+ const missing = HOOKS.filter((meta) => bundledScriptRelativePath(meta, packageDir) !== undefined || meta.bundledEntry !== undefined)
34
+ .map((meta) => meta.name)
35
+ .filter((name) => !listed.includes(name));
36
+ if (missing.length > 0) throw new Error(`the packed release manifest omits: ${missing.join(", ")}`);
37
+
38
+ const binIndex = join(packageDir, "bin", "index.js");
39
+ const dataDir = join(workspace, "release-manifest-data");
40
+ mkdirSync(dataDir, { recursive: true });
41
+ const stores = { HASNA_HOOKS_DATA_DIR: dataDir, HASNA_HOOKS_DB_PATH: join(dataDir, "hooks.db"), HASNA_HOOKS_LOCK_PATH: join(dataDir, "hooks.lock") };
42
+ const verify = spawnSync("bun", ["run", binIndex, "verify", "gitguard", "--json"], { cwd: workspace, env: { ...unconfiguredEnv, ...stores }, encoding: "utf8", timeout: 20000 });
43
+ if (verify.status !== 0) throw new Error(`packed \`hooks verify\` failed (exit ${verify.status}): ${verify.stderr || verify.stdout}`);
44
+ const report = JSON.parse(verify.stdout) as { release: { anchor: string } | null; hooks: Array<{ name: string; ok: boolean; governed_by: string; release: string | null }> };
45
+ const gitguard = report.hooks.find((hook) => hook.name === "gitguard");
46
+ if (!report.release || !gitguard?.ok || gitguard.governed_by !== "release" || !gitguard.release) {
47
+ throw new Error(`packed \`hooks verify\` did not report gitguard as governed by its release: ${verify.stdout.slice(0, 300)}`);
48
+ }
49
+ if (existsSync(join(dataDir, "hooks.lock")) || existsSync(join(dataDir, "hooks.db"))) throw new Error("packed `hooks verify` wrote to the data directory");
50
+
51
+ const entry = (JSON.parse(shipped).hooks as Record<string, { script: string }>).gitguard!;
52
+ const scriptPath = join(packageDir, entry.script);
53
+ const original = readFileSync(scriptPath);
54
+ try {
55
+ writeFileSync(scriptPath, Buffer.concat([original, Buffer.from("\n// changed after the release\n")]));
56
+ const run = spawnSync("bun", ["run", binIndex, "run", "gitguard"], {
57
+ cwd: workspace, env: { ...localEnv, ...stores }, encoding: "utf8", timeout: 20000,
58
+ input: JSON.stringify({ hook_event_name: "PreToolUse", tool_name: "Bash", tool_input: { command: "echo smoke" } }),
59
+ });
60
+ if (run.status !== 2 || !/changed since it was trusted/.test(run.stderr)) {
61
+ throw new Error(`a packed hook script changed after the release ran on its first run (exit ${run.status}): ${run.stderr.slice(0, 300)}`);
62
+ }
63
+ if (existsSync(join(dataDir, "hooks.lock"))) throw new Error("a refused first run of a changed packed script still wrote a pin");
64
+ } finally {
65
+ writeFileSync(scriptPath, original);
66
+ }
67
+ }