@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/dist/shell.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { SUBSTITUTION } from "./script.js";
1
2
  export interface Redirect {
2
3
  fd: string;
3
4
  op: ">" | ">>" | "<" | ">&";
@@ -7,6 +8,12 @@ export interface SimpleCommand {
7
8
  env: Array<[string, string]>;
8
9
  argv: string[];
9
10
  redirects: Redirect[];
11
+ /** Inner commands of `$(...)`, backticks and process substitutions in this command's words. */
12
+ subs: string[];
13
+ /** For each argv word, whether it contains an unquoted unknown value (it could split). */
14
+ loose?: boolean[];
15
+ /** Literal text fed to standard input by a heredoc or here-string, when there is one. */
16
+ stdin?: string;
10
17
  }
11
18
  export type Pipeline = SimpleCommand[];
12
19
  export declare function parseShell(command: string): Pipeline[] | undefined;
package/dist/shell.js CHANGED
@@ -1,201 +1,92 @@
1
- // Parses a bounded subset of POSIX shell: simple commands joined by `;`, newlines, `&&`, `||`
2
- // and `|`, with quoting, leading VAR=value assignments and file redirections. Anything whose
3
- // effects depend on expansion (`$`, backticks, subshells, braces, heredocs, background jobs,
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
- export function parseShell(command) {
7
- if (!command || command.includes("\0") || command.includes("\r"))
1
+ // Compatibility view of the script parser for callers that only handle plain commands: simple
2
+ // commands joined by `;`, newlines, `&&`, `||` and `|`, with literal words. Anything else
3
+ // (variables, compound commands, heredocs, background jobs) returns undefined. The evaluator uses
4
+ // `parseScript` directly.
5
+ import { parseScript, SUBSTITUTION, } from "./script.js";
6
+ export { SUBSTITUTION } from "./script.js";
7
+ /** Text of a word with no variable references; unknown parts become SUBSTITUTION. */
8
+ function literalText(word) {
9
+ let text = "";
10
+ for (const part of word.parts) {
11
+ if (typeof part === "string")
12
+ text += part;
13
+ else if ("unknown" in part)
14
+ text += SUBSTITUTION;
15
+ else
16
+ return undefined;
17
+ }
18
+ return text;
19
+ }
20
+ function plainRedirect(redir) {
21
+ if (redir.op === "<<" || redir.op === "<<<" || !redir.target)
22
+ return undefined;
23
+ const target = literalText(redir.target);
24
+ return target === undefined ? undefined : { fd: redir.fd, op: redir.op, target };
25
+ }
26
+ function plainSimple(command) {
27
+ if (command.kind !== "simple")
28
+ return undefined;
29
+ const simple = command;
30
+ if (!simple.argv.length)
8
31
  return undefined;
32
+ const env = [];
33
+ const subs = [];
34
+ for (const { name, value } of simple.assigns) {
35
+ const text = literalText(value);
36
+ if (text === undefined)
37
+ return undefined;
38
+ env.push([name, text]);
39
+ subs.push(...value.subs);
40
+ }
41
+ const argv = [];
42
+ const loose = [];
43
+ for (const word of simple.argv) {
44
+ const text = literalText(word);
45
+ if (text === undefined)
46
+ return undefined;
47
+ argv.push(text);
48
+ loose.push(word.parts.some((p) => typeof p !== "string" && "unknown" in p && !p.quoted));
49
+ subs.push(...word.subs);
50
+ }
51
+ const redirects = [];
52
+ for (const redir of simple.redirects) {
53
+ const plain = plainRedirect(redir);
54
+ if (!plain)
55
+ return undefined;
56
+ redirects.push(plain);
57
+ subs.push(...(redir.target?.subs ?? []));
58
+ }
59
+ return { env, argv, redirects, subs, loose };
60
+ }
61
+ function flatten(andOr) {
9
62
  const pipelines = [];
10
- let pipeline = [];
11
- let words = [];
12
- let redirects = [];
13
- let pendingRedirect;
14
- let current = "";
15
- let active = false;
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;
21
- let quote = "";
22
- let dangling = false;
23
- const endWord = () => {
24
- if (!active)
25
- return true;
26
- const word = { text: current, assignAt: plain ? eqAt : -1 };
27
- current = "";
28
- active = false;
29
- quoted = false;
30
- plain = true;
31
- eqAt = -1;
32
- if (pendingRedirect) {
33
- redirects.push({ ...pendingRedirect, target: word.text });
34
- pendingRedirect = undefined;
35
- }
36
- else {
37
- words.push(word);
38
- dangling = false;
39
- }
40
- return true;
41
- };
42
- const endSimple = () => {
43
- if (pendingRedirect)
44
- return false;
45
- if (!words.length && !redirects.length)
46
- return true;
47
- const env = [];
48
- let index = 0;
49
- while (index < words.length) {
50
- const word = words[index];
51
- if (!word || word.assignAt < 1 || !ASSIGNMENT_NAME.test(word.text.slice(0, word.assignAt)))
52
- break;
53
- env.push([word.text.slice(0, word.assignAt), word.text.slice(word.assignAt + 1)]);
54
- index++;
55
- }
56
- const argv = words.slice(index).map((word) => word.text);
57
- if (!argv.length)
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;
64
- pipeline.push({ env, argv, redirects });
65
- words = [];
66
- redirects = [];
67
- return true;
68
- };
69
- const endPipeline = (allowEmpty) => {
70
- if (!endSimple())
71
- return false;
72
- if (!pipeline.length)
73
- return allowEmpty;
74
- pipelines.push(pipeline);
75
- pipeline = [];
76
- return true;
77
- };
78
- for (let i = 0; i < command.length; i++) {
79
- const char = command[i];
80
- if (quote) {
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
- }
99
- if (char === quote)
100
- quote = "";
101
- else
102
- current += char;
103
- continue;
104
- }
105
- if (char === "'" || char === '"') {
106
- quote = char;
107
- active = true;
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++;
124
- continue;
125
- }
126
- if (/[`$(){}#!]/.test(char)) {
127
- // `#` only starts a comment at a word boundary, but rejecting it everywhere is simpler.
63
+ for (const node of [andOr.first, ...andOr.rest.map((r) => r.node)]) {
64
+ if (node.negated)
128
65
  return undefined;
129
- }
130
- if (char === " " || char === "\t") {
131
- endWord();
132
- continue;
133
- }
134
- if (char === "\n" || char === ";") {
135
- endWord();
136
- if (dangling || !endPipeline(true))
137
- return undefined;
138
- continue;
139
- }
140
- if (char === "&") {
141
- if (command[i + 1] !== "&")
66
+ const stages = [];
67
+ for (const stage of node.stages) {
68
+ const simple = plainSimple(stage);
69
+ if (!simple)
142
70
  return undefined;
143
- endWord();
144
- if (!endPipeline(false))
145
- return undefined;
146
- dangling = true;
147
- i++;
148
- continue;
149
- }
150
- if (char === "|") {
151
- endWord();
152
- if (command[i + 1] === "|") {
153
- if (!endPipeline(false))
154
- return undefined;
155
- i++;
156
- }
157
- else if (!words.length || !endSimple())
158
- return undefined;
159
- dangling = true;
160
- continue;
71
+ stages.push(simple);
161
72
  }
162
- if (char === ">" || char === "<") {
163
- if (command[i + 1] === "(" || (char === "<" && command[i + 1] === "<"))
164
- return undefined;
165
- let fd = char === ">" ? "1" : "0";
166
- if (active && !quoted && /^[0-9]$/.test(current)) {
167
- fd = current;
168
- current = "";
169
- active = false;
170
- }
171
- else
172
- endWord();
173
- let op = char;
174
- if (char === ">" && command[i + 1] === ">") {
175
- op = ">>";
176
- i++;
177
- }
178
- else if (char === ">" && command[i + 1] === "&") {
179
- op = ">&";
180
- i++;
181
- }
182
- else if (char === ">" && command[i + 1] === "|")
183
- return undefined;
184
- if (pendingRedirect)
185
- return undefined;
186
- pendingRedirect = { fd, op };
187
- continue;
188
- }
189
- if (plain && eqAt < 0 && char === "=")
190
- eqAt = current.length;
191
- current += char;
192
- active = true;
73
+ pipelines.push(stages);
193
74
  }
194
- if (quote)
195
- return undefined;
196
- endWord();
197
- if (dangling || !endPipeline(true))
75
+ return pipelines;
76
+ }
77
+ export function parseShell(command) {
78
+ const script = parseScript(command);
79
+ if (!script)
198
80
  return undefined;
81
+ const pipelines = [];
82
+ for (const item of script.items) {
83
+ if (item.background)
84
+ return undefined;
85
+ const flat = flatten(item.node);
86
+ if (!flat)
87
+ return undefined;
88
+ pipelines.push(...flat);
89
+ }
199
90
  return pipelines.length ? pipelines : undefined;
200
91
  }
201
92
  //# sourceMappingURL=shell.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-corpus/policy-manager",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Deterministic, session-scoped permission decisions with audit logs and bounded AI classification.",
5
5
  "keywords": [
6
6
  "coding-agent",