@eocrm/cli 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +141 -2
- package/hooks/dispatch.mjs +34 -0
- package/lib/api.mjs +151 -0
- package/lib/cli/attribute.mjs +28 -0
- package/lib/cli/hooks.mjs +210 -0
- package/lib/cli/openspec.mjs +102 -0
- package/lib/cli/run.mjs +229 -0
- package/lib/codex.mjs +78 -0
- package/lib/credentials.mjs +207 -0
- package/lib/detect.mjs +279 -0
- package/lib/endpoints.mjs +46 -0
- package/lib/flush.mjs +260 -0
- package/lib/gate.mjs +87 -0
- package/lib/git.mjs +101 -0
- package/lib/ids.mjs +3 -0
- package/lib/login.mjs +283 -0
- package/lib/outbox.mjs +130 -0
- package/lib/paths.mjs +53 -0
- package/lib/plans.mjs +265 -0
- package/lib/reconcile.mjs +213 -0
- package/lib/remind.mjs +170 -0
- package/lib/run.mjs +560 -0
- package/lib/scrub.mjs +515 -0
- package/lib/state.mjs +219 -0
- package/lib/usage.mjs +117 -0
- package/lib/workspace.mjs +109 -0
- package/package.json +34 -4
- package/scripts/cli.mjs +8 -0
- package/scripts/eocrm.mjs +395 -0
package/lib/scrub.mjs
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
const KEYS = '(?:TOKEN|SECRET|PASSWORD|PASSWD|PASSPHRASE|PWD|_PASS(?![A-Z0-9])|_AUTH(?![A-Z0-9_])|API[-_]?KEY|PRIVATE[-_]?KEY)';
|
|
2
|
+
// eocrm's SecretScrubber runs PCRE without /u: \s is ASCII and these are its Unicode spaces.
|
|
3
|
+
const WIDE = '\\u0085\\u00a0\\u1680\\u180e\\u2000-\\u200b\\u2028\\u2029\\u202f\\u205f-\\u2064\\u3000\\ufeff';
|
|
4
|
+
const S = `[\\s${WIDE}]`;
|
|
5
|
+
const H = `[ \\t\\v\\f${WIDE}]`;
|
|
6
|
+
const HW = `[ \\t${WIDE}]`;
|
|
7
|
+
const NS = '[^ \\t\\n\\v\\f\\r]';
|
|
8
|
+
const NAME = `(?<![A-Z0-9_-])(?=[A-Z0-9_-]*${KEYS})[A-Z0-9_-]+`;
|
|
9
|
+
const NAME_PASS = `(?<![A-Z0-9_-])(?=[A-Z0-9_-]*(?:${KEYS}|(?<![A-Z0-9-])PASS(?![A-Z0-9])))[A-Z0-9_-]+`;
|
|
10
|
+
const PRE = String.raw`((?:(?<!\\)\\+)?["']?)`;
|
|
11
|
+
|
|
12
|
+
// X*+ is written X*(?!X): JS has no possessive quantifiers.
|
|
13
|
+
const PATTERNS = [
|
|
14
|
+
[/-{4,5}BEGIN [A-Z ]*(?:PRIVATE|SECRET) KEY(?: BLOCK)?-{4,5}[\s\S]*?(?:-{4,5}END [A-Z ]*(?:PRIVATE|SECRET) KEY(?: BLOCK)?-{4,5}|(?=\n?$))/gi, '[redacted]'],
|
|
15
|
+
[/PuTTY-User-Key-File-\d+:[\s\S]*?(?:Private-MAC:[^\n]*|(?=\n?$))/gi, '[redacted]'],
|
|
16
|
+
[/(?<=https:\/\/|http:\/\/)[^/ \t\n\v\f\r:@]+@/g, (m) => (Buffer.byteLength(m) > 16 ? '[redacted]@' : m)],
|
|
17
|
+
[/(?<![\w/:@.%-])[\w.-]+:[^ \t\n\v\f\r@/:]+@(?=[\w.-]+:)/g, '[redacted]@'],
|
|
18
|
+
[new RegExp(`\\b(bearer|basic)(${S}+["']?)[\\w.~+/=-]{8,}`, 'gi'), '$1$2[redacted]'],
|
|
19
|
+
[new RegExp(`\\b(bearer|basic)(${S}*["']?${S}*(?!${S}))(?:\\$\\([^)\\n]*\\)?|\\$\\{[^}\\n]*\\}?|<\\([^)\\n]*\\)?|\`[^\`\\n]*\`?)`, 'gi'), '$1$2[redacted]'],
|
|
20
|
+
[new RegExp(`(\\bAuthorization${S}*:${S}*token)(${S}+)[\\w.~+/=-]{8,}`, 'gi'), '$1$2[redacted]'],
|
|
21
|
+
[new RegExp(`(?<=(["']))(Cookie${S}*:${S}*)(?:\\\\.|(?!\\1)[^\\\\\\n])*`, 'gi'), '$2[redacted]'],
|
|
22
|
+
[new RegExp(`(?<!["'])(\\bCookie${S}*:${S}*)[^\\n]+`, 'gi'), '$1[redacted]'],
|
|
23
|
+
[null, yamlList],
|
|
24
|
+
// After `KEY:` the value is the rest of the line, else the next non-blank line (fail closed).
|
|
25
|
+
[new RegExp(String.raw`${PRE}(${NAME}\1${S}*:(?![:=])${HW}*(?!${HW}))(?:[|>][-+0-9]*[ \t]*(?:\r?\n[ \t]+[^\r\n]*)+|(?:(?:\r\n?|\n)${H}*(?!${H}))+(?![\r\n])(?!-${H}+\[redacted\](?![^\r\n]))[^\r\n]+|[^\r\n]+)`, 'gi'), '$1$2[redacted]'],
|
|
26
|
+
[new RegExp(String.raw`${PRE}(${NAME_PASS})\1(${S}*(?:=>|[:?+]=|={1,3}|::|<-)${S}*(?!${S}))(?:(")(?:\\.|[^"\\\n])*("?)|(')[^'\n]*('?)|((?<run>\\+)")(?:[^\\\n]|\\+(?![\\"])|(?=\k<run>\\)\\+")*((?:\\+")?)|\[[^\]\n]*\]?${NS}*|\{[^}\n]*\}?${NS}*|(?!-${S})${NS}+)`, 'gi'), '$1$2$1$3$4$6$8[redacted]$5$7$10'],
|
|
27
|
+
[new RegExp(`((?:\\b(?:export|setenv|set[ \\t]+-[A-Z]+)|(?<!${NS})(?:-e|--env))[ \\t]+(?=[A-Z0-9_-]*${KEYS})[A-Z0-9_-]+[ \\t]+)[^\\s=]${NS}*`, 'gi'), '$1[redacted]'],
|
|
28
|
+
[new RegExp(`\\b(IDENTIFIED${S}+BY|PASSWORD)(${S}+)(['"])(?:(?!\\3)[^\\n])*\\3?`, 'gi'), '$1$2$3[redacted]$3'],
|
|
29
|
+
[new RegExp(`\\b(IDENTIFIED${S}+BY${S}+)(?!['"])[^ \\t\\n\\v\\f\\r;'"]+`, 'g'), '$1[redacted]'],
|
|
30
|
+
[new RegExp(`(<(?=[\\w:.-]*${KEYS})[\\w:.-]+(?![\\w:.-])[^<>\\n]*>)[^<\\n]+`, 'gi'), '$1[redacted]'],
|
|
31
|
+
[new RegExp(`(\\bhtpasswd[ \\t]+(?:-[A-Zac-z]*[ \\t]+)*-[A-Zac-z]*b[A-Za-z]*(?:[ \\t]+-[A-Za-z]*)*(?![ \\t]+-)[ \\t]+${NS}+[ \\t]+${NS}+[ \\t]+)${NS}+`, 'g'), '$1[redacted]'],
|
|
32
|
+
[/eok_[A-Za-z0-9]{8,}/g, '[redacted]'],
|
|
33
|
+
[/gh[pousr]_[A-Za-z0-9]{20,}/g, '[redacted]'],
|
|
34
|
+
[/github_pat_[A-Za-z0-9_]{20,}/g, '[redacted]'],
|
|
35
|
+
[/\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{20,}/g, '[redacted]'],
|
|
36
|
+
[/\b[sr]k_(?:live|test)_[A-Za-z0-9]{10,}/g, '[redacted]'],
|
|
37
|
+
[/AKIA[0-9A-Z]{16}/g, '[redacted]'],
|
|
38
|
+
[/\b(?:xox[abeprs]|xapp)-[A-Za-z0-9-]{10,}/g, '[redacted]'],
|
|
39
|
+
[/\bglpat-[\w-]{20,}/g, '[redacted]'],
|
|
40
|
+
[/\bnpm_[A-Za-z0-9]{30,}/g, '[redacted]'],
|
|
41
|
+
[/\bAIza[\w-]{30,}/g, '[redacted]'],
|
|
42
|
+
[/((?<!\\)\\*"(?:auth|password|identitytoken)\\*"[ \t]*:[ \t]*\\*")[^"\\\n]+/gi, '$1[redacted]'],
|
|
43
|
+
[/((?<!\\)\\*'(?:auth|password|identitytoken)\\*'[ \t]*:[ \t]*\\*')[^'\\\n]+/gi, '$1[redacted]'],
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const KEYS_RE = new RegExp(KEYS, 'i');
|
|
47
|
+
const SPACED = new RegExp(S);
|
|
48
|
+
const WIDE_OR_COMMA = new RegExp(`[${WIDE}]|,`, 'g');
|
|
49
|
+
const SUB_FLAG = new RegExp(`(?:^|${S})(-[^\\s${WIDE}]+)`, 'g');
|
|
50
|
+
const USERINFO = /(?<=:\/\/|:\\\/\\\/)[^/ \t\n\v\f\r:@]*:[^ \t\n\v\f\r]*/g;
|
|
51
|
+
const YAML_KEY = new RegExp(`${NAME}["']?[ \\t]*:${H}*\\r?\\n`, 'gi');
|
|
52
|
+
const H_RE = new RegExp(H);
|
|
53
|
+
const MAX_NEST = 4;
|
|
54
|
+
const MYSQL = { '-p': 'any', '--pass': 'any' };
|
|
55
|
+
const LDAP = { '-w': 'any' };
|
|
56
|
+
const CONTAINER = { '-u': 'uid', '--user': 'uid' };
|
|
57
|
+
// Container options taking a value; an unknown one counts as boolean, ending the options early (fail closed).
|
|
58
|
+
const CONTAINER_VALUED = new Set([
|
|
59
|
+
'-a', '-c', '-e', '-f', '-h', '-H', '-l', '-m', '-p', '-u', '-v', '-w', '--add-host', '--attach', '--cap-add', '--cap-drop',
|
|
60
|
+
'--config', '--context', '--cpus', '--device', '--dns', '--entrypoint', '--env', '--env-file', '--file', '--gpus', '--host',
|
|
61
|
+
'--hostname', '--label', '--log-driver', '--log-level', '--log-opt', '--memory', '--mount', '--name', '--network', '--platform',
|
|
62
|
+
'--profile', '--project-directory', '--project-name', '--publish', '--pull', '--restart', '--security-opt', '--shm-size',
|
|
63
|
+
'--tmpfs', '--ulimit', '--user', '--volume', '--volumes-from', '--workdir',
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
const FLAGS = {
|
|
67
|
+
'': {
|
|
68
|
+
'-u': 'userinfo', '--user': 'userinfo', '--cookie': 'any', '--oauth2-bearer': 'any',
|
|
69
|
+
'--proxy-user': 'any', '-pass': 'openssl', '-passin': 'openssl', '-passout': 'openssl',
|
|
70
|
+
},
|
|
71
|
+
curl: { '-u': 'not-url', '--user': 'not-url', '-b': 'any', '-U': 'any', '--pass': 'any', '-E': 'userinfo', '--cert': 'userinfo' },
|
|
72
|
+
mysql: MYSQL, mysqldump: MYSQL, mysqladmin: MYSQL,
|
|
73
|
+
mariadb: MYSQL, 'mariadb-dump': MYSQL, 'mariadb-admin': MYSQL,
|
|
74
|
+
sshpass: { '-p': 'any' },
|
|
75
|
+
'docker login': { '-p': 'any' },
|
|
76
|
+
// uid:gid passes unless another command in the line also takes -u.
|
|
77
|
+
docker: CONTAINER, podman: CONTAINER, nerdctl: CONTAINER,
|
|
78
|
+
'redis-cli': { '-a': 'any', '--pass': 'any' },
|
|
79
|
+
zip: { '-P': 'any' }, unzip: { '-P': 'any' },
|
|
80
|
+
psql: { '--pass': 'any' }, openconnect: { '--pass': 'any' },
|
|
81
|
+
mongosh: { '-p': 'any' }, mongo: { '-p': 'any' }, smbclient: { '-U': 'any' }, openssl: { '-k': 'any' },
|
|
82
|
+
ldapsearch: LDAP, ldapadd: LDAP, ldapmodify: LDAP, ldapdelete: LDAP, ldapwhoami: LDAP,
|
|
83
|
+
'7z': { '-p': 'attached' }, '7za': { '-p': 'attached' }, '7zz': { '-p': 'attached' },
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const LIMITS = {
|
|
87
|
+
stepLabel: 200,
|
|
88
|
+
commitSubject: 200,
|
|
89
|
+
commitRepo: 200,
|
|
90
|
+
testCommand: 500,
|
|
91
|
+
resultLine: 500,
|
|
92
|
+
findingSummary: 1000,
|
|
93
|
+
rulingWhat: 500,
|
|
94
|
+
rulingWhy: 1000,
|
|
95
|
+
rulingCostIfWrong: 500,
|
|
96
|
+
prUrl: 500,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const SUFFIX = '… [truncated]';
|
|
100
|
+
const MAX_SCAN = 16384;
|
|
101
|
+
|
|
102
|
+
export function scrub(text) {
|
|
103
|
+
if (text.length > MAX_SCAN) return '[redacted: unscannable]';
|
|
104
|
+
const hits = [...listPass(text), ...flagPass(text, 0)].sort((x, y) => x[0] - y[0] || y[1] - x[1]);
|
|
105
|
+
let out = '';
|
|
106
|
+
let at = 0;
|
|
107
|
+
// Overlapping hits redact their union.
|
|
108
|
+
for (const [start, end, withText] of hits) {
|
|
109
|
+
if (end <= at) continue;
|
|
110
|
+
out += text.slice(at, Math.max(at, start)) + withText;
|
|
111
|
+
at = end;
|
|
112
|
+
}
|
|
113
|
+
out = (out + text.slice(at)).replace(USERINFO, (m) => {
|
|
114
|
+
const last = m.lastIndexOf('@');
|
|
115
|
+
return last < 0 ? m : `[redacted]${m.slice(last)}`;
|
|
116
|
+
});
|
|
117
|
+
for (const [re, to] of PATTERNS) out = re ? out.replace(re, to) : to(out);
|
|
118
|
+
return jwt(out);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// KEY: followed by YAML list items: each item's value goes (PHP uses \G for the run of items).
|
|
122
|
+
function yamlList(text) {
|
|
123
|
+
let out = '';
|
|
124
|
+
let at = 0;
|
|
125
|
+
YAML_KEY.lastIndex = 0;
|
|
126
|
+
for (let m; (m = YAML_KEY.exec(text));) {
|
|
127
|
+
let p = m.index + m[0].length;
|
|
128
|
+
let items = 0;
|
|
129
|
+
for (let item; (item = listItem(text, p)); items++) {
|
|
130
|
+
out += text.slice(at, item[0]) + '[redacted]';
|
|
131
|
+
[, at, p] = item;
|
|
132
|
+
}
|
|
133
|
+
YAML_KEY.lastIndex = items ? p : m.index + 1;
|
|
134
|
+
}
|
|
135
|
+
return out + text.slice(at);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// (?:H*+\r?\n)*+H*+-H++[^\r\n]++(\r?\n)? from p: the value's start and end, and the item's end.
|
|
139
|
+
function listItem(text, p) {
|
|
140
|
+
const eol = (i) => (text[i] === '\n' ? 1 : text[i] === '\r' && text[i + 1] === '\n' ? 2 : 0);
|
|
141
|
+
const skipH = (i) => {
|
|
142
|
+
while (i < text.length && H_RE.test(text[i])) i++;
|
|
143
|
+
return i;
|
|
144
|
+
};
|
|
145
|
+
let i = skipH(p);
|
|
146
|
+
while (eol(i)) i = skipH(i + eol(i));
|
|
147
|
+
if (text[i] !== '-' || !H_RE.test(text[i + 1] ?? '')) return null;
|
|
148
|
+
const v = skipH(i + 1);
|
|
149
|
+
let e = v;
|
|
150
|
+
while (e < text.length && text[e] !== '\r' && text[e] !== '\n') e++;
|
|
151
|
+
return e > v ? [v, e, e + eol(e)] : null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// eyJ[\w-]{10,}\.[\w-]{10,}\.[\w-]{10,} as a regex backtracks quadratically on "eyJeyJ…".
|
|
155
|
+
function jwt(text) {
|
|
156
|
+
if (!text.includes('eyJ')) return text;
|
|
157
|
+
const runs = [...text.matchAll(/[\w-]+/g)].map((m) => [m.index, m.index + m[0].length]);
|
|
158
|
+
const follows = (a, b) => b[0] === a[1] + 1 && text[a[1]] === '.' && b[1] - b[0] >= 10;
|
|
159
|
+
let out = '';
|
|
160
|
+
let at = 0;
|
|
161
|
+
for (let i = 0; i + 2 < runs.length; i++) {
|
|
162
|
+
const [a, b, c] = runs.slice(i, i + 3);
|
|
163
|
+
const q = a[0] + text.slice(a[0], a[1]).indexOf('eyJ');
|
|
164
|
+
if (q < a[0] || a[1] - q < 13 || !follows(a, b) || !follows(b, c)) continue;
|
|
165
|
+
out += text.slice(at, q) + '[redacted]';
|
|
166
|
+
at = c[1];
|
|
167
|
+
i += 2;
|
|
168
|
+
}
|
|
169
|
+
return out + text.slice(at);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function clean(text, limit) {
|
|
173
|
+
const s = String(text);
|
|
174
|
+
const over = s.length > limit * 4;
|
|
175
|
+
// A cut-off token can be a secret prefix too short for its pattern, so it goes.
|
|
176
|
+
const out = scrub(over ? s.slice(0, limit * 4).replace(/(?:^|\s)\S*$/, '') : s);
|
|
177
|
+
if (!over && out.length <= limit) return out;
|
|
178
|
+
const room = limit - SUFFIX.length;
|
|
179
|
+
return room > 0 ? cut(out, room) + SUFFIX : cut(out, limit);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function cut(text, n) {
|
|
183
|
+
const code = text.charCodeAt(n - 1);
|
|
184
|
+
return text.slice(0, code >= 0xd800 && code <= 0xdbff ? n - 1 : n);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Argv lists and call arguments ("curl", "-b", "x"), across newlines: PHP's
|
|
188
|
+
// LITERAL(?:\s*+,\s*+[\[(]?+\s*+LITERAL)++, scanned once, since a JS regex rescans unclosed literals per quote.
|
|
189
|
+
function listPass(text) {
|
|
190
|
+
const n = text.length;
|
|
191
|
+
// close[q][p]: the closing quote reached from content position p, or -1.
|
|
192
|
+
const close = { '"': new Int32Array(n + 2).fill(-1), "'": new Int32Array(n + 2).fill(-1) };
|
|
193
|
+
for (let p = n - 1; p >= 0; p--) {
|
|
194
|
+
const c = text[p];
|
|
195
|
+
if (c === '\n') continue;
|
|
196
|
+
close['"'][p] = c === '"' ? p : c !== '\\' ? close['"'][p + 1] : p + 1 < n && text[p + 1] !== '\n' ? close['"'][p + 2] : -1;
|
|
197
|
+
close["'"][p] = c === "'" ? p : close["'"][p + 1];
|
|
198
|
+
}
|
|
199
|
+
const literal = (i) => (text[i] === '"' || text[i] === "'" ? close[text[i]][i + 1] : -1);
|
|
200
|
+
const ws = (j) => {
|
|
201
|
+
while (j < n && ' \t\n\v\f\r'.includes(text[j])) j++;
|
|
202
|
+
return j;
|
|
203
|
+
};
|
|
204
|
+
const hits = [];
|
|
205
|
+
for (let i = 0; i < n; i++) {
|
|
206
|
+
let end = literal(i);
|
|
207
|
+
if (end < 0) continue;
|
|
208
|
+
const tokens = [[i + 1, end, text.slice(i + 1, end), false]];
|
|
209
|
+
for (;;) {
|
|
210
|
+
let j = ws(end + 1);
|
|
211
|
+
if (text[j] !== ',') break;
|
|
212
|
+
j = ws(j + 1);
|
|
213
|
+
if (text[j] === '[' || text[j] === '(') j++;
|
|
214
|
+
j = ws(j);
|
|
215
|
+
const e = literal(j);
|
|
216
|
+
if (e < 0) break;
|
|
217
|
+
tokens.push([j + 1, e, text.slice(j + 1, e), false]);
|
|
218
|
+
end = e;
|
|
219
|
+
}
|
|
220
|
+
if (tokens.length < 2) continue;
|
|
221
|
+
hits.push(...redactions(text, tokens, flagsFor(text, tokens, {}), 0));
|
|
222
|
+
i = end;
|
|
223
|
+
}
|
|
224
|
+
return hits;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function flagPass(text, depth) {
|
|
228
|
+
const found = [];
|
|
229
|
+
let carry = {};
|
|
230
|
+
let before = [];
|
|
231
|
+
for (const [inherits, tokens] of segments(text)) {
|
|
232
|
+
// A miscounted bracket over-redacts, never under-redacts.
|
|
233
|
+
carry = flagsFor(text, tokens, inherits ? carry : {});
|
|
234
|
+
found.push(...redactions(text, tokens, carry, depth));
|
|
235
|
+
if (before.length > 1 && awaitsStdin(text, tokens, carry)) found.push([before[1][0], before.at(-1)[1], '[redacted]']);
|
|
236
|
+
before = tokens;
|
|
237
|
+
}
|
|
238
|
+
return found;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const ESCAPED_SPACE = /(?:[tnr]|u00(?:20|09|0[aAdD]))/y;
|
|
242
|
+
|
|
243
|
+
function segments(text) {
|
|
244
|
+
const result = [];
|
|
245
|
+
let tokens = [];
|
|
246
|
+
let start = null;
|
|
247
|
+
let value = '';
|
|
248
|
+
let quote = '';
|
|
249
|
+
let opaque = false;
|
|
250
|
+
let depth = 0;
|
|
251
|
+
let inherits = false;
|
|
252
|
+
const n = text.length;
|
|
253
|
+
|
|
254
|
+
for (let i = 0; i < n; i++) {
|
|
255
|
+
const c = text[i];
|
|
256
|
+
const next = text[i + 1] ?? '';
|
|
257
|
+
if (quote !== '' && c !== '\n') {
|
|
258
|
+
const escapes = quote !== "'";
|
|
259
|
+
ESCAPED_SPACE.lastIndex = i + 1;
|
|
260
|
+
if (escapes && c === '\\' && (next === '\n' || (next === '\r' && text[i + 2] === '\n'))) {
|
|
261
|
+
i += next === '\n' ? 1 : 2;
|
|
262
|
+
opaque ||= quote === '$';
|
|
263
|
+
} else if (c === (quote === '$' ? "'" : quote)) quote = '';
|
|
264
|
+
else if (quote === '"' && c === '\\' && ESCAPED_SPACE.test(text)) {
|
|
265
|
+
value += ' ';
|
|
266
|
+
i = ESCAPED_SPACE.lastIndex - 1;
|
|
267
|
+
} else if (escapes && c === '\\' && next !== '') {
|
|
268
|
+
opaque ||= quote === '$' && next !== "'" && next !== '\\';
|
|
269
|
+
value += text[++i];
|
|
270
|
+
} else value += c;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
let space = c === ' ' || c === '\t' || c === '\r';
|
|
274
|
+
if (c === '\\' && (next === '\n' || (next === '\r' && text[i + 2] === '\n'))) {
|
|
275
|
+
i += next === '\n' ? 1 : 2;
|
|
276
|
+
space = true;
|
|
277
|
+
}
|
|
278
|
+
let separator;
|
|
279
|
+
switch (c) {
|
|
280
|
+
case '\n': case '|': case ';': case '`': separator = true; break;
|
|
281
|
+
// A lone & inside a word is a URL query, not a shell operator.
|
|
282
|
+
case '&': separator = start === null || next === '' || '& \t\r\n'.includes(next); break;
|
|
283
|
+
case '$': separator = next === '(' && (++i, ++depth) > 0; break;
|
|
284
|
+
case '(': separator = start === null && ++depth > 0; break;
|
|
285
|
+
case ')': separator = depth > 0 && depth-- > 0; break;
|
|
286
|
+
case '{': case '}': case '!': separator = start === null && (next === '' || ' \t\r\n'.includes(next)); break;
|
|
287
|
+
default: separator = false;
|
|
288
|
+
}
|
|
289
|
+
if (c === '\n') {
|
|
290
|
+
quote = '';
|
|
291
|
+
depth = 0;
|
|
292
|
+
}
|
|
293
|
+
if (separator || space) {
|
|
294
|
+
if (start !== null) {
|
|
295
|
+
tokens.push([start, i - (c === '$' ? 1 : 0), bare(value), opaque]);
|
|
296
|
+
start = null;
|
|
297
|
+
value = '';
|
|
298
|
+
opaque = false;
|
|
299
|
+
}
|
|
300
|
+
if (separator) {
|
|
301
|
+
if (tokens.length) {
|
|
302
|
+
result.push([inherits, tokens]);
|
|
303
|
+
tokens = [];
|
|
304
|
+
}
|
|
305
|
+
inherits = !'\n|;&'.includes(c);
|
|
306
|
+
}
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
start ??= i;
|
|
310
|
+
if (c === '$' && (next === "'" || next === '"')) {
|
|
311
|
+
quote = next === "'" ? '$' : '"';
|
|
312
|
+
i++;
|
|
313
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
314
|
+
else value += c === '\\' && next !== '' ? text[++i] : c;
|
|
315
|
+
}
|
|
316
|
+
if (start !== null) tokens.push([start, n, bare(value), opaque]);
|
|
317
|
+
if (tokens.length) result.push([inherits, tokens]);
|
|
318
|
+
return result;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Quotes the shell keeps ("$'-p…'") and a list's trailing comma do not hide a flag; a spaced token stays for nested().
|
|
322
|
+
const bare = (v) => (SPACED.test(v) ? v : v.replace(/["']/g, '').replace(/^\$+/, '').replace(/[,)\]]+$/, ''));
|
|
323
|
+
|
|
324
|
+
function command(piece) {
|
|
325
|
+
const t = piece.replaceAll('\\', '/').replace(/\/+$/, '');
|
|
326
|
+
const name = t.slice(t.lastIndexOf('/') + 1).toLowerCase();
|
|
327
|
+
return name.endsWith('.exe') ? name.slice(0, -4) : name;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// PHP's $a + $b: $a's keys and order, then $b's missing ones.
|
|
331
|
+
const plus = (a, b) => {
|
|
332
|
+
const out = { ...a };
|
|
333
|
+
for (const [k, v] of Object.entries(b)) if (!Object.hasOwn(out, k)) out[k] = v;
|
|
334
|
+
return out;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
function flagsFor(text, tokens, inherited) {
|
|
338
|
+
const flags = { ...inherited };
|
|
339
|
+
const names = new Set();
|
|
340
|
+
for (const [start, end, value] of tokens) {
|
|
341
|
+
const raw = text.slice(start, end);
|
|
342
|
+
// A spaced token is a nested command, named when it is scanned.
|
|
343
|
+
const pieces = SPACED.test(value) ? [raw.replace(/^[(){}!`$"']+|[(){}!`$"']+$/g, '')] : [value, ...raw.split(/[^\w./\\-]+/)];
|
|
344
|
+
for (const piece of pieces) names.add(command(piece));
|
|
345
|
+
}
|
|
346
|
+
names.delete('');
|
|
347
|
+
if (names.has('login') && (names.has('docker') || names.has('podman') || names.has('nerdctl'))) names.add('docker login');
|
|
348
|
+
for (const name of names) {
|
|
349
|
+
if (!Object.hasOwn(FLAGS, name)) continue;
|
|
350
|
+
for (const [flag, mode] of Object.entries(FLAGS[name])) {
|
|
351
|
+
flags[flag] = mode === 'any' || (Object.hasOwn(flags, flag) ? flags[flag] : 'uid') === 'uid' ? mode : flags[flag];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return flags;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function redactions(text, tokens, inherited, depth) {
|
|
358
|
+
const found = [];
|
|
359
|
+
const own = plus(inherited, FLAGS['']);
|
|
360
|
+
const inner = plus(Object.fromEntries(Object.entries(inherited).filter(([, m]) => m !== 'uid')), FLAGS['']);
|
|
361
|
+
const [uidStart, uidEnd] = containerOptionsEnd(tokens);
|
|
362
|
+
for (let k = 0; k < tokens.length; k++) {
|
|
363
|
+
const flags = k > uidStart && k < uidEnd ? own : inner;
|
|
364
|
+
const [start, end, value, opaque] = tokens[k];
|
|
365
|
+
// $'…' escapes (\x2d, \055, -) can spell a flag or a command, so the rest of the command goes.
|
|
366
|
+
if (opaque) {
|
|
367
|
+
found.push([start, tokens.at(-1)[1], '[redacted]']);
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
const subs = subFlags(value);
|
|
371
|
+
const last = subs.at(-1);
|
|
372
|
+
if (subs.some((f) => secret(f, flags)) || nested(value, depth)) {
|
|
373
|
+
found.push([start, end, '[redacted]']);
|
|
374
|
+
const next = tokens[k + 1];
|
|
375
|
+
if (next && last !== undefined && secret(last, flags) && value.endsWith(last) && !isFlag(next[2], flags)) {
|
|
376
|
+
found.push([next[0], next[1], '[redacted]']);
|
|
377
|
+
k++;
|
|
378
|
+
}
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
// Else the last item of a packed argv list ["curl","-u"] takes the next word.
|
|
382
|
+
let hit = flagHit(text, tokens, k, flags);
|
|
383
|
+
const item = hit.length ? null : value.match(/,(-[^,)\]}]+)[)\]}]*$/);
|
|
384
|
+
if (item) hit = flagHit(text, tokens.with(k, [start, end, item[1], opaque]), k, flags);
|
|
385
|
+
if (hit.length) {
|
|
386
|
+
found.push(hit);
|
|
387
|
+
k += hit[0] >= end ? 1 : 0;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return found;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function flagHit(text, tokens, k, flags) {
|
|
394
|
+
const value = tokens[k][2];
|
|
395
|
+
if (!value.startsWith('-')) return [];
|
|
396
|
+
const nextIsFlag = isFlag(tokens[k + 1]?.[2] ?? '', flags);
|
|
397
|
+
let hit = flagValue(text, tokens, k, flags, nextIsFlag);
|
|
398
|
+
if (!hit?.length && isKeyword(value)) hit = flagValue(text, tokens, k, { [value.split('=')[0]]: 'any' }, nextIsFlag);
|
|
399
|
+
return hit ?? [];
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Uid mode covers only `docker [compose|container] run|exec|create` options: the container command's index and the image's.
|
|
403
|
+
function containerOptionsEnd(tokens) {
|
|
404
|
+
const n = tokens.length;
|
|
405
|
+
const engine = (j) => ['docker', 'podman', 'nerdctl'].includes(command(tokens[j][2])) && (j === 0 || !tokens[j - 1][2].startsWith('-'));
|
|
406
|
+
let j = 0;
|
|
407
|
+
while (j < n && !engine(j)) j++;
|
|
408
|
+
const docker = j;
|
|
409
|
+
let sub = false;
|
|
410
|
+
for (j++; j < n; j++) {
|
|
411
|
+
const value = tokens[j][2];
|
|
412
|
+
if (!value.startsWith('-')) {
|
|
413
|
+
if (sub) return [docker, j];
|
|
414
|
+
sub = ['run', 'exec', 'create'].includes(value);
|
|
415
|
+
if (!sub && !['compose', 'container'].includes(value)) return [0, 0];
|
|
416
|
+
} else if (CONTAINER_VALUED.has(value) || (!value.startsWith('--') && CONTAINER_VALUED.has(`-${value.slice(-1)}`) && /^[A-Za-z]+$/.test(value.slice(1)))) j++;
|
|
417
|
+
}
|
|
418
|
+
return [0, 0];
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// The redaction for the flag at k: null when no flag matches, [] when one matches without a secret value.
|
|
422
|
+
function flagValue(text, tokens, k, flags, nextIsFlag) {
|
|
423
|
+
const [, , value] = tokens[k];
|
|
424
|
+
for (const [flag, mode] of Object.entries(flags)) {
|
|
425
|
+
if (value === flag) {
|
|
426
|
+
const next = tokens[k + 1];
|
|
427
|
+
return mode !== 'attached' && next !== undefined && !nextIsFlag
|
|
428
|
+
&& (!next[2].startsWith('-') || !['userinfo', 'not-url', 'uid'].includes(mode)) && accepts(mode, next[2])
|
|
429
|
+
? [next[0], next[1], mask(text.slice(next[0], next[1]), mode)]
|
|
430
|
+
: [];
|
|
431
|
+
}
|
|
432
|
+
let g = glue(flag);
|
|
433
|
+
if (!glued(value, g) || ((mode === 'uid' || mode === 'userinfo') && !accepts('userinfo', value.slice(g.length)))) continue;
|
|
434
|
+
if (g === flag && value[2] === '=' && value.length > 3) g += '=';
|
|
435
|
+
return gluedValue(text, tokens[k], g, mode);
|
|
436
|
+
}
|
|
437
|
+
const c = cluster(value, flags);
|
|
438
|
+
if (c === null) return null;
|
|
439
|
+
const [g, mode] = c;
|
|
440
|
+
return g === value ? flagValue(text, tokens, k, { [value]: mode }, nextIsFlag) : gluedValue(text, tokens[k], g, mode);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function gluedValue(text, [start, end, value], g, mode) {
|
|
444
|
+
if (!accepts(mode, value.slice(g.length))) return [];
|
|
445
|
+
const raw = text.slice(start, end);
|
|
446
|
+
const quote = (raw[0] === '"' || raw[0] === "'") && raw.slice(1, 1 + g.length) === g ? raw[0] : '';
|
|
447
|
+
if (quote === '' && !raw.startsWith(g)) return [start, end, `${g}[redacted]`];
|
|
448
|
+
const at = quote.length + g.length;
|
|
449
|
+
return [start + at, end, mask(quote + raw.slice(at), mode).slice(quote.length)];
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Clustered short flags (-sSfu, -vp): the cluster up to a command's valued flag, and its mode.
|
|
453
|
+
// The every-command defaults stay out, so rsync -avu is not curl -u.
|
|
454
|
+
function cluster(value, flags) {
|
|
455
|
+
const n = value.startsWith('-') ? value.length : 0;
|
|
456
|
+
let uid = null;
|
|
457
|
+
for (let i = 1; i < n && /[A-Za-z0-9]/.test(value[i]); i++) {
|
|
458
|
+
const flag = `-${value[i]}`;
|
|
459
|
+
const mode = Object.hasOwn(flags, flag) ? flags[flag] : null;
|
|
460
|
+
// docker run img zip -ruP x: a later valued flag wins.
|
|
461
|
+
if (mode === 'uid' && !accepts('userinfo', value.slice(i + 1))) uid ??= [value.slice(0, i + 1), mode];
|
|
462
|
+
else if (mode !== null && mode !== (FLAGS[''][flag] ?? null)) return [value.slice(0, i + 1), i > 1 && mode === 'not-url' ? 'userinfo' : mode];
|
|
463
|
+
}
|
|
464
|
+
return uid;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function isFlag(value, flags) {
|
|
468
|
+
if (value.startsWith('--') && isKeyword(value)) return true;
|
|
469
|
+
for (const [flag, mode] of Object.entries(flags)) {
|
|
470
|
+
const g = glue(flag);
|
|
471
|
+
if (value === flag || (glued(value, g) && accepts(mode, value.slice(g.length)))) return true;
|
|
472
|
+
}
|
|
473
|
+
const c = cluster(value, flags);
|
|
474
|
+
if (c !== null && (c[0] === value || accepts(c[1], value.slice(c[0].length)))) return true;
|
|
475
|
+
return subFlags(value).some((f) => secret(f, flags));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const secret = (flag, flags) => isFlag(flag, flags) || isKeyword(flag);
|
|
479
|
+
const isKeyword = (flag) => KEYS_RE.test(flag.split('=')[0]);
|
|
480
|
+
const subFlags = (value) => (SPACED.test(value) ? [...value.matchAll(SUB_FLAG)].map((m) => m[1]) : []);
|
|
481
|
+
|
|
482
|
+
// A token holding a whole command (sh -c "…") is scanned as one; past MAX_NEST it fails closed.
|
|
483
|
+
function nested(value, depth) {
|
|
484
|
+
if (!SPACED.test(value) && !value.includes(',')) return false;
|
|
485
|
+
return depth >= MAX_NEST || flagPass(value.replace(WIDE_OR_COMMA, ' '), depth + 1).length > 0;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// `… | xargs cmd --password`: the value arrives from the segment before.
|
|
489
|
+
function awaitsStdin(text, tokens, flags) {
|
|
490
|
+
const last = tokens.at(-1)[2];
|
|
491
|
+
const all = plus(flags, FLAGS['']);
|
|
492
|
+
return /(?:^|[ \t\n\v\f\r/])xargs(?:[ \t\n\v\f\r]|$)/.test(text.slice(tokens[0][0], tokens.at(-1)[1]))
|
|
493
|
+
&& ((Object.hasOwn(all, last) ? all[last] : 'attached') !== 'attached' || (last.startsWith('-') && isKeyword(last)));
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const glue = (flag) => (flag.startsWith('--') ? `${flag}=` : flag.length === 2 ? flag : null);
|
|
497
|
+
const glued = (value, g) => g !== null && value.length > g.length && value.startsWith(g);
|
|
498
|
+
|
|
499
|
+
function accepts(mode, value) {
|
|
500
|
+
switch (mode) {
|
|
501
|
+
case 'not-url': return !value.includes('://');
|
|
502
|
+
case 'userinfo': return value.includes(':') && !value.includes('://');
|
|
503
|
+
case 'uid': return accepts('userinfo', value) && !/^\d+:\d+$/.test(value);
|
|
504
|
+
case 'openssl': return value.startsWith('pass:');
|
|
505
|
+
default: return true;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function mask(raw, mode) {
|
|
510
|
+
const tail = raw.match(/(?<=["'])[,)\]]+$/)?.[0] ?? '';
|
|
511
|
+
const body = raw.slice(0, raw.length - tail.length);
|
|
512
|
+
const quote = body[0] === '"' || body[0] === "'" ? body[0] : '';
|
|
513
|
+
const close = quote !== '' && body.length > 1 && body.endsWith(quote) ? quote : '';
|
|
514
|
+
return `${quote}${mode === 'openssl' ? 'pass:' : ''}[redacted]${close}${tail}`;
|
|
515
|
+
}
|