@agentic-kit/permissions 0.0.0-stage → 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/LICENSE +23 -0
- package/README.md +279 -2
- package/bash/analyze.d.ts +133 -0
- package/bash/analyze.js +442 -0
- package/bash/args.d.ts +45 -0
- package/bash/args.js +145 -0
- package/bash/braces.d.ts +6 -0
- package/bash/braces.js +72 -0
- package/bash/commands.d.ts +14 -0
- package/bash/commands.js +361 -0
- package/bash/file-commands.d.ts +22 -0
- package/bash/file-commands.js +278 -0
- package/bash/flows.d.ts +67 -0
- package/bash/flows.js +208 -0
- package/bash/git.d.ts +7 -0
- package/bash/git.js +163 -0
- package/bash/network.d.ts +24 -0
- package/bash/network.js +295 -0
- package/bash/quoting.d.ts +16 -0
- package/bash/quoting.js +164 -0
- package/bash/system.d.ts +16 -0
- package/bash/system.js +122 -0
- package/bash/tokenize.d.ts +67 -0
- package/bash/tokenize.js +440 -0
- package/bash/words.d.ts +9 -0
- package/bash/words.js +30 -0
- package/bash/wrappers.d.ts +32 -0
- package/bash/wrappers.js +237 -0
- package/classify.d.ts +22 -0
- package/classify.js +259 -0
- package/engine.d.ts +109 -0
- package/engine.js +144 -0
- package/env.d.ts +13 -0
- package/env.js +37 -0
- package/esm/bash/analyze.d.ts +133 -0
- package/esm/bash/analyze.js +405 -0
- package/esm/bash/args.d.ts +45 -0
- package/esm/bash/args.js +135 -0
- package/esm/bash/braces.d.ts +6 -0
- package/esm/bash/braces.js +69 -0
- package/esm/bash/commands.d.ts +14 -0
- package/esm/bash/commands.js +324 -0
- package/esm/bash/file-commands.d.ts +22 -0
- package/esm/bash/file-commands.js +233 -0
- package/esm/bash/flows.d.ts +67 -0
- package/esm/bash/flows.js +203 -0
- package/esm/bash/git.d.ts +7 -0
- package/esm/bash/git.js +127 -0
- package/esm/bash/network.d.ts +24 -0
- package/esm/bash/network.js +282 -0
- package/esm/bash/quoting.d.ts +16 -0
- package/esm/bash/quoting.js +153 -0
- package/esm/bash/system.d.ts +16 -0
- package/esm/bash/system.js +82 -0
- package/esm/bash/tokenize.d.ts +67 -0
- package/esm/bash/tokenize.js +437 -0
- package/esm/bash/words.d.ts +9 -0
- package/esm/bash/words.js +24 -0
- package/esm/bash/wrappers.d.ts +32 -0
- package/esm/bash/wrappers.js +200 -0
- package/esm/classify.d.ts +22 -0
- package/esm/classify.js +223 -0
- package/esm/engine.d.ts +109 -0
- package/esm/engine.js +108 -0
- package/esm/env.d.ts +13 -0
- package/esm/env.js +34 -0
- package/esm/findings.d.ts +7 -0
- package/esm/findings.js +9 -0
- package/esm/glob.d.ts +15 -0
- package/esm/glob.js +69 -0
- package/esm/grants.d.ts +36 -0
- package/esm/grants.js +99 -0
- package/esm/index.d.ts +19 -0
- package/esm/index.js +18 -0
- package/esm/node-fs.d.ts +6 -0
- package/esm/node-fs.js +34 -0
- package/esm/paths.d.ts +43 -0
- package/esm/paths.js +121 -0
- package/esm/policy.d.ts +21 -0
- package/esm/policy.js +85 -0
- package/esm/prompt.d.ts +14 -0
- package/esm/prompt.js +79 -0
- package/esm/roots.d.ts +75 -0
- package/esm/roots.js +148 -0
- package/esm/secret-files.d.ts +16 -0
- package/esm/secret-files.js +84 -0
- package/esm/secrets/collect.d.ts +19 -0
- package/esm/secrets/collect.js +60 -0
- package/esm/secrets/index.d.ts +5 -0
- package/esm/secrets/index.js +5 -0
- package/esm/secrets/parse.d.ts +19 -0
- package/esm/secrets/parse.js +140 -0
- package/esm/secrets/redact-result.d.ts +8 -0
- package/esm/secrets/redact-result.js +67 -0
- package/esm/secrets/rules.d.ts +34 -0
- package/esm/secrets/rules.js +101 -0
- package/esm/secrets/vault.d.ts +48 -0
- package/esm/secrets/vault.js +158 -0
- package/esm/signals.d.ts +14 -0
- package/esm/signals.js +26 -0
- package/esm/tool-input.d.ts +13 -0
- package/esm/tool-input.js +40 -0
- package/esm/types.d.ts +146 -0
- package/esm/types.js +20 -0
- package/findings.d.ts +7 -0
- package/findings.js +13 -0
- package/glob.d.ts +15 -0
- package/glob.js +73 -0
- package/grants.d.ts +36 -0
- package/grants.js +110 -0
- package/index.d.ts +19 -0
- package/index.js +52 -0
- package/node-fs.d.ts +6 -0
- package/node-fs.js +37 -0
- package/package.json +35 -3
- package/paths.d.ts +43 -0
- package/paths.js +164 -0
- package/policy.d.ts +21 -0
- package/policy.js +88 -0
- package/prompt.d.ts +14 -0
- package/prompt.js +83 -0
- package/roots.d.ts +75 -0
- package/roots.js +192 -0
- package/secret-files.d.ts +16 -0
- package/secret-files.js +124 -0
- package/secrets/collect.d.ts +19 -0
- package/secrets/collect.js +96 -0
- package/secrets/index.d.ts +5 -0
- package/secrets/index.js +21 -0
- package/secrets/parse.d.ts +19 -0
- package/secrets/parse.js +145 -0
- package/secrets/redact-result.d.ts +8 -0
- package/secrets/redact-result.js +70 -0
- package/secrets/rules.d.ts +34 -0
- package/secrets/rules.js +107 -0
- package/secrets/vault.d.ts +48 -0
- package/secrets/vault.js +163 -0
- package/signals.d.ts +14 -0
- package/signals.js +29 -0
- package/tool-input.d.ts +13 -0
- package/tool-input.js +46 -0
- package/types.d.ts +146 -0
- package/types.js +24 -0
package/bash/analyze.js
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Static risk analysis of a bash command. It finds the paths a command touches
|
|
4
|
+
* and the signals that make it risky, and it fails toward asking: anything it
|
|
5
|
+
* cannot see through (substitution, `eval`, `sh -c`, a variable it cannot
|
|
6
|
+
* resolve, a lexer error) raises `shell-dynamic`.
|
|
7
|
+
*
|
|
8
|
+
* This is best effort and is not a security boundary. A program the agent
|
|
9
|
+
* wrote can still do anything the user can; only an OS sandbox stops that.
|
|
10
|
+
*/
|
|
11
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
14
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
15
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
16
|
+
}
|
|
17
|
+
Object.defineProperty(o, k2, desc);
|
|
18
|
+
}) : (function(o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
o[k2] = m[k];
|
|
21
|
+
}));
|
|
22
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
23
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
24
|
+
}) : function(o, v) {
|
|
25
|
+
o["default"] = v;
|
|
26
|
+
});
|
|
27
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
28
|
+
var ownKeys = function(o) {
|
|
29
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
30
|
+
var ar = [];
|
|
31
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
32
|
+
return ar;
|
|
33
|
+
};
|
|
34
|
+
return ownKeys(o);
|
|
35
|
+
};
|
|
36
|
+
return function (mod) {
|
|
37
|
+
if (mod && mod.__esModule) return mod;
|
|
38
|
+
var result = {};
|
|
39
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
40
|
+
__setModuleDefault(result, mod);
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
})();
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.normalizeCommand = normalizeCommand;
|
|
46
|
+
exports.analyzeBash = analyzeBash;
|
|
47
|
+
const path = __importStar(require("node:path"));
|
|
48
|
+
const node_url_1 = require("node:url");
|
|
49
|
+
const braces_1 = require("./braces");
|
|
50
|
+
const commands_1 = require("./commands");
|
|
51
|
+
const flows_1 = require("./flows");
|
|
52
|
+
const tokenize_1 = require("./tokenize");
|
|
53
|
+
const words_1 = require("./words");
|
|
54
|
+
const wrappers_1 = require("./wrappers");
|
|
55
|
+
function normalizeCommand(command) {
|
|
56
|
+
return command.trim().replace(/\s+/g, ' ');
|
|
57
|
+
}
|
|
58
|
+
const MAX_NESTING = 4;
|
|
59
|
+
const MAX_COMMAND_LENGTH = 64 * 1024;
|
|
60
|
+
/** Device paths that are not files. */
|
|
61
|
+
const HARMLESS_DEVICES = new Set([
|
|
62
|
+
'/dev/null',
|
|
63
|
+
'/dev/stdin',
|
|
64
|
+
'/dev/stdout',
|
|
65
|
+
'/dev/stderr',
|
|
66
|
+
'/dev/tty',
|
|
67
|
+
'/dev/zero',
|
|
68
|
+
'/dev/random',
|
|
69
|
+
'/dev/urandom'
|
|
70
|
+
]);
|
|
71
|
+
/** Running `/usr/bin/x` is the same as running `x`. */
|
|
72
|
+
const SYSTEM_BIN_DIRS = new Set(['/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/opt/homebrew/bin']);
|
|
73
|
+
/**
|
|
74
|
+
* Assignments that change which program runs, what it loads (the shell's and
|
|
75
|
+
* every interpreter's preload and library paths), or where a path resolves.
|
|
76
|
+
*/
|
|
77
|
+
const DANGEROUS_VARS = new RegExp(`^(${[
|
|
78
|
+
'PATH|IFS|ENV|BASH_ENV|PROMPT_COMMAND|SHELLOPTS|BASHOPTS|PS4|CDPATH|GLOBIGNORE',
|
|
79
|
+
'GIT_SSH|GIT_SSH_COMMAND|GIT_EXEC_PATH|GIT_EDITOR|GIT_PAGER|EDITOR|VISUAL|PAGER',
|
|
80
|
+
'LD_PRELOAD|LD_LIBRARY_PATH|LD_AUDIT|DYLD_\\w*',
|
|
81
|
+
'NODE_OPTIONS|NODE_PATH|PYTHONPATH|PYTHONSTARTUP|PYTHONHOME|PYTHONINSPECT|PERL5OPT|PERL5LIB|PERLLIB|RUBYOPT|RUBYLIB|JAVA_TOOL_OPTIONS|_JAVA_OPTIONS'
|
|
82
|
+
].join('|')})$`);
|
|
83
|
+
const RESERVED = new Set(['!', '{', '}', 'if', 'then', 'else', 'elif', 'fi', 'do', 'done', 'while', 'until', 'esac', 'coproc']);
|
|
84
|
+
/** The oh-my-pi critical list (`tools/bash.ts`), matched on the raw command. */
|
|
85
|
+
const CRITICAL_PATTERNS = [
|
|
86
|
+
/\brm\s+(?:-\S+\s+)*(?:-[a-z]*[rRfF][a-z]*|--recursive|--force)\s+(?:-\S+\s+)*\//i,
|
|
87
|
+
/\brm\s+(?:-\S+\s+)*--no-preserve-root\b/i,
|
|
88
|
+
/\bsudo\s+rm\b/i,
|
|
89
|
+
/\bchmod\s+-R\s+[0-7]+\s+\//i,
|
|
90
|
+
/\bchmod\s+-R\s+[ugoa+\-=rwxXst,]+\s+\//,
|
|
91
|
+
/\bchown\s+-R\s+\S+\s+\//i,
|
|
92
|
+
/:\(\)\s*\{\s*:\s*\|\s*:/i,
|
|
93
|
+
/>\s*\/dev\/sd[a-z]/i,
|
|
94
|
+
/>\s*\/dev\/disk\d/i,
|
|
95
|
+
/\bmkfs(\.|\b)/i,
|
|
96
|
+
/\bdd\s+if=.+of=\/dev\//i,
|
|
97
|
+
/\bshred\s+\/dev\//i,
|
|
98
|
+
/\bcryptsetup\b/i,
|
|
99
|
+
/>\s*\/etc\/(?:passwd|shadow|sudoers)\b/i,
|
|
100
|
+
/\btee\s+(?:-a\s+)?\/etc\/(?:passwd|shadow|sudoers)\b/i,
|
|
101
|
+
/\b(?:curl|wget|fetch)\b[^|]*\|\s*(?:sudo\s+)?(?:bash|sh|zsh|fish)\b/i,
|
|
102
|
+
/(?:^|[\s;&|(])(?:bash|sh|zsh|source|\.)\s+<\(\s*(?:curl|wget|fetch)\b/i,
|
|
103
|
+
/\beval\s+["'`]?\$\(\s*(?:curl|wget|fetch)\b|\beval\s+`\s*(?:curl|wget|fetch)\b/i,
|
|
104
|
+
/\bkill\s+-9\s+1\b/,
|
|
105
|
+
/(?:^|[\s;&|(])(?:shutdown|poweroff|reboot|halt)(?:\s|$|[;|&])/i,
|
|
106
|
+
/(?:^|[\s;&|(])init\s+0\b/i,
|
|
107
|
+
/\bnc\b[^|;]*\s-[a-zA-Z]*[ec][a-zA-Z]*\s/i
|
|
108
|
+
];
|
|
109
|
+
const PLACEHOLDER_RE = /«secret:[^»]*»/;
|
|
110
|
+
/** Shell options that make `*` match dotfiles. */
|
|
111
|
+
const DOTGLOB_RE = /dotglob|glob_?dots|GLOBIGNORE/i;
|
|
112
|
+
/** `$NAME` and `${NAME…}` in a heredoc body. */
|
|
113
|
+
const BODY_VAR_RE = /\$\{?[#!]?([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
114
|
+
function isHarmlessDevice(abs) {
|
|
115
|
+
return HARMLESS_DEVICES.has(abs) || abs.startsWith('/dev/fd/');
|
|
116
|
+
}
|
|
117
|
+
class Analyzer {
|
|
118
|
+
ctx;
|
|
119
|
+
findings;
|
|
120
|
+
paths = [];
|
|
121
|
+
checks = [];
|
|
122
|
+
tracker = new flows_1.FlowTracker(this);
|
|
123
|
+
/** Variables expanded but never set in the command: they come from the environment. */
|
|
124
|
+
envRefs = new Set();
|
|
125
|
+
/** git remotes the command adds or points elsewhere (`git remote add`, `set-url`, `-c remote.x.url=`). */
|
|
126
|
+
gitRemotes = new Set();
|
|
127
|
+
/** What the command being analyzed reads on stdin. */
|
|
128
|
+
stdin = { sources: [], words: [], listedUnder: null };
|
|
129
|
+
/** Shell variables set earlier in the command. `null` = set to something unknown. */
|
|
130
|
+
vars = new Map();
|
|
131
|
+
/** The shell's working dir at this point. `null` after a `cd` we cannot follow. */
|
|
132
|
+
base;
|
|
133
|
+
/** How many shell strings (`sh -c`, `eval`, …) deep the analysis is. */
|
|
134
|
+
depth = 0;
|
|
135
|
+
constructor(ctx, findings) {
|
|
136
|
+
this.ctx = ctx;
|
|
137
|
+
this.findings = findings;
|
|
138
|
+
this.base = path.resolve(ctx.cwd);
|
|
139
|
+
}
|
|
140
|
+
flag(signal, reason) {
|
|
141
|
+
this.findings.flag(signal, reason);
|
|
142
|
+
}
|
|
143
|
+
dynamic(reason) {
|
|
144
|
+
this.flag('shell-dynamic', reason);
|
|
145
|
+
}
|
|
146
|
+
/** A path the analyzer cannot place: it asks in every mode, so no mode lets it reach a denied root. */
|
|
147
|
+
unresolvable(reason) {
|
|
148
|
+
this.flag('unresolvable-path', reason);
|
|
149
|
+
}
|
|
150
|
+
/** `cd dir` looks `dir` up in `CDPATH` when the command set it and `dir` is not `/…`, `./…` or `../…`. */
|
|
151
|
+
searchesCdPath(dir) {
|
|
152
|
+
return this.vars.has('CDPATH') && !dir.startsWith('/') && !/^\.\.?(\/|$)/.test(dir);
|
|
153
|
+
}
|
|
154
|
+
analyzeString(command) {
|
|
155
|
+
if (this.depth > MAX_NESTING) {
|
|
156
|
+
this.dynamic('the command nests shell strings too deeply to check');
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (command.length > MAX_COMMAND_LENGTH) {
|
|
160
|
+
this.dynamic('the command is too long to check');
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const lexed = (0, tokenize_1.tokenize)(command);
|
|
164
|
+
if (lexed.error)
|
|
165
|
+
this.dynamic(`the command could not be parsed (${lexed.error})`);
|
|
166
|
+
for (const note of lexed.dynamic)
|
|
167
|
+
this.dynamic(`it uses ${note}`);
|
|
168
|
+
for (const cmd of lexed.commands)
|
|
169
|
+
this.analyzeCommand(cmd);
|
|
170
|
+
}
|
|
171
|
+
/** A shell string run by `sh -c`, `eval`, `watch`, …: analyze it in its own scope. */
|
|
172
|
+
nested(text, why) {
|
|
173
|
+
this.dynamic(why);
|
|
174
|
+
const savedBase = this.base;
|
|
175
|
+
const savedVars = new Map(this.vars);
|
|
176
|
+
const savedStdin = this.stdin;
|
|
177
|
+
this.depth++;
|
|
178
|
+
this.analyzeString(text);
|
|
179
|
+
this.depth--;
|
|
180
|
+
this.base = savedBase;
|
|
181
|
+
this.vars = savedVars;
|
|
182
|
+
this.stdin = savedStdin;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* `NAME=value`, inline or through `export`, `declare` or `readonly`.
|
|
186
|
+
* `value` is `null` when it is not known statically.
|
|
187
|
+
*/
|
|
188
|
+
assign(name, value) {
|
|
189
|
+
if (DANGEROUS_VARS.test(name))
|
|
190
|
+
this.dynamic(`it sets ${name}, which changes what runs`);
|
|
191
|
+
this.vars.set(name, value);
|
|
192
|
+
}
|
|
193
|
+
/** A variable the command expands: an env ref unless the command set it. */
|
|
194
|
+
noteRef(name) {
|
|
195
|
+
if (!this.vars.has(name))
|
|
196
|
+
this.envRefs.add(name);
|
|
197
|
+
}
|
|
198
|
+
lookup(name) {
|
|
199
|
+
if (this.vars.has(name))
|
|
200
|
+
return this.vars.get(name);
|
|
201
|
+
if (name === 'HOME')
|
|
202
|
+
return path.resolve(this.ctx.homeDir);
|
|
203
|
+
if (name === 'PWD')
|
|
204
|
+
return this.base;
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* `$NAME`, or `${NAME:-fallback}`. The fallback counts only when this
|
|
209
|
+
* command set the variable to empty: one from outside may hold anything, and
|
|
210
|
+
* a fallback with its own expansion or a `~` is not known either.
|
|
211
|
+
*/
|
|
212
|
+
valueOf(name, fallback) {
|
|
213
|
+
const value = this.lookup(name);
|
|
214
|
+
if (fallback === undefined || value !== '')
|
|
215
|
+
return value;
|
|
216
|
+
return /[$`~]/.test(fallback) ? null : fallback;
|
|
217
|
+
}
|
|
218
|
+
resolveWord(word) {
|
|
219
|
+
let text = '';
|
|
220
|
+
const mask = [];
|
|
221
|
+
let unknown = false;
|
|
222
|
+
const append = (value, expandable) => {
|
|
223
|
+
text += value;
|
|
224
|
+
for (let k = 0; k < value.length; k++)
|
|
225
|
+
mask.push(expandable);
|
|
226
|
+
};
|
|
227
|
+
for (const part of word.parts) {
|
|
228
|
+
if (part.kind === 'lit')
|
|
229
|
+
append(part.text, !part.quoted);
|
|
230
|
+
else if (part.kind === 'tilde')
|
|
231
|
+
append(path.resolve(this.ctx.homeDir), false);
|
|
232
|
+
else if (part.kind === 'unknown') {
|
|
233
|
+
unknown = true;
|
|
234
|
+
if (part.ref)
|
|
235
|
+
this.noteRef(part.ref);
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
this.noteRef(part.name);
|
|
239
|
+
const value = this.valueOf(part.name, part.fallback);
|
|
240
|
+
if (value === null)
|
|
241
|
+
unknown = true;
|
|
242
|
+
else
|
|
243
|
+
append(value, false);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const expanded = [];
|
|
247
|
+
(0, braces_1.expandBraces)(text, mask, expanded);
|
|
248
|
+
return expanded.map((e) => ({
|
|
249
|
+
text: e.text,
|
|
250
|
+
raw: word.raw,
|
|
251
|
+
unknown,
|
|
252
|
+
glob: [...e.text].some((ch, k) => e.mask[k] && (ch === '*' || ch === '?' || ch === '['))
|
|
253
|
+
}));
|
|
254
|
+
}
|
|
255
|
+
/** The absolute location of an argument, or `null` when it is unknown. */
|
|
256
|
+
toAbs(text, base = this.base) {
|
|
257
|
+
if (/^file:\/\//i.test(text)) {
|
|
258
|
+
try {
|
|
259
|
+
return (0, node_url_1.fileURLToPath)(text);
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (text.startsWith('/'))
|
|
266
|
+
return text;
|
|
267
|
+
if (base === null)
|
|
268
|
+
return null;
|
|
269
|
+
return `${base}/${text}`;
|
|
270
|
+
}
|
|
271
|
+
/** Record a path the command touches. `quiet`: the program loads it without printing it. */
|
|
272
|
+
addPath(arg, access, hard, quiet = false) {
|
|
273
|
+
if (arg.unknown) {
|
|
274
|
+
if (hard)
|
|
275
|
+
this.unresolvable(`the path ${arg.raw} is built from a variable or substitution`);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (arg.text === '')
|
|
279
|
+
return;
|
|
280
|
+
const abs = this.toAbs(arg.text);
|
|
281
|
+
if (abs === null) {
|
|
282
|
+
this.unresolvable(`${arg.raw} is used after a cd to an unknown dir`);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (isHarmlessDevice(abs))
|
|
286
|
+
return;
|
|
287
|
+
if (arg.glob) {
|
|
288
|
+
const components = arg.text.split('/');
|
|
289
|
+
const firstGlob = components.findIndex((c) => /[*?[]/.test(c));
|
|
290
|
+
if (components.slice(firstGlob).includes('..')) {
|
|
291
|
+
this.dynamic(`the glob ${arg.raw} climbs out with ..`);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
this.paths.push({ raw: arg.raw, abs, access, hard, glob: arg.glob, quiet });
|
|
296
|
+
}
|
|
297
|
+
destroy(arg, what, rootCounts = false) {
|
|
298
|
+
if (arg === null || arg.unknown) {
|
|
299
|
+
this.flag('destructive', `it runs ${what} on paths that are not known until it runs`);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const abs = this.toAbs(arg.text);
|
|
303
|
+
if (abs === null)
|
|
304
|
+
this.flag('destructive', `it runs ${what} after a cd to an unknown dir`);
|
|
305
|
+
else
|
|
306
|
+
this.checks.push({ raw: arg.raw, abs, what, rootCounts });
|
|
307
|
+
}
|
|
308
|
+
analyzeCommand(cmd) {
|
|
309
|
+
const args = cmd.words.flatMap((w) => this.resolveWord(w));
|
|
310
|
+
const stdinFed = cmd.pipedIn || cmd.stdinDoc || cmd.redirects.some((r) => r.op === '<' && r.target);
|
|
311
|
+
const stdin = this.tracker.stdinOf(cmd);
|
|
312
|
+
this.stdin = stdin;
|
|
313
|
+
for (const doc of cmd.heredocs) {
|
|
314
|
+
if (!doc.quoted)
|
|
315
|
+
for (const m of doc.body.matchAll(BODY_VAR_RE))
|
|
316
|
+
this.noteRef(m[1]);
|
|
317
|
+
}
|
|
318
|
+
const from = this.paths.length;
|
|
319
|
+
const stdout = [];
|
|
320
|
+
for (const r of cmd.redirects) {
|
|
321
|
+
if (!r.target)
|
|
322
|
+
continue;
|
|
323
|
+
const access = r.op === '<' ? 'read' : 'write';
|
|
324
|
+
for (const target of this.resolveWord(r.target)) {
|
|
325
|
+
if (target.unknown) {
|
|
326
|
+
this.unresolvable(`it redirects to ${target.raw}, a path from a variable or substitution`);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
this.addPath(target, access, true);
|
|
330
|
+
const abs = this.toAbs(target.text);
|
|
331
|
+
if (access === 'write' && r.fd !== 2 && abs !== null && !isHarmlessDevice(abs))
|
|
332
|
+
stdout.push({ raw: target.raw, abs });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const run = this.runArgs(args, stdinFed);
|
|
336
|
+
const reads = this.paths.slice(from).filter((p) => p.access === 'read' && !p.quiet);
|
|
337
|
+
const sources = [...reads.map(({ raw, abs }) => ({ raw, abs })), ...stdin.sources];
|
|
338
|
+
this.tracker.record(cmd, run, sources, stdout);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Take assignments, keywords and wrappers off, then apply the command's
|
|
342
|
+
* rules. Returns the program and its operands, or `null` when there is none.
|
|
343
|
+
* A wrapper's working dir (`env -C`) holds for this command only.
|
|
344
|
+
*/
|
|
345
|
+
runArgs(args, stdinFed) {
|
|
346
|
+
const scope = { sudo: false, xargs: null, chdir: false };
|
|
347
|
+
const outer = this.base;
|
|
348
|
+
try {
|
|
349
|
+
return this.run(args, stdinFed, scope);
|
|
350
|
+
}
|
|
351
|
+
finally {
|
|
352
|
+
if (scope.chdir)
|
|
353
|
+
this.base = outer;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
run(args, stdinFed, scope) {
|
|
357
|
+
let i = 0;
|
|
358
|
+
while (i < args.length) {
|
|
359
|
+
const arg = args[i];
|
|
360
|
+
const assignment = /^([A-Za-z_][A-Za-z0-9_]*)\+?=(.*)$/s.exec(arg.text);
|
|
361
|
+
if (assignment && /^[A-Za-z_][A-Za-z0-9_]*\+?=/.test(arg.raw)) {
|
|
362
|
+
this.assign(assignment[1], arg.unknown ? null : assignment[2]);
|
|
363
|
+
i++;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (arg.raw === arg.text && RESERVED.has(arg.text)) {
|
|
367
|
+
i++;
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
if (arg.raw === 'function') {
|
|
371
|
+
this.dynamic('it defines a shell function');
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
if (arg.raw === 'for' || arg.raw === 'select') {
|
|
375
|
+
const inAt = args.findIndex((a, k) => k > i && a.raw === 'in');
|
|
376
|
+
if (inAt > 0)
|
|
377
|
+
for (const item of args.slice(inAt + 1))
|
|
378
|
+
this.operand(item);
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
if (arg.raw === 'case')
|
|
382
|
+
return null;
|
|
383
|
+
const next = (0, wrappers_1.stripWrapper)(this, args, i, scope);
|
|
384
|
+
if (next === i)
|
|
385
|
+
break;
|
|
386
|
+
i = next;
|
|
387
|
+
}
|
|
388
|
+
if (i >= args.length)
|
|
389
|
+
return null;
|
|
390
|
+
const argv0 = args[i];
|
|
391
|
+
if (argv0.unknown) {
|
|
392
|
+
this.dynamic(`the command name ${argv0.raw} comes from a variable or substitution`);
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
if (argv0.glob && !commands_1.TESTS.has(argv0.text)) {
|
|
396
|
+
this.dynamic(`the command name ${argv0.raw} is a glob`);
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
if (argv0.text.includes('/')) {
|
|
400
|
+
const abs = this.toAbs(argv0.text);
|
|
401
|
+
if (abs === null || !SYSTEM_BIN_DIRS.has(path.dirname(path.resolve(abs))))
|
|
402
|
+
this.addPath(argv0, 'exec', true);
|
|
403
|
+
}
|
|
404
|
+
const name = path.basename(argv0.text);
|
|
405
|
+
if (scope.sudo && name === 'rm')
|
|
406
|
+
this.flag('destructive', 'it runs rm as root');
|
|
407
|
+
const rest = scope.xargs ? (0, wrappers_1.xargsOperands)(this, name, args.slice(i + 1), scope.xargs) : args.slice(i + 1);
|
|
408
|
+
(0, commands_1.runCommand)(name, rest, { analyzer: this, stdinFed, xargs: scope.xargs !== null });
|
|
409
|
+
return { name, rest };
|
|
410
|
+
}
|
|
411
|
+
/** A bare operand: a hard path when it looks like one, a soft one otherwise. URLs are skipped. */
|
|
412
|
+
operand(arg, access = 'read') {
|
|
413
|
+
if ((0, words_1.isUrl)(arg.text))
|
|
414
|
+
return;
|
|
415
|
+
const hard = (0, words_1.isPathLike)(arg.text) || (0, words_1.isPathLike)(arg.raw) || /^file:\/\//i.test(arg.text);
|
|
416
|
+
this.addPath(arg, access, hard);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Analyze a bash command, flagging its signals into `findings`. Pure: no fs
|
|
421
|
+
* access; paths are canonicalized by the caller.
|
|
422
|
+
*/
|
|
423
|
+
function analyzeBash(command, ctx, findings) {
|
|
424
|
+
const analyzer = new Analyzer(ctx, findings);
|
|
425
|
+
analyzer.analyzeString(command);
|
|
426
|
+
for (const pattern of CRITICAL_PATTERNS) {
|
|
427
|
+
if (pattern.test(command)) {
|
|
428
|
+
analyzer.flag('destructive', 'it matches a known destructive pattern');
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (PLACEHOLDER_RE.test(command)) {
|
|
433
|
+
analyzer.flag('placeholder-in-command', 'it contains a «secret:…» placeholder');
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
paths: analyzer.paths,
|
|
437
|
+
destructiveChecks: analyzer.checks,
|
|
438
|
+
flows: analyzer.tracker.flows,
|
|
439
|
+
envRefs: [...analyzer.envRefs],
|
|
440
|
+
dotglob: DOTGLOB_RE.test(command)
|
|
441
|
+
};
|
|
442
|
+
}
|
package/bash/args.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Option parsing and the generic operand rule, shared by the command rules. */
|
|
2
|
+
import type { Access } from '../types';
|
|
3
|
+
import type { Analyzer, Arg } from './analyze';
|
|
4
|
+
export interface CommandContext {
|
|
5
|
+
analyzer: Analyzer;
|
|
6
|
+
/** stdin comes from a pipe, a heredoc, a here-string or a `<` redirect. */
|
|
7
|
+
stdinFed: boolean;
|
|
8
|
+
/** Run by `xargs`, so more operands arrive from stdin. */
|
|
9
|
+
xargs: boolean;
|
|
10
|
+
}
|
|
11
|
+
/** An argument the analyzer made up itself (`.` for a default root): fully known, no glob. */
|
|
12
|
+
export declare function literalArg(text: string, raw?: string): Arg;
|
|
13
|
+
export interface Parsed {
|
|
14
|
+
flags: Set<string>;
|
|
15
|
+
values: Map<string, Arg[]>;
|
|
16
|
+
positionals: Arg[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Split options from operands. `withValue` lists the options that take a value
|
|
20
|
+
* (`-o`, `--output`); a short one may be glued (`-ofile`) or part of a cluster
|
|
21
|
+
* (`-sSo file`), a long one may use `=`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function parse(rest: readonly Arg[], withValue?: ReadonlySet<string>): Parsed;
|
|
24
|
+
export interface LeadingOption {
|
|
25
|
+
/** One option: `-c` out of a `-xc` cluster, `--eval` out of `--eval=X`. */
|
|
26
|
+
key: string;
|
|
27
|
+
/** Its value, glued (`-cX`, `--eval=X`) or the next word: only for `withValue` keys. */
|
|
28
|
+
value?: Arg;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A program's own options, up to its first operand (`node -r x main.js -e y`
|
|
32
|
+
* stops at `main.js`: the rest belongs to the script). Reads every spelling
|
|
33
|
+
* that `parse` reads. `next` is the index of the first operand; a word built
|
|
34
|
+
* from a variable stops the scan, because it may be either. An option in
|
|
35
|
+
* `stopAfter` ends the program's options (`python -c X`, `python -m X`).
|
|
36
|
+
*/
|
|
37
|
+
export declare function leadingOptions(rest: readonly Arg[], withValue: ReadonlySet<string>, stopAfter?: ReadonlySet<string>): {
|
|
38
|
+
options: LeadingOption[];
|
|
39
|
+
next: number;
|
|
40
|
+
};
|
|
41
|
+
export declare const has: (p: Parsed, ...keys: string[]) => boolean;
|
|
42
|
+
export declare const valuesOf: (p: Parsed, ...keys: string[]) => Arg[];
|
|
43
|
+
/** The generic rule: operands are reads; `--opt=path` and `KEY=path` values too. */
|
|
44
|
+
export declare function generic(ctx: CommandContext, rest: readonly Arg[]): void;
|
|
45
|
+
export declare function files(ctx: CommandContext, list: readonly Arg[], access: Access): void;
|
package/bash/args.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** Option parsing and the generic operand rule, shared by the command rules. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.valuesOf = exports.has = void 0;
|
|
5
|
+
exports.literalArg = literalArg;
|
|
6
|
+
exports.parse = parse;
|
|
7
|
+
exports.leadingOptions = leadingOptions;
|
|
8
|
+
exports.generic = generic;
|
|
9
|
+
exports.files = files;
|
|
10
|
+
const words_1 = require("./words");
|
|
11
|
+
/** An argument the analyzer made up itself (`.` for a default root): fully known, no glob. */
|
|
12
|
+
function literalArg(text, raw = text) {
|
|
13
|
+
return { text, raw, unknown: false, glob: false };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Split options from operands. `withValue` lists the options that take a value
|
|
17
|
+
* (`-o`, `--output`); a short one may be glued (`-ofile`) or part of a cluster
|
|
18
|
+
* (`-sSo file`), a long one may use `=`.
|
|
19
|
+
*/
|
|
20
|
+
function parse(rest, withValue = new Set()) {
|
|
21
|
+
const flags = new Set();
|
|
22
|
+
const values = new Map();
|
|
23
|
+
const positionals = [];
|
|
24
|
+
const put = (key, value) => {
|
|
25
|
+
if (!value)
|
|
26
|
+
return;
|
|
27
|
+
values.set(key, [...(values.get(key) ?? []), value]);
|
|
28
|
+
};
|
|
29
|
+
for (let k = 0; k < rest.length; k++) {
|
|
30
|
+
const arg = rest[k];
|
|
31
|
+
const t = arg.text;
|
|
32
|
+
if (t === '--') {
|
|
33
|
+
positionals.push(...rest.slice(k + 1));
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
if (t.startsWith('--')) {
|
|
37
|
+
const eq = t.indexOf('=');
|
|
38
|
+
const key = eq < 0 ? t : t.slice(0, eq);
|
|
39
|
+
flags.add(key);
|
|
40
|
+
if (eq >= 0)
|
|
41
|
+
put(key, { ...arg, text: t.slice(eq + 1) });
|
|
42
|
+
else if (withValue.has(key))
|
|
43
|
+
put(key, rest[++k]);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (t.startsWith('-') && t.length > 1 && !arg.unknown) {
|
|
47
|
+
for (let c = 1; c < t.length; c++) {
|
|
48
|
+
const key = `-${t[c]}`;
|
|
49
|
+
flags.add(key);
|
|
50
|
+
if (withValue.has(key)) {
|
|
51
|
+
const glued = t.slice(c + 1);
|
|
52
|
+
put(key, glued ? { ...arg, text: glued } : rest[++k]);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
positionals.push(arg);
|
|
59
|
+
}
|
|
60
|
+
return { flags, values, positionals };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A program's own options, up to its first operand (`node -r x main.js -e y`
|
|
64
|
+
* stops at `main.js`: the rest belongs to the script). Reads every spelling
|
|
65
|
+
* that `parse` reads. `next` is the index of the first operand; a word built
|
|
66
|
+
* from a variable stops the scan, because it may be either. An option in
|
|
67
|
+
* `stopAfter` ends the program's options (`python -c X`, `python -m X`).
|
|
68
|
+
*/
|
|
69
|
+
function leadingOptions(rest, withValue, stopAfter = new Set()) {
|
|
70
|
+
const options = [];
|
|
71
|
+
let k = 0;
|
|
72
|
+
for (; k < rest.length; k++) {
|
|
73
|
+
const arg = rest[k];
|
|
74
|
+
const t = arg.text;
|
|
75
|
+
if (t === '--')
|
|
76
|
+
return { options, next: k + 1 };
|
|
77
|
+
if (arg.unknown || !t.startsWith('-') || t === '-')
|
|
78
|
+
break;
|
|
79
|
+
if (t.startsWith('--')) {
|
|
80
|
+
const eq = t.indexOf('=');
|
|
81
|
+
const key = eq < 0 ? t : t.slice(0, eq);
|
|
82
|
+
if (eq >= 0)
|
|
83
|
+
options.push({ key, value: { ...arg, text: t.slice(eq + 1) } });
|
|
84
|
+
else
|
|
85
|
+
options.push(withValue.has(key) ? { key, value: rest[++k] } : { key });
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
for (let c = 1; c < t.length; c++) {
|
|
89
|
+
const key = `-${t[c]}`;
|
|
90
|
+
if (!withValue.has(key)) {
|
|
91
|
+
options.push({ key });
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const glued = t.slice(c + 1);
|
|
95
|
+
options.push({ key, value: glued ? { ...arg, text: glued } : rest[++k] });
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (stopAfter.has(options[options.length - 1]?.key ?? ''))
|
|
100
|
+
return { options, next: k + 1 };
|
|
101
|
+
}
|
|
102
|
+
return { options, next: k };
|
|
103
|
+
}
|
|
104
|
+
const has = (p, ...keys) => keys.some((key) => p.flags.has(key));
|
|
105
|
+
exports.has = has;
|
|
106
|
+
const valuesOf = (p, ...keys) => keys.flatMap((key) => p.values.get(key) ?? []);
|
|
107
|
+
exports.valuesOf = valuesOf;
|
|
108
|
+
/** Options that load an env file into the program without printing it. */
|
|
109
|
+
const ENV_FILE_OPTIONS = new Set(['--env-file', '--env-file-if-exists']);
|
|
110
|
+
/** The generic rule: operands are reads; `--opt=path` and `KEY=path` values too. */
|
|
111
|
+
function generic(ctx, rest) {
|
|
112
|
+
const { analyzer } = ctx;
|
|
113
|
+
let afterDashes = false;
|
|
114
|
+
for (let k = 0; k < rest.length; k++) {
|
|
115
|
+
const arg = rest[k];
|
|
116
|
+
const t = arg.text;
|
|
117
|
+
if (t === '--') {
|
|
118
|
+
afterDashes = true;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!afterDashes && t.startsWith('-')) {
|
|
122
|
+
const eq = t.indexOf('=');
|
|
123
|
+
const key = eq < 0 ? t : t.slice(0, eq);
|
|
124
|
+
const quiet = ENV_FILE_OPTIONS.has(key);
|
|
125
|
+
if (eq > 0 && (quiet || (0, words_1.isPathLike)(t.slice(eq + 1)))) {
|
|
126
|
+
analyzer.addPath({ ...arg, text: t.slice(eq + 1) }, 'read', true, quiet);
|
|
127
|
+
}
|
|
128
|
+
else if (eq < 0 && quiet && rest[k + 1]) {
|
|
129
|
+
analyzer.addPath(rest[++k], 'read', true, true);
|
|
130
|
+
}
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const kv = /^[A-Za-z_][\w.-]*=(.+)$/.exec(t);
|
|
134
|
+
if (kv && (0, words_1.isPathLike)(kv[1]) && !(0, words_1.isUrl)(kv[1])) {
|
|
135
|
+
analyzer.addPath({ ...arg, text: kv[1] }, 'read', true);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
analyzer.operand(arg);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function files(ctx, list, access) {
|
|
142
|
+
for (const arg of list)
|
|
143
|
+
if (!(0, words_1.isUrl)(arg.text))
|
|
144
|
+
ctx.analyzer.addPath(arg, access, true);
|
|
145
|
+
}
|
package/bash/braces.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Brace expansion (`{a,b}`, `{1..3}`) on the unquoted parts of a word. */
|
|
2
|
+
/** Expand `{a,b}` and `{1..3}` in the unquoted parts of a word. */
|
|
3
|
+
export declare function expandBraces(text: string, mask: boolean[], out: {
|
|
4
|
+
text: string;
|
|
5
|
+
mask: boolean[];
|
|
6
|
+
}[]): void;
|