@jalaxy33/pi-permission 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +164 -2
- package/docs/configuration.md +244 -0
- package/index.ts +99 -0
- package/package.json +54 -4
- package/rules/index.json +76 -0
- package/rules/packs/bulk-delete.json +83 -0
- package/rules/packs/database.json +30 -0
- package/rules/packs/dynamic-exec.json +105 -0
- package/rules/packs/git-local.json +81 -0
- package/rules/packs/git-remote.json +27 -0
- package/rules/packs/infra.json +45 -0
- package/rules/packs/irreversible.json +93 -0
- package/rules/packs/privilege.json +50 -0
- package/rules/packs/publish.json +33 -0
- package/rules/packs/secrets.json +43 -0
- package/schema.json +348 -0
- package/src/argv.ts +77 -0
- package/src/config.ts +204 -0
- package/src/decide.ts +200 -0
- package/src/gate.ts +233 -0
- package/src/match.ts +59 -0
- package/src/ops.ts +252 -0
- package/src/outside.ts +87 -0
- package/src/paths.ts +212 -0
- package/src/prefixes.ts +83 -0
- package/src/program.ts +19 -0
- package/src/prompt.ts +275 -0
- package/src/rulesets.ts +454 -0
- package/src/scripts.ts +31 -0
- package/src/tokenize.ts +460 -0
- package/src/types.ts +167 -0
- package/src/unlayer.ts +48 -0
- package/src/validate.ts +125 -0
package/src/tokenize.ts
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quote-aware command splitter.
|
|
3
|
+
*
|
|
4
|
+
* Turns a raw bash command string into simple commands (argv lists) and
|
|
5
|
+
* throws away text that is not a command: heredoc bodies and comments are
|
|
6
|
+
* skipped, and command and process substitutions are tokenized as their own
|
|
7
|
+
* commands. Text the splitter cannot resolve (an unterminated quote, `$(` or
|
|
8
|
+
* heredoc) is reported as a hazard, so the gate can refuse to guess.
|
|
9
|
+
*
|
|
10
|
+
* Every word carries what it lost on the way here: an expansion, a quote, an
|
|
11
|
+
* escape or a glob. A program name that came from one of those is not a name,
|
|
12
|
+
* and the gate treats it as unreadable rather than matching on a guess.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export interface Redirect {
|
|
16
|
+
op: ">" | ">>" | "<";
|
|
17
|
+
target: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** What a word went through before it became text. */
|
|
21
|
+
export interface WordMeta {
|
|
22
|
+
/** `$(...)`, backticks, process substitution, `$VAR`, `$((...))`, `$'...'`. */
|
|
23
|
+
expansion: boolean;
|
|
24
|
+
quote: boolean;
|
|
25
|
+
escape: boolean;
|
|
26
|
+
/** Unquoted `*`, `?` or `[`. */
|
|
27
|
+
glob: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SimpleCommand {
|
|
31
|
+
argv: string[];
|
|
32
|
+
/** Parallel to `argv`. */
|
|
33
|
+
words: WordMeta[];
|
|
34
|
+
redirects: Redirect[];
|
|
35
|
+
/** Body of the first heredoc this command reads (`bash <<EOF`). */
|
|
36
|
+
heredoc?: string;
|
|
37
|
+
/** Text fed on stdin by a here string (`bash <<< "..."`). */
|
|
38
|
+
stdinText?: string;
|
|
39
|
+
/** Stdin comes from a pipe, so it is not visible in the command text. */
|
|
40
|
+
stdinPiped?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Tokenized {
|
|
44
|
+
commands: SimpleCommand[];
|
|
45
|
+
/** Parts of the command text the splitter could not resolve. */
|
|
46
|
+
hazards: string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const BLANK = /\s/;
|
|
50
|
+
|
|
51
|
+
function isSeparator(ch: string): boolean {
|
|
52
|
+
return ch === ";" || ch === "&" || ch === "|" || ch === "\n";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function blankMeta(): WordMeta {
|
|
56
|
+
return { expansion: false, quote: false, escape: false, glob: false };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface Scanned {
|
|
60
|
+
inner: string;
|
|
61
|
+
next: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Reads `(...)` open at `open`; nested parentheses are balanced. */
|
|
65
|
+
function scanParen(input: string, open: number): Scanned | undefined {
|
|
66
|
+
let depth = 1;
|
|
67
|
+
let i = open + 1;
|
|
68
|
+
while (i < input.length) {
|
|
69
|
+
const ch = input[i] as string;
|
|
70
|
+
if (ch === "\\") {
|
|
71
|
+
i += 2;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (ch === "(") depth++;
|
|
75
|
+
else if (ch === ")") {
|
|
76
|
+
depth--;
|
|
77
|
+
if (depth === 0) return { inner: input.slice(open + 1, i), next: i + 1 };
|
|
78
|
+
}
|
|
79
|
+
i++;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Reads a backtick pair open at `open`. */
|
|
85
|
+
function scanBacktick(input: string, open: number): Scanned | undefined {
|
|
86
|
+
let i = open + 1;
|
|
87
|
+
while (i < input.length) {
|
|
88
|
+
const ch = input[i] as string;
|
|
89
|
+
if (ch === "\\") {
|
|
90
|
+
i += 2;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (ch === "`") return { inner: input.slice(open + 1, i), next: i + 1 };
|
|
94
|
+
i++;
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface Quoted {
|
|
100
|
+
text: string;
|
|
101
|
+
next: number;
|
|
102
|
+
/** Substitutions the shell still runs inside the quotes. */
|
|
103
|
+
subs: string[];
|
|
104
|
+
/** The quoted text holds an expansion, so its value is not literal. */
|
|
105
|
+
expansion: boolean;
|
|
106
|
+
unterminated: boolean;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Reads a quoted string starting at `start`. Double quotes honour backslash
|
|
111
|
+
* escapes and keep running `$(...)` and `` `...` ``; single quotes keep
|
|
112
|
+
* everything literally, the way the shell does.
|
|
113
|
+
*/
|
|
114
|
+
function readQuoted(input: string, start: number): Quoted {
|
|
115
|
+
const quote = input[start] as string;
|
|
116
|
+
const subs: string[] = [];
|
|
117
|
+
let expansion = false;
|
|
118
|
+
let text = "";
|
|
119
|
+
let i = start + 1;
|
|
120
|
+
|
|
121
|
+
while (i < input.length && input[i] !== quote) {
|
|
122
|
+
const ch = input[i] as string;
|
|
123
|
+
if (quote === '"') {
|
|
124
|
+
if (ch === "\\") {
|
|
125
|
+
// Inside double quotes bash only removes the backslash before these.
|
|
126
|
+
// Everywhere else it stays, which is what keeps `"C:\\repo"` intact.
|
|
127
|
+
const next = input[i + 1];
|
|
128
|
+
if (next !== undefined && "$`\"\\\n".includes(next)) {
|
|
129
|
+
text += next;
|
|
130
|
+
i += 2;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
text += ch;
|
|
134
|
+
i++;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (ch === "`") {
|
|
138
|
+
const found = scanBacktick(input, i);
|
|
139
|
+
if (!found) return { text, next: input.length, subs, expansion: true, unterminated: true };
|
|
140
|
+
subs.push(found.inner);
|
|
141
|
+
expansion = true;
|
|
142
|
+
i = found.next;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (ch === "$" && input[i + 1] === "(") {
|
|
146
|
+
const found = scanParen(input, i + 1);
|
|
147
|
+
if (!found) return { text, next: input.length, subs, expansion: true, unterminated: true };
|
|
148
|
+
subs.push(found.inner);
|
|
149
|
+
expansion = true;
|
|
150
|
+
i = found.next;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (ch === "$") expansion = true;
|
|
154
|
+
}
|
|
155
|
+
text += ch;
|
|
156
|
+
i++;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (i >= input.length) return { text, next: input.length, subs, expansion: true, unterminated: true };
|
|
160
|
+
return { text, next: i + 1, subs, expansion, unterminated: false };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Index after the closing `))` of an arithmetic expansion. `open` points at the
|
|
165
|
+
* first of the two opening parentheses.
|
|
166
|
+
*/
|
|
167
|
+
function scanArithmetic(input: string, open: number): number | undefined {
|
|
168
|
+
let depth = 2;
|
|
169
|
+
let i = open + 2;
|
|
170
|
+
while (i < input.length) {
|
|
171
|
+
const ch = input[i] as string;
|
|
172
|
+
if (ch === "(") depth++;
|
|
173
|
+
else if (ch === ")") {
|
|
174
|
+
depth--;
|
|
175
|
+
if (depth === 0) return i + 1;
|
|
176
|
+
}
|
|
177
|
+
i++;
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function tokenizeDetailed(input: string): Tokenized {
|
|
183
|
+
const commands: SimpleCommand[] = [];
|
|
184
|
+
const hazards: string[] = [];
|
|
185
|
+
const subs: string[] = [];
|
|
186
|
+
let argv: string[] = [];
|
|
187
|
+
let words: WordMeta[] = [];
|
|
188
|
+
let redirects: Redirect[] = [];
|
|
189
|
+
let word = "";
|
|
190
|
+
let meta = blankMeta();
|
|
191
|
+
let inWord = false;
|
|
192
|
+
let piped = false;
|
|
193
|
+
let pendingStdin: string | undefined;
|
|
194
|
+
let heredocs: string[] = [];
|
|
195
|
+
|
|
196
|
+
const flushWord = () => {
|
|
197
|
+
if (inWord) {
|
|
198
|
+
argv.push(word);
|
|
199
|
+
words.push(meta);
|
|
200
|
+
word = "";
|
|
201
|
+
meta = blankMeta();
|
|
202
|
+
inWord = false;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const endCommand = () => {
|
|
207
|
+
flushWord();
|
|
208
|
+
if (argv.length > 0) {
|
|
209
|
+
const command: SimpleCommand = { argv, words, redirects };
|
|
210
|
+
if (piped) command.stdinPiped = true;
|
|
211
|
+
if (pendingStdin !== undefined) command.stdinText = pendingStdin;
|
|
212
|
+
pendingStdin = undefined;
|
|
213
|
+
commands.push(command);
|
|
214
|
+
piped = false;
|
|
215
|
+
}
|
|
216
|
+
argv = [];
|
|
217
|
+
words = [];
|
|
218
|
+
redirects = [];
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// Skip to the end of a pending heredoc body, keeping the body text.
|
|
222
|
+
const skipHeredoc = (from: number, delimiter: string): { next: number; found: boolean; body: string } => {
|
|
223
|
+
let i = from;
|
|
224
|
+
while (i < input.length) {
|
|
225
|
+
const nl = input.indexOf("\n", i);
|
|
226
|
+
const lineEnd = nl === -1 ? input.length : nl;
|
|
227
|
+
if (input.slice(i, lineEnd).replace(/^\t+/, "").trimEnd() === delimiter) {
|
|
228
|
+
return {
|
|
229
|
+
next: nl === -1 ? input.length : nl + 1,
|
|
230
|
+
found: true,
|
|
231
|
+
body: input.slice(from, i),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (nl === -1) return { next: input.length, found: false, body: input.slice(from) };
|
|
235
|
+
i = nl + 1;
|
|
236
|
+
}
|
|
237
|
+
return { next: input.length, found: false, body: input.slice(from) };
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const readRedirTarget = (from: number): { target: string; next: number; subs: string[] } => {
|
|
241
|
+
let i = from;
|
|
242
|
+
while (i < input.length && (input[i] === " " || input[i] === "\t")) i++;
|
|
243
|
+
let target = "";
|
|
244
|
+
const found: string[] = [];
|
|
245
|
+
while (i < input.length && !BLANK.test(input[i] as string) && !isSeparator(input[i] as string)) {
|
|
246
|
+
const ch = input[i] as string;
|
|
247
|
+
if (ch === "'" || ch === '"') {
|
|
248
|
+
const quoted = readQuoted(input, i);
|
|
249
|
+
target += quoted.text;
|
|
250
|
+
found.push(...quoted.subs);
|
|
251
|
+
i = quoted.next;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
target += ch;
|
|
255
|
+
i++;
|
|
256
|
+
}
|
|
257
|
+
return { target, next: i, subs: found };
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
let i = 0;
|
|
261
|
+
while (i < input.length) {
|
|
262
|
+
const ch = input[i] as string;
|
|
263
|
+
|
|
264
|
+
if (ch === "\\") {
|
|
265
|
+
word += input[i + 1] ?? "";
|
|
266
|
+
meta.escape = true;
|
|
267
|
+
inWord = true;
|
|
268
|
+
i += 2;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (ch === "'" || ch === '"') {
|
|
273
|
+
const quoted = readQuoted(input, i);
|
|
274
|
+
if (quoted.unterminated) hazards.push("unterminated quote");
|
|
275
|
+
subs.push(...quoted.subs);
|
|
276
|
+
word += quoted.text;
|
|
277
|
+
meta.quote = true;
|
|
278
|
+
if (quoted.expansion) meta.expansion = true;
|
|
279
|
+
inWord = true;
|
|
280
|
+
i = quoted.next;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (ch === "`") {
|
|
285
|
+
const found = scanBacktick(input, i);
|
|
286
|
+
if (!found) {
|
|
287
|
+
hazards.push("unterminated backtick");
|
|
288
|
+
i = input.length;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
subs.push(found.inner);
|
|
292
|
+
meta.expansion = true;
|
|
293
|
+
i = found.next;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (ch === "$" && input[i + 1] === "(") {
|
|
298
|
+
// `$((...))` is arithmetic: a value, not a command.
|
|
299
|
+
if (input[i + 2] === "(") {
|
|
300
|
+
const next = scanArithmetic(input, i + 1);
|
|
301
|
+
if (next === undefined) {
|
|
302
|
+
hazards.push("unterminated arithmetic expansion");
|
|
303
|
+
i = input.length;
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
meta.expansion = true;
|
|
307
|
+
i = next;
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
const found = scanParen(input, i + 1);
|
|
311
|
+
if (!found) {
|
|
312
|
+
hazards.push("unterminated command substitution");
|
|
313
|
+
i = input.length;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
subs.push(found.inner);
|
|
317
|
+
meta.expansion = true;
|
|
318
|
+
i = found.next;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// `<(cmd)` and `>(cmd)` run a command, so their text is checked too.
|
|
323
|
+
if ((ch === "<" || ch === ">") && input[i + 1] === "(") {
|
|
324
|
+
const found = scanParen(input, i + 1);
|
|
325
|
+
if (!found) {
|
|
326
|
+
hazards.push("unterminated process substitution");
|
|
327
|
+
i = input.length;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
subs.push(found.inner);
|
|
331
|
+
meta.expansion = true;
|
|
332
|
+
i = found.next;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (ch === "$") {
|
|
337
|
+
// `$VAR`, `${VAR}` and `$'...'`: the word's value is decided at runtime.
|
|
338
|
+
if (/[A-Za-z_{@*#?$!'-]/.test(input[i + 1] ?? "")) meta.expansion = true;
|
|
339
|
+
word += ch;
|
|
340
|
+
inWord = true;
|
|
341
|
+
i++;
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (ch === "<" && input[i + 1] === "<") {
|
|
346
|
+
// `<<<word` feeds `word` on stdin. It is not an operand, so it is read
|
|
347
|
+
// here and attached to the command as data.
|
|
348
|
+
if (input[i + 2] === "<") {
|
|
349
|
+
const redir = readRedirTarget(i + 3);
|
|
350
|
+
subs.push(...redir.subs);
|
|
351
|
+
pendingStdin = redir.target;
|
|
352
|
+
i = redir.next;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
let j = i + 2;
|
|
356
|
+
if (input[j] === "-") j++;
|
|
357
|
+
while (input[j] === " " || input[j] === "\t") j++;
|
|
358
|
+
let delimiter = "";
|
|
359
|
+
if (input[j] === "'" || input[j] === '"') {
|
|
360
|
+
const quoted = readQuoted(input, j);
|
|
361
|
+
delimiter = quoted.text;
|
|
362
|
+
j = quoted.next;
|
|
363
|
+
} else {
|
|
364
|
+
while (j < input.length && !BLANK.test(input[j] as string) && !isSeparator(input[j] as string)) {
|
|
365
|
+
delimiter += input[j];
|
|
366
|
+
j++;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (delimiter) heredocs.push(delimiter);
|
|
370
|
+
i = j;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (ch === "&" && input[i + 1] === ">") {
|
|
375
|
+
// `&>file` / `&>>file` redirect both streams; the `&` belongs to the redirect.
|
|
376
|
+
const append = input[i + 2] === ">";
|
|
377
|
+
flushWord();
|
|
378
|
+
const redir = readRedirTarget(i + (append ? 3 : 2));
|
|
379
|
+
subs.push(...redir.subs);
|
|
380
|
+
redirects.push({ op: append ? ">>" : ">", target: redir.target });
|
|
381
|
+
i = redir.next;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (ch === ">") {
|
|
386
|
+
const append = input[i + 1] === ">";
|
|
387
|
+
// `>|` overrides noclobber and is the same write.
|
|
388
|
+
const clobber = input[i + 1] === "|";
|
|
389
|
+
flushWord();
|
|
390
|
+
const redir = readRedirTarget(i + (append || clobber ? 2 : 1));
|
|
391
|
+
subs.push(...redir.subs);
|
|
392
|
+
redirects.push({ op: append ? ">>" : ">", target: redir.target });
|
|
393
|
+
i = redir.next;
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (ch === "<") {
|
|
398
|
+
flushWord();
|
|
399
|
+
const redir = readRedirTarget(i + 1);
|
|
400
|
+
subs.push(...redir.subs);
|
|
401
|
+
redirects.push({ op: "<", target: redir.target });
|
|
402
|
+
i = redir.next;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (isSeparator(ch)) {
|
|
407
|
+
const pushedTo = commands.length;
|
|
408
|
+
endCommand();
|
|
409
|
+
if (ch === "|") {
|
|
410
|
+
// `||` is logical or; only a single `|` feeds the next command.
|
|
411
|
+
if (input[i + 1] === "|") i += 1;
|
|
412
|
+
else if (commands.length >= pushedTo) piped = true;
|
|
413
|
+
}
|
|
414
|
+
i++;
|
|
415
|
+
if (ch === "\n") {
|
|
416
|
+
for (const delimiter of heredocs) {
|
|
417
|
+
const skipped = skipHeredoc(i, delimiter);
|
|
418
|
+
if (!skipped.found) hazards.push("unterminated heredoc");
|
|
419
|
+
const owner = commands[commands.length - 1];
|
|
420
|
+
if (owner && owner.heredoc === undefined) owner.heredoc = skipped.body;
|
|
421
|
+
i = skipped.next;
|
|
422
|
+
}
|
|
423
|
+
heredocs = [];
|
|
424
|
+
}
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (ch === "#" && !inWord) {
|
|
429
|
+
const nl = input.indexOf("\n", i);
|
|
430
|
+
i = nl === -1 ? input.length : nl;
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (BLANK.test(ch)) {
|
|
435
|
+
flushWord();
|
|
436
|
+
i++;
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (ch === "*" || ch === "?" || ch === "[") meta.glob = true;
|
|
441
|
+
word += ch;
|
|
442
|
+
inWord = true;
|
|
443
|
+
i++;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
endCommand();
|
|
447
|
+
if (heredocs.length > 0) hazards.push("unterminated heredoc");
|
|
448
|
+
|
|
449
|
+
for (const sub of subs) {
|
|
450
|
+
const inner = tokenizeDetailed(sub);
|
|
451
|
+
commands.push(...inner.commands);
|
|
452
|
+
hazards.push(...inner.hazards);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return { commands, hazards };
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export function tokenize(input: string): SimpleCommand[] {
|
|
459
|
+
return tokenizeDetailed(input).commands;
|
|
460
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { OutsideRule } from "./outside";
|
|
2
|
+
|
|
3
|
+
export type Section = "deny" | "ask" | "allow";
|
|
4
|
+
|
|
5
|
+
export const SECTIONS: Section[] = ["deny", "ask", "allow"];
|
|
6
|
+
|
|
7
|
+
/** What a rule match does. Commands, path rules and defaults share the vocabulary. */
|
|
8
|
+
export type Action = "allow" | "ask" | "deny";
|
|
9
|
+
|
|
10
|
+
export type PathAction = Action;
|
|
11
|
+
|
|
12
|
+
export interface Rule {
|
|
13
|
+
id: string;
|
|
14
|
+
/** Program name(s). A `*` wildcard is allowed (e.g. `mkfs.*`). */
|
|
15
|
+
program: string | string[];
|
|
16
|
+
/** Subcommand or operand: matches when any of these appears as a positional. */
|
|
17
|
+
args?: string[];
|
|
18
|
+
/** Program subcommand: matches the first positional, after global value flags. */
|
|
19
|
+
subcommand?: string[];
|
|
20
|
+
/** Matches when any of these flags is present. */
|
|
21
|
+
anyFlag?: string[];
|
|
22
|
+
/** Matches only when all of these flags are present. */
|
|
23
|
+
allFlags?: string[];
|
|
24
|
+
/** Never matches when any of these flags is present. */
|
|
25
|
+
unlessFlag?: string[];
|
|
26
|
+
/** Last resort: regex over the joined argv of one simple command. */
|
|
27
|
+
argvRegex?: string;
|
|
28
|
+
reason?: string;
|
|
29
|
+
terminate?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CompiledRule extends Rule {
|
|
33
|
+
re?: RegExp;
|
|
34
|
+
programRes?: RegExp[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface CompileResult {
|
|
38
|
+
rules: CompiledRule[];
|
|
39
|
+
warnings: string[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface GateRules {
|
|
43
|
+
deny: CompiledRule[];
|
|
44
|
+
ask: CompiledRule[];
|
|
45
|
+
allow: CompiledRule[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface Verdict {
|
|
49
|
+
action: Action;
|
|
50
|
+
reasons: string[];
|
|
51
|
+
/** Session grant keys of the matched ask rules, or of the unmatched fallback. */
|
|
52
|
+
grants: string[];
|
|
53
|
+
terminate: boolean;
|
|
54
|
+
/** False when the verdict comes from `defaultPermission.commands` instead of a rule. */
|
|
55
|
+
matched: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface CommandsSection {
|
|
59
|
+
deny?: Rule[];
|
|
60
|
+
ask?: Rule[];
|
|
61
|
+
allow?: Rule[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A leaf: rules only, never a reference to another ruleset. */
|
|
65
|
+
export interface RuleSet {
|
|
66
|
+
description?: string;
|
|
67
|
+
commands?: CommandsSection;
|
|
68
|
+
paths?: PathRule[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A permission level. Only the shipped index defines presets. */
|
|
72
|
+
export interface Preset {
|
|
73
|
+
description?: string;
|
|
74
|
+
/** Higher is stricter. A preset's members are the previous level plus its additions. */
|
|
75
|
+
level: number;
|
|
76
|
+
/** Ruleset ids, flat. A preset never includes another preset. */
|
|
77
|
+
include: string[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** `{ "id": "git-local", "remove": ["git-add"] }` */
|
|
81
|
+
export interface IncludeEntry {
|
|
82
|
+
id: string;
|
|
83
|
+
/** Rule ids to drop from the referenced ruleset. */
|
|
84
|
+
remove?: string[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export type IncludeRef = string | IncludeEntry;
|
|
88
|
+
export type PresetRef = string | IncludeEntry;
|
|
89
|
+
|
|
90
|
+
export interface DefaultPermission {
|
|
91
|
+
commands?: Action;
|
|
92
|
+
paths?: Action;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface OutsideCwdConfig {
|
|
96
|
+
/** What to do when a tool touches a path outside the working directory. */
|
|
97
|
+
mode?: "allow" | "ask" | "block";
|
|
98
|
+
/** Absolute, `~`, or cwd-relative globs that stay allowed. */
|
|
99
|
+
allowPaths?: string[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A config file. The shipped `rules/index.json` is one too. */
|
|
103
|
+
export interface ConfigFile extends RuleSet {
|
|
104
|
+
$schema?: string;
|
|
105
|
+
/** Named rulesets: inline rules or a path to a ruleset file. Merged by id. */
|
|
106
|
+
rulesets?: Record<string, RuleSet | string>;
|
|
107
|
+
/** Permission levels. Only the shipped index may define them. */
|
|
108
|
+
presets?: Record<string, Preset>;
|
|
109
|
+
/** The active level. The last scope that declares it wins. */
|
|
110
|
+
preset?: PresetRef;
|
|
111
|
+
/** Extra rulesets. The last scope that declares it replaces the list. */
|
|
112
|
+
include?: IncludeRef[];
|
|
113
|
+
defaultPermission?: DefaultPermission;
|
|
114
|
+
outsideCwd?: OutsideCwdConfig;
|
|
115
|
+
enabled?: { commands?: boolean; paths?: boolean };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type PathOp = "read" | "write" | "delete";
|
|
119
|
+
|
|
120
|
+
export interface PathRule {
|
|
121
|
+
id: string;
|
|
122
|
+
/** Globs. No slash: matches the basename. `~`/`/`: absolute. Else relative to cwd. */
|
|
123
|
+
patterns: string[];
|
|
124
|
+
/** Patterns that cancel the rule. */
|
|
125
|
+
except?: string[];
|
|
126
|
+
/** Default: deny. A deny rule on the same path wins over ask and allow. */
|
|
127
|
+
action?: PathAction;
|
|
128
|
+
/** Default: all operations. */
|
|
129
|
+
ops?: PathOp[];
|
|
130
|
+
/** Only protect paths that already exist. Default false. */
|
|
131
|
+
onlyIfExists?: boolean;
|
|
132
|
+
message?: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface CompiledPathRule extends PathRule {
|
|
136
|
+
compiled: Array<{ value: string; kind: "base" | "abs" | "rel"; re: RegExp }>;
|
|
137
|
+
compiledExcept: Array<{ value: string; kind: "base" | "abs" | "rel"; re: RegExp }>;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** One ruleset that ended up in the composition, for `/permission`. */
|
|
141
|
+
export interface ActiveRuleSet {
|
|
142
|
+
id: string;
|
|
143
|
+
source?: string;
|
|
144
|
+
/** Rules contributed after removals. */
|
|
145
|
+
rules: number;
|
|
146
|
+
/** Path rules contributed after removals. */
|
|
147
|
+
paths: number;
|
|
148
|
+
/** Rule ids removed from this ruleset by an include entry. */
|
|
149
|
+
removed: string[];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface ResolvedConfig {
|
|
153
|
+
enabled: { commands: boolean; paths: boolean };
|
|
154
|
+
commands: GateRules;
|
|
155
|
+
paths: CompiledPathRule[];
|
|
156
|
+
outside: OutsideRule;
|
|
157
|
+
defaultPermission: { commands: Action; paths: Action };
|
|
158
|
+
/** Active preset, when one is selected. */
|
|
159
|
+
preset?: { name: string; level: number };
|
|
160
|
+
/** Selected rulesets in composition order. */
|
|
161
|
+
rulesets: ActiveRuleSet[];
|
|
162
|
+
/** Config files read, in load order. */
|
|
163
|
+
sources: string[];
|
|
164
|
+
/** Set when the config cannot be honored: every gated tool is blocked. */
|
|
165
|
+
fatal?: string;
|
|
166
|
+
warnings: string[];
|
|
167
|
+
}
|
package/src/unlayer.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Peels the layers between a command string and the program that runs.
|
|
3
|
+
*
|
|
4
|
+
* Two things hide a program from the rule matcher:
|
|
5
|
+
*
|
|
6
|
+
* - shell keywords, which the splitter leaves in front of the command
|
|
7
|
+
* (`if true; then rm -rf x; fi` puts `then` where the program should be);
|
|
8
|
+
* - wrapper programs whose own argv carries the real command.
|
|
9
|
+
*
|
|
10
|
+
* Only wrappers with a closed option table are peeled here. `env -S`, `xargs`,
|
|
11
|
+
* `find -exec`, `busybox`, `eval` and `source` build or hide their command at
|
|
12
|
+
* runtime, so they stay whole and remain a reason to ask.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { stripPrefixes } from "./prefixes";
|
|
16
|
+
|
|
17
|
+
/** Segments that never carry a command of their own. */
|
|
18
|
+
const SKIP_WORDS = new Set(["for", "in", "case", "fi", "done", "esac", "}"]);
|
|
19
|
+
|
|
20
|
+
/** Keywords that precede a command; the command follows them. */
|
|
21
|
+
const PREFIX_WORDS = new Set(["then", "do", "else", "elif", "if", "while", "until", "!", "{"]);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The innermost command of one simple command, or undefined when the segment
|
|
25
|
+
* holds no command at all. Word-exact, position zero only: `cp a in` keeps `in`
|
|
26
|
+
* as an operand.
|
|
27
|
+
*/
|
|
28
|
+
export function unlayer(argv: string[]): string[] | undefined {
|
|
29
|
+
let current = argv;
|
|
30
|
+
|
|
31
|
+
for (let step = 0; step < 4; step += 1) {
|
|
32
|
+
const head = current[0];
|
|
33
|
+
if (head === undefined) return current;
|
|
34
|
+
if (SKIP_WORDS.has(head)) return undefined;
|
|
35
|
+
if (PREFIX_WORDS.has(head)) {
|
|
36
|
+
current = current.slice(1);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const stripped = stripPrefixes(current);
|
|
40
|
+
if (stripped !== current && stripped.length > 0) {
|
|
41
|
+
current = stripped;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
return current;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return current;
|
|
48
|
+
}
|