@agent-corpus/policy-manager 0.2.1 → 0.4.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/README.md +11 -1
- package/dist/evidence.js +5 -3
- package/dist/flow.d.ts +9 -0
- package/dist/flow.js +698 -0
- package/dist/merged.js +1 -1
- package/dist/permissive.d.ts +15 -6
- package/dist/permissive.js +247 -63
- package/dist/script.d.ts +101 -0
- package/dist/script.js +912 -0
- package/dist/shell.d.ts +7 -0
- package/dist/shell.js +82 -191
- package/package.json +1 -1
package/dist/script.js
ADDED
|
@@ -0,0 +1,912 @@
|
|
|
1
|
+
// Parses a bounded subset of POSIX shell into a syntax tree so a caller can judge every command
|
|
2
|
+
// that would run. Anything whose effects the caller could not judge is rejected (undefined) and
|
|
3
|
+
// falls back to a human: brace expansion, arithmetic, functions, unquoted-delimiter heredocs whose
|
|
4
|
+
// bodies expand, `$'...'`, `${X:-y}`, `<&`, `>|`, `<>`, `;&`, `[[` aside (a plain word here).
|
|
5
|
+
//
|
|
6
|
+
// Understood: simple commands with quoting, escapes, leading `NAME=value` assignments and
|
|
7
|
+
// redirections (including quoted-delimiter heredocs and here-strings); pipelines, `&&`, `||`, `;`,
|
|
8
|
+
// newlines and `&`; `( ... )`, `{ ...; }`, `for`, `while`, `until`, `if`, `case`; comments;
|
|
9
|
+
// `$HOME`; variable references (`$NAME`, `${NAME}`), which are left for the caller to resolve;
|
|
10
|
+
// `$(...)`, backticks and `<(...)`/`>(...)`, whose inner commands are returned for the caller to
|
|
11
|
+
// judge; and parameters with no knowable value (`$?`, `$#`, `$$`, PIPESTATUS), which are unknown.
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
/** Stands for the unknown output of an expansion inside a word. */
|
|
14
|
+
export const SUBSTITUTION = "";
|
|
15
|
+
class Reject extends Error {
|
|
16
|
+
}
|
|
17
|
+
const reject = () => {
|
|
18
|
+
throw new Reject();
|
|
19
|
+
};
|
|
20
|
+
const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
21
|
+
const UNKNOWN_PARAMETER = /^(?:PIPESTATUS|pipestatus)(?:\[[0-9@*]+\])?$/;
|
|
22
|
+
const MAX_INPUT = 200_000;
|
|
23
|
+
const MAX_DEPTH = 6;
|
|
24
|
+
const MAX_NODES = 400;
|
|
25
|
+
// --- scanning helpers -------------------------------------------------------------------------
|
|
26
|
+
function closingBacktick(command, start) {
|
|
27
|
+
for (let j = start; j < command.length; j++) {
|
|
28
|
+
if (command[j] === "\\")
|
|
29
|
+
j++;
|
|
30
|
+
else if (command[j] === "`")
|
|
31
|
+
return j;
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
/** Body of a backtick substitution: backslash escapes `\``, `\\` and `\$` are removed first. */
|
|
36
|
+
function backtickBody(raw) {
|
|
37
|
+
return raw.replace(/\\([`\\$])/g, "$1");
|
|
38
|
+
}
|
|
39
|
+
/** Reads a heredoc delimiter word starting at `at`. */
|
|
40
|
+
function heredocDelimiter(command, at) {
|
|
41
|
+
let i = at;
|
|
42
|
+
while (command[i] === " " || command[i] === "\t")
|
|
43
|
+
i++;
|
|
44
|
+
let delimiter = "";
|
|
45
|
+
let quoted = false;
|
|
46
|
+
const start = i;
|
|
47
|
+
while (i < command.length && !/[\s;|&<>()]/.test(command[i])) {
|
|
48
|
+
const c = command[i];
|
|
49
|
+
if (c === "'" || c === '"') {
|
|
50
|
+
const end = command.indexOf(c, i + 1);
|
|
51
|
+
if (end < 0)
|
|
52
|
+
return undefined;
|
|
53
|
+
delimiter += command.slice(i + 1, end);
|
|
54
|
+
quoted = true;
|
|
55
|
+
i = end + 1;
|
|
56
|
+
}
|
|
57
|
+
else if (c === "\\") {
|
|
58
|
+
if (i + 1 >= command.length || command[i + 1] === "\n")
|
|
59
|
+
return undefined;
|
|
60
|
+
delimiter += command[i + 1];
|
|
61
|
+
quoted = true;
|
|
62
|
+
i += 2;
|
|
63
|
+
}
|
|
64
|
+
else if (c === "$" || c === "`") {
|
|
65
|
+
return undefined; // an expanding delimiter is not worth modelling
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
delimiter += c;
|
|
69
|
+
i++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (i === start || !delimiter)
|
|
73
|
+
return undefined;
|
|
74
|
+
return { delimiter, quoted, next: i };
|
|
75
|
+
}
|
|
76
|
+
function heredocBody(command, start, delimiter, strip) {
|
|
77
|
+
const lines = [];
|
|
78
|
+
let pos = start;
|
|
79
|
+
while (pos <= command.length) {
|
|
80
|
+
const newline = command.indexOf("\n", pos);
|
|
81
|
+
const raw = newline < 0 ? command.slice(pos) : command.slice(pos, newline);
|
|
82
|
+
const line = strip ? raw.replace(/^\t+/, "") : raw;
|
|
83
|
+
if (line === delimiter)
|
|
84
|
+
return { body: lines.join("\n"), next: newline < 0 ? command.length : newline + 1 };
|
|
85
|
+
if (newline < 0)
|
|
86
|
+
return undefined;
|
|
87
|
+
lines.push(line);
|
|
88
|
+
pos = newline + 1;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
/** Index of the `)` that closes a `$(` whose body starts at `start`, honouring quotes and nesting. */
|
|
93
|
+
function closingParen(command, start) {
|
|
94
|
+
let depth = 1;
|
|
95
|
+
let j = start;
|
|
96
|
+
const here = [];
|
|
97
|
+
while (j < command.length) {
|
|
98
|
+
const c = command[j];
|
|
99
|
+
if (c === "\\")
|
|
100
|
+
j += 2;
|
|
101
|
+
else if (c === "'") {
|
|
102
|
+
const end = command.indexOf("'", j + 1);
|
|
103
|
+
if (end < 0)
|
|
104
|
+
return undefined;
|
|
105
|
+
j = end + 1;
|
|
106
|
+
}
|
|
107
|
+
else if (c === '"') {
|
|
108
|
+
j++;
|
|
109
|
+
while (j < command.length && command[j] !== '"') {
|
|
110
|
+
const d = command[j];
|
|
111
|
+
if (d === "\\")
|
|
112
|
+
j += 2;
|
|
113
|
+
else if (d === "$" && command[j + 1] === "(") {
|
|
114
|
+
const end = closingParen(command, j + 2);
|
|
115
|
+
if (end === undefined)
|
|
116
|
+
return undefined;
|
|
117
|
+
j = end + 1;
|
|
118
|
+
}
|
|
119
|
+
else if (d === "`") {
|
|
120
|
+
const end = closingBacktick(command, j + 1);
|
|
121
|
+
if (end === undefined)
|
|
122
|
+
return undefined;
|
|
123
|
+
j = end + 1;
|
|
124
|
+
}
|
|
125
|
+
else
|
|
126
|
+
j++;
|
|
127
|
+
}
|
|
128
|
+
if (j >= command.length)
|
|
129
|
+
return undefined;
|
|
130
|
+
j++;
|
|
131
|
+
}
|
|
132
|
+
else if (c === "`") {
|
|
133
|
+
const end = closingBacktick(command, j + 1);
|
|
134
|
+
if (end === undefined)
|
|
135
|
+
return undefined;
|
|
136
|
+
j = end + 1;
|
|
137
|
+
}
|
|
138
|
+
else if (c === "<" && command[j + 1] === "<" && command[j + 2] !== "<") {
|
|
139
|
+
const strip = command[j + 2] === "-";
|
|
140
|
+
const read = heredocDelimiter(command, j + (strip ? 3 : 2));
|
|
141
|
+
if (!read)
|
|
142
|
+
return undefined;
|
|
143
|
+
here.push({ delimiter: read.delimiter, strip });
|
|
144
|
+
j = read.next;
|
|
145
|
+
}
|
|
146
|
+
else if (c === "\n" && here.length) {
|
|
147
|
+
let at = j + 1;
|
|
148
|
+
for (const h of here.splice(0)) {
|
|
149
|
+
const body = heredocBody(command, at, h.delimiter, h.strip);
|
|
150
|
+
if (!body)
|
|
151
|
+
return undefined;
|
|
152
|
+
at = body.next;
|
|
153
|
+
}
|
|
154
|
+
j = at;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
if (c === "(")
|
|
158
|
+
depth++;
|
|
159
|
+
else if (c === ")" && --depth === 0)
|
|
160
|
+
return j;
|
|
161
|
+
j++;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Reads the expansion that starts at `command[at] === "$"`, or undefined for one that is not
|
|
168
|
+
* understood. Only HOME has a value known before anything runs; `$?`, `$#`, `$$` and PIPESTATUS
|
|
169
|
+
* entries vary (and an out-of-range array element is empty), so they are unknown.
|
|
170
|
+
*/
|
|
171
|
+
function expansion(command, at, quoted = false) {
|
|
172
|
+
const rest = command.slice(at + 1);
|
|
173
|
+
// A `$` that cannot start an expansion is an ordinary character: the end of a regex such as
|
|
174
|
+
// "^\s*$" or "foo$". `$'...'` and `$"..."` are not, so those stay unrecognised.
|
|
175
|
+
const after = rest[0];
|
|
176
|
+
// `$[`, `$=`, `$^` and `$~` are arithmetic or zsh expansions, so they are not in this set.
|
|
177
|
+
const literal = quoted ? /[\s"\\/.:,;)|&<>\]}+%]/ : /[\s/.:,;)|&<>\]}+%]/;
|
|
178
|
+
if (after !== undefined ? literal.test(after) : !quoted)
|
|
179
|
+
return { kind: "text", text: "$", next: at + 1 };
|
|
180
|
+
const home = () => {
|
|
181
|
+
const path = homedir();
|
|
182
|
+
// The text is inserted as-is and never re-parsed, so a backslash (a Windows separator) is
|
|
183
|
+
// harmless; whitespace and glob characters would split or expand when unquoted.
|
|
184
|
+
return /[\s*?[\]'"$`]/.test(path) ? undefined : path;
|
|
185
|
+
};
|
|
186
|
+
if (rest[0] === "(") {
|
|
187
|
+
if (rest[1] === "(")
|
|
188
|
+
return undefined; // arithmetic expansion
|
|
189
|
+
const end = closingParen(command, at + 2);
|
|
190
|
+
if (end === undefined)
|
|
191
|
+
return undefined;
|
|
192
|
+
return { kind: "unknown", next: end + 1, inner: command.slice(at + 2, end) };
|
|
193
|
+
}
|
|
194
|
+
let name;
|
|
195
|
+
let next;
|
|
196
|
+
if (rest[0] === "{") {
|
|
197
|
+
const close = rest.indexOf("}");
|
|
198
|
+
name = close > 0 ? rest.slice(1, close) : undefined;
|
|
199
|
+
next = at + close + 2;
|
|
200
|
+
if (name === undefined || (!NAME.test(name) && !UNKNOWN_PARAMETER.test(name)))
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
else if (rest[0] && "?#$".includes(rest[0])) {
|
|
204
|
+
return { kind: "unknown", next: at + 2 };
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
name = /^[A-Za-z_][A-Za-z0-9_]*/.exec(rest)?.[0];
|
|
208
|
+
if (!name)
|
|
209
|
+
return undefined;
|
|
210
|
+
next = at + 1 + name.length;
|
|
211
|
+
}
|
|
212
|
+
if (UNKNOWN_PARAMETER.test(name))
|
|
213
|
+
return { kind: "unknown", next };
|
|
214
|
+
if (name === "HOME") {
|
|
215
|
+
const text = home();
|
|
216
|
+
return text === undefined ? undefined : { kind: "text", text, next };
|
|
217
|
+
}
|
|
218
|
+
return { kind: "var", name, next };
|
|
219
|
+
}
|
|
220
|
+
function tokenize(src) {
|
|
221
|
+
const tokens = [];
|
|
222
|
+
const pendingHere = [];
|
|
223
|
+
let parts = [];
|
|
224
|
+
let lit = "";
|
|
225
|
+
let subs = [];
|
|
226
|
+
let active = false;
|
|
227
|
+
let quotedAny = false;
|
|
228
|
+
let headPlain = true;
|
|
229
|
+
let assignName;
|
|
230
|
+
let braceChar = false;
|
|
231
|
+
let globChar = false;
|
|
232
|
+
let quote = "";
|
|
233
|
+
let pendingRedirect;
|
|
234
|
+
const flush = () => {
|
|
235
|
+
if (lit) {
|
|
236
|
+
parts.push(lit);
|
|
237
|
+
lit = "";
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
const noAssign = () => {
|
|
241
|
+
if (assignName === undefined)
|
|
242
|
+
headPlain = false;
|
|
243
|
+
};
|
|
244
|
+
const endWord = () => {
|
|
245
|
+
if (!active)
|
|
246
|
+
return;
|
|
247
|
+
flush();
|
|
248
|
+
const bare = parts.length === 1 && (parts[0] === "{" || parts[0] === "}");
|
|
249
|
+
if (braceChar && !(bare && !quotedAny && assignName === undefined))
|
|
250
|
+
reject();
|
|
251
|
+
const word = { parts, subs, quoted: quotedAny, assign: assignName, glob: globChar };
|
|
252
|
+
parts = [];
|
|
253
|
+
subs = [];
|
|
254
|
+
active = false;
|
|
255
|
+
quotedAny = false;
|
|
256
|
+
headPlain = true;
|
|
257
|
+
assignName = undefined;
|
|
258
|
+
braceChar = false;
|
|
259
|
+
globChar = false;
|
|
260
|
+
if (pendingRedirect) {
|
|
261
|
+
tokens.push({ t: "redir", redir: { ...pendingRedirect, target: word } });
|
|
262
|
+
pendingRedirect = undefined;
|
|
263
|
+
}
|
|
264
|
+
else
|
|
265
|
+
tokens.push({ t: "word", w: word });
|
|
266
|
+
};
|
|
267
|
+
const operator = (op) => {
|
|
268
|
+
endWord();
|
|
269
|
+
if (pendingRedirect)
|
|
270
|
+
reject();
|
|
271
|
+
tokens.push({ t: "op", op });
|
|
272
|
+
};
|
|
273
|
+
const appendUnknown = (quoted, inner) => {
|
|
274
|
+
flush();
|
|
275
|
+
parts.push(inner === undefined ? { unknown: true, quoted } : { unknown: true, quoted, inner });
|
|
276
|
+
if (inner !== undefined)
|
|
277
|
+
subs.push(inner);
|
|
278
|
+
};
|
|
279
|
+
const appendExpansion = (e, quoted) => {
|
|
280
|
+
if (e.kind === "text")
|
|
281
|
+
lit += e.text;
|
|
282
|
+
else if (e.kind === "var") {
|
|
283
|
+
flush();
|
|
284
|
+
parts.push({ name: e.name, quoted });
|
|
285
|
+
}
|
|
286
|
+
else
|
|
287
|
+
appendUnknown(quoted, e.inner);
|
|
288
|
+
};
|
|
289
|
+
for (let i = 0; i < src.length; i++) {
|
|
290
|
+
const char = src[i];
|
|
291
|
+
if (quote) {
|
|
292
|
+
if (char === "\n" && pendingHere.length)
|
|
293
|
+
reject(); // a heredoc body cannot start mid-quote
|
|
294
|
+
if (quote === '"') {
|
|
295
|
+
if (char === "\\") {
|
|
296
|
+
// Inside double quotes a backslash only escapes `"`, `\`, `$` and a backtick; before any
|
|
297
|
+
// other character it is literal. Either way nothing is expanded or executed.
|
|
298
|
+
const next = src[i + 1];
|
|
299
|
+
if (next === undefined)
|
|
300
|
+
reject();
|
|
301
|
+
if (next === "\n") {
|
|
302
|
+
i++; // a backslash-newline is a line continuation: both characters vanish
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if ('"\\$`'.includes(next)) {
|
|
306
|
+
lit += next;
|
|
307
|
+
i++;
|
|
308
|
+
}
|
|
309
|
+
else
|
|
310
|
+
lit += char;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (char === "$") {
|
|
314
|
+
const e = expansion(src, i, true);
|
|
315
|
+
if (!e)
|
|
316
|
+
reject();
|
|
317
|
+
appendExpansion(e, true);
|
|
318
|
+
i = e.next - 1;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (char === "`") {
|
|
322
|
+
const end = closingBacktick(src, i + 1);
|
|
323
|
+
if (end === undefined)
|
|
324
|
+
reject();
|
|
325
|
+
appendUnknown(true, backtickBody(src.slice(i + 1, end)));
|
|
326
|
+
i = end;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (char === quote)
|
|
331
|
+
quote = "";
|
|
332
|
+
else
|
|
333
|
+
lit += char;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
if (char === "'" || char === '"') {
|
|
337
|
+
quote = char;
|
|
338
|
+
active = true;
|
|
339
|
+
quotedAny = true;
|
|
340
|
+
noAssign();
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (char === "\\") {
|
|
344
|
+
// An unquoted backslash makes the next character literal (`\.`, `\|`, `\ `).
|
|
345
|
+
const next = src[i + 1];
|
|
346
|
+
if (next === undefined)
|
|
347
|
+
reject();
|
|
348
|
+
if (next === "\n") {
|
|
349
|
+
i++; // line continuation
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
lit += next;
|
|
353
|
+
active = true;
|
|
354
|
+
quotedAny = true;
|
|
355
|
+
noAssign();
|
|
356
|
+
i++;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (char === "$") {
|
|
360
|
+
const e = expansion(src, i);
|
|
361
|
+
if (!e)
|
|
362
|
+
reject();
|
|
363
|
+
noAssign();
|
|
364
|
+
active = true;
|
|
365
|
+
appendExpansion(e, false);
|
|
366
|
+
i = e.next - 1;
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
if (char === "`") {
|
|
370
|
+
const end = closingBacktick(src, i + 1);
|
|
371
|
+
if (end === undefined)
|
|
372
|
+
reject();
|
|
373
|
+
noAssign();
|
|
374
|
+
active = true;
|
|
375
|
+
appendUnknown(false, backtickBody(src.slice(i + 1, end)));
|
|
376
|
+
i = end;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (char === "#" && !active) {
|
|
380
|
+
// A comment runs to the end of the line; `#` inside a word is an ordinary character.
|
|
381
|
+
while (i + 1 < src.length && src[i + 1] !== "\n")
|
|
382
|
+
i++;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
if (char === "!" &&
|
|
386
|
+
!(active === false && (src[i + 1] === undefined || /\s/.test(src[i + 1])))) {
|
|
387
|
+
reject(); // history expansion and `!` inside words
|
|
388
|
+
}
|
|
389
|
+
if (char === " " || char === "\t") {
|
|
390
|
+
endWord();
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
if (char === "\n") {
|
|
394
|
+
operator("\n");
|
|
395
|
+
if (pendingHere.length) {
|
|
396
|
+
let at = i + 1;
|
|
397
|
+
for (const h of pendingHere.splice(0)) {
|
|
398
|
+
const body = heredocBody(src, at, h.delimiter, h.strip);
|
|
399
|
+
if (!body)
|
|
400
|
+
reject();
|
|
401
|
+
const text = body.body;
|
|
402
|
+
// An unquoted delimiter means the body expands; only a body with nothing to expand is
|
|
403
|
+
// modelled.
|
|
404
|
+
if (!h.quoted && /[$`\\]/.test(text))
|
|
405
|
+
reject();
|
|
406
|
+
h.redir.body = text;
|
|
407
|
+
at = body.next;
|
|
408
|
+
}
|
|
409
|
+
i = at - 1;
|
|
410
|
+
}
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
if (char === ";") {
|
|
414
|
+
if (src[i + 1] === ";") {
|
|
415
|
+
operator(";;");
|
|
416
|
+
i++;
|
|
417
|
+
if (src[i + 1] === "&")
|
|
418
|
+
reject();
|
|
419
|
+
}
|
|
420
|
+
else if (src[i + 1] === "&")
|
|
421
|
+
reject();
|
|
422
|
+
else
|
|
423
|
+
operator(";");
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (char === "&") {
|
|
427
|
+
if (src[i + 1] === "&") {
|
|
428
|
+
operator("&&");
|
|
429
|
+
i++;
|
|
430
|
+
}
|
|
431
|
+
else if (src[i + 1] === ">") {
|
|
432
|
+
endWord();
|
|
433
|
+
if (pendingRedirect)
|
|
434
|
+
reject();
|
|
435
|
+
const append = src[i + 2] === ">";
|
|
436
|
+
pendingRedirect = { fd: "&", op: append ? ">>" : ">" };
|
|
437
|
+
i += append ? 2 : 1;
|
|
438
|
+
}
|
|
439
|
+
else
|
|
440
|
+
operator("&");
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (char === "|") {
|
|
444
|
+
if (src[i + 1] === "|") {
|
|
445
|
+
operator("||");
|
|
446
|
+
i++;
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
operator("|");
|
|
450
|
+
if (src[i + 1] === "&")
|
|
451
|
+
i++;
|
|
452
|
+
}
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (char === "(") {
|
|
456
|
+
if (active || src[i + 1] === "(")
|
|
457
|
+
reject();
|
|
458
|
+
operator("(");
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
if (char === ")") {
|
|
462
|
+
operator(")");
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
if ((char === ">" || char === "<") && active && !quotedAny && assignName === undefined) {
|
|
466
|
+
// bash reads `12>file` as a redirection of descriptor 12; zsh reads `12` as a command. Either
|
|
467
|
+
// way the next word is the command, so a multi-digit prefix is not worth modelling.
|
|
468
|
+
if (parts.length === 0 && /^[0-9]{2,}$/.test(lit))
|
|
469
|
+
reject();
|
|
470
|
+
}
|
|
471
|
+
if (char === "{" || char === "}")
|
|
472
|
+
braceChar = true;
|
|
473
|
+
if (char === "*" || char === "?" || char === "[")
|
|
474
|
+
globChar = true;
|
|
475
|
+
if (char === ">" || char === "<") {
|
|
476
|
+
if (src[i + 1] === "(") {
|
|
477
|
+
// Process substitution: a path to a pipe (`/dev/fd/N`) whose command is judged.
|
|
478
|
+
const end = closingParen(src, i + 2);
|
|
479
|
+
if (end === undefined)
|
|
480
|
+
reject();
|
|
481
|
+
noAssign();
|
|
482
|
+
active = true;
|
|
483
|
+
subs.push(src.slice(i + 2, end));
|
|
484
|
+
lit += "/dev/fd/63";
|
|
485
|
+
i = end;
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
if (char === "<" && src[i + 1] === "<" && src[i + 2] === "<") {
|
|
489
|
+
endWord();
|
|
490
|
+
if (pendingRedirect)
|
|
491
|
+
reject();
|
|
492
|
+
pendingRedirect = { fd: "0", op: "<<<" };
|
|
493
|
+
i += 2;
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
if (char === "<" && src[i + 1] === "<") {
|
|
497
|
+
const strip = src[i + 2] === "-";
|
|
498
|
+
const read = heredocDelimiter(src, i + (strip ? 3 : 2));
|
|
499
|
+
if (!read)
|
|
500
|
+
reject();
|
|
501
|
+
const r = read;
|
|
502
|
+
let fd = "0";
|
|
503
|
+
if (active &&
|
|
504
|
+
!quotedAny &&
|
|
505
|
+
assignName === undefined &&
|
|
506
|
+
parts.length === 0 &&
|
|
507
|
+
/^[0-9]$/.test(lit)) {
|
|
508
|
+
fd = lit;
|
|
509
|
+
lit = "";
|
|
510
|
+
active = false;
|
|
511
|
+
}
|
|
512
|
+
else
|
|
513
|
+
endWord();
|
|
514
|
+
if (pendingRedirect)
|
|
515
|
+
reject();
|
|
516
|
+
const redir = { fd, op: "<<" };
|
|
517
|
+
tokens.push({ t: "redir", redir });
|
|
518
|
+
pendingHere.push({ redir, delimiter: r.delimiter, strip, quoted: r.quoted });
|
|
519
|
+
i = r.next - 1;
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
let fd = char === ">" ? "1" : "0";
|
|
523
|
+
if (active &&
|
|
524
|
+
!quotedAny &&
|
|
525
|
+
assignName === undefined &&
|
|
526
|
+
parts.length === 0 &&
|
|
527
|
+
/^[0-9]$/.test(lit)) {
|
|
528
|
+
fd = lit;
|
|
529
|
+
lit = "";
|
|
530
|
+
active = false;
|
|
531
|
+
}
|
|
532
|
+
else
|
|
533
|
+
endWord();
|
|
534
|
+
let op = char;
|
|
535
|
+
if (char === ">" && src[i + 1] === ">") {
|
|
536
|
+
op = ">>";
|
|
537
|
+
i++;
|
|
538
|
+
}
|
|
539
|
+
else if (char === ">" && src[i + 1] === "&") {
|
|
540
|
+
op = ">&";
|
|
541
|
+
i++;
|
|
542
|
+
}
|
|
543
|
+
else if (char === ">" && src[i + 1] === "|")
|
|
544
|
+
reject();
|
|
545
|
+
else if (char === "<" && (src[i + 1] === "&" || src[i + 1] === ">"))
|
|
546
|
+
reject();
|
|
547
|
+
if (pendingRedirect)
|
|
548
|
+
reject();
|
|
549
|
+
pendingRedirect = { fd, op };
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (headPlain && assignName === undefined && parts.length === 0 && char === "=") {
|
|
553
|
+
if (NAME.test(lit)) {
|
|
554
|
+
assignName = lit;
|
|
555
|
+
lit = "";
|
|
556
|
+
headPlain = false;
|
|
557
|
+
active = true;
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
headPlain = false;
|
|
561
|
+
}
|
|
562
|
+
if (assignName === undefined && !/[A-Za-z0-9_]/.test(char))
|
|
563
|
+
headPlain = false;
|
|
564
|
+
lit += char;
|
|
565
|
+
active = true;
|
|
566
|
+
}
|
|
567
|
+
if (quote || pendingHere.length)
|
|
568
|
+
reject();
|
|
569
|
+
endWord();
|
|
570
|
+
if (pendingRedirect)
|
|
571
|
+
reject();
|
|
572
|
+
return tokens;
|
|
573
|
+
}
|
|
574
|
+
// --- parser -----------------------------------------------------------------------------------
|
|
575
|
+
const KEYWORDS = new Set([
|
|
576
|
+
"if",
|
|
577
|
+
"then",
|
|
578
|
+
"elif",
|
|
579
|
+
"else",
|
|
580
|
+
"fi",
|
|
581
|
+
"for",
|
|
582
|
+
"in",
|
|
583
|
+
"do",
|
|
584
|
+
"done",
|
|
585
|
+
"while",
|
|
586
|
+
"until",
|
|
587
|
+
"case",
|
|
588
|
+
"esac",
|
|
589
|
+
"{",
|
|
590
|
+
"}",
|
|
591
|
+
"function",
|
|
592
|
+
"select",
|
|
593
|
+
"coproc",
|
|
594
|
+
// zsh forms that run the rest of the line or a loop body in the current shell
|
|
595
|
+
"repeat",
|
|
596
|
+
"foreach",
|
|
597
|
+
"always",
|
|
598
|
+
]);
|
|
599
|
+
function plainWord(w) {
|
|
600
|
+
if (w.quoted || w.assign !== undefined || w.parts.length !== 1)
|
|
601
|
+
return undefined;
|
|
602
|
+
const only = w.parts[0];
|
|
603
|
+
return typeof only === "string" ? only : undefined;
|
|
604
|
+
}
|
|
605
|
+
/** A `NAME=value` word used as an argument keeps its text. */
|
|
606
|
+
function asArgument(w) {
|
|
607
|
+
return w.assign === undefined
|
|
608
|
+
? w
|
|
609
|
+
: { ...w, assign: undefined, parts: [`${w.assign}=`, ...w.parts] };
|
|
610
|
+
}
|
|
611
|
+
class Parser {
|
|
612
|
+
tokens;
|
|
613
|
+
pos = 0;
|
|
614
|
+
nodes = 0;
|
|
615
|
+
constructor(tokens) {
|
|
616
|
+
this.tokens = tokens;
|
|
617
|
+
}
|
|
618
|
+
parseScript() {
|
|
619
|
+
const list = this.list(new Set(), 0);
|
|
620
|
+
if (this.pos < this.tokens.length)
|
|
621
|
+
reject();
|
|
622
|
+
return list;
|
|
623
|
+
}
|
|
624
|
+
peek() {
|
|
625
|
+
return this.tokens[this.pos];
|
|
626
|
+
}
|
|
627
|
+
keyword(tok) {
|
|
628
|
+
if (tok?.t !== "word")
|
|
629
|
+
return undefined;
|
|
630
|
+
const text = plainWord(tok.w);
|
|
631
|
+
return text !== undefined && KEYWORDS.has(text) ? text : undefined;
|
|
632
|
+
}
|
|
633
|
+
atTerminator(terms) {
|
|
634
|
+
const tok = this.peek();
|
|
635
|
+
if (!tok)
|
|
636
|
+
return false;
|
|
637
|
+
if (tok.t === "op")
|
|
638
|
+
return terms.has(tok.op);
|
|
639
|
+
return tok.t === "word" && terms.has(plainWord(tok.w) ?? "\0");
|
|
640
|
+
}
|
|
641
|
+
expectKeyword(word) {
|
|
642
|
+
const tok = this.peek();
|
|
643
|
+
if (tok?.t !== "word" || plainWord(tok.w) !== word)
|
|
644
|
+
reject();
|
|
645
|
+
this.pos++;
|
|
646
|
+
}
|
|
647
|
+
skipNewlines() {
|
|
648
|
+
while (this.peek()?.t === "op" && this.peek().op === "\n")
|
|
649
|
+
this.pos++;
|
|
650
|
+
}
|
|
651
|
+
list(terms, depth) {
|
|
652
|
+
const items = [];
|
|
653
|
+
for (;;) {
|
|
654
|
+
for (;;) {
|
|
655
|
+
const t = this.peek();
|
|
656
|
+
if (t?.t === "op" && (t.op === ";" || t.op === "\n"))
|
|
657
|
+
this.pos++;
|
|
658
|
+
else
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
if (this.pos >= this.tokens.length || this.atTerminator(terms))
|
|
662
|
+
break;
|
|
663
|
+
const node = this.andOr(depth);
|
|
664
|
+
let background = false;
|
|
665
|
+
const next = this.peek();
|
|
666
|
+
if (next?.t === "op" && next.op === "&") {
|
|
667
|
+
background = true;
|
|
668
|
+
this.pos++;
|
|
669
|
+
}
|
|
670
|
+
else if (next &&
|
|
671
|
+
!this.atTerminator(terms) &&
|
|
672
|
+
!(next.t === "op" && (next.op === ";" || next.op === "\n")))
|
|
673
|
+
reject();
|
|
674
|
+
items.push({ node, background });
|
|
675
|
+
}
|
|
676
|
+
return { items };
|
|
677
|
+
}
|
|
678
|
+
andOr(depth) {
|
|
679
|
+
const first = this.pipeline(depth);
|
|
680
|
+
const rest = [];
|
|
681
|
+
for (;;) {
|
|
682
|
+
const t = this.peek();
|
|
683
|
+
if (t?.t !== "op" || (t.op !== "&&" && t.op !== "||"))
|
|
684
|
+
break;
|
|
685
|
+
this.pos++;
|
|
686
|
+
this.skipNewlines();
|
|
687
|
+
rest.push({ op: t.op, node: this.pipeline(depth) });
|
|
688
|
+
}
|
|
689
|
+
return { first, rest };
|
|
690
|
+
}
|
|
691
|
+
pipeline(depth) {
|
|
692
|
+
let negated = false;
|
|
693
|
+
const first = this.peek();
|
|
694
|
+
if (first?.t === "word" && plainWord(first.w) === "!") {
|
|
695
|
+
negated = true;
|
|
696
|
+
this.pos++;
|
|
697
|
+
}
|
|
698
|
+
const stages = [this.command(depth)];
|
|
699
|
+
for (;;) {
|
|
700
|
+
const t = this.peek();
|
|
701
|
+
if (t?.t !== "op" || t.op !== "|")
|
|
702
|
+
break;
|
|
703
|
+
this.pos++;
|
|
704
|
+
this.skipNewlines();
|
|
705
|
+
stages.push(this.command(depth));
|
|
706
|
+
}
|
|
707
|
+
return { stages, negated };
|
|
708
|
+
}
|
|
709
|
+
redirects() {
|
|
710
|
+
const out = [];
|
|
711
|
+
for (let t = this.peek(); t?.t === "redir"; t = this.peek()) {
|
|
712
|
+
out.push(t.redir);
|
|
713
|
+
this.pos++;
|
|
714
|
+
}
|
|
715
|
+
return out;
|
|
716
|
+
}
|
|
717
|
+
command(depth) {
|
|
718
|
+
if (++this.nodes > MAX_NODES)
|
|
719
|
+
reject();
|
|
720
|
+
const tok = this.peek();
|
|
721
|
+
if (!tok)
|
|
722
|
+
reject();
|
|
723
|
+
if (tok?.t === "op") {
|
|
724
|
+
if (tok.op !== "(")
|
|
725
|
+
reject();
|
|
726
|
+
if (depth >= MAX_DEPTH)
|
|
727
|
+
reject();
|
|
728
|
+
this.pos++;
|
|
729
|
+
const body = this.list(new Set([")"]), depth + 1);
|
|
730
|
+
if (this.peek()?.t !== "op" || this.peek().op !== ")")
|
|
731
|
+
reject();
|
|
732
|
+
this.pos++;
|
|
733
|
+
return { kind: "subshell", body, redirects: this.redirects() };
|
|
734
|
+
}
|
|
735
|
+
const word = this.keyword(tok);
|
|
736
|
+
if (word === undefined)
|
|
737
|
+
return this.simple();
|
|
738
|
+
if (depth >= MAX_DEPTH)
|
|
739
|
+
reject();
|
|
740
|
+
this.pos++;
|
|
741
|
+
switch (word) {
|
|
742
|
+
case "{": {
|
|
743
|
+
const body = this.list(new Set(["}"]), depth + 1);
|
|
744
|
+
this.expectKeyword("}");
|
|
745
|
+
return { kind: "group", body, redirects: this.redirects() };
|
|
746
|
+
}
|
|
747
|
+
case "for": {
|
|
748
|
+
const nameTok = this.peek();
|
|
749
|
+
const name = nameTok?.t === "word" ? plainWord(nameTok.w) : undefined;
|
|
750
|
+
if (name === undefined || !NAME.test(name) || KEYWORDS.has(name))
|
|
751
|
+
reject();
|
|
752
|
+
this.pos++;
|
|
753
|
+
this.expectKeyword("in");
|
|
754
|
+
const words = [];
|
|
755
|
+
for (;;) {
|
|
756
|
+
const t = this.peek();
|
|
757
|
+
if (t?.t !== "word")
|
|
758
|
+
break;
|
|
759
|
+
words.push(asArgument(t.w));
|
|
760
|
+
this.pos++;
|
|
761
|
+
}
|
|
762
|
+
const sep = this.peek();
|
|
763
|
+
if (sep?.t !== "op" || (sep.op !== ";" && sep.op !== "\n"))
|
|
764
|
+
reject();
|
|
765
|
+
this.skipSeparators();
|
|
766
|
+
this.expectKeyword("do");
|
|
767
|
+
const body = this.list(new Set(["done"]), depth + 1);
|
|
768
|
+
this.expectKeyword("done");
|
|
769
|
+
return { kind: "for", name: name, words, body, redirects: this.redirects() };
|
|
770
|
+
}
|
|
771
|
+
case "while":
|
|
772
|
+
case "until": {
|
|
773
|
+
const cond = this.list(new Set(["do"]), depth + 1);
|
|
774
|
+
this.expectKeyword("do");
|
|
775
|
+
const body = this.list(new Set(["done"]), depth + 1);
|
|
776
|
+
this.expectKeyword("done");
|
|
777
|
+
return { kind: "while", cond, body, redirects: this.redirects() };
|
|
778
|
+
}
|
|
779
|
+
case "if": {
|
|
780
|
+
const clauses = [];
|
|
781
|
+
let otherwise;
|
|
782
|
+
for (;;) {
|
|
783
|
+
const cond = this.list(new Set(["then"]), depth + 1);
|
|
784
|
+
this.expectKeyword("then");
|
|
785
|
+
const body = this.list(new Set(["elif", "else", "fi"]), depth + 1);
|
|
786
|
+
clauses.push({ cond, body });
|
|
787
|
+
const next = this.peek();
|
|
788
|
+
const kw = next?.t === "word" ? plainWord(next.w) : undefined;
|
|
789
|
+
if (kw === "elif") {
|
|
790
|
+
this.pos++;
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
if (kw === "else") {
|
|
794
|
+
this.pos++;
|
|
795
|
+
otherwise = this.list(new Set(["fi"]), depth + 1);
|
|
796
|
+
}
|
|
797
|
+
this.expectKeyword("fi");
|
|
798
|
+
break;
|
|
799
|
+
}
|
|
800
|
+
return { kind: "if", clauses, otherwise, redirects: this.redirects() };
|
|
801
|
+
}
|
|
802
|
+
case "case": {
|
|
803
|
+
const subject = this.peek();
|
|
804
|
+
if (subject?.t !== "word")
|
|
805
|
+
reject();
|
|
806
|
+
this.pos++;
|
|
807
|
+
this.expectKeyword("in");
|
|
808
|
+
const branches = [];
|
|
809
|
+
for (;;) {
|
|
810
|
+
this.skipSeparators();
|
|
811
|
+
const t = this.peek();
|
|
812
|
+
if (t?.t === "word" && plainWord(t.w) === "esac") {
|
|
813
|
+
this.pos++;
|
|
814
|
+
break;
|
|
815
|
+
}
|
|
816
|
+
if (t?.t === "op" && t.op === "(")
|
|
817
|
+
this.pos++;
|
|
818
|
+
const patterns = [];
|
|
819
|
+
for (;;) {
|
|
820
|
+
const p = this.peek();
|
|
821
|
+
if (p?.t !== "word")
|
|
822
|
+
reject();
|
|
823
|
+
patterns.push(asArgument(p.w));
|
|
824
|
+
this.pos++;
|
|
825
|
+
const sep = this.peek();
|
|
826
|
+
if (sep?.t === "op" && sep.op === "|") {
|
|
827
|
+
this.pos++;
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
break;
|
|
831
|
+
}
|
|
832
|
+
const close = this.peek();
|
|
833
|
+
if (close?.t !== "op" || close.op !== ")")
|
|
834
|
+
reject();
|
|
835
|
+
this.pos++;
|
|
836
|
+
const body = this.list(new Set([";;", "esac"]), depth + 1);
|
|
837
|
+
branches.push({ patterns, body });
|
|
838
|
+
const end = this.peek();
|
|
839
|
+
if (end?.t === "op" && end.op === ";;")
|
|
840
|
+
this.pos++;
|
|
841
|
+
}
|
|
842
|
+
return {
|
|
843
|
+
kind: "case",
|
|
844
|
+
word: asArgument(subject.w),
|
|
845
|
+
branches,
|
|
846
|
+
redirects: this.redirects(),
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
default:
|
|
850
|
+
return reject(); // then, fi, do, done, esac, in, }, function, select, coproc out of place
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
skipSeparators() {
|
|
854
|
+
for (;;) {
|
|
855
|
+
const t = this.peek();
|
|
856
|
+
if (t?.t === "op" && (t.op === ";" || t.op === "\n"))
|
|
857
|
+
this.pos++;
|
|
858
|
+
else
|
|
859
|
+
break;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
simple() {
|
|
863
|
+
const assigns = [];
|
|
864
|
+
const argv = [];
|
|
865
|
+
const redirects = [];
|
|
866
|
+
for (let t = this.peek(); t; t = this.peek()) {
|
|
867
|
+
if (t.t === "op")
|
|
868
|
+
break;
|
|
869
|
+
if (t.t === "redir")
|
|
870
|
+
redirects.push(t.redir);
|
|
871
|
+
else if (argv.length === 0 && t.w.assign !== undefined)
|
|
872
|
+
assigns.push({ name: t.w.assign, value: t.w });
|
|
873
|
+
else {
|
|
874
|
+
// A program name never contains `=`; a leading word that does is an assignment this
|
|
875
|
+
// parser cannot represent (`FOO+=x`, `FOO[0]=x`, quoted or escaped names).
|
|
876
|
+
if (argv.length === 0 && t.w.parts.some((p) => typeof p === "string" && p.includes("=")))
|
|
877
|
+
reject();
|
|
878
|
+
argv.push(asArgument(t.w));
|
|
879
|
+
}
|
|
880
|
+
this.pos++;
|
|
881
|
+
}
|
|
882
|
+
const after = this.peek();
|
|
883
|
+
if (after?.t === "op" && after.op === "(")
|
|
884
|
+
reject();
|
|
885
|
+
if (!assigns.length && !argv.length && !redirects.length)
|
|
886
|
+
reject();
|
|
887
|
+
// zsh also recognises a reserved word after leading assignments or redirections, so a command
|
|
888
|
+
// named like one is not a plain command here.
|
|
889
|
+
const head = argv[0] ? plainWord(argv[0]) : undefined;
|
|
890
|
+
if (head !== undefined && KEYWORDS.has(head))
|
|
891
|
+
reject();
|
|
892
|
+
return { kind: "simple", assigns, argv, redirects };
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
export function parseScript(command) {
|
|
896
|
+
if (!command ||
|
|
897
|
+
command.length > MAX_INPUT ||
|
|
898
|
+
command.includes("\0") ||
|
|
899
|
+
command.includes("\r") ||
|
|
900
|
+
command.includes(SUBSTITUTION))
|
|
901
|
+
return undefined;
|
|
902
|
+
try {
|
|
903
|
+
const script = new Parser(tokenize(command)).parseScript();
|
|
904
|
+
return script.items.length ? script : undefined;
|
|
905
|
+
}
|
|
906
|
+
catch (error) {
|
|
907
|
+
if (error instanceof Reject)
|
|
908
|
+
return undefined;
|
|
909
|
+
throw error;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
//# sourceMappingURL=script.js.map
|