@odingard/guardian-hook 0.3.0 → 0.3.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 +15 -4
- package/dist/agent-sdk.js +1055 -10
- package/dist/index.js +1055 -10
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -2,6 +2,456 @@
|
|
|
2
2
|
|
|
3
3
|
// src/classify.ts
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
|
+
|
|
6
|
+
// src/shell.ts
|
|
7
|
+
var MAX_NESTING = 16;
|
|
8
|
+
var isBlank = (c) => c === " " || c === " ";
|
|
9
|
+
var OPERATOR_START = /* @__PURE__ */ new Set([";", "&", "|", "\n", "(", ")", "<", ">"]);
|
|
10
|
+
function parseShell(src) {
|
|
11
|
+
const st = {
|
|
12
|
+
src,
|
|
13
|
+
pos: 0,
|
|
14
|
+
ambiguous: false,
|
|
15
|
+
nesting: 0,
|
|
16
|
+
heredocs: [],
|
|
17
|
+
bodySubs: []
|
|
18
|
+
};
|
|
19
|
+
const parsed = parseList(st, false);
|
|
20
|
+
if (st.pos < src.length) st.ambiguous = true;
|
|
21
|
+
if (st.bodySubs.length > 0) {
|
|
22
|
+
parsed.commands.push(bodyCommand(st.bodySubs));
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
commands: parsed.commands,
|
|
26
|
+
ambiguous: st.ambiguous || parsed.ambiguous
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function bodyCommand(subs) {
|
|
30
|
+
return { words: [], writes: [], pipeline: -1, stage: 0, subs };
|
|
31
|
+
}
|
|
32
|
+
function parseList(st, inSub) {
|
|
33
|
+
const commands = [];
|
|
34
|
+
let pipeline = 0;
|
|
35
|
+
let stage = 0;
|
|
36
|
+
let words = [];
|
|
37
|
+
let writes = [];
|
|
38
|
+
let depth = 0;
|
|
39
|
+
let pendingWrite = false;
|
|
40
|
+
let pendingHeredoc = null;
|
|
41
|
+
let pendingHereString = false;
|
|
42
|
+
const endCommand = (nextIsPipe) => {
|
|
43
|
+
if (words.length > 0 || writes.length > 0) {
|
|
44
|
+
commands.push({ words, writes, pipeline, stage, subs: [] });
|
|
45
|
+
}
|
|
46
|
+
words = [];
|
|
47
|
+
writes = [];
|
|
48
|
+
if (nextIsPipe) stage++;
|
|
49
|
+
else {
|
|
50
|
+
pipeline++;
|
|
51
|
+
stage = 0;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const { src } = st;
|
|
55
|
+
while (st.pos < src.length) {
|
|
56
|
+
const c = src[st.pos];
|
|
57
|
+
if (isBlank(c)) {
|
|
58
|
+
st.pos++;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (c === "\\" && src[st.pos + 1] === "\n") {
|
|
62
|
+
st.pos += 2;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (c === "#") {
|
|
66
|
+
while (st.pos < src.length && src[st.pos] !== "\n") st.pos++;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (c === "\n") {
|
|
70
|
+
st.pos++;
|
|
71
|
+
endCommand(false);
|
|
72
|
+
readHeredocBodies(st);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (c === ";" || c === "&" || c === "|") {
|
|
76
|
+
const two = src.slice(st.pos, st.pos + 2);
|
|
77
|
+
if (c === "&" && src[st.pos + 1] === ">") {
|
|
78
|
+
st.pos += src[st.pos + 2] === ">" ? 3 : 2;
|
|
79
|
+
pendingWrite = true;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (two === "&&" || two === "||" || two === ";;" || two === "|&") {
|
|
83
|
+
st.pos += 2;
|
|
84
|
+
endCommand(two === "|&");
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
st.pos++;
|
|
88
|
+
endCommand(c === "|");
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (c === "(") {
|
|
92
|
+
st.pos++;
|
|
93
|
+
depth++;
|
|
94
|
+
endCommand(false);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (c === ")") {
|
|
98
|
+
if (depth > 0) {
|
|
99
|
+
st.pos++;
|
|
100
|
+
depth--;
|
|
101
|
+
endCommand(false);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (inSub) {
|
|
105
|
+
endCommand(false);
|
|
106
|
+
return { commands, ambiguous: false };
|
|
107
|
+
}
|
|
108
|
+
st.pos++;
|
|
109
|
+
st.ambiguous = true;
|
|
110
|
+
endCommand(false);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (c === "<" || c === ">") {
|
|
114
|
+
if (src[st.pos + 1] === "(") {
|
|
115
|
+
const w2 = readWord(st);
|
|
116
|
+
if (w2) addWord(w2);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const op = /^(<<<|<<-|<<|<>|<&|>>|>&|>\||<|>)/.exec(src.slice(st.pos))[0];
|
|
120
|
+
st.pos += op.length;
|
|
121
|
+
if (op === "<<" || op === "<<-") pendingHeredoc = { stripTabs: op === "<<-" };
|
|
122
|
+
else if (op === "<<<") pendingHereString = true;
|
|
123
|
+
else if (op === ">&" || op === "<&") {
|
|
124
|
+
const m = /^[ \t]*([0-9]+|-)(?=[\s;&|)<>]|$)/.exec(src.slice(st.pos));
|
|
125
|
+
if (m) st.pos += m[0].length;
|
|
126
|
+
else if (op === ">&") pendingWrite = true;
|
|
127
|
+
} else if (op !== "<") pendingWrite = true;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const w = readWord(st);
|
|
131
|
+
if (!w) continue;
|
|
132
|
+
addWord(w);
|
|
133
|
+
}
|
|
134
|
+
if (inSub) st.ambiguous = true;
|
|
135
|
+
if (depth > 0) st.ambiguous = true;
|
|
136
|
+
endCommand(false);
|
|
137
|
+
return { commands, ambiguous: false };
|
|
138
|
+
function addWord(w) {
|
|
139
|
+
if (pendingHeredoc) {
|
|
140
|
+
st.heredocs.push({
|
|
141
|
+
delimiter: w.value,
|
|
142
|
+
stripTabs: pendingHeredoc.stripTabs,
|
|
143
|
+
quoted: w.quoted
|
|
144
|
+
});
|
|
145
|
+
pendingHeredoc = null;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (pendingHereString) {
|
|
149
|
+
pendingHereString = false;
|
|
150
|
+
collectSubs(w);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (pendingWrite) {
|
|
154
|
+
pendingWrite = false;
|
|
155
|
+
writes.push(w);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const next = src[st.pos];
|
|
159
|
+
if (/^[0-9]+$/.test(w.value) && !w.quoted && (next === ">" || next === "<")) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
words.push(w);
|
|
163
|
+
}
|
|
164
|
+
function collectSubs(w) {
|
|
165
|
+
if (w.subs.length > 0) {
|
|
166
|
+
commands.push({ words: [], writes: [], pipeline: -1, stage: 0, subs: w.subs });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function readHeredocBodies(st) {
|
|
171
|
+
while (st.heredocs.length > 0) {
|
|
172
|
+
const doc = st.heredocs.shift();
|
|
173
|
+
const lines = [];
|
|
174
|
+
let found = false;
|
|
175
|
+
while (st.pos < st.src.length) {
|
|
176
|
+
let end = st.src.indexOf("\n", st.pos);
|
|
177
|
+
if (end < 0) end = st.src.length;
|
|
178
|
+
const raw = st.src.slice(st.pos, end);
|
|
179
|
+
st.pos = Math.min(end + 1, st.src.length);
|
|
180
|
+
const line = doc.stripTabs ? raw.replace(/^\t+/, "") : raw;
|
|
181
|
+
if (line === doc.delimiter) {
|
|
182
|
+
found = true;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
lines.push(raw);
|
|
186
|
+
}
|
|
187
|
+
void found;
|
|
188
|
+
if (!doc.quoted) {
|
|
189
|
+
const body = lines.join("\n");
|
|
190
|
+
if (/\$\(|`/.test(body)) {
|
|
191
|
+
const inner = { ...st, src: body, pos: 0, heredocs: [], bodySubs: [] };
|
|
192
|
+
const w = readDoubleQuotedBody(inner);
|
|
193
|
+
st.ambiguous ||= inner.ambiguous;
|
|
194
|
+
if (w.subs.length > 0) st.bodySubs.push(...w.subs);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function readDoubleQuotedBody(st) {
|
|
200
|
+
const word = { value: "", dynamic: false, subs: [] };
|
|
201
|
+
while (st.pos < st.src.length) {
|
|
202
|
+
const c = st.src[st.pos];
|
|
203
|
+
if (c === "\\") {
|
|
204
|
+
st.pos += 2;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (c === "$" || c === "`") {
|
|
208
|
+
readExpansion(st, word);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
st.pos++;
|
|
212
|
+
}
|
|
213
|
+
return word;
|
|
214
|
+
}
|
|
215
|
+
function readWord(st) {
|
|
216
|
+
const { src } = st;
|
|
217
|
+
const word = {
|
|
218
|
+
value: "",
|
|
219
|
+
dynamic: false,
|
|
220
|
+
subs: [],
|
|
221
|
+
quoted: false
|
|
222
|
+
};
|
|
223
|
+
const start = st.pos;
|
|
224
|
+
while (st.pos < src.length) {
|
|
225
|
+
const c = src[st.pos];
|
|
226
|
+
if (isBlank(c) || c === "\n") break;
|
|
227
|
+
if ((c === "<" || c === ">") && src[st.pos + 1] === "(") {
|
|
228
|
+
st.pos += 2;
|
|
229
|
+
word.dynamic = true;
|
|
230
|
+
word.subs.push(parseNested(st));
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (OPERATOR_START.has(c)) break;
|
|
234
|
+
if (c === "\\") {
|
|
235
|
+
if (st.pos + 1 < src.length) {
|
|
236
|
+
if (src[st.pos + 1] !== "\n") word.value += src[st.pos + 1];
|
|
237
|
+
st.pos += 2;
|
|
238
|
+
} else {
|
|
239
|
+
st.pos++;
|
|
240
|
+
}
|
|
241
|
+
word.quoted = true;
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (c === "'") {
|
|
245
|
+
const end = src.indexOf("'", st.pos + 1);
|
|
246
|
+
if (end < 0) {
|
|
247
|
+
st.ambiguous = true;
|
|
248
|
+
word.value += src.slice(st.pos + 1);
|
|
249
|
+
st.pos = src.length;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
word.value += src.slice(st.pos + 1, end);
|
|
253
|
+
st.pos = end + 1;
|
|
254
|
+
word.quoted = true;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (c === '"') {
|
|
258
|
+
st.pos++;
|
|
259
|
+
word.quoted = true;
|
|
260
|
+
let closed = false;
|
|
261
|
+
while (st.pos < src.length) {
|
|
262
|
+
const d = src[st.pos];
|
|
263
|
+
if (d === '"') {
|
|
264
|
+
st.pos++;
|
|
265
|
+
closed = true;
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
if (d === "\\") {
|
|
269
|
+
const n = src[st.pos + 1];
|
|
270
|
+
if (n === void 0) {
|
|
271
|
+
st.pos++;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if ('$`"\\\n'.includes(n)) {
|
|
275
|
+
if (n !== "\n") word.value += n;
|
|
276
|
+
} else {
|
|
277
|
+
word.value += d + n;
|
|
278
|
+
}
|
|
279
|
+
st.pos += 2;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (d === "$" || d === "`") {
|
|
283
|
+
readExpansion(st, word);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
word.value += d;
|
|
287
|
+
st.pos++;
|
|
288
|
+
}
|
|
289
|
+
if (!closed) st.ambiguous = true;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (c === "$" && src[st.pos + 1] === "'") {
|
|
293
|
+
let i = st.pos + 2;
|
|
294
|
+
let out = "";
|
|
295
|
+
let closed = false;
|
|
296
|
+
while (i < src.length) {
|
|
297
|
+
const d = src[i];
|
|
298
|
+
if (d === "'") {
|
|
299
|
+
closed = true;
|
|
300
|
+
i++;
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
if (d === "\\" && i + 1 < src.length) {
|
|
304
|
+
const n = src[i + 1];
|
|
305
|
+
const simple = { n: "\n", t: " ", "\\": "\\", "'": "'", '"': '"' };
|
|
306
|
+
if (simple[n] !== void 0) {
|
|
307
|
+
out += simple[n];
|
|
308
|
+
i += 2;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const hex = /^x([0-9a-fA-F]{1,2})/.exec(src.slice(i + 1));
|
|
312
|
+
if (hex) {
|
|
313
|
+
out += String.fromCharCode(parseInt(hex[1], 16));
|
|
314
|
+
i += 1 + hex[0].length;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const oct = /^([0-7]{1,3})/.exec(src.slice(i + 1));
|
|
318
|
+
if (oct) {
|
|
319
|
+
out += String.fromCharCode(parseInt(oct[1], 8));
|
|
320
|
+
i += 1 + oct[0].length;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
st.ambiguous = true;
|
|
324
|
+
out += n;
|
|
325
|
+
i += 2;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
out += d;
|
|
329
|
+
i++;
|
|
330
|
+
}
|
|
331
|
+
if (!closed) st.ambiguous = true;
|
|
332
|
+
word.value += out;
|
|
333
|
+
word.quoted = true;
|
|
334
|
+
st.pos = i;
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (c === "$" || c === "`") {
|
|
338
|
+
readExpansion(st, word);
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
word.value += c;
|
|
342
|
+
st.pos++;
|
|
343
|
+
}
|
|
344
|
+
return st.pos > start ? word : null;
|
|
345
|
+
}
|
|
346
|
+
function readExpansion(st, word) {
|
|
347
|
+
const { src } = st;
|
|
348
|
+
const c = src[st.pos];
|
|
349
|
+
if (c === "`") {
|
|
350
|
+
let i = st.pos + 1;
|
|
351
|
+
let inner = "";
|
|
352
|
+
while (i < src.length && src[i] !== "`") {
|
|
353
|
+
if (src[i] === "\\" && i + 1 < src.length) {
|
|
354
|
+
inner += src[i + 1];
|
|
355
|
+
i += 2;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
inner += src[i];
|
|
359
|
+
i++;
|
|
360
|
+
}
|
|
361
|
+
if (i >= src.length) st.ambiguous = true;
|
|
362
|
+
word.value += "`" + inner + "`";
|
|
363
|
+
word.dynamic = true;
|
|
364
|
+
word.subs.push(parseString(st, inner));
|
|
365
|
+
st.pos = Math.min(i + 1, src.length);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const n = src[st.pos + 1];
|
|
369
|
+
if (n === "(" && src[st.pos + 2] === "(") {
|
|
370
|
+
let i = st.pos + 3;
|
|
371
|
+
let level = 2;
|
|
372
|
+
while (i < src.length && level > 0) {
|
|
373
|
+
if (src[i] === "(") level++;
|
|
374
|
+
else if (src[i] === ")") level--;
|
|
375
|
+
i++;
|
|
376
|
+
}
|
|
377
|
+
if (level > 0) st.ambiguous = true;
|
|
378
|
+
word.value += src.slice(st.pos, i);
|
|
379
|
+
word.dynamic = true;
|
|
380
|
+
st.pos = i;
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (n === "(") {
|
|
384
|
+
const start = st.pos;
|
|
385
|
+
st.pos += 2;
|
|
386
|
+
word.subs.push(parseNested(st));
|
|
387
|
+
word.value += src.slice(start, st.pos);
|
|
388
|
+
word.dynamic = true;
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (n === "{") {
|
|
392
|
+
let i = st.pos + 2;
|
|
393
|
+
let level = 1;
|
|
394
|
+
while (i < src.length && level > 0) {
|
|
395
|
+
if (src[i] === "{") level++;
|
|
396
|
+
else if (src[i] === "}") level--;
|
|
397
|
+
i++;
|
|
398
|
+
}
|
|
399
|
+
if (level > 0) st.ambiguous = true;
|
|
400
|
+
const text = src.slice(st.pos, i);
|
|
401
|
+
word.value += text;
|
|
402
|
+
word.dynamic = true;
|
|
403
|
+
if (/\$\(|`/.test(text)) {
|
|
404
|
+
word.subs.push(parseString(st, text.slice(2, -1)));
|
|
405
|
+
}
|
|
406
|
+
st.pos = i;
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const name = /^\$([A-Za-z_][A-Za-z0-9_]*|[0-9@*#?$!-])/.exec(src.slice(st.pos));
|
|
410
|
+
if (name) {
|
|
411
|
+
word.value += name[0];
|
|
412
|
+
word.dynamic = true;
|
|
413
|
+
st.pos += name[0].length;
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
word.value += "$";
|
|
417
|
+
st.pos++;
|
|
418
|
+
}
|
|
419
|
+
function parseNested(st) {
|
|
420
|
+
if (st.nesting >= MAX_NESTING) {
|
|
421
|
+
st.ambiguous = true;
|
|
422
|
+
st.pos = st.src.length;
|
|
423
|
+
return { commands: [], ambiguous: true };
|
|
424
|
+
}
|
|
425
|
+
st.nesting++;
|
|
426
|
+
const savedHeredocs = st.heredocs;
|
|
427
|
+
st.heredocs = [];
|
|
428
|
+
const inner = parseList(st, true);
|
|
429
|
+
st.heredocs = savedHeredocs;
|
|
430
|
+
st.nesting--;
|
|
431
|
+
if (st.src[st.pos] === ")") st.pos++;
|
|
432
|
+
else st.ambiguous = true;
|
|
433
|
+
return inner;
|
|
434
|
+
}
|
|
435
|
+
function parseString(st, text) {
|
|
436
|
+
if (st.nesting >= MAX_NESTING) {
|
|
437
|
+
st.ambiguous = true;
|
|
438
|
+
return { commands: [], ambiguous: true };
|
|
439
|
+
}
|
|
440
|
+
const inner = {
|
|
441
|
+
src: text,
|
|
442
|
+
pos: 0,
|
|
443
|
+
ambiguous: false,
|
|
444
|
+
nesting: st.nesting + 1,
|
|
445
|
+
heredocs: [],
|
|
446
|
+
bodySubs: []
|
|
447
|
+
};
|
|
448
|
+
const parsed = parseList(inner, false);
|
|
449
|
+
if (inner.bodySubs.length) parsed.commands.push(bodyCommand(inner.bodySubs));
|
|
450
|
+
st.ambiguous ||= inner.ambiguous;
|
|
451
|
+
return { commands: parsed.commands, ambiguous: inner.ambiguous };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// src/classify.ts
|
|
5
455
|
var HOOK_TOOLS = [
|
|
6
456
|
{
|
|
7
457
|
name: "shell",
|
|
@@ -16,7 +466,7 @@ var HOOK_TOOLS = [
|
|
|
16
466
|
{
|
|
17
467
|
name: "shell_destructive",
|
|
18
468
|
suggestedMode: "ask",
|
|
19
|
-
description: "Destructive commands: rm -rf, git reset --hard, git clean, force pushes, dropping databases, disk formatting."
|
|
469
|
+
description: "Destructive commands: rm -rf (also through xargs or find -exec), git reset --hard, git clean, force pushes and remote branch deletes, dropping databases, disk formatting, piping a download into a shell. Also changes to the agent's own MCP servers, hooks or permissions (claude mcp add, writes to .mcp.json or .claude/settings.json), and commands the hook cannot parse."
|
|
20
470
|
},
|
|
21
471
|
{
|
|
22
472
|
name: "git_push",
|
|
@@ -67,6 +517,9 @@ var DESTRUCTIVE = [
|
|
|
67
517
|
/\b(drop|truncate)\s+(table|database|schema)\b/i,
|
|
68
518
|
/\bdelete\s+from\s+\w+\s*(;|$)/i,
|
|
69
519
|
/\b(mkfs|dd\s+if=|shred|wipefs)\b/,
|
|
520
|
+
/\bdd\b.*\bof=/,
|
|
521
|
+
/\brsync\b.*\s--(delete\S*|remove-source-files)\b/,
|
|
522
|
+
/\btruncate\b.*(\s-s\s*0\b|\s--size[= ]0\b)/,
|
|
70
523
|
/\bchmod\s+-R\s+777\b/,
|
|
71
524
|
/\bfind\b.*\s-delete\b/
|
|
72
525
|
];
|
|
@@ -162,12 +615,599 @@ function startsInteractiveSession(command) {
|
|
|
162
615
|
if (/\bwhile\s+(?:IFS=\S*\s+)?read\b/.test(command)) return true;
|
|
163
616
|
return command.split(/&&|\|\||[;&|\n()]|\$\(|`/).some((segment) => startsSession(segment));
|
|
164
617
|
}
|
|
618
|
+
var RANK = {
|
|
619
|
+
shell: 0,
|
|
620
|
+
shell_session: 1,
|
|
621
|
+
git_push: 2,
|
|
622
|
+
shell_deploy: 3,
|
|
623
|
+
shell_destructive: 4
|
|
624
|
+
};
|
|
625
|
+
var BY_RANK = [
|
|
626
|
+
"shell",
|
|
627
|
+
"shell_session",
|
|
628
|
+
"git_push",
|
|
629
|
+
"shell_deploy",
|
|
630
|
+
"shell_destructive"
|
|
631
|
+
];
|
|
632
|
+
var ASK = RANK.shell_destructive;
|
|
633
|
+
var MAX_DEPTH = 12;
|
|
634
|
+
var EDGE = String.raw`(?:^|[\s/\\'"=:,(~])`;
|
|
635
|
+
var AGENT_CONFIG_PATH = new RegExp(
|
|
636
|
+
[
|
|
637
|
+
EDGE + String.raw`(?:\.?mcp\.json|\.claude\.json|settings\.local\.json|managed-settings\.json|managed-mcp\.json|claude_desktop_config\.json|guardian-hooks\.json|cordis\.patch\.ya?ml)(?![\w-])`,
|
|
638
|
+
EDGE + String.raw`\.claude(?:[/\\](?:settings[^/\\\s'"]*\.json|hooks|agents|commands|skills|plugins|output-styles)(?![\w-])|[/\\]?(?=$|[\s'"]))`,
|
|
639
|
+
EDGE + String.raw`\.(?:codex|cursor|gemini|dsh)(?=$|[/\\\s'"])`
|
|
640
|
+
].join("|"),
|
|
641
|
+
"i"
|
|
642
|
+
);
|
|
643
|
+
var READERS = new Set(
|
|
644
|
+
"cat tac less more head tail grep egrep fgrep zgrep rg ag ack ls dir stat file wc diff cmp comm jq bat batcat md5sum sha1sum sha256sum sha512sum shasum b2sum cksum echo printf test [ [[ find fd realpath readlink dirname basename du tree type which".split(" ")
|
|
645
|
+
);
|
|
646
|
+
var GIT_READS = new Set(
|
|
647
|
+
"status log show diff blame ls-files grep add commit check-ignore cat-file rev-parse ls-tree shortlog describe".split(" ")
|
|
648
|
+
);
|
|
649
|
+
var RESERVED = new Set(
|
|
650
|
+
"if then else elif fi do done while until for in case esac ! { } select function".split(
|
|
651
|
+
" "
|
|
652
|
+
)
|
|
653
|
+
);
|
|
654
|
+
var ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*(\[[^\]]*\])?\+?=/;
|
|
655
|
+
var SHELLS = new Set(
|
|
656
|
+
"sh bash zsh dash ksh mksh ash yash posh fish busybox".split(" ")
|
|
657
|
+
);
|
|
658
|
+
var FETCHERS = new Set(
|
|
659
|
+
"curl wget fetch http https xh aria2c iwr irm invoke-webrequest invoke-restmethod".split(
|
|
660
|
+
" "
|
|
661
|
+
)
|
|
662
|
+
);
|
|
663
|
+
var BULK_DELETE = /* @__PURE__ */ new Set(["rm", "unlink", "shred"]);
|
|
664
|
+
var EVALUATORS = /* @__PURE__ */ new Set(["eval", "source", ".", "iex", "invoke-expression"]);
|
|
665
|
+
var WRAPPER_SPECS = {
|
|
666
|
+
nohup: {},
|
|
667
|
+
command: {},
|
|
668
|
+
builtin: {},
|
|
669
|
+
chronic: {},
|
|
670
|
+
unbuffer: {},
|
|
671
|
+
busybox: {},
|
|
672
|
+
time: { value: ["-f", "-o", "--format", "--output"] },
|
|
673
|
+
nice: { value: ["-n", "--adjustment"] },
|
|
674
|
+
ionice: { value: ["-c", "-n", "-p", "-P", "-u", "--class", "--classdata"] },
|
|
675
|
+
stdbuf: { value: ["-i", "-o", "-e", "--input", "--output", "--error"] },
|
|
676
|
+
timeout: { value: ["-s", "-k", "--signal", "--kill-after"], positional: 1 },
|
|
677
|
+
exec: { value: ["-a"] },
|
|
678
|
+
sudo: {
|
|
679
|
+
value: [
|
|
680
|
+
...SUDO_VALUE_OPTS,
|
|
681
|
+
"--user",
|
|
682
|
+
"--group",
|
|
683
|
+
"--host",
|
|
684
|
+
"--prompt",
|
|
685
|
+
"--chdir",
|
|
686
|
+
"--role",
|
|
687
|
+
"--type",
|
|
688
|
+
"--close-from",
|
|
689
|
+
"--other-user",
|
|
690
|
+
"--command-timeout"
|
|
691
|
+
]
|
|
692
|
+
},
|
|
693
|
+
doas: { value: ["-u", "-C"] },
|
|
694
|
+
xargs: {
|
|
695
|
+
value: [
|
|
696
|
+
"-a",
|
|
697
|
+
"--arg-file",
|
|
698
|
+
"-d",
|
|
699
|
+
"--delimiter",
|
|
700
|
+
"-E",
|
|
701
|
+
"-I",
|
|
702
|
+
"-L",
|
|
703
|
+
"-n",
|
|
704
|
+
"--max-args",
|
|
705
|
+
"-P",
|
|
706
|
+
"--max-procs",
|
|
707
|
+
"-s",
|
|
708
|
+
"--max-chars",
|
|
709
|
+
"--process-slot-var",
|
|
710
|
+
"--max-lines"
|
|
711
|
+
]
|
|
712
|
+
},
|
|
713
|
+
chroot: { value: ["--userspec", "--groups"], positional: 1 },
|
|
714
|
+
taskset: { positional: 1 },
|
|
715
|
+
flock: {
|
|
716
|
+
value: ["-w", "-E", "--timeout", "--conflict-exit-code"],
|
|
717
|
+
positional: 1
|
|
718
|
+
},
|
|
719
|
+
setsid: {},
|
|
720
|
+
caffeinate: { value: ["-t", "-w"] }
|
|
721
|
+
};
|
|
722
|
+
var leaf = (rank, base = "", args = []) => ({
|
|
723
|
+
rank,
|
|
724
|
+
base,
|
|
725
|
+
args
|
|
726
|
+
});
|
|
727
|
+
function baseName(value) {
|
|
728
|
+
return (value.split(/[\\/]/).pop() ?? "").toLowerCase().replace(/\.exe$/, "");
|
|
729
|
+
}
|
|
730
|
+
var literal = (value) => ({ value, dynamic: false, subs: [] });
|
|
731
|
+
function skipOptions(args, spec) {
|
|
732
|
+
const value = new Set(spec.value ?? []);
|
|
733
|
+
let i = 0;
|
|
734
|
+
let unknown = false;
|
|
735
|
+
while (i < args.length) {
|
|
736
|
+
const a = args[i].value;
|
|
737
|
+
if (a === "--") {
|
|
738
|
+
i++;
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
if (a.startsWith("--")) {
|
|
742
|
+
if (a.includes("=")) i++;
|
|
743
|
+
else if (value.has(a)) i += 2;
|
|
744
|
+
else {
|
|
745
|
+
unknown = true;
|
|
746
|
+
i++;
|
|
747
|
+
}
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (a.startsWith("-") && a.length > 1) {
|
|
751
|
+
let takes = false;
|
|
752
|
+
for (let k = 1; k < a.length; k++) {
|
|
753
|
+
if (value.has(`-${a[k]}`)) {
|
|
754
|
+
takes = k === a.length - 1;
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
i += takes ? 2 : 1;
|
|
759
|
+
continue;
|
|
760
|
+
}
|
|
761
|
+
break;
|
|
762
|
+
}
|
|
763
|
+
let positional = spec.positional ?? 0;
|
|
764
|
+
while (positional-- > 0 && i < args.length) i++;
|
|
765
|
+
return { start: i, unknown };
|
|
766
|
+
}
|
|
767
|
+
function rankText(text, depth) {
|
|
768
|
+
if (depth > MAX_DEPTH) return ASK;
|
|
769
|
+
return rankParsed(parseShell(text), depth + 1);
|
|
770
|
+
}
|
|
771
|
+
function rankParsed(parsed, depth) {
|
|
772
|
+
if (depth > MAX_DEPTH) return ASK;
|
|
773
|
+
let rank = parsed.ambiguous ? ASK : 0;
|
|
774
|
+
const stages = /* @__PURE__ */ new Map();
|
|
775
|
+
for (const cmd of parsed.commands) {
|
|
776
|
+
const l = rankArgv(cmd.words, depth);
|
|
777
|
+
rank = Math.max(rank, l.rank);
|
|
778
|
+
if (cmd.writes.some((w) => AGENT_CONFIG_PATH.test(w.value))) rank = ASK;
|
|
779
|
+
const subs = [
|
|
780
|
+
...cmd.subs,
|
|
781
|
+
...cmd.words.flatMap((w) => w.subs),
|
|
782
|
+
...cmd.writes.flatMap((w) => w.subs)
|
|
783
|
+
];
|
|
784
|
+
for (const sub of subs) rank = Math.max(rank, rankParsed(sub, depth + 1));
|
|
785
|
+
if (runsCode(l) && cmd.words.some((w) => w.subs.some((s) => fetches(s, depth + 1)))) {
|
|
786
|
+
rank = ASK;
|
|
787
|
+
}
|
|
788
|
+
if (cmd.pipeline >= 0) {
|
|
789
|
+
const list = stages.get(cmd.pipeline) ?? [];
|
|
790
|
+
list.push(l);
|
|
791
|
+
stages.set(cmd.pipeline, list);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
for (const list of stages.values()) {
|
|
795
|
+
const first = list.findIndex((l) => FETCHERS.has(l.base));
|
|
796
|
+
if (first >= 0 && list.slice(first + 1).some((l) => readsCodeFromStdin(l))) {
|
|
797
|
+
rank = ASK;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
return rank;
|
|
801
|
+
}
|
|
802
|
+
function fetches(parsed, depth) {
|
|
803
|
+
return parsed.commands.some(
|
|
804
|
+
(c) => FETCHERS.has(rankArgv(c.words, depth).base) || [...c.subs, ...c.words.flatMap((w) => w.subs)].some(
|
|
805
|
+
(s) => fetches(s, depth + 1)
|
|
806
|
+
)
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
var isInterpreter = (base) => INTERPRETERS.has(base) || /^python[0-9.]*$/.test(base);
|
|
810
|
+
function runsCode(l) {
|
|
811
|
+
return EVALUATORS.has(l.base) || isInterpreter(l.base);
|
|
812
|
+
}
|
|
813
|
+
function readsCodeFromStdin(l) {
|
|
814
|
+
if (EVALUATORS.has(l.base)) return true;
|
|
815
|
+
if (!isInterpreter(l.base)) return false;
|
|
816
|
+
const options = [];
|
|
817
|
+
let script = false;
|
|
818
|
+
for (const a of l.args) {
|
|
819
|
+
if (a === "--") break;
|
|
820
|
+
if (a === "-") return true;
|
|
821
|
+
if (a.startsWith("-") || a.startsWith("+")) options.push(a);
|
|
822
|
+
else {
|
|
823
|
+
script = true;
|
|
824
|
+
break;
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
if (options.some((a) => /^-[a-zA-Z]*s[a-zA-Z]*$/.test(a))) return true;
|
|
828
|
+
if (options.some((a) => /^-[a-zA-Z]*[cem][a-zA-Z]*$/.test(a))) return false;
|
|
829
|
+
return !script;
|
|
830
|
+
}
|
|
831
|
+
function rankArgv(words, depth) {
|
|
832
|
+
if (depth > MAX_DEPTH) return leaf(ASK);
|
|
833
|
+
let i = 0;
|
|
834
|
+
while (i < words.length && (ASSIGNMENT.test(words[i].value) || RESERVED.has(words[i].value))) {
|
|
835
|
+
i++;
|
|
836
|
+
}
|
|
837
|
+
if (i >= words.length) return leaf(0);
|
|
838
|
+
const head = words[i];
|
|
839
|
+
const argWords = words.slice(i + 1);
|
|
840
|
+
if (head.dynamic) return leaf(ASK);
|
|
841
|
+
const base = baseName(head.value);
|
|
842
|
+
const args = argWords.map((w) => w.value);
|
|
843
|
+
const joined = [base, ...args].join(" ");
|
|
844
|
+
let rank = 0;
|
|
845
|
+
if (DESTRUCTIVE.some((re) => re.test(joined))) rank = ASK;
|
|
846
|
+
else if (DEPLOY.some((re) => re.test(joined))) rank = RANK.shell_deploy;
|
|
847
|
+
const done = (r, b = base, a = args) => leaf(Math.max(rank, r), b, a);
|
|
848
|
+
const inner = (from) => rankArgv(from, depth + 1);
|
|
849
|
+
const pathCheck = (values) => values.some((v) => AGENT_CONFIG_PATH.test(v)) ? ASK : 0;
|
|
850
|
+
if (base === "env") return done(rankEnv(argWords, depth));
|
|
851
|
+
if (base === "sudoedit") return done(pathCheck(args));
|
|
852
|
+
if (Object.hasOwn(WRAPPER_SPECS, base)) {
|
|
853
|
+
if (base === "sudo" && args.some((a) => /^-[a-zA-Z]*e[a-zA-Z]*$/.test(a))) {
|
|
854
|
+
return done(pathCheck(args));
|
|
855
|
+
}
|
|
856
|
+
if (base === "command" && args.some((a) => /^-[a-zA-Z]*[vV]/.test(a))) {
|
|
857
|
+
return done(0);
|
|
858
|
+
}
|
|
859
|
+
if (base === "flock") {
|
|
860
|
+
const c = args.findIndex((a) => a === "-c" || a === "--command");
|
|
861
|
+
if (c >= 0) return done(rankText(args[c + 1] ?? "", depth));
|
|
862
|
+
}
|
|
863
|
+
const { start, unknown } = skipOptions(argWords, WRAPPER_SPECS[base]);
|
|
864
|
+
let rest = argWords.slice(start);
|
|
865
|
+
if (base === "xargs" && rest.length === 0) rest = [literal("echo")];
|
|
866
|
+
const l = inner(rest);
|
|
867
|
+
if (base === "xargs" && BULK_DELETE.has(l.base)) l.rank = ASK;
|
|
868
|
+
const alt = unknown ? inner(argWords.slice(start + 1)).rank : 0;
|
|
869
|
+
return leaf(Math.max(rank, l.rank, alt), l.base, l.args);
|
|
870
|
+
}
|
|
871
|
+
if (base === "watch") {
|
|
872
|
+
const { start } = skipOptions(argWords, {
|
|
873
|
+
value: ["-n", "--interval", "-d", "-q", "--equexit"]
|
|
874
|
+
});
|
|
875
|
+
return done(rankText(args.slice(start).join(" "), depth));
|
|
876
|
+
}
|
|
877
|
+
if (SHELLS.has(base)) {
|
|
878
|
+
return done(rankShell(base, argWords, depth));
|
|
879
|
+
}
|
|
880
|
+
if (base === "su") {
|
|
881
|
+
const c = args.findIndex((a) => /^(-c|--command)$/.test(a));
|
|
882
|
+
const eq = args.find((a) => a.startsWith("--command="));
|
|
883
|
+
if (c >= 0) return done(rankText(args[c + 1] ?? "", depth));
|
|
884
|
+
if (eq) return done(rankText(eq.slice("--command=".length), depth));
|
|
885
|
+
return done(0);
|
|
886
|
+
}
|
|
887
|
+
if (base === "eval" || base === "iex" || base === "invoke-expression") {
|
|
888
|
+
return done(rankText(args.join(" "), depth));
|
|
889
|
+
}
|
|
890
|
+
if (base === "find") return done(rankFind(argWords, depth));
|
|
891
|
+
if (["npx", "bunx", "pnpx", "npm", "pnpm", "yarn"].includes(base)) {
|
|
892
|
+
const l = rankRunner(base, argWords, depth);
|
|
893
|
+
if (l) return leaf(Math.max(rank, l.rank), l.base, l.args);
|
|
894
|
+
return done(0);
|
|
895
|
+
}
|
|
896
|
+
if (base === "rm") {
|
|
897
|
+
const recursive = argsBeforeDashDash(args).some(
|
|
898
|
+
(a) => a === "--recursive" || /^-[a-zA-Z]*[rR]/.test(a)
|
|
899
|
+
);
|
|
900
|
+
return done(Math.max(recursive ? ASK : 0, pathCheck(args)));
|
|
901
|
+
}
|
|
902
|
+
if (base === "chmod") return done(Math.max(rankChmod(args), pathCheck(args)));
|
|
903
|
+
if (base === "git") return done(rankGit(argWords, depth));
|
|
904
|
+
if (base === "claude" || base === "codex" || base === "gemini") {
|
|
905
|
+
return done(rankAgentCli(base, args));
|
|
906
|
+
}
|
|
907
|
+
if (READERS.has(base)) return done(0);
|
|
908
|
+
return done(pathCheck(args));
|
|
909
|
+
}
|
|
910
|
+
function argsBeforeDashDash(args) {
|
|
911
|
+
const end = args.indexOf("--");
|
|
912
|
+
return end < 0 ? args : args.slice(0, end);
|
|
913
|
+
}
|
|
914
|
+
function rankEnv(argWords, depth) {
|
|
915
|
+
let i = 0;
|
|
916
|
+
while (i < argWords.length) {
|
|
917
|
+
const a = argWords[i].value;
|
|
918
|
+
if (a === "--") {
|
|
919
|
+
i++;
|
|
920
|
+
break;
|
|
921
|
+
}
|
|
922
|
+
if (a === "-S" || a === "--split-string") {
|
|
923
|
+
const text = [a, ...argWords.slice(i + 1).map((w) => w.value)].slice(1).join(" ");
|
|
924
|
+
return rankText(text, depth);
|
|
925
|
+
}
|
|
926
|
+
if (a.startsWith("--split-string=") || /^-S./.test(a)) {
|
|
927
|
+
const first = a.startsWith("--") ? a.slice(15) : a.slice(2);
|
|
928
|
+
return rankText(
|
|
929
|
+
[first, ...argWords.slice(i + 1).map((w) => w.value)].join(" "),
|
|
930
|
+
depth
|
|
931
|
+
);
|
|
932
|
+
}
|
|
933
|
+
if (/^(-u|-C|-P|--unset|--chdir)$/.test(a)) {
|
|
934
|
+
i += 2;
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
if (a.startsWith("-") && a.length > 1) {
|
|
938
|
+
i++;
|
|
939
|
+
continue;
|
|
940
|
+
}
|
|
941
|
+
break;
|
|
942
|
+
}
|
|
943
|
+
return rankArgv(argWords.slice(i), depth + 1).rank;
|
|
944
|
+
}
|
|
945
|
+
function rankShell(base, argWords, depth) {
|
|
946
|
+
let code = false;
|
|
947
|
+
for (let i = 0; i < argWords.length; i++) {
|
|
948
|
+
const a = argWords[i].value;
|
|
949
|
+
if (a === "--") break;
|
|
950
|
+
if (/^(--rcfile|--init-file|-o|\+o|-O|\+O)$/.test(a)) {
|
|
951
|
+
i++;
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
if (a === "--command" || base === "fish" && a === "-c") {
|
|
955
|
+
return rankText(argWords[i + 1]?.value ?? "", depth);
|
|
956
|
+
}
|
|
957
|
+
if (/^[-+][a-zA-Z]+$/.test(a)) {
|
|
958
|
+
if (/^-[a-zA-Z]*c/.test(a)) code = true;
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (a.startsWith("-")) continue;
|
|
962
|
+
if (code) {
|
|
963
|
+
if (base === "busybox")
|
|
964
|
+
return rankArgv(argWords.slice(i), depth + 1).rank;
|
|
965
|
+
return rankText(a, depth);
|
|
966
|
+
}
|
|
967
|
+
if (base === "busybox") return rankArgv(argWords.slice(i), depth + 1).rank;
|
|
968
|
+
return 0;
|
|
969
|
+
}
|
|
970
|
+
return 0;
|
|
971
|
+
}
|
|
972
|
+
function rankFind(argWords, depth) {
|
|
973
|
+
let rank = 0;
|
|
974
|
+
for (let i = 0; i < argWords.length; i++) {
|
|
975
|
+
const a = argWords[i].value;
|
|
976
|
+
if (a === "-delete") rank = ASK;
|
|
977
|
+
if (/^-(exec|execdir|ok|okdir)$/.test(a)) {
|
|
978
|
+
let end = i + 1;
|
|
979
|
+
while (end < argWords.length && argWords[end].value !== ";" && argWords[end].value !== "+") {
|
|
980
|
+
end++;
|
|
981
|
+
}
|
|
982
|
+
const l = rankArgv(argWords.slice(i + 1, end), depth + 1);
|
|
983
|
+
rank = Math.max(rank, BULK_DELETE.has(l.base) ? ASK : l.rank);
|
|
984
|
+
i = end;
|
|
985
|
+
}
|
|
986
|
+
if (/^-f(print|print0|printf|ls)$/.test(a)) {
|
|
987
|
+
if (AGENT_CONFIG_PATH.test(argWords[i + 1]?.value ?? "")) rank = ASK;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
return rank;
|
|
991
|
+
}
|
|
992
|
+
function rankRunner(base, argWords, depth) {
|
|
993
|
+
let rest = argWords;
|
|
994
|
+
if (base === "npm" || base === "pnpm" || base === "yarn") {
|
|
995
|
+
const sub = rest[0]?.value;
|
|
996
|
+
if (!["exec", "x", "dlx"].includes(sub ?? "")) return null;
|
|
997
|
+
rest = rest.slice(1);
|
|
998
|
+
}
|
|
999
|
+
let i = 0;
|
|
1000
|
+
while (i < rest.length) {
|
|
1001
|
+
const a = rest[i].value;
|
|
1002
|
+
if (a === "--") {
|
|
1003
|
+
i++;
|
|
1004
|
+
break;
|
|
1005
|
+
}
|
|
1006
|
+
if (a === "-c" || a === "--call")
|
|
1007
|
+
return leaf(rankText(rest[i + 1]?.value ?? "", depth));
|
|
1008
|
+
if (a.startsWith("--call=")) return leaf(rankText(a.slice(7), depth));
|
|
1009
|
+
if (a === "-p" || a === "--package") {
|
|
1010
|
+
i += 2;
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
if (a.startsWith("-")) {
|
|
1014
|
+
i++;
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
1017
|
+
break;
|
|
1018
|
+
}
|
|
1019
|
+
const pkg = rest[i];
|
|
1020
|
+
if (!pkg) return null;
|
|
1021
|
+
if (pkg.dynamic) return leaf(ASK);
|
|
1022
|
+
const name = /^(?:@[^/]+\/)?([^@]+)(?:@.*)?$/.exec(pkg.value)?.[1] ?? pkg.value;
|
|
1023
|
+
const bin = name === "claude-code" ? "claude" : name;
|
|
1024
|
+
return rankArgv([literal(bin), ...rest.slice(i + 1)], depth + 1);
|
|
1025
|
+
}
|
|
1026
|
+
function rankChmod(args) {
|
|
1027
|
+
const opts = argsBeforeDashDash(args);
|
|
1028
|
+
const recursive = opts.some(
|
|
1029
|
+
(a) => a === "--recursive" || /^-[a-zA-Z]*R/.test(a)
|
|
1030
|
+
);
|
|
1031
|
+
if (!recursive) return 0;
|
|
1032
|
+
const modes = args.filter(
|
|
1033
|
+
(a) => !a.startsWith("-") || /^-[rwxXst]+$/.test(a)
|
|
1034
|
+
);
|
|
1035
|
+
const worldWritable = modes.some(
|
|
1036
|
+
(m) => /^0*[0-7]?[0-7]?[0-7]?[2367]$/.test(m) && m.length >= 3 || /(^|,)[ugo]*[ao][ugo]*[+=][rwxXst]*w/.test(m)
|
|
1037
|
+
);
|
|
1038
|
+
return worldWritable ? ASK : 0;
|
|
1039
|
+
}
|
|
1040
|
+
var GIT_VALUE_OPTS = /* @__PURE__ */ new Set([
|
|
1041
|
+
"-C",
|
|
1042
|
+
"-c",
|
|
1043
|
+
"--git-dir",
|
|
1044
|
+
"--work-tree",
|
|
1045
|
+
"--namespace",
|
|
1046
|
+
"--super-prefix",
|
|
1047
|
+
"--config-env",
|
|
1048
|
+
"--exec-path",
|
|
1049
|
+
"--list-cmds",
|
|
1050
|
+
"--attr-source"
|
|
1051
|
+
]);
|
|
1052
|
+
var GIT_EXEC_KEY = /^(core\.(pager|editor|sshcommand|askpass|gitproxy)|pager\..+|sequence\.editor|.+\.(textconv|command|cmd|helper|program|clean|smudge|process|driver|tool)|credential\..*|uploadpack\.packobjectshook|gpg\..*program|.*\.(receivepack|uploadpack))$/i;
|
|
1053
|
+
function rankGit(argWords, depth) {
|
|
1054
|
+
const args = argWords.map((w) => w.value);
|
|
1055
|
+
let rank = 0;
|
|
1056
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
1057
|
+
let i = 0;
|
|
1058
|
+
while (i < args.length) {
|
|
1059
|
+
const a = args[i];
|
|
1060
|
+
if (!a.startsWith("-")) break;
|
|
1061
|
+
let opt = a;
|
|
1062
|
+
let val;
|
|
1063
|
+
if (a.startsWith("--") && a.includes("=")) {
|
|
1064
|
+
opt = a.slice(0, a.indexOf("="));
|
|
1065
|
+
val = a.slice(a.indexOf("=") + 1);
|
|
1066
|
+
i++;
|
|
1067
|
+
} else if (GIT_VALUE_OPTS.has(a)) {
|
|
1068
|
+
val = args[i + 1] ?? "";
|
|
1069
|
+
if (argWords[i + 1]?.dynamic && (a === "-c" || a === "--config-env")) {
|
|
1070
|
+
rank = ASK;
|
|
1071
|
+
}
|
|
1072
|
+
i += 2;
|
|
1073
|
+
} else {
|
|
1074
|
+
i++;
|
|
1075
|
+
}
|
|
1076
|
+
if (opt === "-c" && val !== void 0) {
|
|
1077
|
+
const eq = val.indexOf("=");
|
|
1078
|
+
const key = (eq < 0 ? val : val.slice(0, eq)).toLowerCase();
|
|
1079
|
+
const value = eq < 0 ? "" : val.slice(eq + 1);
|
|
1080
|
+
if (key.startsWith("alias.")) aliases.set(key.slice(6), value);
|
|
1081
|
+
else if (key === "core.hookspath" || key === "core.fsmonitor") {
|
|
1082
|
+
if (!/^(false|0|no|off|)$/i.test(value)) rank = ASK;
|
|
1083
|
+
} else if (GIT_EXEC_KEY.test(key)) {
|
|
1084
|
+
rank = Math.max(rank, rankText(value, depth));
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
if (opt === "--config-env" && val !== void 0) {
|
|
1088
|
+
const key = val.split("=")[0].toLowerCase();
|
|
1089
|
+
if (key.startsWith("alias.") || GIT_EXEC_KEY.test(key) || /hookspath|fsmonitor/.test(key)) {
|
|
1090
|
+
rank = ASK;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
if (i >= args.length) return rank;
|
|
1095
|
+
if (argWords[i].dynamic) return ASK;
|
|
1096
|
+
const sub = args[i];
|
|
1097
|
+
const rest = args.slice(i + 1);
|
|
1098
|
+
const alias = aliases.get(sub.toLowerCase());
|
|
1099
|
+
if (alias !== void 0) {
|
|
1100
|
+
if (alias.startsWith("!")) {
|
|
1101
|
+
return Math.max(
|
|
1102
|
+
rank,
|
|
1103
|
+
rankText(`${alias.slice(1)} ${rest.join(" ")}`, depth)
|
|
1104
|
+
);
|
|
1105
|
+
}
|
|
1106
|
+
const expanded = parseShell(alias);
|
|
1107
|
+
const aliasWords = expanded.commands[0]?.words ?? [];
|
|
1108
|
+
return Math.max(
|
|
1109
|
+
rank,
|
|
1110
|
+
expanded.ambiguous ? ASK : 0,
|
|
1111
|
+
rankGit([...aliasWords, ...argWords.slice(i + 1)], depth + 1)
|
|
1112
|
+
);
|
|
1113
|
+
}
|
|
1114
|
+
const opts = argsBeforeDashDash(rest);
|
|
1115
|
+
const has = (...names) => opts.some((a) => names.includes(a.split("=")[0]));
|
|
1116
|
+
const short = (letter) => opts.some((a) => new RegExp(`^-[a-zA-Z]*${letter}`).test(a));
|
|
1117
|
+
let sr = 0;
|
|
1118
|
+
switch (sub) {
|
|
1119
|
+
case "push": {
|
|
1120
|
+
const forced = has(
|
|
1121
|
+
"--force",
|
|
1122
|
+
"--force-with-lease",
|
|
1123
|
+
"--force-if-includes",
|
|
1124
|
+
"--mirror",
|
|
1125
|
+
"--delete",
|
|
1126
|
+
"--prune"
|
|
1127
|
+
) || opts.some(
|
|
1128
|
+
(a) => /^-[a-zA-Z]+$/.test(a) && /[fd]/.test(a.replace(/o.*$/, ""))
|
|
1129
|
+
) || rest.some((a) => /^[+:]/.test(a));
|
|
1130
|
+
sr = forced ? ASK : RANK.git_push;
|
|
1131
|
+
break;
|
|
1132
|
+
}
|
|
1133
|
+
case "reset":
|
|
1134
|
+
sr = has("--hard", "--merge") ? ASK : 0;
|
|
1135
|
+
break;
|
|
1136
|
+
case "clean":
|
|
1137
|
+
sr = has("--force") || short("f") ? ASK : 0;
|
|
1138
|
+
break;
|
|
1139
|
+
case "branch":
|
|
1140
|
+
sr = short("D") || (has("--delete") || short("d")) && (has("--force") || short("f")) ? ASK : 0;
|
|
1141
|
+
break;
|
|
1142
|
+
case "checkout":
|
|
1143
|
+
sr = rest.includes("--") || has("--force") || short("f") ? ASK : 0;
|
|
1144
|
+
break;
|
|
1145
|
+
case "switch":
|
|
1146
|
+
sr = has("--discard-changes", "--force") || short("f") ? ASK : 0;
|
|
1147
|
+
break;
|
|
1148
|
+
case "restore":
|
|
1149
|
+
sr = has("--source") || short("s") ? ASK : 0;
|
|
1150
|
+
break;
|
|
1151
|
+
case "stash":
|
|
1152
|
+
sr = ["drop", "clear"].includes(rest[0] ?? "") ? ASK : 0;
|
|
1153
|
+
break;
|
|
1154
|
+
case "update-ref":
|
|
1155
|
+
sr = short("d") ? ASK : 0;
|
|
1156
|
+
break;
|
|
1157
|
+
case "reflog":
|
|
1158
|
+
sr = ["expire", "delete"].includes(rest[0] ?? "") ? ASK : 0;
|
|
1159
|
+
break;
|
|
1160
|
+
case "filter-branch":
|
|
1161
|
+
case "filter-repo":
|
|
1162
|
+
sr = ASK;
|
|
1163
|
+
break;
|
|
1164
|
+
}
|
|
1165
|
+
if (!GIT_READS.has(sub) && rest.some((a) => AGENT_CONFIG_PATH.test(a)))
|
|
1166
|
+
sr = ASK;
|
|
1167
|
+
return Math.max(rank, sr);
|
|
1168
|
+
}
|
|
1169
|
+
var AGENT_CONFIG_SUBCOMMANDS = /* @__PURE__ */ new Set([
|
|
1170
|
+
"mcp",
|
|
1171
|
+
"plugin",
|
|
1172
|
+
"plugins",
|
|
1173
|
+
"config"
|
|
1174
|
+
]);
|
|
1175
|
+
var AGENT_READ_ACTIONS = /* @__PURE__ */ new Set([
|
|
1176
|
+
"list",
|
|
1177
|
+
"ls",
|
|
1178
|
+
"get",
|
|
1179
|
+
"show",
|
|
1180
|
+
"serve",
|
|
1181
|
+
"help",
|
|
1182
|
+
"--help",
|
|
1183
|
+
"-h"
|
|
1184
|
+
]);
|
|
1185
|
+
var AGENT_WIDENING_OPTS = /^(--mcp-config|--settings|--dangerously-skip-permissions|--allow-dangerously-skip-permissions|--allowed-tools|--allowedtools|--add-dir|--plugin-dir|--dangerously-bypass-approvals-and-sandbox|--yolo|--config)(=|$)/i;
|
|
1186
|
+
function rankAgentCli(base, args) {
|
|
1187
|
+
if (args.some((a) => AGENT_WIDENING_OPTS.test(a))) return ASK;
|
|
1188
|
+
if (base === "codex" && args.includes("-c")) return ASK;
|
|
1189
|
+
const mode = args.findIndex((a) => a === "--permission-mode");
|
|
1190
|
+
const modeValue = mode >= 0 ? args[mode + 1] : args.find((a) => a.startsWith("--permission-mode="))?.slice(18);
|
|
1191
|
+
if (modeValue !== void 0 && !/^(default|plan)$/i.test(modeValue))
|
|
1192
|
+
return ASK;
|
|
1193
|
+
const positionals2 = args.filter((a) => !a.startsWith("-"));
|
|
1194
|
+
const sub = positionals2.findIndex((p) => AGENT_CONFIG_SUBCOMMANDS.has(p));
|
|
1195
|
+
if (sub < 0) return 0;
|
|
1196
|
+
const action = positionals2[sub + 1];
|
|
1197
|
+
if (action === void 0 || AGENT_READ_ACTIONS.has(action)) return 0;
|
|
1198
|
+
return ASK;
|
|
1199
|
+
}
|
|
165
1200
|
function classifyCommand(command) {
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
if (
|
|
169
|
-
if (
|
|
170
|
-
|
|
1201
|
+
let rank = 0;
|
|
1202
|
+
if (DESTRUCTIVE.some((re) => re.test(command))) rank = ASK;
|
|
1203
|
+
else if (DEPLOY.some((re) => re.test(command))) rank = RANK.shell_deploy;
|
|
1204
|
+
else if (GIT_PUSH.test(command)) rank = RANK.git_push;
|
|
1205
|
+
else if (startsInteractiveSession(command)) rank = RANK.shell_session;
|
|
1206
|
+
if (rank < ASK) rank = Math.max(rank, rankText(command, 0));
|
|
1207
|
+
return BY_RANK[rank];
|
|
1208
|
+
}
|
|
1209
|
+
function isAgentConfigPath(path) {
|
|
1210
|
+
return AGENT_CONFIG_PATH.test(path);
|
|
171
1211
|
}
|
|
172
1212
|
function mcpToolName(server, tool) {
|
|
173
1213
|
const part = (s) => s.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").replace(/^(\d)/, "x$1") || "x";
|
|
@@ -221,17 +1261,20 @@ function patchTargets(patch) {
|
|
|
221
1261
|
}
|
|
222
1262
|
return targets;
|
|
223
1263
|
}
|
|
1264
|
+
var AGENT_CONFIG_NOTE = "(agent MCP, hook, permission or plugin config)";
|
|
224
1265
|
function fileWrite(operation, path, content, cwd, summaryVerb) {
|
|
1266
|
+
const config = isAgentConfigPath(path);
|
|
225
1267
|
return {
|
|
226
|
-
toolName: "file_write",
|
|
1268
|
+
toolName: config ? "shell_destructive" : "file_write",
|
|
227
1269
|
arguments: {
|
|
228
1270
|
operation,
|
|
229
1271
|
path,
|
|
230
1272
|
contentBytes: Buffer.byteLength(content),
|
|
231
1273
|
contentDigest: digest(content),
|
|
1274
|
+
...config ? { touches: "agent-config" } : {},
|
|
232
1275
|
...cwd
|
|
233
1276
|
},
|
|
234
|
-
summary: `${summaryVerb} ${path}`
|
|
1277
|
+
summary: `${summaryVerb} ${path}${config ? ` ${AGENT_CONFIG_NOTE}` : ""}`
|
|
235
1278
|
};
|
|
236
1279
|
}
|
|
237
1280
|
function governedCall(call) {
|
|
@@ -279,10 +1322,12 @@ function governedCall(call) {
|
|
|
279
1322
|
const targets = patchTargets(patch);
|
|
280
1323
|
const paths = [...new Set(targets.map((t) => t.path))];
|
|
281
1324
|
const shown = paths.slice(0, MAX_PATHS).map((p) => bounded(p, 500));
|
|
1325
|
+
const config = paths.some((p) => isAgentConfigPath(p));
|
|
282
1326
|
return {
|
|
283
|
-
toolName: "file_write",
|
|
1327
|
+
toolName: config ? "shell_destructive" : "file_write",
|
|
284
1328
|
arguments: {
|
|
285
1329
|
operation: "apply_patch",
|
|
1330
|
+
...config ? { touches: "agent-config" } : {},
|
|
286
1331
|
path: shown[0] ?? "",
|
|
287
1332
|
paths: shown,
|
|
288
1333
|
...paths.length > MAX_PATHS ? { morePaths: paths.length - MAX_PATHS } : {},
|
|
@@ -291,7 +1336,7 @@ function governedCall(call) {
|
|
|
291
1336
|
contentDigest: digest(patch),
|
|
292
1337
|
...cwd
|
|
293
1338
|
},
|
|
294
|
-
summary: paths.length === 0 ? "Apply a patch (no file headers found)" : paths.length === 1 ? `Patch ${bounded(paths[0], 160)}` : `Patch ${paths.length} files: ${bounded(paths.join(", "), 160)}`
|
|
1339
|
+
summary: (paths.length === 0 ? "Apply a patch (no file headers found)" : paths.length === 1 ? `Patch ${bounded(paths[0], 160)}` : `Patch ${paths.length} files: ${bounded(paths.join(", "), 160)}`) + (config ? ` ${AGENT_CONFIG_NOTE}` : "")
|
|
295
1340
|
};
|
|
296
1341
|
}
|
|
297
1342
|
if (tool === "WebFetch" || tool === "web_fetch") {
|