@agentic-kit/permissions 0.0.0-stage → 0.2.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 +23 -0
- package/README.md +278 -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 +38 -4
- 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
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Commands that read, write, copy and delete files: which operands they touch
|
|
4
|
+
* with which access, which forms destroy data, and where a copy puts a file's
|
|
5
|
+
* contents.
|
|
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.tee = tee;
|
|
42
|
+
exports.findRoots = findRoots;
|
|
43
|
+
exports.find = find;
|
|
44
|
+
exports.sed = sed;
|
|
45
|
+
exports.grep = grep;
|
|
46
|
+
exports.awk = awk;
|
|
47
|
+
exports.remove = remove;
|
|
48
|
+
exports.copyLike = copyLike;
|
|
49
|
+
exports.chmodLike = chmodLike;
|
|
50
|
+
exports.dd = dd;
|
|
51
|
+
const path = __importStar(require("node:path"));
|
|
52
|
+
const args_1 = require("./args");
|
|
53
|
+
/**
|
|
54
|
+
* Record that the contents of `sources` (or of `stdin`) land in `dest`: a dir
|
|
55
|
+
* when `dir`, where each file keeps its name.
|
|
56
|
+
*/
|
|
57
|
+
function copies(ctx, what, sources, dest, dir, stdin = []) {
|
|
58
|
+
const { analyzer } = ctx;
|
|
59
|
+
const to = dest.unknown ? null : analyzer.toAbs(dest.text);
|
|
60
|
+
if (to === null)
|
|
61
|
+
return;
|
|
62
|
+
const from = [...stdin];
|
|
63
|
+
for (const s of sources) {
|
|
64
|
+
const abs = s.unknown ? null : analyzer.toAbs(s.text);
|
|
65
|
+
if (abs !== null)
|
|
66
|
+
from.push({ raw: s.raw, abs });
|
|
67
|
+
}
|
|
68
|
+
if (from.length > 0)
|
|
69
|
+
analyzer.tracker.flows.push({ sources: from, to: { kind: 'file', raw: dest.raw, abs: to, dir }, what });
|
|
70
|
+
}
|
|
71
|
+
/** `tee file…`: stdin lands in each file. */
|
|
72
|
+
function tee(ctx, rest) {
|
|
73
|
+
const ops = (0, args_1.parse)(rest).positionals;
|
|
74
|
+
(0, args_1.files)(ctx, ops, 'write');
|
|
75
|
+
for (const op of ops)
|
|
76
|
+
copies(ctx, 'tee', [], op, false, ctx.analyzer.stdin.sources);
|
|
77
|
+
}
|
|
78
|
+
/** Options that come before `find`'s roots. */
|
|
79
|
+
const FIND_LEADING = new Set(['-H', '-L', '-P', '-E', '-X', '-s', '-x', '-d', '-O0', '-O1', '-O2', '-O3']);
|
|
80
|
+
/** The dirs `find` searches (`.` when none is given), and the index of its first expression word. */
|
|
81
|
+
function findRoots(rest) {
|
|
82
|
+
let k = 0;
|
|
83
|
+
while (k < rest.length && FIND_LEADING.has(rest[k].text))
|
|
84
|
+
k++;
|
|
85
|
+
const roots = [];
|
|
86
|
+
while (k < rest.length && !/^[-(!)]/.test(rest[k].text))
|
|
87
|
+
roots.push(rest[k++]);
|
|
88
|
+
return { roots: roots.length > 0 ? roots : [(0, args_1.literalArg)('.')], next: k };
|
|
89
|
+
}
|
|
90
|
+
function find(ctx, rest) {
|
|
91
|
+
const { analyzer } = ctx;
|
|
92
|
+
const { roots, next } = findRoots(rest);
|
|
93
|
+
for (const r of roots)
|
|
94
|
+
analyzer.addPath(r, 'read', true);
|
|
95
|
+
for (let k = next; k < rest.length; k++) {
|
|
96
|
+
const t = rest[k].text;
|
|
97
|
+
if (t === '-delete') {
|
|
98
|
+
for (const r of roots)
|
|
99
|
+
analyzer.destroy(r, 'find -delete');
|
|
100
|
+
}
|
|
101
|
+
else if (/^-(fprint0?|fprintf|fls)$/.test(t) && rest[k + 1]) {
|
|
102
|
+
analyzer.addPath(rest[++k], 'write', true);
|
|
103
|
+
}
|
|
104
|
+
else if (/^-(exec|execdir|ok|okdir)$/.test(t)) {
|
|
105
|
+
const end = rest.findIndex((a, j) => j > k && (a.text === ';' || a.text === '+'));
|
|
106
|
+
const sub = rest.slice(k + 1, end < 0 ? rest.length : end);
|
|
107
|
+
k = end < 0 ? rest.length : end;
|
|
108
|
+
if (sub.length === 0)
|
|
109
|
+
continue;
|
|
110
|
+
// `{}` stands for each match: a path inside each root.
|
|
111
|
+
for (const r of roots) {
|
|
112
|
+
const replaced = sub.map((a) => (a.text.includes('{}') ? { ...a, text: a.text.split('{}').join(`${r.text}/{}`) } : a));
|
|
113
|
+
analyzer.runArgs(replaced, false);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function sed(ctx, rest) {
|
|
119
|
+
const p = (0, args_1.parse)(rest, new Set(['-e', '-f', '--expression', '--file', '-l']));
|
|
120
|
+
const inPlace = (0, args_1.has)(p, '-i', '--in-place') || [...p.flags].some((f) => f.startsWith('-i'));
|
|
121
|
+
let ops = p.positionals;
|
|
122
|
+
// BSD `sed -i '' …`: the empty word is the backup suffix.
|
|
123
|
+
if (inPlace && ops[0]?.text === '')
|
|
124
|
+
ops = ops.slice(1);
|
|
125
|
+
for (const f of (0, args_1.valuesOf)(p, '-f', '--file'))
|
|
126
|
+
ctx.analyzer.addPath(f, 'read', true);
|
|
127
|
+
if ((0, args_1.valuesOf)(p, '-e', '--expression', '-f', '--file').length === 0)
|
|
128
|
+
ops = ops.slice(1);
|
|
129
|
+
(0, args_1.files)(ctx, ops, inPlace ? 'write' : 'read');
|
|
130
|
+
}
|
|
131
|
+
function grep(ctx, rest) {
|
|
132
|
+
const p = (0, args_1.parse)(rest, new Set([
|
|
133
|
+
'-e',
|
|
134
|
+
'-f',
|
|
135
|
+
'-m',
|
|
136
|
+
'-A',
|
|
137
|
+
'-B',
|
|
138
|
+
'-C',
|
|
139
|
+
'-g',
|
|
140
|
+
'-t',
|
|
141
|
+
'-T',
|
|
142
|
+
'-d',
|
|
143
|
+
'-D',
|
|
144
|
+
'--regexp',
|
|
145
|
+
'--file',
|
|
146
|
+
'--max-count',
|
|
147
|
+
'--glob',
|
|
148
|
+
'--type',
|
|
149
|
+
'--type-not',
|
|
150
|
+
'--include',
|
|
151
|
+
'--exclude',
|
|
152
|
+
'--exclude-dir',
|
|
153
|
+
'--context',
|
|
154
|
+
'--after-context',
|
|
155
|
+
'--before-context'
|
|
156
|
+
]));
|
|
157
|
+
for (const f of (0, args_1.valuesOf)(p, '-f', '--file'))
|
|
158
|
+
ctx.analyzer.addPath(f, 'read', true);
|
|
159
|
+
const ops = (0, args_1.valuesOf)(p, '-e', '--regexp', '-f', '--file').length ? p.positionals : p.positionals.slice(1);
|
|
160
|
+
for (const op of ops)
|
|
161
|
+
ctx.analyzer.operand(op);
|
|
162
|
+
}
|
|
163
|
+
function awk(name, ctx, rest) {
|
|
164
|
+
const p = (0, args_1.parse)(rest, new Set(['-f', '-v', '-F', '-i', '--file', '--include']));
|
|
165
|
+
let ops = p.positionals;
|
|
166
|
+
const progFiles = (0, args_1.valuesOf)(p, '-f', '--file');
|
|
167
|
+
for (const f of progFiles)
|
|
168
|
+
ctx.analyzer.addPath(f, 'read', true);
|
|
169
|
+
if (progFiles.length === 0) {
|
|
170
|
+
const program = ops[0];
|
|
171
|
+
ops = ops.slice(1);
|
|
172
|
+
if (program && (program.unknown || /\bsystem\s*\(|\|/.test(program.text))) {
|
|
173
|
+
ctx.analyzer.dynamic(`its ${name} program runs commands`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const inPlace = (0, args_1.valuesOf)(p, '-i', '--include').some((v) => v.text === 'inplace');
|
|
177
|
+
for (const op of ops) {
|
|
178
|
+
if (/^[A-Za-z_]\w*=/.test(op.text))
|
|
179
|
+
continue;
|
|
180
|
+
if (inPlace)
|
|
181
|
+
ctx.analyzer.addPath(op, 'write', true);
|
|
182
|
+
else
|
|
183
|
+
ctx.analyzer.operand(op);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function remove(name, ctx, rest) {
|
|
187
|
+
const { analyzer } = ctx;
|
|
188
|
+
const p = (0, args_1.parse)(rest);
|
|
189
|
+
if ((0, args_1.has)(p, '--no-preserve-root'))
|
|
190
|
+
analyzer.flag('destructive', `it runs ${name} --no-preserve-root`);
|
|
191
|
+
const forceful = name === 'rimraf' ||
|
|
192
|
+
name === 'shred' ||
|
|
193
|
+
(0, args_1.has)(p, '-r', '-R', '-f', '--recursive', '--force');
|
|
194
|
+
for (const op of p.positionals) {
|
|
195
|
+
analyzer.addPath(op, 'write', true);
|
|
196
|
+
if (forceful)
|
|
197
|
+
analyzer.destroy(op, `${name} ${[...p.flags].join(' ')}`.trim(), true);
|
|
198
|
+
}
|
|
199
|
+
if (ctx.xargs && (name === 'rm' || name === 'rimraf' || name === 'shred' || name === 'unlink')) {
|
|
200
|
+
analyzer.destroy(null, `xargs ${name}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function copyLike(name, ctx, rest) {
|
|
204
|
+
const { analyzer } = ctx;
|
|
205
|
+
const p = (0, args_1.parse)(rest, new Set(['-t', '--target-directory', '-S', '--suffix', '-m', '--mode', '-o', '--owner', '-g', '--group']));
|
|
206
|
+
const targetDir = (0, args_1.valuesOf)(p, '-t', '--target-directory')[0];
|
|
207
|
+
const ops = p.positionals;
|
|
208
|
+
const sourceAccess = name === 'mv' ? 'write' : 'read';
|
|
209
|
+
if (name === 'install' && (0, args_1.has)(p, '-d'))
|
|
210
|
+
return (0, args_1.files)(ctx, ops, 'write');
|
|
211
|
+
if (targetDir) {
|
|
212
|
+
analyzer.addPath(targetDir, 'write', true);
|
|
213
|
+
if (name !== 'ln')
|
|
214
|
+
copies(ctx, name, ops, targetDir, true);
|
|
215
|
+
return (0, args_1.files)(ctx, ops, sourceAccess);
|
|
216
|
+
}
|
|
217
|
+
if (ops.length === 1 && name === 'ln') {
|
|
218
|
+
analyzer.addPath(ops[0], 'read', true);
|
|
219
|
+
analyzer.addPath({ ...ops[0], text: path.basename(ops[0].text) }, 'write', true);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (ops.length === 0)
|
|
223
|
+
return;
|
|
224
|
+
const dest = ops[ops.length - 1];
|
|
225
|
+
(0, args_1.files)(ctx, ops.slice(0, -1), sourceAccess);
|
|
226
|
+
analyzer.addPath(dest, 'write', true);
|
|
227
|
+
if (name !== 'ln')
|
|
228
|
+
copies(ctx, name, ops.slice(0, -1), dest, ops.length > 2 || dest.text.endsWith('/'));
|
|
229
|
+
}
|
|
230
|
+
function chmodLike(name, ctx, rest) {
|
|
231
|
+
let recursive = false;
|
|
232
|
+
let reference = false;
|
|
233
|
+
const operands = [];
|
|
234
|
+
for (const arg of rest) {
|
|
235
|
+
const t = arg.text;
|
|
236
|
+
if (/^-[RvcfhHLP]+$/.test(t) || t === '--recursive' || t.startsWith('--')) {
|
|
237
|
+
if (/^-[a-zA-Z]*R/.test(t) || t === '--recursive')
|
|
238
|
+
recursive = true;
|
|
239
|
+
if (t.startsWith('--reference'))
|
|
240
|
+
reference = true;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
operands.push(arg);
|
|
244
|
+
}
|
|
245
|
+
// The first operand is the mode or owner (`755`, `+x`, `-w`, `user:group`).
|
|
246
|
+
for (const op of reference ? operands : operands.slice(1)) {
|
|
247
|
+
ctx.analyzer.addPath(op, 'write', true);
|
|
248
|
+
if (recursive)
|
|
249
|
+
ctx.analyzer.destroy(op, `${name} -R`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function dd(ctx, rest) {
|
|
253
|
+
const input = rest.find((a) => a.text.startsWith('if='));
|
|
254
|
+
const output = rest.find((a) => a.text.startsWith('of='));
|
|
255
|
+
if (output) {
|
|
256
|
+
const dest = { ...output, text: output.text.slice(3) };
|
|
257
|
+
if (input)
|
|
258
|
+
copies(ctx, 'dd', [{ ...input, text: input.text.slice(3) }], dest, false);
|
|
259
|
+
else
|
|
260
|
+
copies(ctx, 'dd', [], dest, false, ctx.analyzer.stdin.sources);
|
|
261
|
+
}
|
|
262
|
+
for (const arg of rest) {
|
|
263
|
+
const m = /^(if|of)=(.*)$/.exec(arg.text);
|
|
264
|
+
if (!m)
|
|
265
|
+
continue;
|
|
266
|
+
const target = { ...arg, text: m[2] };
|
|
267
|
+
if (m[1] === 'if') {
|
|
268
|
+
ctx.analyzer.addPath(target, 'read', true);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
if (m[2].startsWith('/dev/') && !/^\/dev\/(null|stdout|stderr)$/.test(m[2])) {
|
|
272
|
+
ctx.analyzer.flag('destructive', `dd writes raw blocks to ${m[2]}`);
|
|
273
|
+
}
|
|
274
|
+
ctx.analyzer.addPath(target, 'write', true);
|
|
275
|
+
ctx.analyzer.destroy(target, 'dd of=');
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
package/bash/flows.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where file contents go inside one command line: down pipes, into
|
|
3
|
+
* redirections and copies, onto the clipboard, and through programs that
|
|
4
|
+
* change them. The analyzer only records the flows; the classifier decides
|
|
5
|
+
* which sources are secret files, since that needs the real paths.
|
|
6
|
+
*/
|
|
7
|
+
import type { Analyzer, Arg, Run } from './analyze';
|
|
8
|
+
import type { SimpleCommand } from './tokenize';
|
|
9
|
+
export interface Source {
|
|
10
|
+
raw: string;
|
|
11
|
+
abs: string;
|
|
12
|
+
}
|
|
13
|
+
export type FlowSink =
|
|
14
|
+
/** A copy into a file, or into a dir when `dir` (the file keeps its name). */
|
|
15
|
+
{
|
|
16
|
+
kind: 'file';
|
|
17
|
+
raw: string;
|
|
18
|
+
abs: string;
|
|
19
|
+
dir: boolean;
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'clipboard';
|
|
22
|
+
}
|
|
23
|
+
/** A program whose output is not the text it read (`base64`, `sed`, `node`). */
|
|
24
|
+
| {
|
|
25
|
+
kind: 'transform';
|
|
26
|
+
};
|
|
27
|
+
export interface ContentFlow {
|
|
28
|
+
sources: Source[];
|
|
29
|
+
to: FlowSink;
|
|
30
|
+
/** The program, for the reason: `cp`, `base64`, `>`. */
|
|
31
|
+
what: string;
|
|
32
|
+
}
|
|
33
|
+
/** What one command reads on stdin. */
|
|
34
|
+
export interface Stdin {
|
|
35
|
+
/** Files whose contents arrive on stdin. */
|
|
36
|
+
sources: Source[];
|
|
37
|
+
/** The words on stdin when they are known (`echo a b |`, `<<< a`); `null` when they are not. */
|
|
38
|
+
words: Arg[] | null;
|
|
39
|
+
/**
|
|
40
|
+
* When stdin is a file listing (`find src |`, `git ls-files |`): the roots
|
|
41
|
+
* every listed name lies under. `null` when it is anything else.
|
|
42
|
+
*/
|
|
43
|
+
listedUnder: Arg[] | null;
|
|
44
|
+
}
|
|
45
|
+
/** The program prints what it read as is, or prints nothing of it. */
|
|
46
|
+
export declare function keepsContent(name: string, rest: readonly Arg[]): boolean;
|
|
47
|
+
/** Follows contents from command to command within one analyzed string. */
|
|
48
|
+
export declare class FlowTracker {
|
|
49
|
+
private readonly analyzer;
|
|
50
|
+
readonly flows: ContentFlow[];
|
|
51
|
+
/** The files whose contents each command's stdout carries. */
|
|
52
|
+
private readonly outputs;
|
|
53
|
+
/** The words each `echo` or `printf` printed. */
|
|
54
|
+
private readonly printed;
|
|
55
|
+
/** The roots each file-listing command printed names under. */
|
|
56
|
+
private readonly listed;
|
|
57
|
+
/** Every source seen so far: what a `( … ) |` group may print. */
|
|
58
|
+
private readonly all;
|
|
59
|
+
constructor(analyzer: Analyzer);
|
|
60
|
+
/** What arrives on the command's stdin: from `<`, a here-string, a heredoc, or a pipe. */
|
|
61
|
+
stdinOf(cmd: SimpleCommand): Stdin;
|
|
62
|
+
/**
|
|
63
|
+
* After a command ran: remember what its stdout carries, and record a
|
|
64
|
+
* transform and its stdout redirections as flows.
|
|
65
|
+
*/
|
|
66
|
+
record(cmd: SimpleCommand, run: Run | null, sources: Source[], redirects: Source[]): void;
|
|
67
|
+
}
|
package/bash/flows.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Where file contents go inside one command line: down pipes, into
|
|
4
|
+
* redirections and copies, onto the clipboard, and through programs that
|
|
5
|
+
* change them. The analyzer only records the flows; the classifier decides
|
|
6
|
+
* which sources are secret files, since that needs the real paths.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.FlowTracker = void 0;
|
|
10
|
+
exports.keepsContent = keepsContent;
|
|
11
|
+
const args_1 = require("./args");
|
|
12
|
+
const file_commands_1 = require("./file-commands");
|
|
13
|
+
/** Readers whose output is the file's text unchanged, so redaction still matches it. */
|
|
14
|
+
const VERBATIM = new Set([
|
|
15
|
+
'cat',
|
|
16
|
+
'head',
|
|
17
|
+
'tail',
|
|
18
|
+
'less',
|
|
19
|
+
'more',
|
|
20
|
+
'bat',
|
|
21
|
+
'grep',
|
|
22
|
+
'egrep',
|
|
23
|
+
'fgrep',
|
|
24
|
+
'ggrep',
|
|
25
|
+
'rg',
|
|
26
|
+
'ag',
|
|
27
|
+
'ack',
|
|
28
|
+
'wc',
|
|
29
|
+
'diff',
|
|
30
|
+
'tee',
|
|
31
|
+
'sort',
|
|
32
|
+
'uniq',
|
|
33
|
+
'nl'
|
|
34
|
+
]);
|
|
35
|
+
/**
|
|
36
|
+
* Commands that do not print what they read (`ls`, `test`, `rm`), or whose
|
|
37
|
+
* output another signal already covers (copies, the clipboard, `source`).
|
|
38
|
+
*/
|
|
39
|
+
const NO_CONTENT = new Set([
|
|
40
|
+
'ls',
|
|
41
|
+
'stat',
|
|
42
|
+
'du',
|
|
43
|
+
'file',
|
|
44
|
+
'test',
|
|
45
|
+
'[',
|
|
46
|
+
'[[',
|
|
47
|
+
'touch',
|
|
48
|
+
'cd',
|
|
49
|
+
'pushd',
|
|
50
|
+
'realpath',
|
|
51
|
+
'readlink',
|
|
52
|
+
'basename',
|
|
53
|
+
'dirname',
|
|
54
|
+
'rm',
|
|
55
|
+
'rmdir',
|
|
56
|
+
'unlink',
|
|
57
|
+
'shred',
|
|
58
|
+
'srm',
|
|
59
|
+
'rimraf',
|
|
60
|
+
'chmod',
|
|
61
|
+
'chown',
|
|
62
|
+
'chgrp',
|
|
63
|
+
'mkdir',
|
|
64
|
+
'find',
|
|
65
|
+
'git',
|
|
66
|
+
'source',
|
|
67
|
+
'.',
|
|
68
|
+
'cp',
|
|
69
|
+
'mv',
|
|
70
|
+
'install',
|
|
71
|
+
'ln',
|
|
72
|
+
'dd',
|
|
73
|
+
'pbcopy',
|
|
74
|
+
'xclip',
|
|
75
|
+
'xsel',
|
|
76
|
+
'wl-copy'
|
|
77
|
+
]);
|
|
78
|
+
/** `head -c`, `grep -o`, `rg -r`: a verbatim reader that cuts values apart. */
|
|
79
|
+
function trims(name, rest) {
|
|
80
|
+
const flags = rest.map((a) => a.text).filter((t) => t.startsWith('-'));
|
|
81
|
+
if (name === 'head' || name === 'tail')
|
|
82
|
+
return flags.some((t) => /^(-c|--bytes)/.test(t));
|
|
83
|
+
if (name === 'rg' && flags.some((t) => /^(-r|--replace)/.test(t)))
|
|
84
|
+
return true;
|
|
85
|
+
return flags.some((t) => t === '--only-matching' || /^-[a-zA-Z]*o[a-zA-Z]*$/.test(t));
|
|
86
|
+
}
|
|
87
|
+
/** The program prints what it read as is, or prints nothing of it. */
|
|
88
|
+
function keepsContent(name, rest) {
|
|
89
|
+
if (NO_CONTENT.has(name))
|
|
90
|
+
return true;
|
|
91
|
+
return VERBATIM.has(name) && !trims(name, rest);
|
|
92
|
+
}
|
|
93
|
+
/** Split words the way `xargs` does (on whitespace). `null` when one is not known. */
|
|
94
|
+
function splitWords(args) {
|
|
95
|
+
if (args.some((a) => a.unknown))
|
|
96
|
+
return null;
|
|
97
|
+
return args.flatMap((a) => a.text
|
|
98
|
+
.split(/\s+/)
|
|
99
|
+
.filter(Boolean)
|
|
100
|
+
.map((text) => (0, args_1.literalArg)(text, a.raw)));
|
|
101
|
+
}
|
|
102
|
+
/** The words `echo` or `printf` prints; `null` when one is not known. */
|
|
103
|
+
function printedWords(name, rest) {
|
|
104
|
+
if (name === 'printf')
|
|
105
|
+
return rest[0]?.text === '-v' ? [] : splitWords(rest);
|
|
106
|
+
let k = 0;
|
|
107
|
+
while (k < rest.length && /^-[neE]+$/.test(rest[k].text))
|
|
108
|
+
k++;
|
|
109
|
+
return splitWords(rest.slice(k));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The roots a file-listing command prints names under: `find`, `ls`, and
|
|
113
|
+
* `git ls-files`. `null` for any other command, an unknown root, or a `find`
|
|
114
|
+
* that follows symlinks (`-L`), whose names can leave its roots.
|
|
115
|
+
*/
|
|
116
|
+
function listedRoots(name, rest) {
|
|
117
|
+
if (rest.some((a) => a.unknown))
|
|
118
|
+
return null;
|
|
119
|
+
if (name === 'find') {
|
|
120
|
+
if (rest.some((a) => a.text === '-L' || a.text === '-follow'))
|
|
121
|
+
return null;
|
|
122
|
+
return (0, file_commands_1.findRoots)(rest).roots;
|
|
123
|
+
}
|
|
124
|
+
if (name === 'ls') {
|
|
125
|
+
const roots = rest.filter((a) => !a.text.startsWith('-'));
|
|
126
|
+
return roots.length > 0 ? roots : [(0, args_1.literalArg)('.')];
|
|
127
|
+
}
|
|
128
|
+
if (name === 'git' && rest[0]?.text === 'ls-files')
|
|
129
|
+
return [(0, args_1.literalArg)('.')];
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const GROUP_END = new Set(['}', 'done', 'fi', 'esac']);
|
|
133
|
+
/** Follows contents from command to command within one analyzed string. */
|
|
134
|
+
class FlowTracker {
|
|
135
|
+
analyzer;
|
|
136
|
+
flows = [];
|
|
137
|
+
/** The files whose contents each command's stdout carries. */
|
|
138
|
+
outputs = new Map();
|
|
139
|
+
/** The words each `echo` or `printf` printed. */
|
|
140
|
+
printed = new Map();
|
|
141
|
+
/** The roots each file-listing command printed names under. */
|
|
142
|
+
listed = new Map();
|
|
143
|
+
/** Every source seen so far: what a `( … ) |` group may print. */
|
|
144
|
+
all = [];
|
|
145
|
+
constructor(analyzer) {
|
|
146
|
+
this.analyzer = analyzer;
|
|
147
|
+
}
|
|
148
|
+
/** What arrives on the command's stdin: from `<`, a here-string, a heredoc, or a pipe. */
|
|
149
|
+
stdinOf(cmd) {
|
|
150
|
+
const { analyzer } = this;
|
|
151
|
+
const sources = [];
|
|
152
|
+
let words = [];
|
|
153
|
+
let listedUnder = null;
|
|
154
|
+
for (const r of cmd.redirects) {
|
|
155
|
+
if (r.op !== '<' || !r.target)
|
|
156
|
+
continue;
|
|
157
|
+
words = null;
|
|
158
|
+
for (const t of analyzer.resolveWord(r.target)) {
|
|
159
|
+
const abs = t.unknown ? null : analyzer.toAbs(t.text);
|
|
160
|
+
if (abs)
|
|
161
|
+
sources.push({ raw: t.raw, abs });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (cmd.heredocs.length > 0)
|
|
165
|
+
words = null;
|
|
166
|
+
for (const w of cmd.hereStrings) {
|
|
167
|
+
const said = splitWords(analyzer.resolveWord(w));
|
|
168
|
+
words = words && said ? [...words, ...said] : null;
|
|
169
|
+
}
|
|
170
|
+
if (cmd.pipedIn) {
|
|
171
|
+
const up = cmd.upstream;
|
|
172
|
+
if (!up || up === 'group' || up.words.every((w) => GROUP_END.has(w.raw))) {
|
|
173
|
+
sources.push(...this.all);
|
|
174
|
+
words = null;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
sources.push(...(this.outputs.get(up) ?? []));
|
|
178
|
+
const said = this.printed.get(up);
|
|
179
|
+
words = words && said ? [...words, ...said] : null;
|
|
180
|
+
const onlyPipe = cmd.redirects.every((r) => r.op !== '<') && cmd.hereStrings.length === 0 && cmd.heredocs.length === 0;
|
|
181
|
+
listedUnder = onlyPipe ? (this.listed.get(up) ?? null) : null;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { sources, words, listedUnder };
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* After a command ran: remember what its stdout carries, and record a
|
|
188
|
+
* transform and its stdout redirections as flows.
|
|
189
|
+
*/
|
|
190
|
+
record(cmd, run, sources, redirects) {
|
|
191
|
+
this.outputs.set(cmd, sources);
|
|
192
|
+
this.all.push(...sources);
|
|
193
|
+
if (!run)
|
|
194
|
+
return;
|
|
195
|
+
if (run.name === 'echo' || run.name === 'printf')
|
|
196
|
+
this.printed.set(cmd, printedWords(run.name, run.rest));
|
|
197
|
+
const roots = listedRoots(run.name, run.rest);
|
|
198
|
+
if (roots)
|
|
199
|
+
this.listed.set(cmd, roots);
|
|
200
|
+
if (sources.length === 0)
|
|
201
|
+
return;
|
|
202
|
+
if (!keepsContent(run.name, run.rest))
|
|
203
|
+
this.flows.push({ sources, to: { kind: 'transform' }, what: run.name });
|
|
204
|
+
for (const r of redirects)
|
|
205
|
+
this.flows.push({ sources, to: { kind: 'file', ...r, dir: false }, what: '>' });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.FlowTracker = FlowTracker;
|
package/bash/git.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* git: options that make it run programs, pushes that rewrite or leave for an
|
|
3
|
+
* unknown remote, config writes, and force-adding a secret file.
|
|
4
|
+
*/
|
|
5
|
+
import type { Arg } from './analyze';
|
|
6
|
+
import { type CommandContext } from './args';
|
|
7
|
+
export declare function git(ctx: CommandContext, rest: readonly Arg[]): void;
|