@hasna/hooks 0.12.5 → 0.12.6
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 +16 -1
- package/bin/index.js +47 -7
- package/bin/native-safety-worker.cjs +1 -1
- package/dist/index.js +43 -7
- package/dist/lib/native-child-env.d.ts +56 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -297,6 +297,13 @@ hooks safety install trash-guard --target sumi --expected-binding-sha256 CURRENT
|
|
|
297
297
|
hooks safety verify --target sumi --execution
|
|
298
298
|
```
|
|
299
299
|
|
|
300
|
+
Hooks registered as `hooks run <hook>` do not go through the native worker, so
|
|
301
|
+
re-registration does not cover them. When a release changes the script of such
|
|
302
|
+
a hook, run `hooks trust <hook>` with the new CLI on every station after
|
|
303
|
+
switching PATH to it, as "Trust model" under "Custom and remote hooks"
|
|
304
|
+
describes. Until then, wherever an old pin exists, it refuses the new script
|
|
305
|
+
and that hook exits 1 on every call.
|
|
306
|
+
|
|
300
307
|
`hooks safety provision --target sumi`, which the package's lifecycle script
|
|
301
308
|
also runs, keeps a binding that still verifies, so it does not move Sumi to a
|
|
302
309
|
worker installed in a different tree; the explicit install above does. That
|
|
@@ -372,6 +379,12 @@ and trust provisioning succeed. Keep existing hook definitions and trust intact
|
|
|
372
379
|
when this check reports `native_unsupported`.
|
|
373
380
|
The version probe has a five-second forced timeout and native discovery a
|
|
374
381
|
fifteen-second lifetime budget. Neither step restarts an existing agent.
|
|
382
|
+
Both run the native with an explicit environment allowlist: `PATH`, `USER`,
|
|
383
|
+
`LOGNAME`, `LANG`, `LC_*`, `TMPDIR`, the XDG base directories and Codex's own
|
|
384
|
+
`CODEX_*` variables, with `HOME`, `NO_COLOR` and `CODEX_HOME` set by Hooks.
|
|
385
|
+
Dynamic-loader (`LD_*`, `DYLD_*`) and runtime-hook (`NODE_*`, `BUN_*`,
|
|
386
|
+
`BASH_ENV`, `ENV`) variables never reach it, because only the executable's
|
|
387
|
+
bytes are verified.
|
|
375
388
|
|
|
376
389
|
Success reports `nativeDiscoveryVerified: true`, `guardVerified: true` and
|
|
377
390
|
`nativeAdoptionVerified: false`. It does not enroll trust, change configuration,
|
|
@@ -449,13 +462,15 @@ hooks install https://example.com/hooks/my-hook/manifest.json
|
|
|
449
462
|
|
|
450
463
|
Custom hooks land in `~/.hasna/hooks/hooks/<name>/`. A custom hook with the same name as a bundled hook takes precedence (visible in `hooks info <name>`).
|
|
451
464
|
|
|
452
|
-
**Trust model.** Every hook script is pinned by sha256 in `~/.hasna/hooks/hooks.lock` and the SQLite `hooks` table. `hooks run` verifies the script hash before executing; if the script changed, the run is refused:
|
|
465
|
+
**Trust model.** Every hook script is pinned by sha256 in `hooks.lock` in the hooks data directory (`~/.hasna/hooks/hooks.lock` unless the data directory resolves elsewhere; `HASNA_HOOKS_LOCK_PATH`, or its alias `HOOKS_LOCK_PATH`, overrides the lock path) and, in local mode, in the SQLite `hooks` table, which is checked first. `hooks run` verifies the script hash before executing; if the script changed, the run is refused and exits 1:
|
|
453
466
|
|
|
454
467
|
```bash
|
|
455
468
|
hooks trust <name> # re-pin the current script content
|
|
456
469
|
hooks update # re-register hooks and refresh pins
|
|
457
470
|
```
|
|
458
471
|
|
|
472
|
+
A hook's first run writes its pin, and so do `hooks sync` (in local mode it pins every bundled hook without running any of them), `hooks update`, `hooks install <name>@<version>`, `hooks update <name>@<version>` and `hooks trust`. A station can therefore hold a pin for a hook that has never run there. After any upgrade that changes the script of a hook registered as `hooks run <hook>`, run `hooks trust <hook>` with the new CLI on every station. Run it as the same user and with the same hooks environment that the harness's `hooks run` sees: the local opt-in or registry credential, and any data directory, DB or lock path override. Otherwise it can write a pin that `hooks run` does not read. Where the hook is not pinned yet, it pins the same sha256 that the first run would have pinned, so it is harmless there.
|
|
473
|
+
|
|
459
474
|
**Registry server.** `hooks serve` exposes the local store over HTTP — catalog, artifacts, and the published lock:
|
|
460
475
|
|
|
461
476
|
```bash
|
package/bin/index.js
CHANGED
|
@@ -36319,6 +36319,51 @@ var init_native_safety_registration = __esm(() => {
|
|
|
36319
36319
|
};
|
|
36320
36320
|
});
|
|
36321
36321
|
|
|
36322
|
+
// src/lib/native-child-env.ts
|
|
36323
|
+
function isRefusedNativeChildEnvName(name) {
|
|
36324
|
+
return REFUSED.has(name) || REFUSED_PREFIXES.some((prefix3) => name.startsWith(prefix3));
|
|
36325
|
+
}
|
|
36326
|
+
function codexInherited(name) {
|
|
36327
|
+
return name.startsWith("CODEX_") && name !== "CODEX_HOME" && name !== "CODEX_APPLY_GIT_CFG" && !name.startsWith("CODEX_NETWORK_PROXY_SNAPSHOT_");
|
|
36328
|
+
}
|
|
36329
|
+
function nativeChildEnv(source, explicit = {}, inherit = () => false) {
|
|
36330
|
+
const env = {};
|
|
36331
|
+
for (const [name, value] of Object.entries(source)) {
|
|
36332
|
+
if (typeof value !== "string" || isRefusedNativeChildEnvName(name))
|
|
36333
|
+
continue;
|
|
36334
|
+
if (INHERITED.has(name) || /^LC_[A-Z]+$/.test(name) || inherit(name))
|
|
36335
|
+
env[name] = value;
|
|
36336
|
+
}
|
|
36337
|
+
for (const [name, value] of Object.entries(explicit)) {
|
|
36338
|
+
if (isRefusedNativeChildEnvName(name))
|
|
36339
|
+
throw new Error(`A native child environment cannot set ${name}.`);
|
|
36340
|
+
if (value === undefined)
|
|
36341
|
+
delete env[name];
|
|
36342
|
+
else
|
|
36343
|
+
env[name] = value;
|
|
36344
|
+
}
|
|
36345
|
+
return env;
|
|
36346
|
+
}
|
|
36347
|
+
var INHERITED, REFUSED_PREFIXES, REFUSED;
|
|
36348
|
+
var init_native_child_env = __esm(() => {
|
|
36349
|
+
INHERITED = new Set([
|
|
36350
|
+
"PATH",
|
|
36351
|
+
"USER",
|
|
36352
|
+
"LOGNAME",
|
|
36353
|
+
"LANG",
|
|
36354
|
+
"TMPDIR",
|
|
36355
|
+
"XDG_CONFIG_HOME",
|
|
36356
|
+
"XDG_CONFIG_DIRS",
|
|
36357
|
+
"XDG_DATA_HOME",
|
|
36358
|
+
"XDG_DATA_DIRS",
|
|
36359
|
+
"XDG_CACHE_HOME",
|
|
36360
|
+
"XDG_STATE_HOME",
|
|
36361
|
+
"XDG_RUNTIME_DIR"
|
|
36362
|
+
]);
|
|
36363
|
+
REFUSED_PREFIXES = ["LD_", "DYLD_", "NODE_", "BUN_", "BASH_FUNC_"];
|
|
36364
|
+
REFUSED = new Set(["BASH_ENV", "ENV", "GLIBC_TUNABLES", "GCONV_PATH", "LOCPATH"]);
|
|
36365
|
+
});
|
|
36366
|
+
|
|
36322
36367
|
// src/lib/codex-safety-check.ts
|
|
36323
36368
|
import { spawn as spawn2, execFile as execFile2 } from "child_process";
|
|
36324
36369
|
import { createHash as createHash23 } from "crypto";
|
|
@@ -36373,13 +36418,7 @@ function executable(command) {
|
|
|
36373
36418
|
}
|
|
36374
36419
|
async function connectCodexSafety(options) {
|
|
36375
36420
|
const command = options.command ?? "codex", before = executable(command);
|
|
36376
|
-
const env =
|
|
36377
|
-
for (const key of ["NODE_OPTIONS", "NODE_PATH", "BUN_OPTIONS", "BASH_ENV", "ENV", "DYLD_INSERT_LIBRARIES", "LD_PRELOAD"])
|
|
36378
|
-
delete env[key];
|
|
36379
|
-
if (options.codexHome === undefined)
|
|
36380
|
-
delete env.CODEX_HOME;
|
|
36381
|
-
else
|
|
36382
|
-
env.CODEX_HOME = options.codexHome;
|
|
36421
|
+
const env = nativeChildEnv(process.env, { HOME: options.home, NO_COLOR: "1", CODEX_HOME: options.codexHome }, codexInherited);
|
|
36383
36422
|
let version2;
|
|
36384
36423
|
try {
|
|
36385
36424
|
version2 = (await promisify(execFile2)(before.path, ["--version"], { env, cwd: options.home, timeout: 5000, killSignal: "SIGKILL", maxBuffer: 4096 })).stdout.trim();
|
|
@@ -36596,6 +36635,7 @@ var SUPPORTED_CODEX_SAFETY_VERSIONS, versions, matcher = "^(Bash|apply_patch|App
|
|
|
36596
36635
|
var init_codex_safety_check = __esm(() => {
|
|
36597
36636
|
init_native_safety();
|
|
36598
36637
|
init_native_readiness_command();
|
|
36638
|
+
init_native_child_env();
|
|
36599
36639
|
init_native_safety2();
|
|
36600
36640
|
init_native_readiness();
|
|
36601
36641
|
SUPPORTED_CODEX_SAFETY_VERSIONS = ["codex-cli 0.153.0", "codex-cli 0.154.0", "codex-cli 0.154.0-alpha.6.1", "codex-cli 0.155.0", "codex-cli 0.155.1", "codex-cli 0.156.1", "codex-cli 0.157.0", "codex-cli 0.157.1", "codex-cli 0.158.0", "codex-cli 0.159.0", "codex-cli 0.159.2", "codex-cli 0.159.3", "codex-cli 0.160.0", "codex-cli 0.160.1"];
|
|
@@ -17,4 +17,4 @@ var{defineProperty:fZ,getOwnPropertyNames:t0,getOwnPropertyDescriptor:e0}=Object
|
|
|
17
17
|
`).replace(/%s/g,()=>G[K++]?.value??"")+(K<G.length?" "+G.slice(K).map((H)=>H.value).join(" "):"")}if(z==="cat"){let Y=Z.indexOf("<<");if(Y<0)return null;let G=JZ(Z,Y),K=Z.indexOf(`
|
|
18
18
|
`,Y);if(!G||K<0)return null;let H=LZ(Z,K,[G]);if(H<0)return null;let B=Z.slice(K+1,H);return B.slice(0,B.lastIndexOf(G.delimiter)).replace(/\n$/,"")}return null}function rZ(Z,J,$){for(let X of tZ(Z).bodies){let z=Z0(X);if(z===null)continue;let Q=m(z,J+1,$.debug);if(Q.refused)return Q}return q}function m(Z,J=0,$=!1){if(typeof Z!=="string"||!Z.trim())return q;if(J>r3)return EZ.test(Z)?F("unclassifiable"):q;let{segments:X,trustworthy:z}=K0(Z);if(!z)return EZ.test(Z)?F("unclassifiable"):q;let Q=$,Y=(G)=>m(G,J+1,Q);for(let G of e3(Z)){if(G.runsBody){let K=Y(G.body);if(K.refused)return K}if(!G.quoted){let K=tZ(G.body,"heredoc");if(!K.complete&&EZ.test(G.body))return F("unclassifiable");for(let H of K.bodies){let B=Y(H);if(B.refused)return B}}}for(let G=0;G<X.length;G++){let K=X[G];for(let L of K.words){let v=tZ(L.text);if(!v.complete&&EZ.test(L.text))return F("unclassifiable");for(let k of v.bodies){let w=Y(k);if(w.refused)return w}}let H=l(K),B=K.words.map((L,v)=>({word:XZ(L.text),redirect:K.redirects.has(v)})),U=B.slice(0,H<0?B.length:H).map((L)=>L.word),V=H>=0?o(B[H].word.value):"";if(H<0||["export","declare","typeset","readonly","local"].includes(V)){if(Q=m0([...U,...B.slice(H+1).map((L)=>L.word)],Q),H<0)continue}let C=m0(U,Q),_=B.slice(H+1).filter((L)=>!L.redirect).map((L)=>L.word),O=B.slice(H+1).filter((L)=>L.redirect).map((L)=>L.word),M=B[H].word,j=o(M.value),c={depth:J,appendsArgs:!1,debug:C};if(H>0&&B[H-1].word.value==="function"){let L=_.filter((k)=>!["{","()","(",")"].includes(k.value)),v=$0(L,c);if(v.refused)return v;continue}if(G>0&&d0(K.sepBefore)&&eZ(j,_)){let L=X[G-1],v=o3(Z,L),k=v===null?null:Z0(v);if(k!==null){let w=m(k,J+1,C);if(w.refused)return w}}let p=X[G+1];if(p&&/<\(\s*$/.test(p.sepBefore)&&(yZ.has(j)||j==="source"||j===".")){let L=p.words[0].start-p.sepBefore.length+p.sepBefore.lastIndexOf("("),v=qZ(Z,L),k=v<0?null:Z0(Z.slice(L+1,v-1));if(k!==null){let w=m(k,J+1,C);if(w.refused)return w}}let a=i0(j,M,_,O,K.words.slice(0,H+1),c);if(a.refused)return a}return q}function o3(Z,J){let $=J.words[0],X=J.words[J.words.length-1];return $&&X?Z.slice($.start,X.end):null}function t3(Z){let J=o(Z.value);return/[$`]/.test(J)||J===""?void 0:J}function i0(Z,J,$,X,z,Q){let Y=Q.depth,G=(K)=>m(K,Y+1,Q.debug);if(J.opaque){let K=t3(J);if(K===void 0){let H=$.find((U)=>!U.value.startsWith("-")),B=RZ($,Q);if(!B.refused)return q;return B.rule!=="unclassifiable"||H!==void 0&&!H.opaque&&A3.has(H.value)?B:q}Z=K}if(Z==="gh")return RZ($,Q);if(yZ.has(Z)){let K=Q0($);if(K){let H=G(K.value);if(H.refused||!K.opaque)return H;return rZ(K.raw,Y,Q)}for(let H of X){let B=G(H.value);if(B.refused)return B;if(H.opaque){let U=rZ(H.raw,Y,Q);if(U.refused)return U}}return q}if(p3.has(Z)){let K=$.findIndex((B)=>B.value==="-c"||B.value==="--command"||/^-[A-Za-z]*c$/.test(B.value)),H=K>=0?$[K+1]:void 0;return H?G(H.value):oZ($,Q)}if(Z==="eval"||Z==="watch"){let K=$;if(Z==="watch"){let B=0;while(B<$.length&&$[B].value.startsWith("-")){if($[B].value==="--"){B++;break}if(["-n","--interval","-q","--equexit"].includes($[B].value))B++;B++}K=$.slice(B)}let H=G(K.map((B)=>B.value).join(" "));if(H.refused)return H;for(let B of K){let U=rZ(B.raw,Y,Q);if(U.refused)return U}return q}if(Z==="env")return G(FZ([...z.map((K)=>XZ(K.text).value),...$.map((K)=>K.value)]));if(Z==="ssh"){let K=0;for(;K<$.length;K++){let U=$[K].value;if(!U.startsWith("-"))break;if(a3.has(U))K++}let H=$.slice(K+1);if(H[0]?.value==="--")H.shift();let B=H.map((U)=>U.value).join(" ");return B?G(B):q}if(Z==="xargs"){let K=0,H=!1;for(;K<$.length;K++){let B=$[K].value;if(B==="--"){K++;break}if(!B.startsWith("-"))break;if(/^-I|^-i|^--replace/.test(B))H=!0;if(s3.has(B))K++}return $0($.slice(K),{depth:Y,appendsArgs:!H,debug:Q.debug})}if(Z==="find"){for(let K=0;K<$.length;K++){if(!["-exec","-execdir","-ok","-okdir"].includes($[K].value))continue;let H=K+1;while(H<$.length&&$[H].value!==";"&&$[H].value!=="+")H++;let B=$0($.slice(K+1,H),Q);if(B.refused)return B;K=H}return q}if(d3.has(Z))return oZ($,Q);if(i3.get(Z)?.includes($[0]?.value??""))return oZ($.slice(1),Q);return q}function $0(Z,J){if(Z.length===0)return q;let $=o(Z[0].value);if($==="gh")return RZ(Z.slice(1),J);if(yZ.has($)){let X=Q0(Z.slice(1));return X?m(X.value,J.depth+1,J.debug):q}return i0($,Z[0],Z.slice(1),[],[],J)}function oZ(Z,J){let $=Z.findIndex((X)=>!X.opaque&&o(X.value)==="gh"&&(X.value==="gh"||X.value.includes("/")));return $>=0?RZ(Z.slice($+1),J):q}function Q0(Z){for(let J=0;J<Z.length;J++){let $=Z[J].value;if($==="--")return;if(!$.startsWith("-")&&!$.startsWith("+"))return;if($==="-c"||/^-[A-Za-z]+$/.test($)&&$.includes("c"))return Z.slice(J+1).find((X)=>!X.value.startsWith("-")||X.value==="-");if(/^[-+][A-Za-z]*[oO]$/.test($))J++}return}function e3(Z){let J=[],$=0;for(;;){let X=Z.indexOf("<<",$);if(X<0)break;if($=X+2,Z[X+2]==="<"||X>0&&Z[X-1]==="<"){$=X+3;continue}let z=JZ(Z,X);if(!z)continue;let Q=Z.lastIndexOf(`
|
|
19
19
|
`,X)+1,Y=Z.indexOf(`
|
|
20
|
-
`,X);if(Y<0)continue;let G=LZ(Z,Y,[z]),K=G<0?Z.slice(Y+1):Z.slice(Y+1,G),H=G<0?K:K.slice(0,Math.max(0,K.lastIndexOf(z.delimiter))).replace(/\n$/,""),B=Z.slice(Q,X)+" HEREDOC_OPERATOR "+Z.slice(z.end,Y);J.push({body:H,runsBody:Z4(B),quoted:z.quoted})}return J}function Z4(Z){let{segments:J}=K0(Z),$=J.findIndex((G)=>G.words.some((K)=>K.text==="HEREDOC_OPERATOR"));if($<0)return!1;let X=(G)=>{let K=l(G);if(K<0)return{verb:"",args:[]};let H=G.words.slice(K+1).filter((B)=>B.text!=="HEREDOC_OPERATOR").map((B)=>XZ(B.text));return{verb:o(XZ(G.words[K].text).value),args:H}},{verb:z,args:Q}=X(J[$]);if(z==="ssh"||eZ(z,Q))return!0;if((z==="source"||z===".")&&["/dev/stdin","/dev/fd/0","/proc/self/fd/0"].includes(Q[0]?.value??""))return!0;let Y=J[$+1];if(z==="cat"&&Y&&d0(Y.sepBefore)){let G=X(Y);return G.verb==="ssh"||eZ(G.verb,G.args)}return!1}var $4=/(?:^|[^A-Za-z0-9_.-])gh(?:$|[^A-Za-z0-9_.-])/;function s0(){return{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:J0}}}var J4=new Set(["Bash","Monitor"]);function a0(Z){if(Z?.hook_event_name!=="PreToolUse"||!J4.has(Z.tool_name??""))return{continue:!0};let J=$Z(Z);if(!J.trim())return{continue:!0};try{return m(J).refused?s0():{continue:!0}}catch{return $4.test(J)?s0():{continue:!0}}}function X4(Z){if(Z!=="trash-guard"&&Z!=="workspace-repos-guard")throw Error("Invalid safety capability")}function n0(Z,J){X4(Z);let $=Z,X=JSON.parse(J);if(!X||Array.isArray(X)||X.hook_event_name!=="PreToolUse"||typeof X.tool_name!=="string"||!X.tool_input||typeof X.tool_input!=="object"||Array.isArray(X.tool_input)||typeof X.cwd!=="string"||!Y0.isAbsolute(X.cwd))throw Error("Invalid safety input");if(X.tool_name==="Bash"&&typeof X.tool_input.command!=="string")throw Error("Invalid shell input");if(X.tool_name==="Monitor"&&X.tool_input.command!==void 0&&typeof X.tool_input.command!=="string")throw Error("Invalid monitor input");let z=X.hasna_execution;if(z!==void 0&&(!z||Object.keys(z).sort().join()!=="agent,schema"||z.schema!=="hasna.hooks.execution.v1"||typeof z.agent!=="string"||!/^[A-Za-z0-9][A-Za-z0-9._:@-]{0,127}$/.test(z.agent)))throw Error("Invalid execution capability");let Q=z===void 0?void 0:{runtime:process.execPath,home:SZ.homedir(),path:process.env.PATH,agent:z.agent};if(Q&&(!Y0.isAbsolute(Q.runtime)||!Q.path))throw Error("Invalid bound execution context");if(["apply_patch","ApplyPatch","functions.apply_patch"].includes(X.tool_name)){if(X.tool_input.command!==void 0&&X.tool_input.patch!==void 0&&X.tool_input.command!==X.tool_input.patch)throw Error("Conflicting patch aliases");let O=X.tool_input.command??X.tool_input.patch;if(typeof O!=="string")throw Error("Invalid patch input");X.tool_input={...X.tool_input,command:O,patch:O}}let Y=X.tool_name==="Monitor",G=X.tool_name==="Bash"||Y&&typeof X.tool_input.command==="string",K=Y&&G?{...X,tool_name:"Bash"}:X,H=(O)=>({hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:O}}),B=y0(K).output,U=a0(K),V=G&&NZ(K.tool_input.command,{home:SZ.homedir(),cwd:X.cwd}).handoffHits.length>0,C=$==="trash-guard"&&(!Y||G)?I0(K,{home:SZ.homedir(),cwd:X.cwd,findTrash:DZ,inspectTrash:BZ,execution:Q}):{continue:!0};if(Y&&C.hookSpecificOutput?.permissionDecision==="allow")C=H("[trash-guard] A deletion inside a Monitor command is refused; run it with the Bash tool, where it is redirected into trash.");let _=B.decision==="block"?H(B.reason):U.hookSpecificOutput?U:V?H("[workspace-repos-guard] Deletion under a protected repository checkout is refused."):C;return JSON.stringify({verdict:"continue"in _?null:_})}var z4=2,K4="0.12.
|
|
20
|
+
`,X);if(Y<0)continue;let G=LZ(Z,Y,[z]),K=G<0?Z.slice(Y+1):Z.slice(Y+1,G),H=G<0?K:K.slice(0,Math.max(0,K.lastIndexOf(z.delimiter))).replace(/\n$/,""),B=Z.slice(Q,X)+" HEREDOC_OPERATOR "+Z.slice(z.end,Y);J.push({body:H,runsBody:Z4(B),quoted:z.quoted})}return J}function Z4(Z){let{segments:J}=K0(Z),$=J.findIndex((G)=>G.words.some((K)=>K.text==="HEREDOC_OPERATOR"));if($<0)return!1;let X=(G)=>{let K=l(G);if(K<0)return{verb:"",args:[]};let H=G.words.slice(K+1).filter((B)=>B.text!=="HEREDOC_OPERATOR").map((B)=>XZ(B.text));return{verb:o(XZ(G.words[K].text).value),args:H}},{verb:z,args:Q}=X(J[$]);if(z==="ssh"||eZ(z,Q))return!0;if((z==="source"||z===".")&&["/dev/stdin","/dev/fd/0","/proc/self/fd/0"].includes(Q[0]?.value??""))return!0;let Y=J[$+1];if(z==="cat"&&Y&&d0(Y.sepBefore)){let G=X(Y);return G.verb==="ssh"||eZ(G.verb,G.args)}return!1}var $4=/(?:^|[^A-Za-z0-9_.-])gh(?:$|[^A-Za-z0-9_.-])/;function s0(){return{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:J0}}}var J4=new Set(["Bash","Monitor"]);function a0(Z){if(Z?.hook_event_name!=="PreToolUse"||!J4.has(Z.tool_name??""))return{continue:!0};let J=$Z(Z);if(!J.trim())return{continue:!0};try{return m(J).refused?s0():{continue:!0}}catch{return $4.test(J)?s0():{continue:!0}}}function X4(Z){if(Z!=="trash-guard"&&Z!=="workspace-repos-guard")throw Error("Invalid safety capability")}function n0(Z,J){X4(Z);let $=Z,X=JSON.parse(J);if(!X||Array.isArray(X)||X.hook_event_name!=="PreToolUse"||typeof X.tool_name!=="string"||!X.tool_input||typeof X.tool_input!=="object"||Array.isArray(X.tool_input)||typeof X.cwd!=="string"||!Y0.isAbsolute(X.cwd))throw Error("Invalid safety input");if(X.tool_name==="Bash"&&typeof X.tool_input.command!=="string")throw Error("Invalid shell input");if(X.tool_name==="Monitor"&&X.tool_input.command!==void 0&&typeof X.tool_input.command!=="string")throw Error("Invalid monitor input");let z=X.hasna_execution;if(z!==void 0&&(!z||Object.keys(z).sort().join()!=="agent,schema"||z.schema!=="hasna.hooks.execution.v1"||typeof z.agent!=="string"||!/^[A-Za-z0-9][A-Za-z0-9._:@-]{0,127}$/.test(z.agent)))throw Error("Invalid execution capability");let Q=z===void 0?void 0:{runtime:process.execPath,home:SZ.homedir(),path:process.env.PATH,agent:z.agent};if(Q&&(!Y0.isAbsolute(Q.runtime)||!Q.path))throw Error("Invalid bound execution context");if(["apply_patch","ApplyPatch","functions.apply_patch"].includes(X.tool_name)){if(X.tool_input.command!==void 0&&X.tool_input.patch!==void 0&&X.tool_input.command!==X.tool_input.patch)throw Error("Conflicting patch aliases");let O=X.tool_input.command??X.tool_input.patch;if(typeof O!=="string")throw Error("Invalid patch input");X.tool_input={...X.tool_input,command:O,patch:O}}let Y=X.tool_name==="Monitor",G=X.tool_name==="Bash"||Y&&typeof X.tool_input.command==="string",K=Y&&G?{...X,tool_name:"Bash"}:X,H=(O)=>({hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:O}}),B=y0(K).output,U=a0(K),V=G&&NZ(K.tool_input.command,{home:SZ.homedir(),cwd:X.cwd}).handoffHits.length>0,C=$==="trash-guard"&&(!Y||G)?I0(K,{home:SZ.homedir(),cwd:X.cwd,findTrash:DZ,inspectTrash:BZ,execution:Q}):{continue:!0};if(Y&&C.hookSpecificOutput?.permissionDecision==="allow")C=H("[trash-guard] A deletion inside a Monitor command is refused; run it with the Bash tool, where it is redirected into trash.");let _=B.decision==="block"?H(B.reason):U.hookSpecificOutput?U:V?H("[workspace-repos-guard] Deletion under a protected repository checkout is refused."):C;return JSON.stringify({verdict:"continue"in _?null:_})}var z4=2,K4="0.12.6";function Q4(Z,J){return n0(Z,J)}
|
package/dist/index.js
CHANGED
|
@@ -42479,6 +42479,48 @@ import { constants as constants12, openSync as openSync15, closeSync as closeSyn
|
|
|
42479
42479
|
import { dirname as dirname21, isAbsolute as isAbsolute17, resolve as resolve19 } from "path";
|
|
42480
42480
|
import { promisify } from "util";
|
|
42481
42481
|
|
|
42482
|
+
// src/lib/native-child-env.ts
|
|
42483
|
+
var INHERITED = new Set([
|
|
42484
|
+
"PATH",
|
|
42485
|
+
"USER",
|
|
42486
|
+
"LOGNAME",
|
|
42487
|
+
"LANG",
|
|
42488
|
+
"TMPDIR",
|
|
42489
|
+
"XDG_CONFIG_HOME",
|
|
42490
|
+
"XDG_CONFIG_DIRS",
|
|
42491
|
+
"XDG_DATA_HOME",
|
|
42492
|
+
"XDG_DATA_DIRS",
|
|
42493
|
+
"XDG_CACHE_HOME",
|
|
42494
|
+
"XDG_STATE_HOME",
|
|
42495
|
+
"XDG_RUNTIME_DIR"
|
|
42496
|
+
]);
|
|
42497
|
+
var REFUSED_PREFIXES = ["LD_", "DYLD_", "NODE_", "BUN_", "BASH_FUNC_"];
|
|
42498
|
+
var REFUSED = new Set(["BASH_ENV", "ENV", "GLIBC_TUNABLES", "GCONV_PATH", "LOCPATH"]);
|
|
42499
|
+
function isRefusedNativeChildEnvName(name) {
|
|
42500
|
+
return REFUSED.has(name) || REFUSED_PREFIXES.some((prefix3) => name.startsWith(prefix3));
|
|
42501
|
+
}
|
|
42502
|
+
function codexInherited(name) {
|
|
42503
|
+
return name.startsWith("CODEX_") && name !== "CODEX_HOME" && name !== "CODEX_APPLY_GIT_CFG" && !name.startsWith("CODEX_NETWORK_PROXY_SNAPSHOT_");
|
|
42504
|
+
}
|
|
42505
|
+
function nativeChildEnv(source, explicit = {}, inherit = () => false) {
|
|
42506
|
+
const env = {};
|
|
42507
|
+
for (const [name, value] of Object.entries(source)) {
|
|
42508
|
+
if (typeof value !== "string" || isRefusedNativeChildEnvName(name))
|
|
42509
|
+
continue;
|
|
42510
|
+
if (INHERITED.has(name) || /^LC_[A-Z]+$/.test(name) || inherit(name))
|
|
42511
|
+
env[name] = value;
|
|
42512
|
+
}
|
|
42513
|
+
for (const [name, value] of Object.entries(explicit)) {
|
|
42514
|
+
if (isRefusedNativeChildEnvName(name))
|
|
42515
|
+
throw new Error(`A native child environment cannot set ${name}.`);
|
|
42516
|
+
if (value === undefined)
|
|
42517
|
+
delete env[name];
|
|
42518
|
+
else
|
|
42519
|
+
env[name] = value;
|
|
42520
|
+
}
|
|
42521
|
+
return env;
|
|
42522
|
+
}
|
|
42523
|
+
|
|
42482
42524
|
// src/native-safety.ts
|
|
42483
42525
|
import { execFile } from "child_process";
|
|
42484
42526
|
import { closeSync as closeSync13, constants as constants4, existsSync as existsSync15, fstatSync as fstatSync5, fsyncSync, linkSync as linkSync3, lstatSync as lstatSync10, mkdirSync as mkdirSync12, openSync as openSync13, readFileSync as readFileSync16, realpathSync as realpathSync11, renameSync as renameSync3, unlinkSync as unlinkSync5, writeFileSync as writeFileSync10 } from "fs";
|
|
@@ -42622,13 +42664,7 @@ function executable(command) {
|
|
|
42622
42664
|
}
|
|
42623
42665
|
async function connectCodexSafety(options) {
|
|
42624
42666
|
const command = options.command ?? "codex", before = executable(command);
|
|
42625
|
-
const env =
|
|
42626
|
-
for (const key of ["NODE_OPTIONS", "NODE_PATH", "BUN_OPTIONS", "BASH_ENV", "ENV", "DYLD_INSERT_LIBRARIES", "LD_PRELOAD"])
|
|
42627
|
-
delete env[key];
|
|
42628
|
-
if (options.codexHome === undefined)
|
|
42629
|
-
delete env.CODEX_HOME;
|
|
42630
|
-
else
|
|
42631
|
-
env.CODEX_HOME = options.codexHome;
|
|
42667
|
+
const env = nativeChildEnv(process.env, { HOME: options.home, NO_COLOR: "1", CODEX_HOME: options.codexHome }, codexInherited);
|
|
42632
42668
|
let version2;
|
|
42633
42669
|
try {
|
|
42634
42670
|
version2 = (await promisify(execFile2)(before.path, ["--version"], { env, cwd: options.home, timeout: 5000, killSignal: "SIGKILL", maxBuffer: 4096 })).stdout.trim();
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment for a native executable that Hooks verifies and then runs, such
|
|
3
|
+
* as the Codex app-server used by `hooks safety verify|trust --target codex`.
|
|
4
|
+
*
|
|
5
|
+
* Hooks pins only the native's main executable bytes. Dynamic-loader and
|
|
6
|
+
* runtime-hook variables would load code that the pin never covered: a glibc
|
|
7
|
+
* build honours LD_AUDIT, LD_PRELOAD and LD_LIBRARY_PATH before main() runs,
|
|
8
|
+
* so the native's own pre-main scrubbing is too late. The child therefore gets
|
|
9
|
+
* an explicit allowlist, never the parent's environment minus a denylist.
|
|
10
|
+
*
|
|
11
|
+
* Inherited from the parent, by exact name unless noted:
|
|
12
|
+
* - PATH: the native resolves its helper executables through it.
|
|
13
|
+
* - USER, LOGNAME: the user identity some native paths read.
|
|
14
|
+
* - LANG and every LC_* name: locale selection only (LOCPATH and GCONV_PATH,
|
|
15
|
+
* which load locale and iconv modules from a directory, are refused).
|
|
16
|
+
* - TMPDIR: Codex derives the temporary writable roots in its permission
|
|
17
|
+
* profiles from it, so a different value changes what config/read reports.
|
|
18
|
+
* - The XDG base directories, so a native that resolves configuration, data,
|
|
19
|
+
* cache, state or its runtime directory (and the default session bus under
|
|
20
|
+
* XDG_RUNTIME_DIR) through them sees the same paths as the agent session.
|
|
21
|
+
* - Whatever a caller's `inherit` predicate admits (Codex: `codexInherited`).
|
|
22
|
+
*
|
|
23
|
+
* Not inherited, deliberately:
|
|
24
|
+
* - Proxy and TLS-trust settings outside the caller's namespace (HTTP_PROXY,
|
|
25
|
+
* HTTPS_PROXY, ALL_PROXY, NO_PROXY, SSL_CERT_FILE, SSL_CERT_DIR). The Codex
|
|
26
|
+
* app-server still reads local configuration and may make its plugin-sync
|
|
27
|
+
* network call; CODEX_* names, such as CODEX_CA_CERTIFICATE, are inherited.
|
|
28
|
+
* - Credentials outside the native's own namespace, logging and backtrace
|
|
29
|
+
* switches (RUST_LOG would grow stderr toward the child's byte budget),
|
|
30
|
+
* SHELL, TERM and DBUS_SESSION_BUS_ADDRESS (a D-Bus address may name an
|
|
31
|
+
* executable transport; XDG_RUNTIME_DIR still locates the default bus).
|
|
32
|
+
*
|
|
33
|
+
* Refused unconditionally, whether inherited or passed in `explicit`:
|
|
34
|
+
* every LD_* and DYLD_* name, every NODE_* and BUN_* name, BASH_ENV, ENV,
|
|
35
|
+
* BASH_FUNC_* exported functions, GLIBC_TUNABLES, GCONV_PATH and LOCPATH.
|
|
36
|
+
* An explicit refused name is a programming error and throws.
|
|
37
|
+
*/
|
|
38
|
+
/** True for a name that must never reach a verified native child. */
|
|
39
|
+
export declare function isRefusedNativeChildEnvName(name: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Codex's own namespace is inherited so the app-server resolves the same
|
|
42
|
+
* configuration, state and auth home as the agent session (for example
|
|
43
|
+
* CODEX_SQLITE_HOME). CODEX_HOME is never inherited here: the caller sets or
|
|
44
|
+
* removes it explicitly. Two kinds of CODEX_* name are dropped because they
|
|
45
|
+
* make Codex run code it was not asked to run:
|
|
46
|
+
* - CODEX_NETWORK_PROXY_SNAPSHOT_*: Codex's carriers that restore BASH_ENV,
|
|
47
|
+
* ENV, ZDOTDIR and brokered variables inside its tool shells, which would
|
|
48
|
+
* re-import names this module refuses.
|
|
49
|
+
* - CODEX_APPLY_GIT_CFG: inline `git -c` settings, which can name commands.
|
|
50
|
+
*/
|
|
51
|
+
export declare function codexInherited(name: string): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Build a native child's environment from `source` (normally process.env).
|
|
54
|
+
* `explicit` is applied last: a string sets the name, `undefined` removes it.
|
|
55
|
+
*/
|
|
56
|
+
export declare function nativeChildEnv(source: Readonly<Record<string, string | undefined>>, explicit?: Readonly<Record<string, string | undefined>>, inherit?: (name: string) => boolean): Record<string, string>;
|
package/package.json
CHANGED