@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/src/types.ts ADDED
@@ -0,0 +1,167 @@
1
+ import type { OutsideRule } from "./outside";
2
+
3
+ export type Section = "deny" | "ask" | "allow";
4
+
5
+ export const SECTIONS: Section[] = ["deny", "ask", "allow"];
6
+
7
+ /** What a rule match does. Commands, path rules and defaults share the vocabulary. */
8
+ export type Action = "allow" | "ask" | "deny";
9
+
10
+ export type PathAction = Action;
11
+
12
+ export interface Rule {
13
+ id: string;
14
+ /** Program name(s). A `*` wildcard is allowed (e.g. `mkfs.*`). */
15
+ program: string | string[];
16
+ /** Subcommand or operand: matches when any of these appears as a positional. */
17
+ args?: string[];
18
+ /** Program subcommand: matches the first positional, after global value flags. */
19
+ subcommand?: string[];
20
+ /** Matches when any of these flags is present. */
21
+ anyFlag?: string[];
22
+ /** Matches only when all of these flags are present. */
23
+ allFlags?: string[];
24
+ /** Never matches when any of these flags is present. */
25
+ unlessFlag?: string[];
26
+ /** Last resort: regex over the joined argv of one simple command. */
27
+ argvRegex?: string;
28
+ reason?: string;
29
+ terminate?: boolean;
30
+ }
31
+
32
+ export interface CompiledRule extends Rule {
33
+ re?: RegExp;
34
+ programRes?: RegExp[];
35
+ }
36
+
37
+ export interface CompileResult {
38
+ rules: CompiledRule[];
39
+ warnings: string[];
40
+ }
41
+
42
+ export interface GateRules {
43
+ deny: CompiledRule[];
44
+ ask: CompiledRule[];
45
+ allow: CompiledRule[];
46
+ }
47
+
48
+ export interface Verdict {
49
+ action: Action;
50
+ reasons: string[];
51
+ /** Session grant keys of the matched ask rules, or of the unmatched fallback. */
52
+ grants: string[];
53
+ terminate: boolean;
54
+ /** False when the verdict comes from `defaultPermission.commands` instead of a rule. */
55
+ matched: boolean;
56
+ }
57
+
58
+ export interface CommandsSection {
59
+ deny?: Rule[];
60
+ ask?: Rule[];
61
+ allow?: Rule[];
62
+ }
63
+
64
+ /** A leaf: rules only, never a reference to another ruleset. */
65
+ export interface RuleSet {
66
+ description?: string;
67
+ commands?: CommandsSection;
68
+ paths?: PathRule[];
69
+ }
70
+
71
+ /** A permission level. Only the shipped index defines presets. */
72
+ export interface Preset {
73
+ description?: string;
74
+ /** Higher is stricter. A preset's members are the previous level plus its additions. */
75
+ level: number;
76
+ /** Ruleset ids, flat. A preset never includes another preset. */
77
+ include: string[];
78
+ }
79
+
80
+ /** `{ "id": "git-local", "remove": ["git-add"] }` */
81
+ export interface IncludeEntry {
82
+ id: string;
83
+ /** Rule ids to drop from the referenced ruleset. */
84
+ remove?: string[];
85
+ }
86
+
87
+ export type IncludeRef = string | IncludeEntry;
88
+ export type PresetRef = string | IncludeEntry;
89
+
90
+ export interface DefaultPermission {
91
+ commands?: Action;
92
+ paths?: Action;
93
+ }
94
+
95
+ export interface OutsideCwdConfig {
96
+ /** What to do when a tool touches a path outside the working directory. */
97
+ mode?: "allow" | "ask" | "block";
98
+ /** Absolute, `~`, or cwd-relative globs that stay allowed. */
99
+ allowPaths?: string[];
100
+ }
101
+
102
+ /** A config file. The shipped `rules/index.json` is one too. */
103
+ export interface ConfigFile extends RuleSet {
104
+ $schema?: string;
105
+ /** Named rulesets: inline rules or a path to a ruleset file. Merged by id. */
106
+ rulesets?: Record<string, RuleSet | string>;
107
+ /** Permission levels. Only the shipped index may define them. */
108
+ presets?: Record<string, Preset>;
109
+ /** The active level. The last scope that declares it wins. */
110
+ preset?: PresetRef;
111
+ /** Extra rulesets. The last scope that declares it replaces the list. */
112
+ include?: IncludeRef[];
113
+ defaultPermission?: DefaultPermission;
114
+ outsideCwd?: OutsideCwdConfig;
115
+ enabled?: { commands?: boolean; paths?: boolean };
116
+ }
117
+
118
+ export type PathOp = "read" | "write" | "delete";
119
+
120
+ export interface PathRule {
121
+ id: string;
122
+ /** Globs. No slash: matches the basename. `~`/`/`: absolute. Else relative to cwd. */
123
+ patterns: string[];
124
+ /** Patterns that cancel the rule. */
125
+ except?: string[];
126
+ /** Default: deny. A deny rule on the same path wins over ask and allow. */
127
+ action?: PathAction;
128
+ /** Default: all operations. */
129
+ ops?: PathOp[];
130
+ /** Only protect paths that already exist. Default false. */
131
+ onlyIfExists?: boolean;
132
+ message?: string;
133
+ }
134
+
135
+ export interface CompiledPathRule extends PathRule {
136
+ compiled: Array<{ value: string; kind: "base" | "abs" | "rel"; re: RegExp }>;
137
+ compiledExcept: Array<{ value: string; kind: "base" | "abs" | "rel"; re: RegExp }>;
138
+ }
139
+
140
+ /** One ruleset that ended up in the composition, for `/permission`. */
141
+ export interface ActiveRuleSet {
142
+ id: string;
143
+ source?: string;
144
+ /** Rules contributed after removals. */
145
+ rules: number;
146
+ /** Path rules contributed after removals. */
147
+ paths: number;
148
+ /** Rule ids removed from this ruleset by an include entry. */
149
+ removed: string[];
150
+ }
151
+
152
+ export interface ResolvedConfig {
153
+ enabled: { commands: boolean; paths: boolean };
154
+ commands: GateRules;
155
+ paths: CompiledPathRule[];
156
+ outside: OutsideRule;
157
+ defaultPermission: { commands: Action; paths: Action };
158
+ /** Active preset, when one is selected. */
159
+ preset?: { name: string; level: number };
160
+ /** Selected rulesets in composition order. */
161
+ rulesets: ActiveRuleSet[];
162
+ /** Config files read, in load order. */
163
+ sources: string[];
164
+ /** Set when the config cannot be honored: every gated tool is blocked. */
165
+ fatal?: string;
166
+ warnings: string[];
167
+ }
@@ -0,0 +1,125 @@
1
+ import { SECTIONS } from "./types";
2
+
3
+ /**
4
+ * Field names the loader reads. The lists mirror schema.json;
5
+ * test/schema.test.ts compares both sides.
6
+ */
7
+ export const FIELDS = {
8
+ config: [
9
+ "$schema",
10
+ "description",
11
+ "rulesets",
12
+ "presets",
13
+ "preset",
14
+ "include",
15
+ "defaultPermission",
16
+ "outsideCwd",
17
+ "enabled",
18
+ "commands",
19
+ "paths",
20
+ ],
21
+ ruleset: ["$schema", "description", "commands", "paths"],
22
+ preset: ["description", "level", "include"],
23
+ includeEntry: ["id", "remove"],
24
+ defaults: ["commands", "paths"],
25
+ enabled: ["commands", "paths"],
26
+ outside: ["mode", "allowPaths"],
27
+ commands: SECTIONS,
28
+ rule: ["id", "program", "args", "subcommand", "anyFlag", "allFlags", "unlessFlag", "argvRegex", "reason", "terminate"],
29
+ pathRule: ["id", "patterns", "except", "action", "ops", "onlyIfExists", "message"],
30
+ } as const;
31
+
32
+ export type ConfigKind = "config" | "ruleset";
33
+
34
+ type Json = Record<string, unknown>;
35
+
36
+ export function isObject(value: unknown): value is Json {
37
+ return typeof value === "object" && value !== null && !Array.isArray(value);
38
+ }
39
+
40
+ function reportUnknown(
41
+ value: unknown,
42
+ allowed: readonly string[],
43
+ path: string,
44
+ label: string,
45
+ warnings: string[],
46
+ ): void {
47
+ if (!isObject(value)) return;
48
+ for (const key of Object.keys(value)) {
49
+ if (allowed.includes(key)) continue;
50
+ warnings.push(`${path}: unknown field "${label ? `${label}.` : ""}${key}"`);
51
+ }
52
+ }
53
+
54
+ function checkList(
55
+ value: unknown,
56
+ allowed: readonly string[],
57
+ path: string,
58
+ label: string,
59
+ warnings: string[],
60
+ ): void {
61
+ if (!Array.isArray(value)) return;
62
+ value.forEach((item, index) => reportUnknown(item, allowed, path, `${label}[${index}]`, warnings));
63
+ }
64
+
65
+ function checkCommands(value: unknown, path: string, label: string, warnings: string[]): void {
66
+ if (!isObject(value)) return;
67
+ reportUnknown(value, FIELDS.commands, path, label, warnings);
68
+ for (const section of SECTIONS) {
69
+ checkList(value[section], FIELDS.rule, path, `${label}.${section}`, warnings);
70
+ }
71
+ }
72
+
73
+ function checkRuleSet(value: unknown, path: string, label: string, warnings: string[]): void {
74
+ if (!isObject(value)) return;
75
+ reportUnknown(value, FIELDS.ruleset, path, label, warnings);
76
+ checkCommands(value.commands, path, `${label}.commands`, warnings);
77
+ checkList(value.paths, FIELDS.pathRule, path, `${label}.paths`, warnings);
78
+ }
79
+
80
+ function checkEntry(value: unknown, path: string, label: string, warnings: string[]): void {
81
+ if (typeof value === "string") return;
82
+ reportUnknown(value, FIELDS.includeEntry, path, label, warnings);
83
+ }
84
+
85
+ /** Warns once per field name the loader would silently drop. */
86
+ export function checkFields(
87
+ file: unknown,
88
+ path: string,
89
+ warnings: string[],
90
+ kind: ConfigKind = "config",
91
+ ): void {
92
+ if (!isObject(file)) return;
93
+
94
+ reportUnknown(file, kind === "config" ? FIELDS.config : FIELDS.ruleset, path, "", warnings);
95
+ if (kind === "ruleset") {
96
+ checkCommands(file.commands, path, "commands", warnings);
97
+ checkList(file.paths, FIELDS.pathRule, path, "paths", warnings);
98
+ return;
99
+ }
100
+
101
+ reportUnknown(file.enabled, FIELDS.enabled, path, "enabled", warnings);
102
+ reportUnknown(file.outsideCwd, FIELDS.outside, path, "outsideCwd", warnings);
103
+ reportUnknown(file.defaultPermission, FIELDS.defaults, path, "defaultPermission", warnings);
104
+
105
+ checkCommands(file.commands, path, "commands", warnings);
106
+ checkList(file.paths, FIELDS.pathRule, path, "paths", warnings);
107
+
108
+ if (isObject(file.rulesets)) {
109
+ for (const [name, value] of Object.entries(file.rulesets)) {
110
+ if (typeof value === "string") continue;
111
+ checkRuleSet(value, path, `rulesets.${name}`, warnings);
112
+ }
113
+ }
114
+
115
+ if (isObject(file.presets)) {
116
+ for (const [name, value] of Object.entries(file.presets)) {
117
+ reportUnknown(value, FIELDS.preset, path, `presets.${name}`, warnings);
118
+ }
119
+ }
120
+
121
+ checkEntry(file.preset, path, "preset", warnings);
122
+ if (Array.isArray(file.include)) {
123
+ file.include.forEach((entry, index) => checkEntry(entry, path, `include[${index}]`, warnings));
124
+ }
125
+ }