@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/system.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Commands that reach past the project into the machine: jobs and services
|
|
4
|
+
* that outlive the session, the macOS keychain, the clipboard, and archivers
|
|
5
|
+
* that take their file names from stdin.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.CLIPBOARD = void 0;
|
|
42
|
+
exports.persistence = persistence;
|
|
43
|
+
exports.security = security;
|
|
44
|
+
exports.clipboard = clipboard;
|
|
45
|
+
exports.namesFromStdin = namesFromStdin;
|
|
46
|
+
const path = __importStar(require("node:path"));
|
|
47
|
+
const roots_1 = require("../roots");
|
|
48
|
+
const args_1 = require("./args");
|
|
49
|
+
const words_1 = require("./words");
|
|
50
|
+
const LAUNCHCTL_LOADS = new Set(['load', 'bootstrap', 'submit', 'enable']);
|
|
51
|
+
/** `crontab`, `at`, `launchctl load`, `defaults write`: a change that lasts past this session. */
|
|
52
|
+
function persistence(name, ctx, rest) {
|
|
53
|
+
const words = rest.map((a) => a.text);
|
|
54
|
+
const say = (what) => ctx.analyzer.flag('persistence', `it makes a change that outlasts this session: ${what}`);
|
|
55
|
+
if (name === 'crontab' && !words.includes('-l'))
|
|
56
|
+
say('crontab installs or changes cron jobs');
|
|
57
|
+
if ((name === 'at' || name === 'batch') && !words.some((w) => w === '-l' || w === '-c'))
|
|
58
|
+
say(`${name} schedules a command to run later`);
|
|
59
|
+
if (name === 'launchctl') {
|
|
60
|
+
const sub = words.find((w) => !w.startsWith('-'));
|
|
61
|
+
if (sub && LAUNCHCTL_LOADS.has(sub))
|
|
62
|
+
say(`launchctl ${sub} starts a background service`);
|
|
63
|
+
}
|
|
64
|
+
if (name === 'defaults' && words.some((w) => w === 'write' || w === 'import'))
|
|
65
|
+
say('defaults write changes app or system settings');
|
|
66
|
+
(0, args_1.generic)(ctx, rest.filter((a) => (0, words_1.isPathLike)(a.text)));
|
|
67
|
+
}
|
|
68
|
+
const KEYCHAIN_READS = new Set(['find-generic-password', 'find-internet-password', 'dump-keychain', 'export']);
|
|
69
|
+
/** `security find-generic-password`, …: a read of the keychain, which the hard-deny list blocks. */
|
|
70
|
+
function security(ctx, rest) {
|
|
71
|
+
const { analyzer } = ctx;
|
|
72
|
+
const sub = rest.find((a) => !a.text.startsWith('-'));
|
|
73
|
+
const pem = sub?.text === 'find-certificate' && rest.some((a) => /^-[a-zA-Z]*p/.test(a.text));
|
|
74
|
+
if (sub && (KEYCHAIN_READS.has(sub.text) || pem)) {
|
|
75
|
+
analyzer.addPath((0, args_1.literalArg)(path.join(analyzer.ctx.homeDir, roots_1.KEYCHAIN_DIR), `security ${sub.text}`), 'read', true);
|
|
76
|
+
}
|
|
77
|
+
(0, args_1.generic)(ctx, rest.filter((a) => (0, words_1.isPathLike)(a.text)));
|
|
78
|
+
}
|
|
79
|
+
exports.CLIPBOARD = new Set(['pbcopy', 'pbpaste', 'xclip', 'xsel', 'wl-copy', 'wl-paste']);
|
|
80
|
+
/** xclip options whose value is the next word. */
|
|
81
|
+
const XCLIP_WITH_VALUE = new Set(['-selection', '-sel', '-target', '-t', '-display', '-d', '-loops', '-l']);
|
|
82
|
+
/** The clipboard: a paste shows the model what the user copied, a copy may carry a secret file out. */
|
|
83
|
+
function clipboard(name, ctx, rest) {
|
|
84
|
+
const { analyzer } = ctx;
|
|
85
|
+
const words = rest.map((a) => a.text);
|
|
86
|
+
const pastes = name === 'pbpaste' ||
|
|
87
|
+
name === 'wl-paste' ||
|
|
88
|
+
(name === 'xclip' && words.some((w) => w === '-o' || w === '-out')) ||
|
|
89
|
+
(name === 'xsel' && (words.some((w) => /^(-[a-zA-Z]*o|--output)$/.test(w)) || (!ctx.stdinFed && !words.includes('-i'))));
|
|
90
|
+
if (pastes) {
|
|
91
|
+
analyzer.flag('clipboard', `it reads the clipboard with ${name}, which may hold anything the user copied`);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
analyzer.flag('clipboard', `it puts text on the clipboard with ${name}`);
|
|
95
|
+
const sources = [...analyzer.stdin.sources];
|
|
96
|
+
if (name === 'xclip') {
|
|
97
|
+
for (let k = 0; k < rest.length; k++) {
|
|
98
|
+
if (XCLIP_WITH_VALUE.has(rest[k].text))
|
|
99
|
+
k++;
|
|
100
|
+
else if (!rest[k].text.startsWith('-')) {
|
|
101
|
+
analyzer.addPath(rest[k], 'read', true);
|
|
102
|
+
const abs = rest[k].unknown ? null : analyzer.toAbs(rest[k].text);
|
|
103
|
+
if (abs !== null)
|
|
104
|
+
sources.push({ raw: rest[k].raw, abs });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (sources.length > 0)
|
|
109
|
+
analyzer.tracker.flows.push({ sources, to: { kind: 'clipboard' }, what: name });
|
|
110
|
+
}
|
|
111
|
+
/** `cpio -o`, `tar -T -`, `zip -@`: the file names come from stdin. */
|
|
112
|
+
function namesFromStdin(name, rest) {
|
|
113
|
+
const words = rest.map((a) => a.text);
|
|
114
|
+
if (name === 'cpio')
|
|
115
|
+
return words.some((w) => /^-[a-zA-Z]*[op]/.test(w) || w === '--create' || w === '--pass-through');
|
|
116
|
+
if (name === 'zip')
|
|
117
|
+
return words.includes('-@');
|
|
118
|
+
if (name === 'tar') {
|
|
119
|
+
return words.some((w, k) => w === '-T-' || w === '--files-from=-' || ((w === '-T' || w === '--files-from') && words[k + 1] === '-'));
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A POSIX-shell lexer that is good enough to find what a command touches.
|
|
3
|
+
*
|
|
4
|
+
* It splits a command into simple commands (on `;`, `&&`, `||`, `|`, `&`,
|
|
5
|
+
* newline and subshell parens), keeps quoting so expansion can be judged, and
|
|
6
|
+
* records redirections and heredocs. Command substitution (`$(…)`, backticks,
|
|
7
|
+
* `<(…)`) is lexed recursively, so the commands inside it are analyzed too.
|
|
8
|
+
*
|
|
9
|
+
* It never executes or expands anything that needs the environment. What it
|
|
10
|
+
* cannot know it marks: a word part is `unknown`, and constructs that hide code
|
|
11
|
+
* add a `dynamic` note. Any lexer error (an unterminated quote) is reported as
|
|
12
|
+
* `error`, and the analyzer turns both into `shell-dynamic`.
|
|
13
|
+
*/
|
|
14
|
+
/** One piece of a word. `quoted` pieces take no globbing, brace or tilde expansion. */
|
|
15
|
+
export type Part = {
|
|
16
|
+
kind: 'lit';
|
|
17
|
+
text: string;
|
|
18
|
+
quoted: boolean;
|
|
19
|
+
} | {
|
|
20
|
+
kind: 'var';
|
|
21
|
+
name: string;
|
|
22
|
+
quoted: boolean;
|
|
23
|
+
fallback?: string;
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'tilde';
|
|
26
|
+
}
|
|
27
|
+
/** `ref` names the variable a complex `${…}` reads (`${TOKEN:0:4}`). */
|
|
28
|
+
| {
|
|
29
|
+
kind: 'unknown';
|
|
30
|
+
ref?: string;
|
|
31
|
+
};
|
|
32
|
+
export interface Word {
|
|
33
|
+
parts: Part[];
|
|
34
|
+
/** The source text of the word. */
|
|
35
|
+
raw: string;
|
|
36
|
+
}
|
|
37
|
+
export interface Redirect {
|
|
38
|
+
op: string;
|
|
39
|
+
fd: number | null;
|
|
40
|
+
/** `null` for an fd duplication (`2>&1`) or a heredoc. */
|
|
41
|
+
target: Word | null;
|
|
42
|
+
}
|
|
43
|
+
export interface Heredoc {
|
|
44
|
+
quoted: boolean;
|
|
45
|
+
body: string;
|
|
46
|
+
}
|
|
47
|
+
export interface SimpleCommand {
|
|
48
|
+
words: Word[];
|
|
49
|
+
redirects: Redirect[];
|
|
50
|
+
heredocs: Heredoc[];
|
|
51
|
+
/** stdin is the previous command's stdout. */
|
|
52
|
+
pipedIn: boolean;
|
|
53
|
+
/** stdin is a heredoc or a here-string. */
|
|
54
|
+
stdinDoc: boolean;
|
|
55
|
+
/** The words after `<<<`. */
|
|
56
|
+
hereStrings: Word[];
|
|
57
|
+
/** The command whose stdout this one reads, or `group` after `( … ) |`. */
|
|
58
|
+
upstream?: SimpleCommand | 'group';
|
|
59
|
+
}
|
|
60
|
+
export interface Lexed {
|
|
61
|
+
commands: SimpleCommand[];
|
|
62
|
+
/** Notes on constructs that run code the analyzer cannot see. */
|
|
63
|
+
dynamic: string[];
|
|
64
|
+
error?: string;
|
|
65
|
+
}
|
|
66
|
+
/** Lex a command. Never throws: a lexer error comes back in `error`. */
|
|
67
|
+
export declare function tokenize(command: string): Lexed;
|
package/bash/tokenize.js
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* A POSIX-shell lexer that is good enough to find what a command touches.
|
|
4
|
+
*
|
|
5
|
+
* It splits a command into simple commands (on `;`, `&&`, `||`, `|`, `&`,
|
|
6
|
+
* newline and subshell parens), keeps quoting so expansion can be judged, and
|
|
7
|
+
* records redirections and heredocs. Command substitution (`$(…)`, backticks,
|
|
8
|
+
* `<(…)`) is lexed recursively, so the commands inside it are analyzed too.
|
|
9
|
+
*
|
|
10
|
+
* It never executes or expands anything that needs the environment. What it
|
|
11
|
+
* cannot know it marks: a word part is `unknown`, and constructs that hide code
|
|
12
|
+
* add a `dynamic` note. Any lexer error (an unterminated quote) is reported as
|
|
13
|
+
* `error`, and the analyzer turns both into `shell-dynamic`.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.tokenize = tokenize;
|
|
17
|
+
const quoting_1 = require("./quoting");
|
|
18
|
+
const MAX_DEPTH = 6;
|
|
19
|
+
const OPERATOR_CHARS = new Set([';', '&', '|', '(', ')', '<', '>']);
|
|
20
|
+
const NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
21
|
+
const SPECIAL_PARAMS = new Set(['?', '$', '!', '#', '@', '*', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']);
|
|
22
|
+
function newCommand() {
|
|
23
|
+
return { words: [], redirects: [], heredocs: [], pipedIn: false, stdinDoc: false, hereStrings: [] };
|
|
24
|
+
}
|
|
25
|
+
class Lexer {
|
|
26
|
+
s;
|
|
27
|
+
depth;
|
|
28
|
+
commands = [];
|
|
29
|
+
dynamic = [];
|
|
30
|
+
current = newCommand();
|
|
31
|
+
pending = [];
|
|
32
|
+
pipeNext = false;
|
|
33
|
+
upstream = null;
|
|
34
|
+
i = 0;
|
|
35
|
+
constructor(s, depth) {
|
|
36
|
+
this.s = s;
|
|
37
|
+
this.depth = depth;
|
|
38
|
+
}
|
|
39
|
+
run() {
|
|
40
|
+
const { s } = this;
|
|
41
|
+
while (this.i < s.length) {
|
|
42
|
+
const ch = s[this.i];
|
|
43
|
+
if (ch === ' ' || ch === '\t' || ch === '\r') {
|
|
44
|
+
this.i++;
|
|
45
|
+
}
|
|
46
|
+
else if (ch === '\\' && s[this.i + 1] === '\n') {
|
|
47
|
+
this.i += 2;
|
|
48
|
+
}
|
|
49
|
+
else if (ch === '\n') {
|
|
50
|
+
this.i++;
|
|
51
|
+
// A pipe may continue on the next line.
|
|
52
|
+
if (!this.pipeNext || this.current.words.length > 0 || this.current.redirects.length > 0)
|
|
53
|
+
this.endCommand(false);
|
|
54
|
+
this.readHeredocs();
|
|
55
|
+
}
|
|
56
|
+
else if (ch === '#') {
|
|
57
|
+
const nl = s.indexOf('\n', this.i);
|
|
58
|
+
this.i = nl < 0 ? s.length : nl;
|
|
59
|
+
}
|
|
60
|
+
else if (OPERATOR_CHARS.has(ch)) {
|
|
61
|
+
this.operator();
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const start = this.i;
|
|
65
|
+
const parts = this.readWord();
|
|
66
|
+
if (parts)
|
|
67
|
+
this.current.words.push({ parts, raw: s.slice(start, this.i) });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (this.pending.length > 0)
|
|
71
|
+
throw new quoting_1.LexError('unterminated heredoc');
|
|
72
|
+
this.endCommand(false);
|
|
73
|
+
}
|
|
74
|
+
endCommand(pipe) {
|
|
75
|
+
const cmd = this.current;
|
|
76
|
+
if (cmd.words.length > 0 || cmd.redirects.length > 0) {
|
|
77
|
+
cmd.pipedIn = cmd.pipedIn || this.pipeNext;
|
|
78
|
+
if (cmd.pipedIn && this.upstream)
|
|
79
|
+
cmd.upstream = this.upstream;
|
|
80
|
+
this.commands.push(cmd);
|
|
81
|
+
this.pipeNext = pipe;
|
|
82
|
+
this.upstream = pipe ? cmd : null;
|
|
83
|
+
}
|
|
84
|
+
else if (pipe) {
|
|
85
|
+
// `( … ) | x`: the group's output is piped on.
|
|
86
|
+
this.pipeNext = true;
|
|
87
|
+
this.upstream = 'group';
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
this.pipeNext = false;
|
|
91
|
+
this.upstream = null;
|
|
92
|
+
}
|
|
93
|
+
this.current = newCommand();
|
|
94
|
+
}
|
|
95
|
+
operator() {
|
|
96
|
+
const { s } = this;
|
|
97
|
+
const ch = s[this.i];
|
|
98
|
+
const two = s.slice(this.i, this.i + 2);
|
|
99
|
+
if (ch === '<' || ch === '>' || two === '&>') {
|
|
100
|
+
this.redirect(null);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (two === '&&' || two === '||' || two === ';;' || two === ';&') {
|
|
104
|
+
this.i += 2;
|
|
105
|
+
this.endCommand(false);
|
|
106
|
+
}
|
|
107
|
+
else if (two === '|&') {
|
|
108
|
+
this.i += 2;
|
|
109
|
+
this.endCommand(true);
|
|
110
|
+
}
|
|
111
|
+
else if (ch === '|') {
|
|
112
|
+
this.i++;
|
|
113
|
+
this.endCommand(true);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
// `;`, `&`, `(`, `)`
|
|
117
|
+
if (ch === '(' && s[this.i + 1] === ')') {
|
|
118
|
+
// `name()`: a function definition hides a body that runs later.
|
|
119
|
+
this.dynamic.push('defines a shell function');
|
|
120
|
+
this.i += 2;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
this.i++;
|
|
124
|
+
this.endCommand(false);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
redirect(fd) {
|
|
128
|
+
const { s } = this;
|
|
129
|
+
const ops = ['&>>', '<<<', '<<-', '&>', '>>', '>|', '>&', '<<', '<>', '<&', '<(', '>(', '<', '>'];
|
|
130
|
+
const op = ops.find((candidate) => s.startsWith(candidate, this.i));
|
|
131
|
+
if (op === '<(' || op === '>(') {
|
|
132
|
+
// Process substitution: a word whose value comes from a command.
|
|
133
|
+
const start = this.i;
|
|
134
|
+
const parts = this.readWord();
|
|
135
|
+
if (parts)
|
|
136
|
+
this.current.words.push({ parts, raw: s.slice(start, this.i) });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this.i += op.length;
|
|
140
|
+
while (s[this.i] === ' ' || s[this.i] === '\t')
|
|
141
|
+
this.i++;
|
|
142
|
+
if (this.i >= s.length || s[this.i] === '\n' || OPERATOR_CHARS.has(s[this.i])) {
|
|
143
|
+
throw new quoting_1.LexError(`redirection ${op} has no target`);
|
|
144
|
+
}
|
|
145
|
+
const start = this.i;
|
|
146
|
+
const parts = this.readWord();
|
|
147
|
+
if (!parts)
|
|
148
|
+
throw new quoting_1.LexError(`redirection ${op} has no target`);
|
|
149
|
+
const target = { parts, raw: s.slice(start, this.i) };
|
|
150
|
+
const cmd = this.current;
|
|
151
|
+
if (op === '<<' || op === '<<-') {
|
|
152
|
+
const quoted = parts.some((p) => p.kind === 'lit' && p.quoted);
|
|
153
|
+
const delim = parts.map((p) => (p.kind === 'lit' ? p.text : '')).join('');
|
|
154
|
+
this.pending.push({ delim, quoted, stripTabs: op === '<<-', command: cmd });
|
|
155
|
+
cmd.stdinDoc = true;
|
|
156
|
+
cmd.redirects.push({ op, fd, target: null });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (op === '<<<') {
|
|
160
|
+
cmd.stdinDoc = true;
|
|
161
|
+
cmd.hereStrings.push(target);
|
|
162
|
+
cmd.redirects.push({ op, fd, target: null });
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if ((op === '>&' || op === '<&') && /^(\d+-?|-)$/.test(target.raw)) {
|
|
166
|
+
cmd.redirects.push({ op, fd, target: null });
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
cmd.redirects.push({ op, fd, target });
|
|
170
|
+
}
|
|
171
|
+
readHeredocs() {
|
|
172
|
+
const { s } = this;
|
|
173
|
+
for (const doc of this.pending) {
|
|
174
|
+
let body = '';
|
|
175
|
+
let found = false;
|
|
176
|
+
while (this.i < s.length) {
|
|
177
|
+
const nl = s.indexOf('\n', this.i);
|
|
178
|
+
const line = s.slice(this.i, nl < 0 ? s.length : nl);
|
|
179
|
+
this.i = nl < 0 ? s.length : nl + 1;
|
|
180
|
+
const check = doc.stripTabs ? line.replace(/^\t+/, '') : line;
|
|
181
|
+
if (check === doc.delim) {
|
|
182
|
+
found = true;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
body += `${line}\n`;
|
|
186
|
+
}
|
|
187
|
+
if (!found)
|
|
188
|
+
throw new quoting_1.LexError('unterminated heredoc');
|
|
189
|
+
if (!doc.quoted)
|
|
190
|
+
this.scanExpansions(body);
|
|
191
|
+
doc.command.heredocs.push({ quoted: doc.quoted, body });
|
|
192
|
+
}
|
|
193
|
+
this.pending = [];
|
|
194
|
+
}
|
|
195
|
+
/** An unquoted heredoc body expands `$(…)` and backticks: lex them. */
|
|
196
|
+
scanExpansions(body) {
|
|
197
|
+
for (let i = 0; i < body.length; i++) {
|
|
198
|
+
const ch = body[i];
|
|
199
|
+
if (ch === '\\')
|
|
200
|
+
i++;
|
|
201
|
+
else if (ch === '$' && body[i + 1] === '(') {
|
|
202
|
+
const end = (0, quoting_1.matchParen)(body, i + 1);
|
|
203
|
+
this.substitute(body.slice(i + 2, end), 'command substitution in a heredoc');
|
|
204
|
+
i = end;
|
|
205
|
+
}
|
|
206
|
+
else if (ch === '`') {
|
|
207
|
+
const end = (0, quoting_1.matchBacktick)(body, i);
|
|
208
|
+
this.substitute((0, quoting_1.unescapeBackticks)(body.slice(i + 1, end)), 'backtick substitution in a heredoc');
|
|
209
|
+
i = end;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
substitute(inner, note) {
|
|
214
|
+
this.dynamic.push(note);
|
|
215
|
+
if (this.depth >= MAX_DEPTH)
|
|
216
|
+
throw new quoting_1.LexError('substitutions nested too deep');
|
|
217
|
+
const sub = new Lexer(inner, this.depth + 1);
|
|
218
|
+
sub.run();
|
|
219
|
+
this.commands.push(...sub.commands);
|
|
220
|
+
this.dynamic.push(...sub.dynamic);
|
|
221
|
+
}
|
|
222
|
+
/** Read one word starting at `this.i`, up to unquoted whitespace or an operator. */
|
|
223
|
+
readWord() {
|
|
224
|
+
const { s } = this;
|
|
225
|
+
const parts = [];
|
|
226
|
+
const lit = (text, quoted) => {
|
|
227
|
+
const last = parts[parts.length - 1];
|
|
228
|
+
if (last && last.kind === 'lit' && last.quoted === quoted)
|
|
229
|
+
last.text += text;
|
|
230
|
+
else
|
|
231
|
+
parts.push({ kind: 'lit', text, quoted });
|
|
232
|
+
};
|
|
233
|
+
const wordStart = this.i;
|
|
234
|
+
while (this.i < s.length) {
|
|
235
|
+
const ch = s[this.i];
|
|
236
|
+
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r')
|
|
237
|
+
break;
|
|
238
|
+
if ((ch === '<' || ch === '>') && s[this.i + 1] === '(') {
|
|
239
|
+
const end = (0, quoting_1.matchParen)(s, this.i + 1);
|
|
240
|
+
this.substitute(s.slice(this.i + 2, end), 'process substitution');
|
|
241
|
+
parts.push({ kind: 'unknown' });
|
|
242
|
+
this.i = end + 1;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (OPERATOR_CHARS.has(ch)) {
|
|
246
|
+
// `2>file`: digits right before a redirection are its fd.
|
|
247
|
+
if ((ch === '<' || ch === '>') && this.i > wordStart) {
|
|
248
|
+
const text = s.slice(wordStart, this.i);
|
|
249
|
+
if (/^\d+$/.test(text)) {
|
|
250
|
+
parts.length = 0;
|
|
251
|
+
this.redirect(Number(text));
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
if (ch === '\\') {
|
|
258
|
+
if (s[this.i + 1] === '\n') {
|
|
259
|
+
this.i += 2;
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (this.i + 1 < s.length)
|
|
263
|
+
lit(s[this.i + 1], true);
|
|
264
|
+
this.i += 2;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (ch === "'") {
|
|
268
|
+
const end = s.indexOf("'", this.i + 1);
|
|
269
|
+
if (end < 0)
|
|
270
|
+
throw new quoting_1.LexError('unterminated single quote');
|
|
271
|
+
lit(s.slice(this.i + 1, end), true);
|
|
272
|
+
this.i = end + 1;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (ch === '"') {
|
|
276
|
+
this.readDouble(parts, lit);
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (ch === '`') {
|
|
280
|
+
const end = (0, quoting_1.matchBacktick)(s, this.i);
|
|
281
|
+
this.substitute((0, quoting_1.unescapeBackticks)(s.slice(this.i + 1, end)), 'backtick substitution');
|
|
282
|
+
parts.push({ kind: 'unknown' });
|
|
283
|
+
this.i = end + 1;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (ch === '$') {
|
|
287
|
+
this.readDollar(parts, lit, false);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (ch === '~' && this.atTildePosition(parts)) {
|
|
291
|
+
const rest = s.slice(this.i + 1);
|
|
292
|
+
const user = /^[^/\s;&|()<>:]*/.exec(rest)[0];
|
|
293
|
+
if (user === '') {
|
|
294
|
+
parts.push({ kind: 'tilde' });
|
|
295
|
+
this.i++;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
this.dynamic.push(`~${user} expansion`);
|
|
299
|
+
lit(`~${user}`, false);
|
|
300
|
+
this.i += 1 + user.length;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
lit(ch, false);
|
|
304
|
+
this.i++;
|
|
305
|
+
}
|
|
306
|
+
return parts;
|
|
307
|
+
}
|
|
308
|
+
/** `~` expands at the start of a word and right after `NAME=` (an assignment). */
|
|
309
|
+
atTildePosition(parts) {
|
|
310
|
+
if (parts.length === 0)
|
|
311
|
+
return true;
|
|
312
|
+
if (parts.length !== 1)
|
|
313
|
+
return false;
|
|
314
|
+
const only = parts[0];
|
|
315
|
+
return only.kind === 'lit' && !only.quoted && /^[A-Za-z_][A-Za-z0-9_]*=$/.test(only.text);
|
|
316
|
+
}
|
|
317
|
+
readDouble(parts, lit) {
|
|
318
|
+
const { s } = this;
|
|
319
|
+
this.i++;
|
|
320
|
+
while (this.i < s.length) {
|
|
321
|
+
const ch = s[this.i];
|
|
322
|
+
if (ch === '"') {
|
|
323
|
+
this.i++;
|
|
324
|
+
if (parts.length === 0)
|
|
325
|
+
lit('', true);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (ch === '\\') {
|
|
329
|
+
const next = s[this.i + 1];
|
|
330
|
+
if (next === '\n') {
|
|
331
|
+
this.i += 2;
|
|
332
|
+
}
|
|
333
|
+
else if (next === '"' || next === '\\' || next === '$' || next === '`') {
|
|
334
|
+
lit(next, true);
|
|
335
|
+
this.i += 2;
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
lit('\\', true);
|
|
339
|
+
this.i++;
|
|
340
|
+
}
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (ch === '`') {
|
|
344
|
+
const end = (0, quoting_1.matchBacktick)(s, this.i);
|
|
345
|
+
this.substitute((0, quoting_1.unescapeBackticks)(s.slice(this.i + 1, end)), 'backtick substitution');
|
|
346
|
+
parts.push({ kind: 'unknown' });
|
|
347
|
+
this.i = end + 1;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (ch === '$') {
|
|
351
|
+
this.readDollar(parts, lit, true);
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
lit(ch, true);
|
|
355
|
+
this.i++;
|
|
356
|
+
}
|
|
357
|
+
throw new quoting_1.LexError('unterminated double quote');
|
|
358
|
+
}
|
|
359
|
+
readDollar(parts, lit, quoted) {
|
|
360
|
+
const { s } = this;
|
|
361
|
+
const next = s[this.i + 1];
|
|
362
|
+
if (next === '(' && s[this.i + 2] === '(') {
|
|
363
|
+
const end = (0, quoting_1.matchParen)(s, this.i + 1);
|
|
364
|
+
const inner = s.slice(this.i + 3, end - 1);
|
|
365
|
+
if (/\$\(|`/.test(inner))
|
|
366
|
+
this.substitute(inner.replace(/^[^$`]*/, ''), 'command substitution in arithmetic');
|
|
367
|
+
parts.push({ kind: 'unknown' });
|
|
368
|
+
this.i = end + 1;
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (next === '(') {
|
|
372
|
+
const end = (0, quoting_1.matchParen)(s, this.i + 1);
|
|
373
|
+
this.substitute(s.slice(this.i + 2, end), 'command substitution $(…)');
|
|
374
|
+
parts.push({ kind: 'unknown' });
|
|
375
|
+
this.i = end + 1;
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (next === '{') {
|
|
379
|
+
const end = (0, quoting_1.matchBrace)(s, this.i + 1);
|
|
380
|
+
const inner = s.slice(this.i + 2, end);
|
|
381
|
+
this.i = end + 1;
|
|
382
|
+
if (NAME_RE.test(inner)) {
|
|
383
|
+
parts.push({ kind: 'var', name: inner, quoted });
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (SPECIAL_PARAMS.has(inner)) {
|
|
387
|
+
parts.push({ kind: 'unknown' });
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const ref = /^[#!]?([A-Za-z_][A-Za-z0-9_]*)/.exec(inner)?.[1];
|
|
391
|
+
const withDefault = /^([A-Za-z_][A-Za-z0-9_]*):?[-=](.*)$/s.exec(inner);
|
|
392
|
+
this.dynamic.push(`parameter expansion \${${inner}}`);
|
|
393
|
+
if (/\$\(|`/.test(inner))
|
|
394
|
+
this.scanExpansions(inner);
|
|
395
|
+
if (withDefault) {
|
|
396
|
+
parts.push({ kind: 'var', name: withDefault[1], quoted, fallback: (0, quoting_1.stripQuotes)(withDefault[2]) });
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
parts.push(ref ? { kind: 'unknown', ref } : { kind: 'unknown' });
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (next === "'" && !quoted) {
|
|
403
|
+
const close = (0, quoting_1.findAnsiClose)(s, this.i + 2);
|
|
404
|
+
lit((0, quoting_1.decodeAnsiC)(s.slice(this.i + 2, close)), true);
|
|
405
|
+
this.i = close + 1;
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (next === '"' && !quoted) {
|
|
409
|
+
// `$"…"` is a locale string: a double-quoted string.
|
|
410
|
+
this.i++;
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const name = /^[A-Za-z_][A-Za-z0-9_]*/.exec(s.slice(this.i + 1))?.[0];
|
|
414
|
+
if (name) {
|
|
415
|
+
parts.push({ kind: 'var', name, quoted });
|
|
416
|
+
this.i += 1 + name.length;
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (next !== undefined && SPECIAL_PARAMS.has(next)) {
|
|
420
|
+
parts.push({ kind: 'unknown' });
|
|
421
|
+
this.i += 2;
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
lit('$', quoted);
|
|
425
|
+
this.i++;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/** Lex a command. Never throws: a lexer error comes back in `error`. */
|
|
429
|
+
function tokenize(command) {
|
|
430
|
+
const lexer = new Lexer(command, 0);
|
|
431
|
+
try {
|
|
432
|
+
lexer.run();
|
|
433
|
+
return { commands: lexer.commands, dynamic: lexer.dynamic };
|
|
434
|
+
}
|
|
435
|
+
catch (error) {
|
|
436
|
+
if (!(error instanceof quoting_1.LexError))
|
|
437
|
+
throw error;
|
|
438
|
+
return { commands: lexer.commands, dynamic: lexer.dynamic, error: error.message };
|
|
439
|
+
}
|
|
440
|
+
}
|
package/bash/words.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Small predicates on argument text, shared by the analyzer and the command rules. */
|
|
2
|
+
/** Has a `/`, or starts with `.` or `~`: the agent meant a path. */
|
|
3
|
+
export declare function isPathLike(text: string): boolean;
|
|
4
|
+
/** A network URL (`https://…`, `ssh://…`). `file://` is a path, not a URL. */
|
|
5
|
+
export declare function isUrl(text: string): boolean;
|
|
6
|
+
/** `host:path`, `user@host:path`, `rsync://…`; not `./a:b` or `/a:b`. */
|
|
7
|
+
export declare function isRemoteSpec(text: string): boolean;
|
|
8
|
+
/** `localhost`, `127.x.x.x`, `::1` and `*.localhost`: reachable only from this machine. */
|
|
9
|
+
export declare function isLoopbackHost(host: string): boolean;
|
package/bash/words.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** Small predicates on argument text, shared by the analyzer and the command rules. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isPathLike = isPathLike;
|
|
5
|
+
exports.isUrl = isUrl;
|
|
6
|
+
exports.isRemoteSpec = isRemoteSpec;
|
|
7
|
+
exports.isLoopbackHost = isLoopbackHost;
|
|
8
|
+
/** Has a `/`, or starts with `.` or `~`: the agent meant a path. */
|
|
9
|
+
function isPathLike(text) {
|
|
10
|
+
return text.includes('/') || text.startsWith('.') || text.startsWith('~');
|
|
11
|
+
}
|
|
12
|
+
/** A network URL (`https://…`, `ssh://…`). `file://` is a path, not a URL. */
|
|
13
|
+
function isUrl(text) {
|
|
14
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(text) && !/^file:\/\//i.test(text);
|
|
15
|
+
}
|
|
16
|
+
/** `host:path`, `user@host:path`, `rsync://…`; not `./a:b` or `/a:b`. */
|
|
17
|
+
function isRemoteSpec(text) {
|
|
18
|
+
if (/^rsync:\/\//i.test(text))
|
|
19
|
+
return true;
|
|
20
|
+
if (text.startsWith('/') || text.startsWith('.') || text.startsWith('~'))
|
|
21
|
+
return false;
|
|
22
|
+
const colon = text.indexOf(':');
|
|
23
|
+
const slash = text.indexOf('/');
|
|
24
|
+
return colon > 0 && (slash < 0 || colon < slash);
|
|
25
|
+
}
|
|
26
|
+
/** `localhost`, `127.x.x.x`, `::1` and `*.localhost`: reachable only from this machine. */
|
|
27
|
+
function isLoopbackHost(host) {
|
|
28
|
+
const h = host.toLowerCase().replace(/^\[|\]$/g, '');
|
|
29
|
+
return h === 'localhost' || h === '::1' || /^127\.\d+\.\d+\.\d+$/.test(h) || h.endsWith('.localhost');
|
|
30
|
+
}
|