@misconfig/cli 1.1.0 → 1.3.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/CHANGELOG.md +153 -0
- package/LICENSE +1 -0
- package/README.md +108 -28
- package/bin/misconfig.js +28 -1
- package/engine/catalog.js +498 -99
- package/engine/decode.js +41 -0
- package/engine/explain.js +793 -79
- package/engine/flags.js +124 -0
- package/engine/glob.js +178 -0
- package/engine/index.js +2549 -317
- package/engine/known.js +324 -0
- package/engine/locations.js +96 -47
- package/engine/mask.js +74 -21
- package/engine/named.js +92 -0
- package/engine/parse.js +442 -42
- package/engine/paths.js +80 -16
- package/lib/discover.js +635 -97
- package/lib/lookup.js +178 -0
- package/lib/main.js +180 -23
- package/lib/print.js +251 -74
- package/lib/private.js +47 -0
- package/package.json +21 -20
package/engine/parse.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// Small tolerant parsers for agent config files. They never throw: a file
|
|
2
|
-
// that can't be parsed returns undefined and
|
|
2
|
+
// that can't be parsed returns undefined, and the report says it couldn't be
|
|
3
|
+
// read. Keys that could change object prototypes are ignored.
|
|
3
4
|
|
|
4
5
|
// JSON with comments and trailing commas (VS Code, OpenCode, Zed settings).
|
|
5
6
|
export function parseJSONC(text) {
|
|
6
|
-
const s = String(text).replace(
|
|
7
|
+
const s = String(text).replace(/^\ufeff/, "");
|
|
7
8
|
let out = "";
|
|
8
9
|
let inString = false;
|
|
9
10
|
for (let i = 0; i < s.length; i++) {
|
|
@@ -31,8 +32,17 @@ export function parseJSONC(text) {
|
|
|
31
32
|
}
|
|
32
33
|
i++;
|
|
33
34
|
} else if (c === ",") {
|
|
35
|
+
// A trailing comma may be followed by comments before the closing bracket.
|
|
34
36
|
let j = i + 1;
|
|
35
|
-
|
|
37
|
+
for (;;) {
|
|
38
|
+
while (j < s.length && /\s/.test(s[j])) j++;
|
|
39
|
+
if (s[j] === "/" && s[j + 1] === "/") {
|
|
40
|
+
while (j < s.length && s[j] !== "\n") j++;
|
|
41
|
+
} else if (s[j] === "/" && s[j + 1] === "*") {
|
|
42
|
+
const end = s.indexOf("*/", j + 2);
|
|
43
|
+
j = end < 0 ? s.length : end + 2;
|
|
44
|
+
} else break;
|
|
45
|
+
}
|
|
36
46
|
if (s[j] !== "}" && s[j] !== "]") out += c;
|
|
37
47
|
} else out += c;
|
|
38
48
|
}
|
|
@@ -48,14 +58,24 @@ export function parseJSONC(text) {
|
|
|
48
58
|
export function parseTOML(text) {
|
|
49
59
|
const root = {};
|
|
50
60
|
let table = root;
|
|
51
|
-
const lines = String(text).replace(
|
|
61
|
+
const lines = String(text).replace(/^\ufeff/, "").split(/\r?\n/);
|
|
52
62
|
try {
|
|
53
63
|
for (let i = 0; i < lines.length; i++) {
|
|
54
64
|
const line = stripComment(lines[i]).trim();
|
|
55
65
|
if (!line) continue;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
// Table headers, [name] and [[name]], read without a regular
|
|
67
|
+
// expression so a long header can't slow parsing down.
|
|
68
|
+
if (line.startsWith("[") && line.endsWith("]")) {
|
|
69
|
+
const array = line.startsWith("[[") && line.endsWith("]]");
|
|
70
|
+
const keys = splitKey((array ? line.slice(2, -2) : line.slice(1, -1)).trim());
|
|
71
|
+
if (keys.some((k) => UNSAFE_KEYS.has(k))) {
|
|
72
|
+
table = {};
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (!array) {
|
|
76
|
+
table = ensure(root, keys);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
59
79
|
const parent = ensure(root, keys.slice(0, -1));
|
|
60
80
|
const last = keys[keys.length - 1];
|
|
61
81
|
if (!Array.isArray(parent[last])) parent[last] = [];
|
|
@@ -63,21 +83,21 @@ export function parseTOML(text) {
|
|
|
63
83
|
parent[last].push(table);
|
|
64
84
|
continue;
|
|
65
85
|
}
|
|
66
|
-
m = line.match(/^\[\s*(.+?)\s*\]$/);
|
|
67
|
-
if (m) {
|
|
68
|
-
table = ensure(root, splitKey(m[1]));
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
86
|
const eq = findTopLevel(line, "=");
|
|
72
87
|
if (eq < 0) continue;
|
|
73
88
|
const keys = splitKey(line.slice(0, eq).trim());
|
|
74
89
|
let raw = line.slice(eq + 1).trim();
|
|
75
|
-
|
|
90
|
+
const state = scan(raw, { depth: 0, quote: null, triple: null });
|
|
91
|
+
for (let added = 0; (state.depth > 0 || state.triple) && i + 1 < lines.length && added < 5000; added++) {
|
|
76
92
|
i++;
|
|
77
|
-
|
|
93
|
+
const next = stripComment(lines[i]);
|
|
94
|
+
raw += "\n" + next;
|
|
95
|
+
scan("\n" + next, state);
|
|
78
96
|
}
|
|
97
|
+
const last = keys[keys.length - 1];
|
|
98
|
+
if (UNSAFE_KEYS.has(last) || keys.some((k) => UNSAFE_KEYS.has(k))) continue;
|
|
79
99
|
const parent = ensure(table, keys.slice(0, -1));
|
|
80
|
-
parent[
|
|
100
|
+
parent[last] = parseValue(raw.trim());
|
|
81
101
|
}
|
|
82
102
|
return root;
|
|
83
103
|
} catch {
|
|
@@ -85,9 +105,12 @@ export function parseTOML(text) {
|
|
|
85
105
|
}
|
|
86
106
|
}
|
|
87
107
|
|
|
108
|
+
const UNSAFE_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
109
|
+
|
|
88
110
|
function ensure(obj, keys) {
|
|
89
111
|
let cur = obj;
|
|
90
112
|
for (const k of keys) {
|
|
113
|
+
if (UNSAFE_KEYS.has(k)) return {};
|
|
91
114
|
if (cur[k] === undefined || typeof cur[k] !== "object" || cur[k] === null) cur[k] = {};
|
|
92
115
|
cur = Array.isArray(cur[k]) ? cur[k][cur[k].length - 1] ?? (cur[k][0] = {}) : cur[k];
|
|
93
116
|
}
|
|
@@ -106,13 +129,14 @@ function splitKey(raw) {
|
|
|
106
129
|
let key = "";
|
|
107
130
|
while (j < s.length && s[j] !== q) {
|
|
108
131
|
if (q === '"' && s[j] === "\\") {
|
|
109
|
-
|
|
110
|
-
|
|
132
|
+
const length = s[j + 1] === "u" ? 6 : s[j + 1] === "U" ? 10 : 2;
|
|
133
|
+
key += s.slice(j, j + length);
|
|
134
|
+
j += length;
|
|
111
135
|
continue;
|
|
112
136
|
}
|
|
113
137
|
key += s[j++];
|
|
114
138
|
}
|
|
115
|
-
keys.push(key);
|
|
139
|
+
keys.push(q === '"' ? tomlBasic(key) : key);
|
|
116
140
|
i = j + 1;
|
|
117
141
|
} else {
|
|
118
142
|
let j = i;
|
|
@@ -152,21 +176,26 @@ function findTopLevel(s, ch) {
|
|
|
152
176
|
return -1;
|
|
153
177
|
}
|
|
154
178
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
else if (
|
|
179
|
+
// Updates bracket depth and string state for one more chunk of a value.
|
|
180
|
+
function scan(chunk, state) {
|
|
181
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
182
|
+
const c = chunk[i];
|
|
183
|
+
if (state.triple) {
|
|
184
|
+
if (chunk.startsWith(state.triple, i)) {
|
|
185
|
+
i += 2;
|
|
186
|
+
state.triple = null;
|
|
187
|
+
}
|
|
188
|
+
} else if (state.quote) {
|
|
189
|
+
if (c === "\\" && state.quote === '"') i++;
|
|
190
|
+
else if (c === state.quote || c === "\n") state.quote = null;
|
|
191
|
+
} else if (chunk.startsWith('"""', i) || chunk.startsWith("'''", i)) {
|
|
192
|
+
state.triple = chunk.slice(i, i + 3);
|
|
193
|
+
i += 2;
|
|
194
|
+
} else if (c === '"' || c === "'") state.quote = c;
|
|
195
|
+
else if (c === "[" || c === "{") state.depth++;
|
|
196
|
+
else if (c === "]" || c === "}") state.depth--;
|
|
168
197
|
}
|
|
169
|
-
return
|
|
198
|
+
return state;
|
|
170
199
|
}
|
|
171
200
|
|
|
172
201
|
function splitTopLevel(inner) {
|
|
@@ -191,22 +220,61 @@ function splitTopLevel(inner) {
|
|
|
191
220
|
return parts.map((p) => p.trim()).filter(Boolean);
|
|
192
221
|
}
|
|
193
222
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
223
|
+
// Arrays and tables nested deeper than this are left as text.
|
|
224
|
+
const MAX_DEPTH = 32;
|
|
225
|
+
|
|
226
|
+
// The text of a basic string, escapes and all: \\n, \\uXXXX and \\UXXXXXXXX,
|
|
227
|
+
// and in a multi-line string a backslash at the end of a line, which joins
|
|
228
|
+
// it to the next one.
|
|
229
|
+
function tomlBasic(text) {
|
|
230
|
+
let out = "";
|
|
231
|
+
for (let i = 0; i < text.length; i++) {
|
|
232
|
+
const c = text[i];
|
|
233
|
+
if (c !== "\\") {
|
|
234
|
+
out += c;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const n = text[i + 1];
|
|
238
|
+
if (n === undefined) break;
|
|
239
|
+
const simple = { b: "\b", t: "\t", n: "\n", f: "\f", r: "\r", e: "\x1b", '"': '"', "\\": "\\" }[n];
|
|
240
|
+
if (simple !== undefined) {
|
|
241
|
+
out += simple;
|
|
242
|
+
i++;
|
|
243
|
+
} else if (n === "u" || n === "U") {
|
|
244
|
+
const hex = text.slice(i + 2, i + (n === "u" ? 6 : 10));
|
|
245
|
+
const code = hex.length === (n === "u" ? 4 : 8) && /^[0-9A-Fa-f]+$/.test(hex) ? Number.parseInt(hex, 16) : NaN;
|
|
246
|
+
out += code <= 0x10ffff ? String.fromCodePoint(code) : "";
|
|
247
|
+
i += 1 + hex.length;
|
|
248
|
+
} else if (/[ \t\r\n]/.test(n)) {
|
|
249
|
+
// The end of a line: skip it and the spaces that start the next.
|
|
250
|
+
let j = i + 1;
|
|
251
|
+
while (j < text.length && /[ \t]/.test(text[j])) j++;
|
|
252
|
+
if (text[j] === "\r") j++;
|
|
253
|
+
if (text[j] !== "\n") {
|
|
254
|
+
out += c;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
while (j < text.length && /\s/.test(text[j])) j++;
|
|
258
|
+
i = j - 1;
|
|
259
|
+
} else {
|
|
260
|
+
out += n;
|
|
261
|
+
i++;
|
|
202
262
|
}
|
|
203
263
|
}
|
|
264
|
+
return out;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function parseValue(raw, depth = 0) {
|
|
268
|
+
if (depth > MAX_DEPTH) return raw;
|
|
269
|
+
if (raw.startsWith('"""')) return tomlBasic(raw.slice(3, raw.lastIndexOf('"""')).replace(/^\r?\n/, ""));
|
|
270
|
+
if (raw.startsWith("'''")) return raw.slice(3, raw.lastIndexOf("'''")).replace(/^\r?\n/, "");
|
|
271
|
+
if (raw.startsWith('"')) return tomlBasic(raw.slice(1, raw.lastIndexOf('"')));
|
|
204
272
|
if (raw.startsWith("'")) return raw.slice(1, raw.lastIndexOf("'"));
|
|
205
273
|
if (raw === "true") return true;
|
|
206
274
|
if (raw === "false") return false;
|
|
207
275
|
if (/^[+-]?\d[\d_]*(\.\d+)?$/.test(raw)) return Number(raw.replace(/_/g, ""));
|
|
208
276
|
if (raw.startsWith("[")) {
|
|
209
|
-
return splitTopLevel(raw.slice(1, raw.lastIndexOf("]")).replace(/\n/g, " ")).map(parseValue);
|
|
277
|
+
return splitTopLevel(raw.slice(1, raw.lastIndexOf("]")).replace(/\n/g, " ")).map((part) => parseValue(part, depth + 1));
|
|
210
278
|
}
|
|
211
279
|
if (raw.startsWith("{")) {
|
|
212
280
|
const obj = {};
|
|
@@ -214,9 +282,341 @@ function parseValue(raw) {
|
|
|
214
282
|
const eq = findTopLevel(part, "=");
|
|
215
283
|
if (eq < 0) continue;
|
|
216
284
|
const keys = splitKey(part.slice(0, eq));
|
|
217
|
-
|
|
285
|
+
if (keys.some((k) => UNSAFE_KEYS.has(k))) continue;
|
|
286
|
+
ensure(obj, keys.slice(0, -1))[keys[keys.length - 1]] = parseValue(part.slice(eq + 1).trim(), depth + 1);
|
|
218
287
|
}
|
|
219
288
|
return obj;
|
|
220
289
|
}
|
|
221
290
|
return raw;
|
|
222
291
|
}
|
|
292
|
+
|
|
293
|
+
// Enough YAML for the agent settings written in it (Continue's config.yaml
|
|
294
|
+
// and Goose's config.yaml): block mappings and lists, plain and quoted
|
|
295
|
+
// scalars, comments, and simple [a, b] and {a: b} values on one line.
|
|
296
|
+
// Anchors, tags and multi-line strings aren't used there and are kept as
|
|
297
|
+
// text. Returns undefined when the top isn't a mapping or a list.
|
|
298
|
+
export function parseYAML(text) {
|
|
299
|
+
const items = [];
|
|
300
|
+
for (const raw of String(text).replace(/^\ufeff/, "").split(/\r?\n/).slice(0, 20_000)) {
|
|
301
|
+
if (/^\s*(?:#|$)/.test(raw) || /^(?:---|\.\.\.)(?:\s|$)/.test(raw)) continue;
|
|
302
|
+
// Tabs count as indentation here, as a lenient reader would take them.
|
|
303
|
+
const indent = /^[ \t]*/.exec(raw)[0].length;
|
|
304
|
+
items.push({ indent, text: yamlStripComment(raw.slice(indent)).trimEnd() });
|
|
305
|
+
}
|
|
306
|
+
// A document written as one flow mapping or list, such as {a: 1, b: 2}.
|
|
307
|
+
const whole = items.map((x) => x.text).join(" ").trim();
|
|
308
|
+
if (/^\{[\s\S]*\}$|^\[[\s\S]*\]$/.test(whole) && whole.length <= 200_000) {
|
|
309
|
+
const flow = yamlScalar(whole);
|
|
310
|
+
return flow && typeof flow === "object" ? flow : undefined;
|
|
311
|
+
}
|
|
312
|
+
const anchors = new Map();
|
|
313
|
+
// Aliases share what they point to, so a chain of them can describe a
|
|
314
|
+
// huge document in a few lines. Past this many nodes, aliases read as null.
|
|
315
|
+
let aliasBudget = 20_000;
|
|
316
|
+
const sizeOf = (v, cap = 20_001) => {
|
|
317
|
+
let n = 0;
|
|
318
|
+
const stack = [v];
|
|
319
|
+
while (stack.length && n < cap) {
|
|
320
|
+
const x = stack.pop();
|
|
321
|
+
n++;
|
|
322
|
+
if (x && typeof x === "object") stack.push(...(Array.isArray(x) ? x : Object.values(x)).slice(0, cap));
|
|
323
|
+
}
|
|
324
|
+
return n;
|
|
325
|
+
};
|
|
326
|
+
let i = 0;
|
|
327
|
+
const node = (indent, depth) => {
|
|
328
|
+
if (i >= items.length || items[i].indent < indent || depth > 20) return null;
|
|
329
|
+
return /^-(?:\s|$)/.test(items[i].text) ? list(items[i].indent, depth) : map(items[i].indent, depth);
|
|
330
|
+
};
|
|
331
|
+
// A value after "key:" or "- ": tags are dropped, &name records it and
|
|
332
|
+
// *name uses what was recorded, and | or > take the lines indented below.
|
|
333
|
+
const value = (rest, indent, depth) => {
|
|
334
|
+
let text = rest.replace(/^!{1,2}[^\s]*\s*/, "");
|
|
335
|
+
const anchor = /^&([^\s]{1,100})\s*/.exec(text);
|
|
336
|
+
if (anchor) text = text.slice(anchor[0].length).replace(/^!{1,2}[^\s]*\s*/, "");
|
|
337
|
+
let out;
|
|
338
|
+
const alias = /^\*([^\s]{1,100})$/.exec(text);
|
|
339
|
+
if (alias) {
|
|
340
|
+
out = anchors.has(alias[1]) ? anchors.get(alias[1]) : null;
|
|
341
|
+
if (out && typeof out === "object" && (aliasBudget -= sizeOf(out)) < 0) out = null;
|
|
342
|
+
}
|
|
343
|
+
else if (/^[|>][+-]?\d?$/.test(text)) {
|
|
344
|
+
const lines = [];
|
|
345
|
+
while (i < items.length && items[i].indent > indent && lines.length < 10_000) lines.push(items[i++].text);
|
|
346
|
+
out = lines.join(text.startsWith("|") ? "\n" : " ");
|
|
347
|
+
} else if (text) out = yamlScalar(text, depth);
|
|
348
|
+
else if (i < items.length && (items[i].indent > indent || (items[i].indent === indent && /^-(?:\s|$)/.test(items[i].text)))) out = node(items[i].indent, depth + 1);
|
|
349
|
+
else out = null;
|
|
350
|
+
if (anchor && anchors.size < 1000) anchors.set(anchor[1], out);
|
|
351
|
+
return out;
|
|
352
|
+
};
|
|
353
|
+
const list = (indent, depth) => {
|
|
354
|
+
const out = [];
|
|
355
|
+
while (i < items.length && items[i].indent === indent && /^-(?:\s|$)/.test(items[i].text) && out.length < 10_000) {
|
|
356
|
+
const rest = items[i].text.slice(1).trimStart();
|
|
357
|
+
if (rest && yamlKey(rest)) {
|
|
358
|
+
// "- name: x" starts a mapping whose keys line up after the dash.
|
|
359
|
+
items[i] = { indent: indent + (items[i].text.length - rest.length), text: rest };
|
|
360
|
+
out.push(map(items[i].indent, depth + 1));
|
|
361
|
+
} else {
|
|
362
|
+
i++;
|
|
363
|
+
out.push(value(rest, indent, depth));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return out;
|
|
367
|
+
};
|
|
368
|
+
const map = (indent, depth) => {
|
|
369
|
+
const out = {};
|
|
370
|
+
while (i < items.length && items[i].indent === indent && !/^-(?:\s|$)/.test(items[i].text)) {
|
|
371
|
+
const kv = yamlKey(items[i].text);
|
|
372
|
+
i++;
|
|
373
|
+
if (!kv) continue;
|
|
374
|
+
const [key, rest] = kv;
|
|
375
|
+
const v = value(rest, indent, depth);
|
|
376
|
+
if (!UNSAFE_KEYS.has(key)) out[key] = v;
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
};
|
|
380
|
+
const top = node(0, 0);
|
|
381
|
+
return top && typeof top === "object" ? top : undefined;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// The text before a comment: "#" at the start or after a space, outside quotes.
|
|
385
|
+
function yamlStripComment(line) {
|
|
386
|
+
let quote = null;
|
|
387
|
+
for (let j = 0; j < line.length; j++) {
|
|
388
|
+
const c = line[j];
|
|
389
|
+
if (quote) {
|
|
390
|
+
if (c === quote) quote = null;
|
|
391
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
392
|
+
else if (c === "#" && (j === 0 || /\s/.test(line[j - 1]))) return line.slice(0, j);
|
|
393
|
+
}
|
|
394
|
+
return line;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// "key: value" or "key:", with a plain or quoted key, as [key, value].
|
|
398
|
+
function yamlKey(text) {
|
|
399
|
+
const m = /^(?:"((?:[^"\\]|\\.){0,200})"|'((?:[^']|''){0,200})'|([^\s"'[\]{}#,:][^:#]{0,200}?))\s*:(?:\s+(.*))?$/.exec(text);
|
|
400
|
+
if (!m) return null;
|
|
401
|
+
const key = m[1] ?? (m[2] !== undefined ? m[2].replace(/''/g, "'") : m[3].trim());
|
|
402
|
+
return [key, (m[4] ?? "").trim()];
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function yamlScalar(text, depth = 0) {
|
|
406
|
+
const t = text.trim();
|
|
407
|
+
if (t.startsWith('"') && t.endsWith('"') && t.length >= 2) return t.slice(1, -1).replace(/\\(["\\/])/g, "$1").replace(/\\n/g, "\n").replace(/\\t/g, "\t");
|
|
408
|
+
if (t.startsWith("'") && t.endsWith("'") && t.length >= 2) return t.slice(1, -1).replace(/''/g, "'");
|
|
409
|
+
if (depth < 10 && t.startsWith("[") && t.endsWith("]")) return yamlFlowItems(t.slice(1, -1)).map((x) => yamlScalar(x, depth + 1));
|
|
410
|
+
if (depth < 10 && t.startsWith("{") && t.endsWith("}")) {
|
|
411
|
+
const out = {};
|
|
412
|
+
for (const part of yamlFlowItems(t.slice(1, -1))) {
|
|
413
|
+
const kv = yamlKey(part);
|
|
414
|
+
if (kv && !UNSAFE_KEYS.has(kv[0])) out[kv[0]] = yamlScalar(kv[1], depth + 1);
|
|
415
|
+
}
|
|
416
|
+
return out;
|
|
417
|
+
}
|
|
418
|
+
if (/^(?:true|yes|on)$/i.test(t)) return true;
|
|
419
|
+
if (/^(?:false|no|off)$/i.test(t)) return false;
|
|
420
|
+
if (/^(?:null|~)$/i.test(t) || t === "") return null;
|
|
421
|
+
return t;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// The items of a one-line [..] or {..}, split at commas outside quotes and brackets.
|
|
425
|
+
function yamlFlowItems(inner) {
|
|
426
|
+
const parts = [];
|
|
427
|
+
let depth = 0;
|
|
428
|
+
let quote = null;
|
|
429
|
+
let start = 0;
|
|
430
|
+
for (let j = 0; j < inner.length; j++) {
|
|
431
|
+
const c = inner[j];
|
|
432
|
+
if (quote) {
|
|
433
|
+
if (c === quote) quote = null;
|
|
434
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
435
|
+
else if (c === "[" || c === "{") depth++;
|
|
436
|
+
else if (c === "]" || c === "}") depth--;
|
|
437
|
+
else if (c === "," && depth === 0) {
|
|
438
|
+
parts.push(inner.slice(start, j));
|
|
439
|
+
start = j + 1;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
parts.push(inner.slice(start));
|
|
443
|
+
return parts.map((p) => p.trim()).filter(Boolean).slice(0, 1000);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// --- Codex rules files -------------------------------------------------------
|
|
447
|
+
//
|
|
448
|
+
// A Codex .rules file is Starlark. Only its prefix_rule(...) calls matter
|
|
449
|
+
// here: each one's pattern, a list whose items are a word or a list of
|
|
450
|
+
// alternative words, and its decision, which is "allow" when a rule doesn't
|
|
451
|
+
// give one. Comments and the text inside strings are skipped, so a rule
|
|
452
|
+
// can't hide behind a parenthesis in its justification, and a name set to a
|
|
453
|
+
// string or list at the top of the file is looked up where a rule uses it.
|
|
454
|
+
// Rules made by functions or loops aren't followed.
|
|
455
|
+
|
|
456
|
+
const RULES_MAX = 200_000;
|
|
457
|
+
|
|
458
|
+
function starSpace(src, i) {
|
|
459
|
+
while (i < src.length) {
|
|
460
|
+
if (/\s/.test(src[i])) i++;
|
|
461
|
+
else if (src[i] === "#") {
|
|
462
|
+
const nl = src.indexOf("\n", i);
|
|
463
|
+
i = nl < 0 ? src.length : nl + 1;
|
|
464
|
+
} else break;
|
|
465
|
+
}
|
|
466
|
+
return i;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// A string literal at i, as [text, end], or null. Handles '', "", triple
|
|
470
|
+
// quotes and one r or b prefix.
|
|
471
|
+
function starString(src, i) {
|
|
472
|
+
let j = i;
|
|
473
|
+
let raw = false;
|
|
474
|
+
if (/[rRbB]/.test(src[j]) && /["']/.test(src[j + 1] ?? "")) {
|
|
475
|
+
raw = /[rR]/.test(src[j]);
|
|
476
|
+
j++;
|
|
477
|
+
}
|
|
478
|
+
const q = src[j];
|
|
479
|
+
if (q !== '"' && q !== "'") return null;
|
|
480
|
+
const close = src.startsWith(q.repeat(3), j) ? q.repeat(3) : q;
|
|
481
|
+
let k = j + close.length;
|
|
482
|
+
let text = "";
|
|
483
|
+
while (k < src.length) {
|
|
484
|
+
if (src.startsWith(close, k)) return [text, k + close.length];
|
|
485
|
+
const c = src[k];
|
|
486
|
+
if (close.length === 1 && c === "\n") return [text, k];
|
|
487
|
+
if (c === "\\" && k + 1 < src.length) {
|
|
488
|
+
const n = src[k + 1];
|
|
489
|
+
text += raw ? c + n : n === "n" ? "\n" : n === "t" ? "\t" : n;
|
|
490
|
+
k += 2;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
text += c;
|
|
494
|
+
k++;
|
|
495
|
+
}
|
|
496
|
+
return [text, k];
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// A value at i, as [value, end]: a string, a list, or { name } for anything
|
|
500
|
+
// else, such as a name or a number.
|
|
501
|
+
function starValue(src, i, depth = 0) {
|
|
502
|
+
i = starSpace(src, i);
|
|
503
|
+
const str = starString(src, i);
|
|
504
|
+
if (str) return str;
|
|
505
|
+
const open = src[i];
|
|
506
|
+
if ((open === "[" || open === "(") && depth < 4) {
|
|
507
|
+
const close = open === "[" ? "]" : ")";
|
|
508
|
+
const items = [];
|
|
509
|
+
let j = i + 1;
|
|
510
|
+
// Anything but values and commas, such as "pu" + "sh", is code this
|
|
511
|
+
// reader doesn't run, so the list holds a null where it was.
|
|
512
|
+
let odd = false;
|
|
513
|
+
for (let n = 0; n < 200; n++) {
|
|
514
|
+
j = starSpace(src, j);
|
|
515
|
+
if (j >= src.length) break;
|
|
516
|
+
if (src[j] === close) return [odd ? [...items, null] : items, j + 1];
|
|
517
|
+
const [v, next] = starValue(src, j, depth + 1);
|
|
518
|
+
if (next <= j) {
|
|
519
|
+
odd = true;
|
|
520
|
+
j++;
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
items.push(v);
|
|
524
|
+
j = starSpace(src, next);
|
|
525
|
+
if (src[j] === ",") j++;
|
|
526
|
+
else if (src[j] !== close) odd = true;
|
|
527
|
+
}
|
|
528
|
+
return [odd ? [...items, null] : items, j];
|
|
529
|
+
}
|
|
530
|
+
const m = /^(?:[A-Za-z_][\w.]*|-?\d[\w.]*)/.exec(src.slice(i, i + 200));
|
|
531
|
+
return m ? [{ name: m[0] }, i + m[0].length] : [undefined, i];
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// The arguments of a call whose "(" is just before i, as [{ named,
|
|
535
|
+
// positional }, end].
|
|
536
|
+
function starArgs(src, i) {
|
|
537
|
+
const named = {};
|
|
538
|
+
const positional = [];
|
|
539
|
+
let complex = false;
|
|
540
|
+
let j = i;
|
|
541
|
+
for (let n = 0; n < 50; n++) {
|
|
542
|
+
j = starSpace(src, j);
|
|
543
|
+
if (j >= src.length) break;
|
|
544
|
+
if (src[j] === ")") return [{ named, positional, complex }, j + 1];
|
|
545
|
+
const key = /^([A-Za-z_]\w*)\s*=(?!=)/.exec(src.slice(j, j + 100));
|
|
546
|
+
if (key) j = starSpace(src, j + key[0].length);
|
|
547
|
+
const [v, next] = starValue(src, j);
|
|
548
|
+
if (next <= j) {
|
|
549
|
+
complex = true;
|
|
550
|
+
j++;
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if (key) {
|
|
554
|
+
if (!Object.hasOwn(named, key[1]) && !UNSAFE_KEYS.has(key[1])) named[key[1]] = v;
|
|
555
|
+
} else positional.push(v);
|
|
556
|
+
j = starSpace(src, next);
|
|
557
|
+
if (src[j] === ",") j++;
|
|
558
|
+
else if (src[j] !== ")") complex = true;
|
|
559
|
+
}
|
|
560
|
+
return [{ named, positional, complex: true }, j];
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
export function parseCodexRules(text) {
|
|
564
|
+
const src = String(text ?? "").slice(0, RULES_MAX);
|
|
565
|
+
const names = new Map();
|
|
566
|
+
const calls = [];
|
|
567
|
+
let i = 0;
|
|
568
|
+
while (i < src.length && calls.length < 500) {
|
|
569
|
+
const c = src[i];
|
|
570
|
+
if (c === "#") {
|
|
571
|
+
const nl = src.indexOf("\n", i);
|
|
572
|
+
i = nl < 0 ? src.length : nl + 1;
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
const str = /\w/.test(src[i - 1] ?? "") ? null : starString(src, i);
|
|
576
|
+
if (str) {
|
|
577
|
+
i = Math.max(str[1], i + 1);
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
const word = /\w/.test(src[i - 1] ?? "") || src[i - 1] === "." ? null : /^[A-Za-z_]\w*/.exec(src.slice(i, i + 100))?.[0];
|
|
581
|
+
if (!word) {
|
|
582
|
+
i++;
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
let j = starSpace(src, i + word.length);
|
|
586
|
+
if (word === "prefix_rule" && src[j] === "(") {
|
|
587
|
+
const [args, end] = starArgs(src, j + 1);
|
|
588
|
+
calls.push({ index: i, ...args });
|
|
589
|
+
i = end;
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
// A name set at the start of a line, such as SHELLS = ["bash", "sh"].
|
|
593
|
+
const lineStart = i === 0 || src[i - 1] === "\n";
|
|
594
|
+
if (lineStart && src[j] === "=" && src[j + 1] !== "=") {
|
|
595
|
+
const [v, end] = starValue(src, j + 1);
|
|
596
|
+
if (typeof v === "string" || Array.isArray(v)) names.set(word, v);
|
|
597
|
+
i = Math.max(end, j + 1);
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
i += word.length;
|
|
601
|
+
}
|
|
602
|
+
const resolve = (v, depth = 0) => {
|
|
603
|
+
if (v && typeof v === "object" && !Array.isArray(v)) return depth < 4 && names.has(v.name) ? resolve(names.get(v.name), depth + 1) : undefined;
|
|
604
|
+
if (Array.isArray(v)) return depth < 6 ? v.map((x) => resolve(x, depth + 1)) : [];
|
|
605
|
+
return v;
|
|
606
|
+
};
|
|
607
|
+
// A rule this reader can't follow: made in a function or a loop from a
|
|
608
|
+
// name it doesn't know, or from an expression such as "pu" + "sh".
|
|
609
|
+
return calls.map(({ index, named, positional, complex }) => {
|
|
610
|
+
const pattern = resolve(named.pattern ?? positional[0]);
|
|
611
|
+
const decision = named.decision === undefined ? "allow" : resolve(named.decision);
|
|
612
|
+
const justification = resolve(named.justification);
|
|
613
|
+
const words = Array.isArray(pattern) ? pattern.map((w) => (Array.isArray(w) ? (w.every((x) => typeof x === "string") ? w : null) : typeof w === "string" ? w : null)) : null;
|
|
614
|
+
return {
|
|
615
|
+
index,
|
|
616
|
+
pattern: words,
|
|
617
|
+
decision: typeof decision === "string" ? decision.trim().toLowerCase() : "",
|
|
618
|
+
justification: typeof justification === "string" ? justification : "",
|
|
619
|
+
unread: complex || !words || words.includes(null) || typeof decision !== "string",
|
|
620
|
+
};
|
|
621
|
+
});
|
|
622
|
+
}
|