@agent-corpus/policy-manager 0.2.0 → 0.2.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/dist/permissive.js +2 -1
- package/dist/shell.js +52 -9
- package/package.json +1 -1
package/dist/permissive.js
CHANGED
|
@@ -111,7 +111,8 @@ function unwrap(argv) {
|
|
|
111
111
|
return { argv: current, env };
|
|
112
112
|
let rest = current.slice(1);
|
|
113
113
|
while (rest[0]?.startsWith("-") ||
|
|
114
|
-
|
|
114
|
+
// `env` treats any word with a non-empty name before `=` as an assignment (`A+=1`, `A[0]=1`).
|
|
115
|
+
(program === "env" && /^[^=-][^=]*=/.test(rest[0] ?? "")) ||
|
|
115
116
|
(program === "timeout" && /^[0-9.]+[smhd]?$/.test(rest[0] ?? "")) ||
|
|
116
117
|
(program === "nice" && /^[0-9-]+$/.test(rest[0] ?? ""))) {
|
|
117
118
|
const word = rest[0] ?? "";
|
package/dist/shell.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Parses a bounded subset of POSIX shell: simple commands joined by `;`, newlines, `&&`, `||`
|
|
2
2
|
// and `|`, with quoting, leading VAR=value assignments and file redirections. Anything whose
|
|
3
3
|
// effects depend on expansion (`$`, backticks, subshells, braces, heredocs, background jobs,
|
|
4
|
-
//
|
|
5
|
-
const
|
|
4
|
+
// comments, process substitution) is rejected so a caller can fall back to a human.
|
|
5
|
+
const ASSIGNMENT_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
6
6
|
export function parseShell(command) {
|
|
7
7
|
if (!command || command.includes("\0") || command.includes("\r"))
|
|
8
8
|
return undefined;
|
|
@@ -14,15 +14,21 @@ export function parseShell(command) {
|
|
|
14
14
|
let current = "";
|
|
15
15
|
let active = false;
|
|
16
16
|
let quoted = false;
|
|
17
|
+
// A word is an assignment only if its `NAME=` prefix was typed plainly. Quotes or escapes later,
|
|
18
|
+
// in the value, are fine (`FOO="a b" cmd`); a quote or escape before the `=` is not an assignment.
|
|
19
|
+
let plain = true;
|
|
20
|
+
let eqAt = -1;
|
|
17
21
|
let quote = "";
|
|
18
22
|
let dangling = false;
|
|
19
23
|
const endWord = () => {
|
|
20
24
|
if (!active)
|
|
21
25
|
return true;
|
|
22
|
-
const word = { text: current,
|
|
26
|
+
const word = { text: current, assignAt: plain ? eqAt : -1 };
|
|
23
27
|
current = "";
|
|
24
28
|
active = false;
|
|
25
29
|
quoted = false;
|
|
30
|
+
plain = true;
|
|
31
|
+
eqAt = -1;
|
|
26
32
|
if (pendingRedirect) {
|
|
27
33
|
redirects.push({ ...pendingRedirect, target: word.text });
|
|
28
34
|
pendingRedirect = undefined;
|
|
@@ -42,15 +48,19 @@ export function parseShell(command) {
|
|
|
42
48
|
let index = 0;
|
|
43
49
|
while (index < words.length) {
|
|
44
50
|
const word = words[index];
|
|
45
|
-
if (!word || word.
|
|
51
|
+
if (!word || word.assignAt < 1 || !ASSIGNMENT_NAME.test(word.text.slice(0, word.assignAt)))
|
|
46
52
|
break;
|
|
47
|
-
|
|
48
|
-
env.push([word.text.slice(0, eq), word.text.slice(eq + 1)]);
|
|
53
|
+
env.push([word.text.slice(0, word.assignAt), word.text.slice(word.assignAt + 1)]);
|
|
49
54
|
index++;
|
|
50
55
|
}
|
|
51
56
|
const argv = words.slice(index).map((word) => word.text);
|
|
52
57
|
if (!argv.length)
|
|
53
58
|
return false;
|
|
59
|
+
// Bash also treats `FOO+=x`, `FOO[0]=x` and quoted or escaped forms as assignments and then runs
|
|
60
|
+
// the next word. A program name never contains `=`, so a leading word that still has one is
|
|
61
|
+
// an assignment this parser cannot represent: reject it instead of judging it as the program.
|
|
62
|
+
if (argv[0]?.includes("="))
|
|
63
|
+
return false;
|
|
54
64
|
pipeline.push({ env, argv, redirects });
|
|
55
65
|
words = [];
|
|
56
66
|
redirects = [];
|
|
@@ -68,8 +78,24 @@ export function parseShell(command) {
|
|
|
68
78
|
for (let i = 0; i < command.length; i++) {
|
|
69
79
|
const char = command[i];
|
|
70
80
|
if (quote) {
|
|
71
|
-
if (quote === '"'
|
|
72
|
-
|
|
81
|
+
if (quote === '"') {
|
|
82
|
+
if (char === "\\") {
|
|
83
|
+
// Inside double quotes a backslash only escapes `"`, `\`, `$` and a backtick; before any
|
|
84
|
+
// other character it is literal. Either way nothing is expanded or executed.
|
|
85
|
+
const next = command[i + 1];
|
|
86
|
+
if (next === undefined || next === "\n")
|
|
87
|
+
return undefined;
|
|
88
|
+
if ('"\\$`'.includes(next)) {
|
|
89
|
+
current += next;
|
|
90
|
+
i++;
|
|
91
|
+
}
|
|
92
|
+
else
|
|
93
|
+
current += char;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (char === "$" || char === "`")
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
73
99
|
if (char === quote)
|
|
74
100
|
quote = "";
|
|
75
101
|
else
|
|
@@ -80,9 +106,24 @@ export function parseShell(command) {
|
|
|
80
106
|
quote = char;
|
|
81
107
|
active = true;
|
|
82
108
|
quoted = true;
|
|
109
|
+
if (eqAt < 0)
|
|
110
|
+
plain = false;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (char === "\\") {
|
|
114
|
+
// An unquoted backslash makes the next character literal (`\.`, `\|`, `\ `).
|
|
115
|
+
const next = command[i + 1];
|
|
116
|
+
if (next === undefined || next === "\n")
|
|
117
|
+
return undefined;
|
|
118
|
+
current += next;
|
|
119
|
+
active = true;
|
|
120
|
+
quoted = true;
|
|
121
|
+
if (eqAt < 0)
|
|
122
|
+
plain = false;
|
|
123
|
+
i++;
|
|
83
124
|
continue;
|
|
84
125
|
}
|
|
85
|
-
if (/[`$(){}
|
|
126
|
+
if (/[`$(){}#!]/.test(char)) {
|
|
86
127
|
// `#` only starts a comment at a word boundary, but rejecting it everywhere is simpler.
|
|
87
128
|
return undefined;
|
|
88
129
|
}
|
|
@@ -145,6 +186,8 @@ export function parseShell(command) {
|
|
|
145
186
|
pendingRedirect = { fd, op };
|
|
146
187
|
continue;
|
|
147
188
|
}
|
|
189
|
+
if (plain && eqAt < 0 && char === "=")
|
|
190
|
+
eqAt = current.length;
|
|
148
191
|
current += char;
|
|
149
192
|
active = true;
|
|
150
193
|
}
|
package/package.json
CHANGED