@hasna/hooks 0.14.1 → 0.15.1
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.
- package/README.md +30 -10
- package/bin/hooks/stop-review.js +17 -9
- package/bin/hooks-mcp.js +741 -311
- package/bin/index.js +1669 -632
- package/bin/native-safety-entry.js +2 -2
- package/bin/native-safety-worker.cjs +37 -35
- package/bin/release-manifest.json +297 -0
- package/bin/serve.js +577 -151
- package/dist/index.js +1809 -1176
- package/dist/lib/error-events.d.ts +59 -0
- package/dist/lib/install-record.d.ts +136 -0
- package/dist/lib/manifest.d.ts +6 -0
- package/dist/lib/release-manifest.d.ts +162 -0
- package/dist/lib/store.d.ts +25 -2
- package/dist/openapi.d.ts +147 -0
- package/dist/registry-auth.d.ts +6 -1
- package/dist/registry-http.d.ts +2 -1
- package/dist/sdk/generated.d.ts +26 -0
- package/dist/sdk/index.js +14 -0
- package/hooks/codewith-native-common.ts +190 -6
- package/hooks/hook-signed-link-guard/README.md +46 -0
- package/hooks/hook-signed-link-guard/src/classify.ts +7 -3
- package/hooks/hook-signed-link-guard/src/jq.ts +205 -34
- package/hooks/hook-stop-review/README.md +5 -2
- package/hooks/hook-stop-review/src/jev.ts +9 -4
- package/hooks/hook-stop-review/src/policy.ts +7 -3
- package/hooks/hook-stop-review/src/runtime.ts +5 -4
- package/hooks/hook-trash-guard/README.md +15 -10
- package/hooks/hook-trash-guard/src/hook.ts +130 -12
- package/hooks/hook-workspace-repos-guard/src/hook.ts +27 -12
- package/hooks/pre-bash/README.md +47 -1
- package/hooks/pre-bash/src/hook.ts +168 -12
- package/hooks/shell-words.ts +40 -2
- package/package.json +3 -2
- package/scripts/build-release-manifest.ts +27 -0
- package/scripts/validate-package.ts +5 -1
- package/scripts/validate-release-manifest.ts +67 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hasna/hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"description": "Open source hooks library for AI coding agents - Install safety, quality, and automation hooks with a single command",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"main": "./dist/index.js",
|
|
42
42
|
"types": "./dist/index.d.ts",
|
|
43
43
|
"scripts": {
|
|
44
|
-
"build": "rm -rf dist bin && bun build ./src/cli/index.tsx --outdir ./bin --target bun --external pg --external ink --external react --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/serve.ts --outdir ./bin --target bun --external pg && bun build ./src/mcp/hooks-mcp.ts --outdir ./bin --target bun --external pg --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/sdk/index.ts --outdir ./dist/sdk --target bun && bun build ./src/index.ts ./src/storage.ts --outdir ./dist --target bun --external pg && bun run build:native-safety && bun run build:native-safety-worker && bun run build:bundled-hooks && bun run build:sumi-stop-plugin && bun run build:opencode-stop-plugin && bun run build:types",
|
|
44
|
+
"build": "rm -rf dist bin && bun build ./src/cli/index.tsx --outdir ./bin --target bun --external pg --external ink --external react --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/serve.ts --outdir ./bin --target bun --external pg && bun build ./src/mcp/hooks-mcp.ts --outdir ./bin --target bun --external pg --external chalk --external conf --external @modelcontextprotocol/sdk --external zod && bun build ./src/sdk/index.ts --outdir ./dist/sdk --target bun && bun build ./src/index.ts ./src/storage.ts --outdir ./dist --target bun --external pg && bun run build:native-safety && bun run build:native-safety-worker && bun run build:bundled-hooks && bun run build:release-manifest && bun run build:sumi-stop-plugin && bun run build:opencode-stop-plugin && bun run build:types",
|
|
45
45
|
"build:types": "tsc -p tsconfig.build.json",
|
|
46
46
|
"generate:sdk": "bun run scripts/generate-sdk.ts",
|
|
47
47
|
"verify:generated": "bun run scripts/verify-generated-artifacts.ts",
|
|
@@ -57,6 +57,7 @@
|
|
|
57
57
|
"build:native-readiness": "bun build ./hooks/native-readiness-entry.ts --outdir ./bin --target node --format=cjs && bun build ./src/native-readiness.ts --outdir ./dist --target bun",
|
|
58
58
|
"build:native-safety-worker": "bun scripts/build-native-worker.ts",
|
|
59
59
|
"build:bundled-hooks": "bun scripts/build-bundled-hooks.ts",
|
|
60
|
+
"build:release-manifest": "bun scripts/build-release-manifest.ts",
|
|
60
61
|
"build:sumi-stop-plugin": "bun build ./src/lib/sumi-stop-plugin.ts --outdir ./dist --target node",
|
|
61
62
|
"build:opencode-stop-plugin": "bun build ./src/lib/opencode-stop-plugin.ts --outdir ./dist --target node"
|
|
62
63
|
},
|
|
@@ -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
|
-
//
|
|
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
|
+
}
|