@scopebond/hook 0.2.1 → 0.3.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.
- package/dist/cli.js +161 -18
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +25 -12
- package/dist/init.js.map +1 -1
- package/dist/map.d.ts +12 -6
- package/dist/map.d.ts.map +1 -1
- package/dist/map.js +69 -45
- package/dist/map.js.map +1 -1
- package/dist/minimize.d.ts +5 -0
- package/dist/minimize.d.ts.map +1 -1
- package/dist/minimize.js +58 -12
- package/dist/minimize.js.map +1 -1
- package/dist/runtime.d.ts +11 -3
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +56 -8
- package/dist/runtime.js.map +1 -1
- package/dist/shell.d.ts +26 -0
- package/dist/shell.d.ts.map +1 -0
- package/dist/shell.js +331 -0
- package/dist/shell.js.map +1 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +23 -0
- package/dist/version.js.map +1 -0
- package/package.json +3 -3
package/dist/shell.js
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// Shell decomposition for the hook mapper. A single Bash tool call can carry many
|
|
2
|
+
// commands — `a && b`, `a; b`, `a | b`, `$(c)`, `` `c` ``, `bash -c '…'`, `( c )` —
|
|
3
|
+
// so mapping only the first token lets a denied program ride in behind an allowed
|
|
4
|
+
// one. This module splits a command line into the simple commands it will actually
|
|
5
|
+
// run, so the mapper can emit an intent for each and the runtime can deny if any
|
|
6
|
+
// one of them is out of policy.
|
|
7
|
+
//
|
|
8
|
+
// It is best-effort and fail-safe: anything it cannot parse with confidence is
|
|
9
|
+
// returned as a single `opaque` command, which the caller denies (strict) or
|
|
10
|
+
// observes (non-strict) rather than trusting. Every scan is linear in the input
|
|
11
|
+
// length with no backtracking, so a hostile command cannot stall the hot path.
|
|
12
|
+
const SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh", "ash", "busybox"]);
|
|
13
|
+
const MAX_DEPTH = 6;
|
|
14
|
+
/** Split a command line into top-level simple-command strings at unquoted
|
|
15
|
+
* separators (`;`, newline, `&&`, `||`, `|`, `&`), leaving quotes, `$( )`,
|
|
16
|
+
* backticks and `( )` groups intact inside each piece. */
|
|
17
|
+
function splitTopLevel(src) {
|
|
18
|
+
const segments = [];
|
|
19
|
+
let cur = "";
|
|
20
|
+
let quote = null;
|
|
21
|
+
let paren = 0; // depth of $( ) and ( )
|
|
22
|
+
let backtick = false;
|
|
23
|
+
const flush = () => { const t = cur.trim(); if (t)
|
|
24
|
+
segments.push(t); cur = ""; };
|
|
25
|
+
for (let i = 0; i < src.length; i++) {
|
|
26
|
+
const c = src[i];
|
|
27
|
+
const n = src[i + 1];
|
|
28
|
+
if (quote) {
|
|
29
|
+
cur += c;
|
|
30
|
+
if (quote === "'") {
|
|
31
|
+
if (c === "'")
|
|
32
|
+
quote = null;
|
|
33
|
+
}
|
|
34
|
+
else if (c === "\\" && n !== undefined)
|
|
35
|
+
cur += src[++i];
|
|
36
|
+
else if (c === '"')
|
|
37
|
+
quote = null;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (c === "'" || c === '"') {
|
|
41
|
+
quote = c;
|
|
42
|
+
cur += c;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (c === "`") {
|
|
46
|
+
backtick = !backtick;
|
|
47
|
+
cur += c;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (backtick) {
|
|
51
|
+
cur += c;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (c === "\\" && n !== undefined) {
|
|
55
|
+
cur += c + src[++i];
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (c === "$" && n === "(") {
|
|
59
|
+
paren++;
|
|
60
|
+
cur += "$(";
|
|
61
|
+
i++;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (c === "(") {
|
|
65
|
+
paren++;
|
|
66
|
+
cur += c;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (c === ")") {
|
|
70
|
+
if (paren > 0)
|
|
71
|
+
paren--;
|
|
72
|
+
cur += c;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (paren > 0) {
|
|
76
|
+
cur += c;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (c === ";" || c === "\n") {
|
|
80
|
+
flush();
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (c === "&" && n === "&") {
|
|
84
|
+
flush();
|
|
85
|
+
i++;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (c === "|" && n === "|") {
|
|
89
|
+
flush();
|
|
90
|
+
i++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (c === "&" || c === "|") {
|
|
94
|
+
flush();
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
cur += c;
|
|
98
|
+
}
|
|
99
|
+
flush();
|
|
100
|
+
return { segments, unbalanced: quote !== null || paren !== 0 || backtick };
|
|
101
|
+
}
|
|
102
|
+
/** Pull the contents of command substitutions and subshell groups out of one
|
|
103
|
+
* segment — `$( … )`, `` ` … ` ``, and a leading `( … )` — for recursion, and
|
|
104
|
+
* return the segment with those regions blanked so the outer command tokenizes
|
|
105
|
+
* cleanly. */
|
|
106
|
+
function extractSubstitutions(seg) {
|
|
107
|
+
const inner = [];
|
|
108
|
+
let outer = "";
|
|
109
|
+
let quote = null;
|
|
110
|
+
for (let i = 0; i < seg.length; i++) {
|
|
111
|
+
const c = seg[i];
|
|
112
|
+
const n = seg[i + 1];
|
|
113
|
+
if (quote) {
|
|
114
|
+
outer += c;
|
|
115
|
+
if (quote === "'") {
|
|
116
|
+
if (c === "'")
|
|
117
|
+
quote = null;
|
|
118
|
+
}
|
|
119
|
+
else if (c === "\\" && n !== undefined)
|
|
120
|
+
outer += seg[++i];
|
|
121
|
+
else if (c === '"')
|
|
122
|
+
quote = null;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (c === "'" || c === '"') {
|
|
126
|
+
quote = c;
|
|
127
|
+
outer += c;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (c === "`") {
|
|
131
|
+
const end = seg.indexOf("`", i + 1);
|
|
132
|
+
if (end === -1) {
|
|
133
|
+
outer += c;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
inner.push(seg.slice(i + 1, end));
|
|
137
|
+
i = end;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if ((c === "$" && n === "(") || c === "(") {
|
|
141
|
+
const start = c === "(" ? i + 1 : i + 2;
|
|
142
|
+
let depth = 1;
|
|
143
|
+
let j = start;
|
|
144
|
+
for (; j < seg.length && depth > 0; j++) {
|
|
145
|
+
if (seg[j] === "(")
|
|
146
|
+
depth++;
|
|
147
|
+
else if (seg[j] === ")")
|
|
148
|
+
depth--;
|
|
149
|
+
}
|
|
150
|
+
inner.push(seg.slice(start, depth === 0 ? j - 1 : j));
|
|
151
|
+
i = j - 1;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
outer += c;
|
|
155
|
+
}
|
|
156
|
+
return { outer, inner };
|
|
157
|
+
}
|
|
158
|
+
/** Tokenize a simple command into words, honoring single and double quotes. */
|
|
159
|
+
function tokenize(s) {
|
|
160
|
+
const out = [];
|
|
161
|
+
let cur = "";
|
|
162
|
+
let has = false;
|
|
163
|
+
let quote = null;
|
|
164
|
+
for (let i = 0; i < s.length; i++) {
|
|
165
|
+
const c = s[i];
|
|
166
|
+
const n = s[i + 1];
|
|
167
|
+
if (quote === "'") {
|
|
168
|
+
if (c === "'")
|
|
169
|
+
quote = null;
|
|
170
|
+
else
|
|
171
|
+
cur += c;
|
|
172
|
+
has = true;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (quote === '"') {
|
|
176
|
+
if (c === '"')
|
|
177
|
+
quote = null;
|
|
178
|
+
else if (c === "\\" && n !== undefined)
|
|
179
|
+
cur += s[++i];
|
|
180
|
+
else
|
|
181
|
+
cur += c;
|
|
182
|
+
has = true;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (c === "'" || c === '"') {
|
|
186
|
+
quote = c;
|
|
187
|
+
has = true;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (c === "\\" && n !== undefined) {
|
|
191
|
+
cur += s[++i];
|
|
192
|
+
has = true;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (/\s/.test(c)) {
|
|
196
|
+
if (has) {
|
|
197
|
+
out.push(cur);
|
|
198
|
+
cur = "";
|
|
199
|
+
has = false;
|
|
200
|
+
}
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
cur += c;
|
|
204
|
+
has = true;
|
|
205
|
+
}
|
|
206
|
+
if (has)
|
|
207
|
+
out.push(cur);
|
|
208
|
+
return out;
|
|
209
|
+
}
|
|
210
|
+
const basename = (t) => t.replace(/^.*[\\/]/, "");
|
|
211
|
+
const isAssignment = (t) => /^[A-Za-z_][A-Za-z0-9_]*=/.test(t);
|
|
212
|
+
/** Strip a leading run of `sudo`, `env`, `command`, `nohup`, `nice`, `time`,
|
|
213
|
+
* `xargs`, `exec` wrappers and `NAME=value` assignments, so the program is the
|
|
214
|
+
* real one about to run. `env`/`xargs` consume their own following words as the
|
|
215
|
+
* command, which is handled by simply dropping the wrapper token. */
|
|
216
|
+
function stripPrefixes(tokens) {
|
|
217
|
+
const wrappers = new Set(["sudo", "env", "command", "nohup", "nice", "time", "exec", "stdbuf", "setsid", "xargs"]);
|
|
218
|
+
let i = 0;
|
|
219
|
+
while (i < tokens.length) {
|
|
220
|
+
const t = tokens[i];
|
|
221
|
+
if (isAssignment(t)) {
|
|
222
|
+
i++;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (wrappers.has(basename(t))) {
|
|
226
|
+
i++;
|
|
227
|
+
// `sudo -u user`, `nice -n 10`, `env -i` … skip option words and their args.
|
|
228
|
+
while (i < tokens.length && tokens[i].startsWith("-")) {
|
|
229
|
+
const opt = tokens[i];
|
|
230
|
+
i++;
|
|
231
|
+
if (/^-[unC]$/.test(opt) && i < tokens.length && !tokens[i].startsWith("-"))
|
|
232
|
+
i++;
|
|
233
|
+
}
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
return tokens.slice(i);
|
|
239
|
+
}
|
|
240
|
+
/** Find the script argument of a shell invoked with `-c`, including combined short
|
|
241
|
+
* flags where `c` is last (`-lc`, `-xec`). The script is the next non-option word. */
|
|
242
|
+
function shellScriptArg(argv) {
|
|
243
|
+
for (let i = 0; i < argv.length; i++) {
|
|
244
|
+
if (argv[i] === "-c" || /^-[A-Za-z]*c$/.test(argv[i])) {
|
|
245
|
+
for (let j = i + 1; j < argv.length; j++)
|
|
246
|
+
if (!argv[j].startsWith("-"))
|
|
247
|
+
return argv[j];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
/** Decompose a command line into the simple commands it will run. Recurses into
|
|
253
|
+
* `-c` scripts and command substitutions up to a bounded depth. */
|
|
254
|
+
export function decomposeShell(command, depth = 0) {
|
|
255
|
+
const src = command.trim();
|
|
256
|
+
if (!src)
|
|
257
|
+
return [];
|
|
258
|
+
const opaque = () => [{ program: "", programRaw: "", argv: [], raw: src, opaque: true }];
|
|
259
|
+
if (depth > MAX_DEPTH)
|
|
260
|
+
return opaque();
|
|
261
|
+
const { segments, unbalanced } = splitTopLevel(src);
|
|
262
|
+
if (unbalanced)
|
|
263
|
+
return opaque();
|
|
264
|
+
const out = [];
|
|
265
|
+
for (const seg of segments) {
|
|
266
|
+
const { outer, inner } = extractSubstitutions(seg);
|
|
267
|
+
for (const sub of inner)
|
|
268
|
+
out.push(...decomposeShell(sub, depth + 1));
|
|
269
|
+
const tokens = stripPrefixes(tokenize(outer));
|
|
270
|
+
if (tokens.length === 0)
|
|
271
|
+
continue; // pure substitution/subshell — inner already handled
|
|
272
|
+
const program = basename(tokens[0]);
|
|
273
|
+
const argv = tokens.slice(1);
|
|
274
|
+
out.push({ program, programRaw: tokens[0], argv, raw: seg, opaque: false });
|
|
275
|
+
if (SHELLS.has(program)) {
|
|
276
|
+
const script = shellScriptArg(argv);
|
|
277
|
+
if (script)
|
|
278
|
+
out.push(...decomposeShell(script, depth + 1));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return out.length ? out : opaque();
|
|
282
|
+
}
|
|
283
|
+
/** Parse a `git … push …` simple command into git.push params, or null if it is
|
|
284
|
+
* not a push. Handles global options that take an argument (`-C dir`, `-c k=v`),
|
|
285
|
+
* `--force`/`-f`/`--force-with-lease`, and a leading `+` force refspec. */
|
|
286
|
+
export function parseGitPush(cmd) {
|
|
287
|
+
if (cmd.program !== "git")
|
|
288
|
+
return null;
|
|
289
|
+
const a = cmd.argv;
|
|
290
|
+
let i = 0;
|
|
291
|
+
while (i < a.length) {
|
|
292
|
+
const t = a[i];
|
|
293
|
+
if (t === "-C" || t === "-c" || t === "--git-dir" || t === "--work-tree" || t === "--namespace") {
|
|
294
|
+
i += 2;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (t.startsWith("-")) {
|
|
298
|
+
i++;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
if (a[i] !== "push")
|
|
304
|
+
return null;
|
|
305
|
+
const rest = a.slice(i + 1);
|
|
306
|
+
let force = false;
|
|
307
|
+
const positional = [];
|
|
308
|
+
for (const t of rest) {
|
|
309
|
+
if (t === "--force" || t === "-f" || t === "--force-with-lease" || t.startsWith("--force-with-lease=")) {
|
|
310
|
+
force = true;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (t.startsWith("-"))
|
|
314
|
+
continue;
|
|
315
|
+
positional.push(t);
|
|
316
|
+
}
|
|
317
|
+
const params = { force };
|
|
318
|
+
if (positional[0])
|
|
319
|
+
params.remote = positional[0];
|
|
320
|
+
if (positional[1]) {
|
|
321
|
+
let refspec = positional[1];
|
|
322
|
+
if (refspec.startsWith("+")) {
|
|
323
|
+
force = true;
|
|
324
|
+
params.force = true;
|
|
325
|
+
refspec = refspec.slice(1);
|
|
326
|
+
}
|
|
327
|
+
params.ref = refspec.replace(/^[^:]*:\+?/, ""); // src:dst → dst
|
|
328
|
+
}
|
|
329
|
+
return params;
|
|
330
|
+
}
|
|
331
|
+
//# sourceMappingURL=shell.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.js","sourceRoot":"","sources":["../src/shell.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,oFAAoF;AACpF,kFAAkF;AAClF,mFAAmF;AACnF,iFAAiF;AACjF,gCAAgC;AAChC,EAAE;AACF,+EAA+E;AAC/E,6EAA6E;AAC7E,gFAAgF;AAChF,+EAA+E;AAiB/E,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC;AAC/E,MAAM,SAAS,GAAG,CAAC,CAAC;AAEpB;;2DAE2D;AAC3D,SAAS,aAAa,CAAC,GAAW;IAChC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,IAAI,KAAK,GAAqB,IAAI,CAAC;IACnC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,wBAAwB;IACvC,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,MAAM,KAAK,GAAG,GA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IAC5E,IAAI,UAAU,CAAC,CAAC,CAAC;QAAE,MAAM,CAAC,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IACjD,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QAClB,IAAI,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;QAC5B,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAAC,KAAK,GAAG,IAAI,CAAC;YAAC,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC;YAAC,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAAC,CAAC;QAC/F,MAAM,CAAC,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC,gBAAgB;IAClE,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["// Shell decomposition for the hook mapper. A single Bash tool call can carry many\n// commands — `a && b`, `a; b`, `a | b`, `$(c)`, `` `c` ``, `bash -c '…'`, `( c )` —\n// so mapping only the first token lets a denied program ride in behind an allowed\n// one. This module splits a command line into the simple commands it will actually\n// run, so the mapper can emit an intent for each and the runtime can deny if any\n// one of them is out of policy.\n//\n// It is best-effort and fail-safe: anything it cannot parse with confidence is\n// returned as a single `opaque` command, which the caller denies (strict) or\n// observes (non-strict) rather than trusting. Every scan is linear in the input\n// length with no backtracking, so a hostile command cannot stall the hot path.\n\nexport interface SimpleCommand {\n /** The invoked program's basename, after stripping sudo/env/assignment prefixes.\n * Empty when it cannot be determined. */\n program: string;\n /** The first token whole (pre-basename), so a caller can scrub it before splitting\n * on \"/\": a bare-secret command must not leak a path-fragment as the program. */\n programRaw: string;\n /** Remaining argv (quotes removed), best-effort — used to parse git push. */\n argv: string[];\n /** The raw simple-command text, for the redacted command digest. */\n raw: string;\n /** True when parsing was not confident; the caller must fail closed. */\n opaque: boolean;\n}\n\nconst SHELLS = new Set([\"sh\", \"bash\", \"zsh\", \"dash\", \"ksh\", \"ash\", \"busybox\"]);\nconst MAX_DEPTH = 6;\n\n/** Split a command line into top-level simple-command strings at unquoted\n * separators (`;`, newline, `&&`, `||`, `|`, `&`), leaving quotes, `$( )`,\n * backticks and `( )` groups intact inside each piece. */\nfunction splitTopLevel(src: string): { segments: string[]; unbalanced: boolean } {\n const segments: string[] = [];\n let cur = \"\";\n let quote: '\"' | \"'\" | null = null;\n let paren = 0; // depth of $( ) and ( )\n let backtick = false;\n const flush = () => { const t = cur.trim(); if (t) segments.push(t); cur = \"\"; };\n\n for (let i = 0; i < src.length; i++) {\n const c = src[i];\n const n = src[i + 1];\n if (quote) {\n cur += c;\n if (quote === \"'\") { if (c === \"'\") quote = null; }\n else if (c === \"\\\\\" && n !== undefined) cur += src[++i];\n else if (c === '\"') quote = null;\n continue;\n }\n if (c === \"'\" || c === '\"') { quote = c; cur += c; continue; }\n if (c === \"`\") { backtick = !backtick; cur += c; continue; }\n if (backtick) { cur += c; continue; }\n if (c === \"\\\\\" && n !== undefined) { cur += c + src[++i]; continue; }\n if (c === \"$\" && n === \"(\") { paren++; cur += \"$(\"; i++; continue; }\n if (c === \"(\") { paren++; cur += c; continue; }\n if (c === \")\") { if (paren > 0) paren--; cur += c; continue; }\n if (paren > 0) { cur += c; continue; }\n if (c === \";\" || c === \"\\n\") { flush(); continue; }\n if (c === \"&\" && n === \"&\") { flush(); i++; continue; }\n if (c === \"|\" && n === \"|\") { flush(); i++; continue; }\n if (c === \"&\" || c === \"|\") { flush(); continue; }\n cur += c;\n }\n flush();\n return { segments, unbalanced: quote !== null || paren !== 0 || backtick };\n}\n\n/** Pull the contents of command substitutions and subshell groups out of one\n * segment — `$( … )`, `` ` … ` ``, and a leading `( … )` — for recursion, and\n * return the segment with those regions blanked so the outer command tokenizes\n * cleanly. */\nfunction extractSubstitutions(seg: string): { outer: string; inner: string[] } {\n const inner: string[] = [];\n let outer = \"\";\n let quote: '\"' | \"'\" | null = null;\n for (let i = 0; i < seg.length; i++) {\n const c = seg[i];\n const n = seg[i + 1];\n if (quote) {\n outer += c;\n if (quote === \"'\") { if (c === \"'\") quote = null; }\n else if (c === \"\\\\\" && n !== undefined) outer += seg[++i];\n else if (c === '\"') quote = null;\n continue;\n }\n if (c === \"'\" || c === '\"') { quote = c; outer += c; continue; }\n if (c === \"`\") {\n const end = seg.indexOf(\"`\", i + 1);\n if (end === -1) { outer += c; continue; }\n inner.push(seg.slice(i + 1, end));\n i = end;\n continue;\n }\n if ((c === \"$\" && n === \"(\") || c === \"(\") {\n const start = c === \"(\" ? i + 1 : i + 2;\n let depth = 1;\n let j = start;\n for (; j < seg.length && depth > 0; j++) {\n if (seg[j] === \"(\") depth++;\n else if (seg[j] === \")\") depth--;\n }\n inner.push(seg.slice(start, depth === 0 ? j - 1 : j));\n i = j - 1;\n continue;\n }\n outer += c;\n }\n return { outer, inner };\n}\n\n/** Tokenize a simple command into words, honoring single and double quotes. */\nfunction tokenize(s: string): string[] {\n const out: string[] = [];\n let cur = \"\";\n let has = false;\n let quote: '\"' | \"'\" | null = null;\n for (let i = 0; i < s.length; i++) {\n const c = s[i];\n const n = s[i + 1];\n if (quote === \"'\") { if (c === \"'\") quote = null; else cur += c; has = true; continue; }\n if (quote === '\"') {\n if (c === '\"') quote = null;\n else if (c === \"\\\\\" && n !== undefined) cur += s[++i];\n else cur += c;\n has = true; continue;\n }\n if (c === \"'\" || c === '\"') { quote = c; has = true; continue; }\n if (c === \"\\\\\" && n !== undefined) { cur += s[++i]; has = true; continue; }\n if (/\\s/.test(c)) { if (has) { out.push(cur); cur = \"\"; has = false; } continue; }\n cur += c; has = true;\n }\n if (has) out.push(cur);\n return out;\n}\n\nconst basename = (t: string): string => t.replace(/^.*[\\\\/]/, \"\");\nconst isAssignment = (t: string): boolean => /^[A-Za-z_][A-Za-z0-9_]*=/.test(t);\n\n/** Strip a leading run of `sudo`, `env`, `command`, `nohup`, `nice`, `time`,\n * `xargs`, `exec` wrappers and `NAME=value` assignments, so the program is the\n * real one about to run. `env`/`xargs` consume their own following words as the\n * command, which is handled by simply dropping the wrapper token. */\nfunction stripPrefixes(tokens: string[]): string[] {\n const wrappers = new Set([\"sudo\", \"env\", \"command\", \"nohup\", \"nice\", \"time\", \"exec\", \"stdbuf\", \"setsid\", \"xargs\"]);\n let i = 0;\n while (i < tokens.length) {\n const t = tokens[i];\n if (isAssignment(t)) { i++; continue; }\n if (wrappers.has(basename(t))) {\n i++;\n // `sudo -u user`, `nice -n 10`, `env -i` … skip option words and their args.\n while (i < tokens.length && tokens[i].startsWith(\"-\")) {\n const opt = tokens[i];\n i++;\n if (/^-[unC]$/.test(opt) && i < tokens.length && !tokens[i].startsWith(\"-\")) i++;\n }\n continue;\n }\n break;\n }\n return tokens.slice(i);\n}\n\n/** Find the script argument of a shell invoked with `-c`, including combined short\n * flags where `c` is last (`-lc`, `-xec`). The script is the next non-option word. */\nfunction shellScriptArg(argv: string[]): string | null {\n for (let i = 0; i < argv.length; i++) {\n if (argv[i] === \"-c\" || /^-[A-Za-z]*c$/.test(argv[i])) {\n for (let j = i + 1; j < argv.length; j++) if (!argv[j].startsWith(\"-\")) return argv[j];\n }\n }\n return null;\n}\n\n/** Decompose a command line into the simple commands it will run. Recurses into\n * `-c` scripts and command substitutions up to a bounded depth. */\nexport function decomposeShell(command: string, depth = 0): SimpleCommand[] {\n const src = command.trim();\n if (!src) return [];\n const opaque = (): SimpleCommand[] => [{ program: \"\", programRaw: \"\", argv: [], raw: src, opaque: true }];\n if (depth > MAX_DEPTH) return opaque();\n\n const { segments, unbalanced } = splitTopLevel(src);\n if (unbalanced) return opaque();\n\n const out: SimpleCommand[] = [];\n for (const seg of segments) {\n const { outer, inner } = extractSubstitutions(seg);\n for (const sub of inner) out.push(...decomposeShell(sub, depth + 1));\n\n const tokens = stripPrefixes(tokenize(outer));\n if (tokens.length === 0) continue; // pure substitution/subshell — inner already handled\n const program = basename(tokens[0]);\n const argv = tokens.slice(1);\n out.push({ program, programRaw: tokens[0], argv, raw: seg, opaque: false });\n\n if (SHELLS.has(program)) {\n const script = shellScriptArg(argv);\n if (script) out.push(...decomposeShell(script, depth + 1));\n }\n }\n return out.length ? out : opaque();\n}\n\n/** Parse a `git … push …` simple command into git.push params, or null if it is\n * not a push. Handles global options that take an argument (`-C dir`, `-c k=v`),\n * `--force`/`-f`/`--force-with-lease`, and a leading `+` force refspec. */\nexport function parseGitPush(cmd: SimpleCommand): { force: boolean; remote?: string; ref?: string } | null {\n if (cmd.program !== \"git\") return null;\n const a = cmd.argv;\n let i = 0;\n while (i < a.length) {\n const t = a[i];\n if (t === \"-C\" || t === \"-c\" || t === \"--git-dir\" || t === \"--work-tree\" || t === \"--namespace\") { i += 2; continue; }\n if (t.startsWith(\"-\")) { i++; continue; }\n break;\n }\n if (a[i] !== \"push\") return null;\n const rest = a.slice(i + 1);\n let force = false;\n const positional: string[] = [];\n for (const t of rest) {\n if (t === \"--force\" || t === \"-f\" || t === \"--force-with-lease\" || t.startsWith(\"--force-with-lease=\")) { force = true; continue; }\n if (t.startsWith(\"-\")) continue;\n positional.push(t);\n }\n const params: { force: boolean; remote?: string; ref?: string } = { force };\n if (positional[0]) params.remote = positional[0];\n if (positional[1]) {\n let refspec = positional[1];\n if (refspec.startsWith(\"+\")) { force = true; params.force = true; refspec = refspec.slice(1); }\n params.ref = refspec.replace(/^[^:]*:\\+?/, \"\"); // src:dst → dst\n }\n return params;\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function hookVersion(): string;
|
|
2
|
+
/** The command the agent harness runs for each tool call: a version-pinned npx
|
|
3
|
+
* invocation so a missing global binary is fetched rather than silently skipped
|
|
4
|
+
* (which a harness can treat as "no hook" and fail open). */
|
|
5
|
+
export declare function hookCommand(harness: "claude" | "cursor"): string;
|
|
6
|
+
//# sourceMappingURL=version.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAOA,wBAAgB,WAAW,IAAI,MAAM,CASpC;AAED;;8DAE8D;AAC9D,wBAAgB,WAAW,CAAC,OAAO,EAAE,QAAQ,GAAG,QAAQ,GAAG,MAAM,CAEhE"}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The installed package version, read from the package's own package.json at
|
|
2
|
+
// runtime. Used to pin the `npx` command the hook installs into the agent config,
|
|
3
|
+
// so the harness always runs the exact version the user set up rather than a
|
|
4
|
+
// floating latest.
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
export function hookVersion() {
|
|
7
|
+
try {
|
|
8
|
+
// dist/version.js → ../package.json is the package root in both the source
|
|
9
|
+
// tree and the published tarball.
|
|
10
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
11
|
+
return typeof pkg.version === "string" ? pkg.version : "latest";
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return "latest";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** The command the agent harness runs for each tool call: a version-pinned npx
|
|
18
|
+
* invocation so a missing global binary is fetched rather than silently skipped
|
|
19
|
+
* (which a harness can treat as "no hook" and fail open). */
|
|
20
|
+
export function hookCommand(harness) {
|
|
21
|
+
return `npx -y @scopebond/hook@${hookVersion()} ${harness}`;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,kFAAkF;AAClF,6EAA6E;AAC7E,mBAAmB;AAEnB,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,MAAM,UAAU,WAAW;IACzB,IAAI,CAAC;QACH,2EAA2E;QAC3E,kCAAkC;QAClC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,GAAG,CAAC,iBAAiB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;QAC1F,OAAO,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC;IAClE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,QAAQ,CAAC;IAClB,CAAC;AACH,CAAC;AAED;;8DAE8D;AAC9D,MAAM,UAAU,WAAW,CAAC,OAA4B;IACtD,OAAO,0BAA0B,WAAW,EAAE,IAAI,OAAO,EAAE,CAAC;AAC9D,CAAC","sourcesContent":["// The installed package version, read from the package's own package.json at\n// runtime. Used to pin the `npx` command the hook installs into the agent config,\n// so the harness always runs the exact version the user set up rather than a\n// floating latest.\n\nimport { readFileSync } from \"node:fs\";\n\nexport function hookVersion(): string {\n try {\n // dist/version.js → ../package.json is the package root in both the source\n // tree and the published tarball.\n const pkg = JSON.parse(readFileSync(new URL(\"../package.json\", import.meta.url), \"utf8\"));\n return typeof pkg.version === \"string\" ? pkg.version : \"latest\";\n } catch {\n return \"latest\";\n }\n}\n\n/** The command the agent harness runs for each tool call: a version-pinned npx\n * invocation so a missing global binary is fetched rather than silently skipped\n * (which a harness can treat as \"no hook\" and fail open). */\nexport function hookCommand(harness: \"claude\" | \"cursor\"): string {\n return `npx -y @scopebond/hook@${hookVersion()} ${harness}`;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scopebond/hook",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Scopebond hook for Claude Code and Cursor — checks each coding-agent tool call against policy before it runs (in-path, cooperative M0) and records a signed receipt, locally.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Avouro LLC",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@scopebond/policy-schema": "^0.3.0",
|
|
44
|
-
"@scopebond/
|
|
45
|
-
"@scopebond/
|
|
44
|
+
"@scopebond/sdk": "^0.1.1",
|
|
45
|
+
"@scopebond/gateway": "^0.6.0"
|
|
46
46
|
},
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|