@jalaxy33/pi-permission 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 +21 -0
- package/README.md +134 -2
- package/docs/configuration.md +237 -0
- package/index.ts +97 -0
- package/package.json +54 -4
- package/rules/index.json +71 -0
- package/rules/packs/bulk-delete.json +83 -0
- package/rules/packs/database.json +30 -0
- package/rules/packs/git-local.json +81 -0
- package/rules/packs/git-remote.json +27 -0
- package/rules/packs/infra.json +45 -0
- package/rules/packs/irreversible.json +93 -0
- package/rules/packs/privilege.json +50 -0
- package/rules/packs/publish.json +33 -0
- package/rules/packs/secrets.json +43 -0
- package/schema.json +348 -0
- package/src/argv.ts +67 -0
- package/src/config.ts +201 -0
- package/src/decide.ts +90 -0
- package/src/gate.ts +208 -0
- package/src/match.ts +58 -0
- package/src/ops.ts +244 -0
- package/src/outside.ts +87 -0
- package/src/paths.ts +194 -0
- package/src/prompt.ts +275 -0
- package/src/rulesets.ts +444 -0
- package/src/tokenize.ts +222 -0
- package/src/types.ts +167 -0
- package/src/validate.ts +125 -0
package/src/rulesets.ts
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/** Rule files: shipped defaults, user rulesets, presets, and composition. */
|
|
2
|
+
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { isAbsolute, resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { expandTilde } from "./paths";
|
|
7
|
+
import { checkFields, isObject } from "./validate";
|
|
8
|
+
import {
|
|
9
|
+
SECTIONS,
|
|
10
|
+
type ActiveRuleSet,
|
|
11
|
+
type ConfigFile,
|
|
12
|
+
type IncludeRef,
|
|
13
|
+
type PathRule,
|
|
14
|
+
type Preset,
|
|
15
|
+
type PresetRef,
|
|
16
|
+
type Rule,
|
|
17
|
+
type RuleSet,
|
|
18
|
+
type Section,
|
|
19
|
+
} from "./types";
|
|
20
|
+
|
|
21
|
+
export function defaultConfigPath(): string {
|
|
22
|
+
return fileURLToPath(new URL("../rules/index.json", import.meta.url));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveRulePath(entry: string, baseDir: string): string {
|
|
26
|
+
const expanded = expandTilde(entry);
|
|
27
|
+
return isAbsolute(expanded) ? expanded : resolve(baseDir, expanded);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function readJson(
|
|
31
|
+
path: string,
|
|
32
|
+
warnings: string[],
|
|
33
|
+
kind: "config" | "ruleset" = "config",
|
|
34
|
+
): ConfigFile | undefined {
|
|
35
|
+
let raw: string;
|
|
36
|
+
try {
|
|
37
|
+
raw = readFileSync(path, "utf8");
|
|
38
|
+
} catch {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
const parsed: unknown = JSON.parse(raw);
|
|
43
|
+
if (!isObject(parsed)) {
|
|
44
|
+
warnings.push(`${path}: expected a JSON object`);
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
checkFields(parsed, path, warnings, kind);
|
|
48
|
+
return parsed as ConfigFile;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
warnings.push(`${path}: invalid JSON (${(error as Error).message})`);
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Fields only a config file may carry; a ruleset is rules and nothing else. */
|
|
56
|
+
const CONFIG_ONLY = [
|
|
57
|
+
"rulesets",
|
|
58
|
+
"presets",
|
|
59
|
+
"preset",
|
|
60
|
+
"include",
|
|
61
|
+
"defaultPermission",
|
|
62
|
+
"outsideCwd",
|
|
63
|
+
"enabled",
|
|
64
|
+
] as const;
|
|
65
|
+
|
|
66
|
+
function forbiddenField(value: Record<string, unknown>): string | undefined {
|
|
67
|
+
return CONFIG_ONLY.find((key) => key in value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A ruleset must be a plain object without composition or policy settings. */
|
|
71
|
+
export function checkRuleSetShape(value: unknown, label: string): string | undefined {
|
|
72
|
+
if (!isObject(value)) return `${label}: expected a ruleset object`;
|
|
73
|
+
const forbidden = forbiddenField(value);
|
|
74
|
+
if (forbidden) return `${label}: a ruleset must not contain "${forbidden}"`;
|
|
75
|
+
if (value.commands !== undefined && !isObject(value.commands)) return `${label}: "commands" must be an object`;
|
|
76
|
+
if (value.paths !== undefined && !Array.isArray(value.paths)) return `${label}: "paths" must be an array`;
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface LoadedRuleSet {
|
|
81
|
+
id: string;
|
|
82
|
+
source?: string;
|
|
83
|
+
ruleset?: RuleSet;
|
|
84
|
+
error?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function loadRuleSetRef(
|
|
88
|
+
id: string,
|
|
89
|
+
ref: RuleSet | string,
|
|
90
|
+
baseDir: string,
|
|
91
|
+
warnings: string[],
|
|
92
|
+
): LoadedRuleSet {
|
|
93
|
+
if (typeof ref === "string") {
|
|
94
|
+
const path = resolveRulePath(ref, baseDir);
|
|
95
|
+
const file = readJson(path, warnings, "ruleset");
|
|
96
|
+
if (!file) return { id, source: path, error: `rule file not found or invalid: ${path}` };
|
|
97
|
+
const shape = checkRuleSetShape({ commands: file.commands, paths: file.paths }, path);
|
|
98
|
+
if (shape) return { id, source: path, error: shape };
|
|
99
|
+
const forbidden = forbiddenField(file as unknown as Record<string, unknown>);
|
|
100
|
+
if (forbidden) return { id, source: path, error: `${path}: a ruleset must not contain "${forbidden}"` };
|
|
101
|
+
if (!file.commands && !file.paths) {
|
|
102
|
+
return { id, source: path, error: `${path}: expected "commands" or "paths"` };
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
id,
|
|
106
|
+
source: path,
|
|
107
|
+
ruleset: { description: file.description, commands: file.commands, paths: file.paths },
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const shapeError = checkRuleSetShape(ref, `ruleset "${id}"`);
|
|
112
|
+
if (shapeError) return { id, error: shapeError };
|
|
113
|
+
return { id, ruleset: ref };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface LoadedPreset {
|
|
117
|
+
id: string;
|
|
118
|
+
preset: Preset;
|
|
119
|
+
source: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function loadPresets(file: ConfigFile, source: string, warnings: string[]): Map<string, LoadedPreset> {
|
|
123
|
+
const presets = new Map<string, LoadedPreset>();
|
|
124
|
+
if (file.presets !== undefined && !isObject(file.presets)) {
|
|
125
|
+
warnings.push(`${source}: "presets" must be an object`);
|
|
126
|
+
return presets;
|
|
127
|
+
}
|
|
128
|
+
for (const [id, preset] of Object.entries(file.presets ?? {})) {
|
|
129
|
+
const valid =
|
|
130
|
+
isObject(preset) &&
|
|
131
|
+
typeof preset.level === "number" &&
|
|
132
|
+
Array.isArray(preset.include) &&
|
|
133
|
+
preset.include.every((member) => typeof member === "string");
|
|
134
|
+
if (!valid) {
|
|
135
|
+
warnings.push(`${source}: preset "${id}" needs a numeric level and a string list`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
presets.set(id, { id, preset: preset as Preset, source });
|
|
139
|
+
}
|
|
140
|
+
return presets;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface SelectionEntry {
|
|
144
|
+
id: string;
|
|
145
|
+
/** Rule ids to drop from this ruleset. */
|
|
146
|
+
remove: string[];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface Selection {
|
|
150
|
+
entries: SelectionEntry[];
|
|
151
|
+
preset?: { name: string; level: number };
|
|
152
|
+
warnings: string[];
|
|
153
|
+
fatal?: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function entryOf(ref: IncludeRef | PresetRef, label: string): { id: string; remove: string[] } | string {
|
|
157
|
+
if (typeof ref === "string") return ref.trim() ? { id: ref, remove: [] } : `${label}: empty id`;
|
|
158
|
+
if (!isObject(ref) || typeof ref.id !== "string" || !ref.id) return `${label}: expected an id`;
|
|
159
|
+
if (ref.remove !== undefined) {
|
|
160
|
+
if (!Array.isArray(ref.remove) || !ref.remove.every((value) => typeof value === "string")) {
|
|
161
|
+
return `${label}: remove must be a list of rule ids`;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return { id: ref.id, remove: (ref.remove as string[] | undefined) ?? [] };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Expands the active preset and include list into an ordered set of rulesets.
|
|
169
|
+
* Duplicate ids inside the include list are an error: the list is a set.
|
|
170
|
+
*/
|
|
171
|
+
export function resolveSelection(
|
|
172
|
+
presetRef: PresetRef | undefined,
|
|
173
|
+
includeList: IncludeRef[],
|
|
174
|
+
rulesets: Map<string, LoadedRuleSet>,
|
|
175
|
+
presets: Map<string, LoadedPreset>,
|
|
176
|
+
): Selection {
|
|
177
|
+
const warnings: string[] = [];
|
|
178
|
+
let fatal: string | undefined;
|
|
179
|
+
const entries = new Map<string, SelectionEntry>();
|
|
180
|
+
|
|
181
|
+
const presetName = presetRef === undefined ? undefined : typeof presetRef === "string" ? presetRef : presetRef.id;
|
|
182
|
+
const preset = presetName === undefined ? undefined : presets.get(presetName);
|
|
183
|
+
if (presetName !== undefined && !preset) {
|
|
184
|
+
fatal = `pi-permission: unknown preset "${presetName}"`;
|
|
185
|
+
} else if (preset) {
|
|
186
|
+
const ref = entryOf(presetRef!, `preset "${preset.id}"`);
|
|
187
|
+
if (typeof ref === "string") {
|
|
188
|
+
fatal = ref;
|
|
189
|
+
} else {
|
|
190
|
+
const drop = new Set(ref.remove);
|
|
191
|
+
for (const member of preset.preset.include) {
|
|
192
|
+
if (drop.has(member)) continue;
|
|
193
|
+
if (!rulesets.has(member)) {
|
|
194
|
+
fatal = `pi-permission: preset "${preset.id}" includes unknown ruleset "${member}"`;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
entries.set(member, { id: member, remove: [] });
|
|
198
|
+
}
|
|
199
|
+
for (const member of drop) {
|
|
200
|
+
if (!preset.preset.include.includes(member)) {
|
|
201
|
+
warnings.push(`${preset.source}: preset "${preset.id}": remove "${member}" matches no ruleset`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const seen = new Set<string>();
|
|
208
|
+
for (const [index, raw] of includeList.entries()) {
|
|
209
|
+
const ref = entryOf(raw, `include[${index}]`);
|
|
210
|
+
if (typeof ref === "string") {
|
|
211
|
+
fatal ??= `pi-permission: ${ref}`;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (!rulesets.has(ref.id)) {
|
|
215
|
+
fatal ??= `pi-permission: unknown ruleset "${ref.id}"`;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (seen.has(ref.id)) {
|
|
219
|
+
fatal ??= `pi-permission: ruleset "${ref.id}" is listed twice in include`;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
seen.add(ref.id);
|
|
223
|
+
entries.set(ref.id, ref);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
entries: [...entries.values()],
|
|
228
|
+
preset: preset ? { name: preset.id, level: preset.preset.level } : undefined,
|
|
229
|
+
warnings,
|
|
230
|
+
fatal,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export interface CommandContribution {
|
|
235
|
+
ruleset: string;
|
|
236
|
+
section: Section;
|
|
237
|
+
rule: Rule;
|
|
238
|
+
source?: string;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export interface PathContribution {
|
|
242
|
+
ruleset: string;
|
|
243
|
+
rule: PathRule;
|
|
244
|
+
source?: string;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface SelectedRules {
|
|
248
|
+
commands: CommandContribution[];
|
|
249
|
+
paths: PathContribution[];
|
|
250
|
+
active: ActiveRuleSet[];
|
|
251
|
+
warnings: string[];
|
|
252
|
+
fatal?: string;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function ruleList(ruleset: RuleSet): Array<{ section: Section; rule: Rule }> {
|
|
256
|
+
const out: Array<{ section: Section; rule: Rule }> = [];
|
|
257
|
+
for (const section of SECTIONS) {
|
|
258
|
+
const list = ruleset.commands?.[section];
|
|
259
|
+
if (!Array.isArray(list)) continue;
|
|
260
|
+
for (const rule of list) out.push({ section, rule });
|
|
261
|
+
}
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Collects the rules of every selected ruleset, applying each entry's removals.
|
|
267
|
+
* A rule id shared by two selected rulesets is an error: composition never
|
|
268
|
+
* relies on load order to pick a winner.
|
|
269
|
+
*/
|
|
270
|
+
export function collectSelected(
|
|
271
|
+
selection: Selection,
|
|
272
|
+
rulesets: Map<string, LoadedRuleSet>,
|
|
273
|
+
): SelectedRules {
|
|
274
|
+
const commands: CommandContribution[] = [];
|
|
275
|
+
const paths: PathContribution[] = [];
|
|
276
|
+
const active: ActiveRuleSet[] = [];
|
|
277
|
+
const warnings: string[] = [];
|
|
278
|
+
let fatal: string | undefined = selection.fatal;
|
|
279
|
+
const commandOwner = new Map<string, string>();
|
|
280
|
+
const pathOwner = new Map<string, string>();
|
|
281
|
+
|
|
282
|
+
for (const entry of selection.entries) {
|
|
283
|
+
const loaded = rulesets.get(entry.id);
|
|
284
|
+
if (!loaded) {
|
|
285
|
+
fatal ??= `pi-permission: unknown ruleset "${entry.id}"`;
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (loaded.error || !loaded.ruleset) {
|
|
289
|
+
fatal ??= `pi-permission: ${loaded.error ?? `ruleset "${entry.id}" is empty`}`;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const remove = new Set(entry.remove);
|
|
294
|
+
const seen = new Set<string>();
|
|
295
|
+
const removed = new Set<string>();
|
|
296
|
+
const contributed: CommandContribution[] = [];
|
|
297
|
+
const contributedPaths: PathContribution[] = [];
|
|
298
|
+
|
|
299
|
+
for (const { section, rule } of ruleList(loaded.ruleset)) {
|
|
300
|
+
if (!rule?.id) {
|
|
301
|
+
warnings.push(`ruleset "${entry.id}": ignore rule without id`);
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (seen.has(rule.id)) {
|
|
305
|
+
fatal ??= `pi-permission: ruleset "${entry.id}" defines rule "${rule.id}" twice`;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
seen.add(rule.id);
|
|
309
|
+
if (remove.has(rule.id)) {
|
|
310
|
+
removed.add(rule.id);
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const owner = commandOwner.get(rule.id);
|
|
314
|
+
if (owner) {
|
|
315
|
+
fatal ??= `pi-permission: rule "${rule.id}" is defined by both "${owner}" and "${entry.id}"`;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
commandOwner.set(rule.id, entry.id);
|
|
319
|
+
contributed.push({ ruleset: entry.id, section, rule, source: loaded.source });
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const pathList = Array.isArray(loaded.ruleset.paths) ? loaded.ruleset.paths : [];
|
|
323
|
+
for (const rule of pathList) {
|
|
324
|
+
if (!rule?.id) {
|
|
325
|
+
warnings.push(`ruleset "${entry.id}": ignore path rule without id`);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (seen.has(rule.id)) {
|
|
329
|
+
fatal ??= `pi-permission: ruleset "${entry.id}" defines rule "${rule.id}" twice`;
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
seen.add(rule.id);
|
|
333
|
+
if (remove.has(rule.id)) {
|
|
334
|
+
removed.add(rule.id);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const owner = pathOwner.get(rule.id);
|
|
338
|
+
if (owner) {
|
|
339
|
+
fatal ??= `pi-permission: path rule "${rule.id}" is defined by both "${owner}" and "${entry.id}"`;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
pathOwner.set(rule.id, entry.id);
|
|
343
|
+
contributedPaths.push({ ruleset: entry.id, rule, source: loaded.source });
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
for (const id of remove) {
|
|
347
|
+
if (!seen.has(id)) warnings.push(`ruleset "${entry.id}": remove "${id}" matches no rule`);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
commands.push(...contributed);
|
|
351
|
+
paths.push(...contributedPaths);
|
|
352
|
+
active.push({
|
|
353
|
+
id: entry.id,
|
|
354
|
+
source: loaded.source,
|
|
355
|
+
rules: contributed.length,
|
|
356
|
+
paths: contributedPaths.length,
|
|
357
|
+
removed: [...removed],
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return { commands, paths, active, warnings, fatal };
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export interface OwnRules {
|
|
365
|
+
commands: Map<string, CommandContribution>;
|
|
366
|
+
paths: Map<string, PathContribution>;
|
|
367
|
+
warnings: string[];
|
|
368
|
+
fatal?: string;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Rules written in config files. Later files win by id; duplicates in one file are errors. */
|
|
372
|
+
export function collectOwn(scopes: Array<{ path: string; file: ConfigFile }>): OwnRules {
|
|
373
|
+
const commands = new Map<string, CommandContribution>();
|
|
374
|
+
const paths = new Map<string, PathContribution>();
|
|
375
|
+
const warnings: string[] = [];
|
|
376
|
+
let fatal: string | undefined;
|
|
377
|
+
|
|
378
|
+
for (const scope of scopes) {
|
|
379
|
+
const local = new Set<string>();
|
|
380
|
+
for (const { section, rule } of ruleList(scope.file)) {
|
|
381
|
+
if (!rule?.id) {
|
|
382
|
+
warnings.push(`${scope.path}: ignore rule without id`);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
if (local.has(`rule:${rule.id}`)) {
|
|
386
|
+
fatal ??= `pi-permission: ${scope.path} defines rule "${rule.id}" twice`;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
local.add(`rule:${rule.id}`);
|
|
390
|
+
commands.set(rule.id, { ruleset: scope.path, section, rule, source: scope.path });
|
|
391
|
+
}
|
|
392
|
+
const pathList = Array.isArray(scope.file.paths) ? scope.file.paths : [];
|
|
393
|
+
for (const rule of pathList) {
|
|
394
|
+
if (!rule?.id) {
|
|
395
|
+
warnings.push(`${scope.path}: ignore path rule without id`);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (local.has(`path:${rule.id}`)) {
|
|
399
|
+
fatal ??= `pi-permission: ${scope.path} defines path rule "${rule.id}" twice`;
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
local.add(`path:${rule.id}`);
|
|
403
|
+
paths.set(rule.id, { ruleset: scope.path, rule, source: scope.path });
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return { commands, paths, warnings, fatal };
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export interface MergedRules {
|
|
411
|
+
commands: { deny: Rule[]; ask: Rule[]; allow: Rule[] };
|
|
412
|
+
paths: PathRule[];
|
|
413
|
+
warnings: string[];
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Config rules override selected rulesets by id (a more specific layer wins).
|
|
418
|
+
* A selected deny rule that a config turns into ask or allow is reported.
|
|
419
|
+
*/
|
|
420
|
+
export function mergeRules(selected: SelectedRules, own: OwnRules): MergedRules {
|
|
421
|
+
const warnings: string[] = [];
|
|
422
|
+
const commands: MergedRules["commands"] = { deny: [], ask: [], allow: [] };
|
|
423
|
+
const bundled = new Map<string, CommandContribution>();
|
|
424
|
+
for (const contribution of selected.commands) bundled.set(contribution.rule.id, contribution);
|
|
425
|
+
|
|
426
|
+
for (const contribution of selected.commands) {
|
|
427
|
+
if (own.commands.has(contribution.rule.id)) continue;
|
|
428
|
+
commands[contribution.section].push(contribution.rule);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
for (const [id, entry] of own.commands) {
|
|
432
|
+
const previous = bundled.get(id);
|
|
433
|
+
if (previous && previous.section === "deny" && entry.section !== "deny") {
|
|
434
|
+
warnings.push(`rule "${id}" dropped from deny by ${entry.source}`);
|
|
435
|
+
}
|
|
436
|
+
commands[entry.section].push(entry.rule);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const pathIds = new Set(selected.paths.map((contribution) => contribution.rule.id));
|
|
440
|
+
const paths = selected.paths.map((contribution) => own.paths.get(contribution.rule.id)?.rule ?? contribution.rule);
|
|
441
|
+
for (const [id, entry] of own.paths) if (!pathIds.has(id)) paths.push(entry.rule);
|
|
442
|
+
|
|
443
|
+
return { commands, paths, warnings };
|
|
444
|
+
}
|
package/src/tokenize.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quote-aware command splitter.
|
|
3
|
+
*
|
|
4
|
+
* Turns a raw bash command string into simple commands (argv lists) and
|
|
5
|
+
* throws away text that is not a command: quoted words stay inside argv,
|
|
6
|
+
* heredoc bodies and comments are skipped, and command substitutions are
|
|
7
|
+
* tokenized as their own commands.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface Redirect {
|
|
11
|
+
op: ">" | ">>" | "<";
|
|
12
|
+
target: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SimpleCommand {
|
|
16
|
+
argv: string[];
|
|
17
|
+
redirects: Redirect[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const BLANK = /\s/;
|
|
21
|
+
|
|
22
|
+
function isSeparator(ch: string): boolean {
|
|
23
|
+
return ch === ";" || ch === "&" || ch === "|" || ch === "\n";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Reads a quoted string starting at `start`; double quotes honour backslash escapes. */
|
|
27
|
+
function readQuoted(input: string, start: number): { text: string; next: number } {
|
|
28
|
+
const quote = input[start];
|
|
29
|
+
let text = "";
|
|
30
|
+
let i = start + 1;
|
|
31
|
+
while (i < input.length && input[i] !== quote) {
|
|
32
|
+
if (quote === '"' && input[i] === "\\") {
|
|
33
|
+
text += input[i + 1] ?? "";
|
|
34
|
+
i += 2;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
text += input[i];
|
|
38
|
+
i++;
|
|
39
|
+
}
|
|
40
|
+
return { text, next: i + 1 };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function tokenize(input: string): SimpleCommand[] {
|
|
44
|
+
const commands: SimpleCommand[] = [];
|
|
45
|
+
const subs: string[] = [];
|
|
46
|
+
let argv: string[] = [];
|
|
47
|
+
let redirects: Redirect[] = [];
|
|
48
|
+
let word = "";
|
|
49
|
+
let inWord = false;
|
|
50
|
+
let heredocs: string[] = [];
|
|
51
|
+
|
|
52
|
+
const flushWord = () => {
|
|
53
|
+
if (inWord) {
|
|
54
|
+
argv.push(word);
|
|
55
|
+
word = "";
|
|
56
|
+
inWord = false;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const endCommand = () => {
|
|
61
|
+
flushWord();
|
|
62
|
+
if (argv.length > 0) commands.push({ argv, redirects });
|
|
63
|
+
argv = [];
|
|
64
|
+
redirects = [];
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Skip to the end of a pending heredoc body; returns the index after it.
|
|
68
|
+
const skipHeredoc = (from: number, delimiter: string): number => {
|
|
69
|
+
let i = from;
|
|
70
|
+
while (i < input.length) {
|
|
71
|
+
const nl = input.indexOf("\n", i);
|
|
72
|
+
const lineEnd = nl === -1 ? input.length : nl;
|
|
73
|
+
if (input.slice(i, lineEnd).replace(/^\t+/, "").trimEnd() === delimiter) {
|
|
74
|
+
return nl === -1 ? input.length : nl + 1;
|
|
75
|
+
}
|
|
76
|
+
if (nl === -1) return input.length;
|
|
77
|
+
i = nl + 1;
|
|
78
|
+
}
|
|
79
|
+
return input.length;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const readRedirTarget = (from: number): { target: string; next: number } => {
|
|
83
|
+
let i = from;
|
|
84
|
+
while (i < input.length && (input[i] === " " || input[i] === "\t")) i++;
|
|
85
|
+
let target = "";
|
|
86
|
+
while (i < input.length && !BLANK.test(input[i] as string) && !isSeparator(input[i] as string)) {
|
|
87
|
+
const ch = input[i] as string;
|
|
88
|
+
if (ch === "'" || ch === '"') {
|
|
89
|
+
const quoted = readQuoted(input, i);
|
|
90
|
+
target += quoted.text;
|
|
91
|
+
i = quoted.next;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
target += ch;
|
|
95
|
+
i++;
|
|
96
|
+
}
|
|
97
|
+
return { target, next: i };
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
let i = 0;
|
|
101
|
+
while (i < input.length) {
|
|
102
|
+
const ch = input[i] as string;
|
|
103
|
+
|
|
104
|
+
if (ch === "\\") {
|
|
105
|
+
word += input[i + 1] ?? "";
|
|
106
|
+
inWord = true;
|
|
107
|
+
i += 2;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (ch === "'" || ch === '"') {
|
|
112
|
+
const quoted = readQuoted(input, i);
|
|
113
|
+
word += quoted.text;
|
|
114
|
+
inWord = true;
|
|
115
|
+
i = quoted.next;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (ch === "`") {
|
|
120
|
+
const close = input.indexOf("`", i + 1);
|
|
121
|
+
const stop = close === -1 ? input.length : close;
|
|
122
|
+
subs.push(input.slice(i + 1, stop));
|
|
123
|
+
i = stop + 1;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (ch === "$" && input[i + 1] === "(") {
|
|
128
|
+
let depth = 1;
|
|
129
|
+
let j = i + 2;
|
|
130
|
+
while (j < input.length && depth > 0) {
|
|
131
|
+
const c = input[j] as string;
|
|
132
|
+
if (c === "(") depth++;
|
|
133
|
+
else if (c === ")") depth--;
|
|
134
|
+
if (depth === 0) break;
|
|
135
|
+
j++;
|
|
136
|
+
}
|
|
137
|
+
subs.push(input.slice(i + 2, j));
|
|
138
|
+
i = j + 1;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (ch === "<" && input[i + 1] === "<") {
|
|
143
|
+
let j = i + 2;
|
|
144
|
+
if (input[j] === "-") j++;
|
|
145
|
+
while (input[j] === " " || input[j] === "\t") j++;
|
|
146
|
+
let delimiter = "";
|
|
147
|
+
if (input[j] === "'" || input[j] === '"') {
|
|
148
|
+
const quoted = readQuoted(input, j);
|
|
149
|
+
delimiter = quoted.text;
|
|
150
|
+
j = quoted.next;
|
|
151
|
+
} else {
|
|
152
|
+
while (j < input.length && !BLANK.test(input[j] as string) && !isSeparator(input[j] as string)) {
|
|
153
|
+
delimiter += input[j];
|
|
154
|
+
j++;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (delimiter) heredocs.push(delimiter);
|
|
158
|
+
i = j;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (ch === "&" && input[i + 1] === ">") {
|
|
163
|
+
// `&>file` / `&>>file` redirect both streams; the `&` belongs to the redirect.
|
|
164
|
+
const append = input[i + 2] === ">";
|
|
165
|
+
flushWord();
|
|
166
|
+
const redir = readRedirTarget(i + (append ? 3 : 2));
|
|
167
|
+
redirects.push({ op: append ? ">>" : ">", target: redir.target });
|
|
168
|
+
i = redir.next;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (ch === ">") {
|
|
173
|
+
const append = input[i + 1] === ">";
|
|
174
|
+
// `>|` overrides noclobber and is the same write.
|
|
175
|
+
const clobber = input[i + 1] === "|";
|
|
176
|
+
flushWord();
|
|
177
|
+
const redir = readRedirTarget(i + (append || clobber ? 2 : 1));
|
|
178
|
+
redirects.push({ op: append ? ">>" : ">", target: redir.target });
|
|
179
|
+
i = redir.next;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (ch === "<") {
|
|
184
|
+
flushWord();
|
|
185
|
+
const redir = readRedirTarget(i + 1);
|
|
186
|
+
redirects.push({ op: "<", target: redir.target });
|
|
187
|
+
i = redir.next;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (isSeparator(ch)) {
|
|
192
|
+
endCommand();
|
|
193
|
+
i++;
|
|
194
|
+
if (ch === "\n") {
|
|
195
|
+
for (const delimiter of heredocs) i = skipHeredoc(i, delimiter);
|
|
196
|
+
heredocs = [];
|
|
197
|
+
}
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (ch === "#" && !inWord) {
|
|
202
|
+
const nl = input.indexOf("\n", i);
|
|
203
|
+
i = nl === -1 ? input.length : nl;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (BLANK.test(ch)) {
|
|
208
|
+
flushWord();
|
|
209
|
+
i++;
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
word += ch;
|
|
214
|
+
inWord = true;
|
|
215
|
+
i++;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
endCommand();
|
|
219
|
+
|
|
220
|
+
for (const sub of subs) commands.push(...tokenize(sub));
|
|
221
|
+
return commands;
|
|
222
|
+
}
|