@oyasmi/pipiclaw 0.9.5 → 0.9.6-beta.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.en.md +220 -126
- package/README.md +217 -123
- package/dist/agent/channel-runner.d.ts +1 -1
- package/dist/agent/channel-runner.js +1 -1
- package/dist/agent/prompt/sections.js +1 -1
- package/dist/agent/prompt/types.d.ts +2 -1
- package/dist/agent/session/compaction.d.ts +2 -0
- package/dist/agent/session/compaction.js +22 -14
- package/dist/agent/session/session-host.d.ts +1 -1
- package/dist/agent/session/session-host.js +3 -1
- package/dist/agent/stop-watchdog.js +5 -1
- package/dist/agent/turn/turn-state.d.ts +2 -0
- package/dist/agent/turn/turn-state.js +2 -1
- package/dist/agent/types.d.ts +3 -1
- package/dist/channel/attachments.d.ts +4 -0
- package/dist/channel/attachments.js +4 -0
- package/dist/channel/store.d.ts +1 -1
- package/dist/commands/catalog.js +25 -9
- package/dist/commands/router.js +5 -1
- package/dist/desk/commands.d.ts +14 -2
- package/dist/desk/commands.js +68 -20
- package/dist/desk/registry.d.ts +6 -2
- package/dist/desk/registry.js +70 -4
- package/dist/desk/types.d.ts +17 -0
- package/dist/dingtalk/bot.d.ts +1 -1
- package/dist/dingtalk/bot.js +4 -3
- package/dist/dingtalk/delivery.js +13 -8
- package/dist/dingtalk/inbound-media.d.ts +36 -14
- package/dist/dingtalk/inbound-media.js +71 -12
- package/dist/dingtalk/inbound.d.ts +9 -2
- package/dist/dingtalk/inbound.js +18 -2
- package/dist/dingtalk/media.d.ts +4 -10
- package/dist/dingtalk/media.js +58 -32
- package/dist/events/commands.d.ts +2 -0
- package/dist/events/commands.js +13 -8
- package/dist/events/history.d.ts +9 -0
- package/dist/events/history.js +66 -0
- package/dist/events/parse.js +7 -3
- package/dist/events/types.d.ts +3 -0
- package/dist/events/watcher.d.ts +3 -7
- package/dist/events/watcher.js +51 -56
- package/dist/jobs/kill.d.ts +23 -0
- package/dist/jobs/kill.js +65 -0
- package/dist/jobs/manager.d.ts +19 -18
- package/dist/jobs/manager.js +36 -103
- package/dist/memory/reflect.js +23 -3
- package/dist/memory/session-corpus.d.ts +1 -1
- package/dist/memory/session-corpus.js +70 -1
- package/dist/memory/session-search.js +13 -11
- package/dist/memory/store.d.ts +2 -0
- package/dist/memory/store.js +19 -9
- package/dist/playbooks/agent-delegation.md +5 -4
- package/dist/playbooks/memory-and-learning.md +1 -1
- package/dist/playbooks/task-lead.md +4 -4
- package/dist/runtime/bootstrap.d.ts +6 -0
- package/dist/runtime/bootstrap.js +11 -7
- package/dist/runtime/desk-runtime.d.ts +8 -2
- package/dist/runtime/desk-runtime.js +35 -4
- package/dist/runtime/durable-dispatch.d.ts +14 -2
- package/dist/runtime/durable-dispatch.js +207 -65
- package/dist/runtime/ingress-busy.js +17 -8
- package/dist/runtime/ingress-deps.d.ts +4 -1
- package/dist/runtime/ingress.js +6 -1
- package/dist/runtime/request-execution.js +7 -6
- package/dist/runtime/shutdown.d.ts +1 -0
- package/dist/runtime/shutdown.js +87 -35
- package/dist/runtime/task-control.d.ts +1 -0
- package/dist/runtime/task-control.js +25 -2
- package/dist/runtime/task-lane.d.ts +1 -0
- package/dist/runtime/task-lane.js +21 -6
- package/dist/runtime/task-step.js +11 -2
- package/dist/runtime/turn-run.js +103 -66
- package/dist/security/command-guard.d.ts +1 -1
- package/dist/security/command-guard.js +237 -314
- package/dist/security/path-guard.js +48 -7
- package/dist/security/shell-parse.d.ts +10 -0
- package/dist/security/shell-parse.js +224 -0
- package/dist/security/types.d.ts +2 -0
- package/dist/subagents/commands.d.ts +1 -1
- package/dist/subagents/detached-discovery.d.ts +1 -1
- package/dist/subagents/discovery.d.ts +18 -199
- package/dist/subagents/discovery.js +35 -289
- package/dist/subagents/dispatch.d.ts +1 -2
- package/dist/subagents/dispatch.js +39 -20
- package/dist/subagents/external/antigravity-cli.d.ts +2 -0
- package/dist/subagents/external/antigravity-cli.js +517 -0
- package/dist/subagents/external/claude-code.js +8 -17
- package/dist/subagents/external/codex-cli.js +8 -18
- package/dist/subagents/external/cursor-cli.js +34 -31
- package/dist/subagents/external/dispatch.d.ts +21 -7
- package/dist/subagents/external/dispatch.js +139 -11
- package/dist/subagents/external/exec.js +3 -0
- package/dist/subagents/external/harness.d.ts +57 -33
- package/dist/subagents/external/harness.js +69 -40
- package/dist/subagents/external/reconciliation.js +1 -0
- package/dist/subagents/external/registry.js +4 -1
- package/dist/subagents/external/role-warnings.d.ts +4 -13
- package/dist/subagents/external/role-warnings.js +4 -28
- package/dist/subagents/external/run.d.ts +1 -2
- package/dist/subagents/external/run.js +11 -11
- package/dist/subagents/external/settlement.d.ts +2 -0
- package/dist/subagents/external/settlement.js +8 -2
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/internal/outcome.d.ts +1 -2
- package/dist/subagents/internal/provider-retry.d.ts +1 -2
- package/dist/subagents/internal/provider-retry.js +1 -1
- package/dist/subagents/internal/run.d.ts +1 -2
- package/dist/subagents/internal/state.d.ts +1 -2
- package/dist/subagents/internal/worker.d.ts +1 -2
- package/dist/subagents/internal/worker.js +3 -1
- package/dist/subagents/resolve.d.ts +56 -0
- package/dist/subagents/resolve.js +202 -0
- package/dist/subagents/result.d.ts +1 -2
- package/dist/subagents/result.js +7 -3
- package/dist/subagents/run-record.d.ts +33 -2
- package/dist/subagents/run-record.js +2 -0
- package/dist/subagents/runs.d.ts +2 -24
- package/dist/subagents/runs.js +6 -0
- package/dist/subagents/task-envelope.d.ts +1 -2
- package/dist/subagents/task-envelope.js +8 -4
- package/dist/subagents/types.d.ts +93 -4
- package/dist/subagents/types.js +16 -1
- package/dist/subagents/workspace-lease.d.ts +11 -21
- package/dist/subagents/workspace-lease.js +48 -21
- package/dist/subagents/worktree.d.ts +10 -0
- package/dist/subagents/worktree.js +77 -0
- package/dist/tasks/archive.js +4 -0
- package/dist/tasks/commands.d.ts +3 -0
- package/dist/tasks/commands.js +16 -2
- package/dist/tasks/control.d.ts +2 -0
- package/dist/tasks/delivery.d.ts +2 -0
- package/dist/tasks/delivery.js +141 -21
- package/dist/tasks/loop-log.d.ts +2 -0
- package/dist/tasks/loop-log.js +9 -0
- package/dist/tools/bash.js +2 -0
- package/dist/tools/contract/presentation.js +3 -4
- package/dist/tools/event-manage.d.ts +2 -0
- package/dist/tools/event-manage.js +20 -2
- package/dist/tools/index.d.ts +2 -1
- package/dist/tools/index.js +2 -0
- package/dist/tools/registry.js +3 -1
- package/dist/tools/send-media.js +9 -5
- package/dist/tools/session-search.js +1 -1
- package/dist/tools/subagent-manage.d.ts +1 -1
- package/dist/tools/subagent-manage.js +15 -149
- package/dist/tools/subagent.d.ts +2 -0
- package/dist/tools/subagent.js +6 -1
- package/dist/tools/task-manage/factory.js +2 -2
- package/dist/tools/task-manage/lifecycle.d.ts +3 -3
- package/dist/tools/task-manage/lifecycle.js +49 -9
- package/dist/tools/task-manage/schema.d.ts +4 -1
- package/dist/tools/task-manage/schema.js +4 -1
- package/dist/tools/task-manage/types.d.ts +4 -1
- package/dist/tui/cli.d.ts +1 -6
- package/dist/tui/cli.js +29 -3
- package/dist/tui/client.d.ts +2 -0
- package/dist/tui/client.js +13 -4
- package/dist/usage/ledger.d.ts +1 -1
- package/dist/usage/render.js +1 -1
- package/examples/sub-agents/README.md +39 -73
- package/examples/sub-agents/agents/agy-main.md +23 -0
- package/examples/sub-agents/agents/claude-high.md +9 -9
- package/examples/sub-agents/agents/claude-main.md +9 -9
- package/examples/sub-agents/agents/codex-flash.md +9 -9
- package/examples/sub-agents/agents/codex-high.md +9 -9
- package/examples/sub-agents/agents/codex-main.md +9 -9
- package/examples/sub-agents/agents/codex-review.md +9 -9
- package/examples/sub-agents/agents/cursor-high.md +9 -9
- package/examples/sub-agents/agents/cursor-main.md +9 -9
- package/examples/sub-agents/agents/glm-flash.md +9 -9
- package/examples/sub-agents/agents/glm-high.md +9 -9
- package/package.json +8 -8
- package/examples/sub-agents/agents/scout.md +0 -30
- package/examples/sub-agents/roles/builder-hard.md +0 -34
- package/examples/sub-agents/roles/builder.md +0 -34
- package/examples/sub-agents/roles/explorer.md +0 -39
- package/examples/sub-agents/roles/git-committer.md +0 -43
- package/examples/sub-agents/roles/planner.md +0 -33
- package/examples/sub-agents/roles/reviewer.md +0 -33
- package/examples/sub-agents/roles/verifier.md +0 -35
- package/examples/sub-agents/roles/worker.md +0 -34
|
@@ -3,242 +3,25 @@
|
|
|
3
3
|
* rules and a wrapper-aware scan of every place a command can start.
|
|
4
4
|
*/
|
|
5
5
|
import { basename } from "node:path";
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
let escaped = false;
|
|
13
|
-
for (let i = 0; i < command.length; i++) {
|
|
14
|
-
const char = command[i];
|
|
15
|
-
if (escaped) {
|
|
16
|
-
result += char;
|
|
17
|
-
escaped = false;
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
if (!inSingle && char === "\\") {
|
|
21
|
-
result += char;
|
|
22
|
-
escaped = true;
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
if (!inDouble && char === "'") {
|
|
26
|
-
inSingle = !inSingle;
|
|
27
|
-
result += char;
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
if (!inSingle && char === '"') {
|
|
31
|
-
inDouble = !inDouble;
|
|
32
|
-
result += char;
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
if (!inSingle && !inDouble && char === "#") {
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
result += char;
|
|
39
|
-
}
|
|
40
|
-
return result;
|
|
6
|
+
import { SHELL_DYNAMIC, ShellSyntaxError, shellCommands } from "./shell-parse.js";
|
|
7
|
+
function parseShellWords(command) {
|
|
8
|
+
const commands = shellCommands(command);
|
|
9
|
+
if (commands.length > 1)
|
|
10
|
+
throw new ShellSyntaxError("Expected one command");
|
|
11
|
+
return commands[0] ?? [];
|
|
41
12
|
}
|
|
42
13
|
function splitCommandChain(command) {
|
|
43
|
-
|
|
44
|
-
const normalized = stripComments(stripNullAndNormalize(command));
|
|
45
|
-
function pushAtom(atom) {
|
|
46
|
-
const trimmed = atom.trim();
|
|
47
|
-
if (trimmed) {
|
|
48
|
-
atoms.push(trimmed);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function walk(input) {
|
|
52
|
-
let buffer = "";
|
|
53
|
-
let inSingle = false;
|
|
54
|
-
let inDouble = false;
|
|
55
|
-
let escaped = false;
|
|
56
|
-
let parenDepth = 0;
|
|
57
|
-
for (let i = 0; i < input.length; i++) {
|
|
58
|
-
const char = input[i];
|
|
59
|
-
const next = input[i + 1];
|
|
60
|
-
if (escaped) {
|
|
61
|
-
buffer += char;
|
|
62
|
-
escaped = false;
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if (char === "\\") {
|
|
66
|
-
buffer += char;
|
|
67
|
-
escaped = true;
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (!inDouble && char === "'") {
|
|
71
|
-
inSingle = !inSingle;
|
|
72
|
-
buffer += char;
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (!inSingle && char === '"') {
|
|
76
|
-
inDouble = !inDouble;
|
|
77
|
-
buffer += char;
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
if (!inSingle && char === "`") {
|
|
81
|
-
let j = i + 1;
|
|
82
|
-
let inner = "";
|
|
83
|
-
let innerEscaped = false;
|
|
84
|
-
while (j < input.length) {
|
|
85
|
-
const innerChar = input[j];
|
|
86
|
-
if (innerEscaped) {
|
|
87
|
-
inner += innerChar;
|
|
88
|
-
innerEscaped = false;
|
|
89
|
-
j++;
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (innerChar === "\\") {
|
|
93
|
-
inner += innerChar;
|
|
94
|
-
innerEscaped = true;
|
|
95
|
-
j++;
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (innerChar === "`") {
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
inner += innerChar;
|
|
102
|
-
j++;
|
|
103
|
-
}
|
|
104
|
-
if (j < input.length) {
|
|
105
|
-
walk(inner);
|
|
106
|
-
buffer += "`subshell`";
|
|
107
|
-
i = j;
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
if (!inSingle && char === "$" && next === "(") {
|
|
112
|
-
let j = i + 2;
|
|
113
|
-
let inner = "";
|
|
114
|
-
let depth = 1;
|
|
115
|
-
let innerSingle = false;
|
|
116
|
-
let innerDouble = false;
|
|
117
|
-
let innerEscaped = false;
|
|
118
|
-
while (j < input.length) {
|
|
119
|
-
const innerChar = input[j];
|
|
120
|
-
if (innerEscaped) {
|
|
121
|
-
inner += innerChar;
|
|
122
|
-
innerEscaped = false;
|
|
123
|
-
j++;
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
if (innerChar === "\\") {
|
|
127
|
-
inner += innerChar;
|
|
128
|
-
innerEscaped = true;
|
|
129
|
-
j++;
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
if (!innerDouble && innerChar === "'") {
|
|
133
|
-
innerSingle = !innerSingle;
|
|
134
|
-
inner += innerChar;
|
|
135
|
-
j++;
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
if (!innerSingle && innerChar === '"') {
|
|
139
|
-
innerDouble = !innerDouble;
|
|
140
|
-
inner += innerChar;
|
|
141
|
-
j++;
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
if (!innerSingle && !innerDouble && innerChar === "(") {
|
|
145
|
-
depth++;
|
|
146
|
-
inner += innerChar;
|
|
147
|
-
j++;
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (!innerSingle && !innerDouble && innerChar === ")") {
|
|
151
|
-
depth--;
|
|
152
|
-
if (depth === 0) {
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
inner += innerChar;
|
|
156
|
-
j++;
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
inner += innerChar;
|
|
160
|
-
j++;
|
|
161
|
-
}
|
|
162
|
-
if (depth === 0) {
|
|
163
|
-
walk(inner);
|
|
164
|
-
buffer += "$(subshell)";
|
|
165
|
-
i = j;
|
|
166
|
-
continue;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (!inSingle && !inDouble) {
|
|
170
|
-
if (char === "(") {
|
|
171
|
-
parenDepth++;
|
|
172
|
-
}
|
|
173
|
-
else if (char === ")" && parenDepth > 0) {
|
|
174
|
-
parenDepth--;
|
|
175
|
-
}
|
|
176
|
-
const separator = parenDepth === 0 &&
|
|
177
|
-
(char === ";" ||
|
|
178
|
-
char === "\n" ||
|
|
179
|
-
(char === "|" && next === "|") ||
|
|
180
|
-
(char === "&" && next === "&") ||
|
|
181
|
-
char === "|");
|
|
182
|
-
if (separator) {
|
|
183
|
-
pushAtom(buffer);
|
|
184
|
-
buffer = "";
|
|
185
|
-
if ((char === "|" && next === "|") || (char === "&" && next === "&")) {
|
|
186
|
-
i++;
|
|
187
|
-
}
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
buffer += char;
|
|
192
|
-
}
|
|
193
|
-
pushAtom(buffer);
|
|
194
|
-
}
|
|
195
|
-
walk(normalized);
|
|
196
|
-
return atoms;
|
|
197
|
-
}
|
|
198
|
-
function parseShellWords(command) {
|
|
199
|
-
const words = [];
|
|
200
|
-
let buffer = "";
|
|
201
|
-
let inSingle = false;
|
|
202
|
-
let inDouble = false;
|
|
203
|
-
let escaped = false;
|
|
204
|
-
function pushWord() {
|
|
205
|
-
if (buffer) {
|
|
206
|
-
words.push(buffer);
|
|
207
|
-
buffer = "";
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
for (let i = 0; i < command.length; i++) {
|
|
211
|
-
const char = command[i];
|
|
212
|
-
if (escaped) {
|
|
213
|
-
buffer += char;
|
|
214
|
-
escaped = false;
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
if (!inSingle && char === "\\") {
|
|
218
|
-
escaped = true;
|
|
219
|
-
continue;
|
|
220
|
-
}
|
|
221
|
-
if (!inDouble && char === "'") {
|
|
222
|
-
inSingle = !inSingle;
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
if (!inSingle && char === '"') {
|
|
226
|
-
inDouble = !inDouble;
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
if (!inSingle && !inDouble && WHITESPACE.test(char)) {
|
|
230
|
-
pushWord();
|
|
231
|
-
continue;
|
|
232
|
-
}
|
|
233
|
-
buffer += char;
|
|
234
|
-
}
|
|
235
|
-
pushWord();
|
|
236
|
-
return words;
|
|
14
|
+
return shellCommands(command).map((words) => words.join(" "));
|
|
237
15
|
}
|
|
238
16
|
function parsedFromWords(words) {
|
|
239
17
|
if (words.length === 0) {
|
|
240
18
|
return null;
|
|
241
19
|
}
|
|
20
|
+
if (words[0].includes(SHELL_DYNAMIC.substitution) ||
|
|
21
|
+
words[0].includes(SHELL_DYNAMIC.expansion) ||
|
|
22
|
+
words[0].includes(SHELL_DYNAMIC.glob) ||
|
|
23
|
+
/[*?[\]{}]/.test(words[0]))
|
|
24
|
+
throw new ShellSyntaxError("Dynamic executable is unsupported");
|
|
242
25
|
const normalizedCommand = basename(words[0]).toLowerCase();
|
|
243
26
|
const args = words.slice(1);
|
|
244
27
|
return {
|
|
@@ -270,76 +53,200 @@ const WRAPPER_COMMANDS = new Set([
|
|
|
270
53
|
"parallel",
|
|
271
54
|
]);
|
|
272
55
|
const MAX_GUARD_DEPTH = 6;
|
|
273
|
-
//
|
|
274
|
-
//
|
|
275
|
-
// in `c`. There can be more than one (`bash -c a -c b`), so return all.
|
|
56
|
+
// Shell option clusters may put c anywhere (-lc, -cl). After the first script,
|
|
57
|
+
// further operands are positional parameters, not more shell options.
|
|
276
58
|
function extractShellScripts(parsed) {
|
|
277
|
-
if (!SHELL_COMMANDS.has(parsed.command))
|
|
59
|
+
if (!SHELL_COMMANDS.has(parsed.command))
|
|
278
60
|
return [];
|
|
279
|
-
}
|
|
280
|
-
const scripts = [];
|
|
281
61
|
for (let i = 0; i < parsed.args.length; i++) {
|
|
282
62
|
const arg = parsed.args[i];
|
|
283
|
-
if (/^-[a-
|
|
284
|
-
|
|
285
|
-
|
|
63
|
+
if (/^-[a-zA-Z]*c[a-zA-Z]*$/.test(arg)) {
|
|
64
|
+
if (parsed.args[i + 1] === undefined)
|
|
65
|
+
throw new ShellSyntaxError("Missing shell script");
|
|
66
|
+
return [parsed.args[i + 1]];
|
|
286
67
|
}
|
|
68
|
+
if (!/^-[a-zA-Z]+$/.test(arg) || /^--/.test(arg))
|
|
69
|
+
throw new ShellSyntaxError("Unsupported shell invocation");
|
|
287
70
|
}
|
|
288
|
-
|
|
71
|
+
throw new ShellSyntaxError("Shell invocation without a literal -c script");
|
|
289
72
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
73
|
+
const FIND_OPERANDS = new Set([
|
|
74
|
+
"-name",
|
|
75
|
+
"-iname",
|
|
76
|
+
"-path",
|
|
77
|
+
"-ipath",
|
|
78
|
+
"-wholename",
|
|
79
|
+
"-iwholename",
|
|
80
|
+
"-regex",
|
|
81
|
+
"-iregex",
|
|
82
|
+
"-type",
|
|
83
|
+
"-xtype",
|
|
84
|
+
"-size",
|
|
85
|
+
"-perm",
|
|
86
|
+
"-user",
|
|
87
|
+
"-group",
|
|
88
|
+
"-uid",
|
|
89
|
+
"-gid",
|
|
90
|
+
"-links",
|
|
91
|
+
"-inum",
|
|
92
|
+
"-mtime",
|
|
93
|
+
"-mmin",
|
|
94
|
+
"-ctime",
|
|
95
|
+
"-cmin",
|
|
96
|
+
"-atime",
|
|
97
|
+
"-amin",
|
|
98
|
+
"-newer",
|
|
99
|
+
"-anewer",
|
|
100
|
+
"-cnewer",
|
|
101
|
+
"-printf",
|
|
102
|
+
"-fprint",
|
|
103
|
+
"-fprint0",
|
|
104
|
+
"-fls",
|
|
105
|
+
"-maxdepth",
|
|
106
|
+
"-mindepth",
|
|
107
|
+
"-regextype",
|
|
108
|
+
"-files0-from",
|
|
109
|
+
]);
|
|
110
|
+
function findActions(parsed) {
|
|
111
|
+
const actions = { commands: [], deletes: false };
|
|
112
|
+
if (parsed.command !== "find")
|
|
113
|
+
return actions;
|
|
297
114
|
for (let i = 0; i < parsed.args.length; i++) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (token === ";" || token === "+" || token === "\\;") {
|
|
303
|
-
break;
|
|
304
|
-
}
|
|
305
|
-
words.push(token);
|
|
306
|
-
}
|
|
307
|
-
if (words.length > 0) {
|
|
308
|
-
inner.push(words);
|
|
309
|
-
}
|
|
115
|
+
const arg = parsed.args[i];
|
|
116
|
+
if (FIND_OPERANDS.has(arg) || /^-newer[acmBt][acmBt]$/.test(arg)) {
|
|
117
|
+
i++;
|
|
118
|
+
continue;
|
|
310
119
|
}
|
|
311
|
-
|
|
312
|
-
|
|
120
|
+
if (arg === "-fprintf") {
|
|
121
|
+
i += 2;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (arg === "-delete")
|
|
125
|
+
actions.deletes = true;
|
|
126
|
+
if (!["-exec", "-execdir", "-ok", "-okdir"].includes(arg))
|
|
127
|
+
continue;
|
|
128
|
+
const words = [];
|
|
129
|
+
while (++i < parsed.args.length && ![";", "+"].includes(parsed.args[i]))
|
|
130
|
+
words.push(parsed.args[i]);
|
|
131
|
+
if (!words.length || i === parsed.args.length)
|
|
132
|
+
throw new ShellSyntaxError("Unterminated find execution");
|
|
133
|
+
actions.commands.push(words);
|
|
134
|
+
}
|
|
135
|
+
return actions;
|
|
313
136
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
137
|
+
// Only wrapper options with known operand counts are accepted. Never treat an operand as
|
|
138
|
+
// another executable (e.g. env printf '%s' 'rm -rf /'), or stop scanning after a token budget.
|
|
139
|
+
const WRAPPER_OPTIONS = {
|
|
140
|
+
env: [
|
|
141
|
+
["-i", "--ignore-environment", "-0", "--null"],
|
|
142
|
+
["-u", "--unset", "-C", "--chdir"],
|
|
143
|
+
],
|
|
144
|
+
time: [
|
|
145
|
+
["-p", "-v", "--verbose", "-a", "--append"],
|
|
146
|
+
["-f", "--format", "-o", "--output"],
|
|
147
|
+
],
|
|
148
|
+
nice: [[], ["-n", "--adjustment"]],
|
|
149
|
+
ionice: [
|
|
150
|
+
["-t", "--ignore"],
|
|
151
|
+
["-c", "--class", "-n", "--classdata", "-p", "-P", "-u"],
|
|
152
|
+
],
|
|
153
|
+
nohup: [[], []],
|
|
154
|
+
timeout: [
|
|
155
|
+
["--preserve-status", "--foreground", "-v", "--verbose"],
|
|
156
|
+
["-s", "--signal", "-k", "--kill-after"],
|
|
157
|
+
],
|
|
158
|
+
stdbuf: [[], ["-i", "-o", "-e", "--input", "--output", "--error"]],
|
|
159
|
+
setsid: [["-c", "--ctty", "-f", "--fork", "-w", "--wait"], []],
|
|
160
|
+
chrt: [
|
|
161
|
+
["-b", "-f", "-i", "-o", "-r", "-d", "-R", "-a", "--all-tasks"],
|
|
162
|
+
["-T", "-P", "-D"],
|
|
163
|
+
],
|
|
164
|
+
xargs: [
|
|
165
|
+
["-0", "--null", "-r", "--no-run-if-empty", "-t", "--verbose", "-p", "--interactive", "-x"],
|
|
166
|
+
[
|
|
167
|
+
"-n",
|
|
168
|
+
"--max-args",
|
|
169
|
+
"-P",
|
|
170
|
+
"--max-procs",
|
|
171
|
+
"-I",
|
|
172
|
+
"-d",
|
|
173
|
+
"--delimiter",
|
|
174
|
+
"-s",
|
|
175
|
+
"--max-chars",
|
|
176
|
+
"-L",
|
|
177
|
+
"--max-lines",
|
|
178
|
+
"-E",
|
|
179
|
+
"--eof",
|
|
180
|
+
],
|
|
181
|
+
],
|
|
182
|
+
parallel: [
|
|
183
|
+
["--will-cite", "--keep-order", "-k", "-0", "--null"],
|
|
184
|
+
["-j", "--jobs", "-I", "--replace", "--colsep"],
|
|
185
|
+
],
|
|
186
|
+
};
|
|
333
187
|
function extractWrapperCandidates(parsed) {
|
|
334
|
-
if (!WRAPPER_COMMANDS.has(parsed.command))
|
|
188
|
+
if (!WRAPPER_COMMANDS.has(parsed.command))
|
|
335
189
|
return [];
|
|
190
|
+
const [flags, operands] = WRAPPER_OPTIONS[parsed.command];
|
|
191
|
+
let i = 0;
|
|
192
|
+
while (i < parsed.args.length) {
|
|
193
|
+
const arg = parsed.args[i];
|
|
194
|
+
if (arg === "--") {
|
|
195
|
+
i++;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
if (parsed.command === "env" && /^[A-Za-z_][A-Za-z0-9_]*=/.test(arg)) {
|
|
199
|
+
if (Object.values(SHELL_DYNAMIC).some((tag) => arg.includes(tag)))
|
|
200
|
+
throw new ShellSyntaxError("Dynamic wrapper environment is unsupported");
|
|
201
|
+
i++;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (parsed.command === "nice" && /^-\d+$/.test(arg)) {
|
|
205
|
+
i++;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (!arg.startsWith("-"))
|
|
209
|
+
break;
|
|
210
|
+
if (parsed.command === "xargs" && (arg.startsWith("-I") || arg.startsWith("--replace")))
|
|
211
|
+
throw new ShellSyntaxError("xargs template substitution is unsupported");
|
|
212
|
+
if (flags.includes(arg)) {
|
|
213
|
+
i++;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (operands.includes(arg)) {
|
|
217
|
+
if (++i >= parsed.args.length)
|
|
218
|
+
throw new ShellSyntaxError("Missing wrapper option operand");
|
|
219
|
+
i++;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (operands.some((option) => option.startsWith("--")
|
|
223
|
+
? arg.startsWith(`${option}=`)
|
|
224
|
+
: arg.startsWith(option) && arg.length > option.length)) {
|
|
225
|
+
i++;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
throw new ShellSyntaxError("Unsupported wrapper option");
|
|
336
229
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
230
|
+
if (["timeout", "chrt"].includes(parsed.command))
|
|
231
|
+
i++;
|
|
232
|
+
if (i >= parsed.args.length)
|
|
233
|
+
return [];
|
|
234
|
+
const words = parsed.args.slice(i);
|
|
235
|
+
if (words[0].startsWith("-"))
|
|
236
|
+
throw new ShellSyntaxError("Unresolved wrapper executable");
|
|
237
|
+
if (parsed.command === "parallel") {
|
|
238
|
+
const boundary = words.findIndex((word) => word === ":::" || word === "::::");
|
|
239
|
+
const template = boundary < 0 ? words : words.slice(0, boundary);
|
|
240
|
+
if (!template.length)
|
|
241
|
+
throw new ShellSyntaxError("Missing parallel template");
|
|
242
|
+
// Literal operands stay operands. GNU parallel's command-template operators and shell
|
|
243
|
+
// substitution combinations need a richer grammar; do not guess at those boundaries.
|
|
244
|
+
if (template.slice(1).some((word) => /[;&|`$()<>]/.test(word)))
|
|
245
|
+
throw new ShellSyntaxError("Complex parallel templates are unsupported");
|
|
246
|
+
const quote = (word) => `'${word.replace(/'/g, "'\\''")}'`;
|
|
247
|
+
return shellCommands([template[0], ...template.slice(1).map(quote)].join(" "));
|
|
248
|
+
}
|
|
249
|
+
return [words];
|
|
343
250
|
}
|
|
344
251
|
// An allow pattern exempts an atom only when it matches that atom's command
|
|
345
252
|
// line from the start at a word boundary (exact, or a `pattern + space` prefix).
|
|
@@ -406,7 +313,8 @@ function matchRule(parsed, config) {
|
|
|
406
313
|
matchedText: normalized,
|
|
407
314
|
};
|
|
408
315
|
}
|
|
409
|
-
|
|
316
|
+
const find = findActions(parsed);
|
|
317
|
+
if (find.deletes || find.commands.some((words) => basename(words[0]).toLowerCase() === "rm")) {
|
|
410
318
|
return {
|
|
411
319
|
category: "destructive-file-op",
|
|
412
320
|
rule: "find-delete",
|
|
@@ -471,7 +379,6 @@ function matchRule(parsed, config) {
|
|
|
471
379
|
(parsed.command === "wget" && parsed.args.includes("--post-file")) ||
|
|
472
380
|
(parsed.command === "nc" && parsed.args.some((arg) => /^-[^-]*l/.test(arg) || arg === "--listen")) ||
|
|
473
381
|
(parsed.command === "socat" && /\bexec\b/i.test(argsText)) ||
|
|
474
|
-
normalized.includes("/dev/tcp/") ||
|
|
475
382
|
(normalized.includes("mkfifo") && normalized.includes(" nc")) ||
|
|
476
383
|
(parsed.command === "bash" && parsed.args.includes("-i") && /[>&]/.test(argsText))) {
|
|
477
384
|
return {
|
|
@@ -501,9 +408,7 @@ function matchRule(parsed, config) {
|
|
|
501
408
|
/(os\.system|subprocess|exec\(|eval\()/i.test(argsText)) ||
|
|
502
409
|
(parsed.command === "perl" && parsed.args.includes("-e") && /\b(system|exec)\b/.test(argsText)) ||
|
|
503
410
|
(parsed.command === "ruby" && parsed.args.includes("-e") && /\b(system|exec)\b/.test(argsText)) ||
|
|
504
|
-
(parsed.command === "node" && parsed.args.includes("-e") && /\b(child_process|exec|spawn)\b/.test(argsText))
|
|
505
|
-
/\\x[0-9a-f]{2}/i.test(normalized) ||
|
|
506
|
-
/\$'/.test(normalized)) {
|
|
411
|
+
(parsed.command === "node" && parsed.args.includes("-e") && /\b(child_process|exec|spawn)\b/.test(argsText))) {
|
|
507
412
|
return {
|
|
508
413
|
category: "obfuscation",
|
|
509
414
|
rule: parsed.command,
|
|
@@ -526,27 +431,34 @@ function matchRule(parsed, config) {
|
|
|
526
431
|
}
|
|
527
432
|
// Evaluate a single already-parsed atom: its direct rule, plus any commands it
|
|
528
433
|
// wraps (shell `-c` bodies, find `-exec`, generic wrappers), recursively.
|
|
529
|
-
function inspectParsed(parsed, config, depth) {
|
|
530
|
-
if (
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
434
|
+
function inspectParsed(parsed, config, depth, templateSubstitution = false) {
|
|
435
|
+
if (!WRAPPER_COMMANDS.has(parsed.command) &&
|
|
436
|
+
!["echo", "printf"].includes(parsed.command) &&
|
|
437
|
+
parsed.args.some((arg) => arg.includes(SHELL_DYNAMIC.expansion) || arg.includes(SHELL_DYNAMIC.substitution)))
|
|
438
|
+
throw new ShellSyntaxError("Dynamic command operands are unsupported");
|
|
439
|
+
if (!WRAPPER_COMMANDS.has(parsed.command) &&
|
|
440
|
+
!["echo", "printf", "ls", "cat", "head", "tail", "wc", "sort", "uniq", "cut", "tr", "grep", "rg", "sed"].includes(parsed.command) &&
|
|
441
|
+
parsed.args.some((arg) => arg.includes(SHELL_DYNAMIC.glob)))
|
|
442
|
+
throw new ShellSyntaxError("Dynamic pathname operands are unsupported for this command");
|
|
443
|
+
const direct = atomAllowed(parsed.normalized, config.allowPatterns) ? null : matchRule(parsed, config);
|
|
534
444
|
if (direct) {
|
|
535
445
|
return direct;
|
|
536
446
|
}
|
|
537
447
|
if (depth >= MAX_GUARD_DEPTH) {
|
|
538
|
-
|
|
448
|
+
throw new ShellSyntaxError("Command recursion limit");
|
|
539
449
|
}
|
|
540
450
|
for (const script of extractShellScripts(parsed)) {
|
|
451
|
+
if (templateSubstitution && script.includes("{}"))
|
|
452
|
+
throw new ShellSyntaxError("Template substitution into shell scripts is unsupported");
|
|
541
453
|
const nested = evaluateCommand(script, config, depth + 1);
|
|
542
454
|
if (nested) {
|
|
543
455
|
return nested;
|
|
544
456
|
}
|
|
545
457
|
}
|
|
546
|
-
for (const words of [...
|
|
458
|
+
for (const words of [...findActions(parsed).commands, ...extractWrapperCandidates(parsed)]) {
|
|
547
459
|
const innerParsed = parsedFromWords(words);
|
|
548
460
|
if (innerParsed) {
|
|
549
|
-
const nested = inspectParsed(innerParsed, config, depth + 1);
|
|
461
|
+
const nested = inspectParsed(innerParsed, config, depth + 1, templateSubstitution || parsed.command === "find" || parsed.command === "parallel");
|
|
550
462
|
if (nested) {
|
|
551
463
|
return nested;
|
|
552
464
|
}
|
|
@@ -555,28 +467,25 @@ function inspectParsed(parsed, config, depth) {
|
|
|
555
467
|
return null;
|
|
556
468
|
}
|
|
557
469
|
function evaluateCommand(command, config, depth) {
|
|
558
|
-
const
|
|
470
|
+
const commands = shellCommands(command);
|
|
559
471
|
if (config.blockObfuscation &&
|
|
560
|
-
(
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
/\$'/.test(normalizedWhole))) {
|
|
472
|
+
commands.some((words, index) => words[0] === "base64" &&
|
|
473
|
+
words.some((arg) => arg === "-d" || arg === "--decode") &&
|
|
474
|
+
SHELL_COMMANDS.has(commands[index + 1]?.[0]))) {
|
|
564
475
|
return {
|
|
565
476
|
category: "obfuscation",
|
|
566
|
-
rule: "
|
|
567
|
-
reason: "
|
|
568
|
-
matchedText:
|
|
477
|
+
rule: "decoded-shell",
|
|
478
|
+
reason: "Decoded shell input is not allowed",
|
|
479
|
+
matchedText: command,
|
|
569
480
|
};
|
|
570
481
|
}
|
|
571
|
-
for (const
|
|
572
|
-
const parsed =
|
|
573
|
-
if (!parsed)
|
|
482
|
+
for (const words of commands) {
|
|
483
|
+
const parsed = parsedFromWords(words);
|
|
484
|
+
if (!parsed)
|
|
574
485
|
continue;
|
|
575
|
-
}
|
|
576
486
|
const match = inspectParsed(parsed, config, depth);
|
|
577
|
-
if (match)
|
|
487
|
+
if (match)
|
|
578
488
|
return match;
|
|
579
|
-
}
|
|
580
489
|
}
|
|
581
490
|
return null;
|
|
582
491
|
}
|
|
@@ -584,7 +493,21 @@ export function guardCommand(command, config) {
|
|
|
584
493
|
if (!config.enabled) {
|
|
585
494
|
return { allowed: true };
|
|
586
495
|
}
|
|
587
|
-
|
|
496
|
+
let match;
|
|
497
|
+
try {
|
|
498
|
+
match = evaluateCommand(command, config, 0);
|
|
499
|
+
}
|
|
500
|
+
catch (error) {
|
|
501
|
+
if (!(error instanceof ShellSyntaxError))
|
|
502
|
+
throw error;
|
|
503
|
+
return {
|
|
504
|
+
allowed: false,
|
|
505
|
+
category: "unsupported-shell",
|
|
506
|
+
rule: "shell-parse",
|
|
507
|
+
reason: `${error.message}. Use literal simple commands or separate tool calls.`,
|
|
508
|
+
matchedText: command,
|
|
509
|
+
};
|
|
510
|
+
}
|
|
588
511
|
if (match) {
|
|
589
512
|
return {
|
|
590
513
|
allowed: false,
|