@gotgenes/pi-permission-system 16.0.1 → 16.1.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 +14 -0
- package/package.json +1 -1
- package/src/access-intent/access-path.ts +88 -0
- package/src/access-intent/bash/command-enumeration.ts +154 -0
- package/src/access-intent/bash/cwd-projection.ts +498 -0
- package/src/access-intent/bash/node-text.ts +75 -0
- package/src/access-intent/bash/parser.ts +42 -0
- package/src/access-intent/bash/program.ts +102 -0
- package/src/{handlers/gates/bash-token-classification.ts → access-intent/bash/token-classification.ts} +1 -1
- package/src/access-intent/bash/token-collection.ts +351 -0
- package/src/handlers/gates/bash-command.ts +1 -1
- package/src/handlers/gates/bash-external-directory.ts +19 -33
- package/src/handlers/gates/bash-path-extractor.ts +10 -4
- package/src/handlers/gates/bash-path.ts +2 -2
- package/src/handlers/gates/external-directory-policy.ts +65 -0
- package/src/handlers/gates/external-directory.ts +8 -11
- package/src/handlers/gates/path.ts +1 -3
- package/src/handlers/gates/skill-read.ts +0 -4
- package/src/handlers/gates/tool-call-gate-pipeline.ts +2 -2
- package/src/handlers/gates/tool.ts +1 -1
- package/src/handlers/gates/types.ts +1 -1
- package/src/path-utils.ts +0 -20
- package/test/access-intent/access-path.test.ts +107 -0
- package/test/access-intent/bash/node-text.test.ts +147 -0
- package/test/access-intent/bash/parser.test.ts +19 -0
- package/test/{handlers/gates/bash-program.test.ts → access-intent/bash/program.test.ts} +114 -73
- package/test/{handlers/gates/bash-token-classification.test.ts → access-intent/bash/token-classification.test.ts} +1 -1
- package/test/access-intent/bash/token-collection.test.ts +300 -0
- package/test/handlers/external-directory-symlink-acceptance.test.ts +2 -3
- package/test/handlers/gates/bash-command-metamorphic.test.ts +2 -3
- package/test/handlers/gates/bash-external-directory.test.ts +2 -10
- package/test/handlers/gates/bash-path.test.ts +2 -2
- package/test/handlers/gates/external-directory-policy.test.ts +112 -0
- package/test/handlers/gates/external-directory.test.ts +0 -5
- package/test/handlers/gates/tool-call-gate-pipeline.test.ts +7 -3
- package/test/path-utils.test.ts +0 -34
- package/src/handlers/gates/bash-program.ts +0 -1143
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
2
|
+
import { AccessPath } from "#src/access-intent/access-path";
|
|
3
|
+
import {
|
|
4
|
+
ARG_NODE_TYPES,
|
|
5
|
+
SKIP_SUBTREE_TYPES,
|
|
6
|
+
} from "#src/access-intent/bash/node-text";
|
|
7
|
+
import type { TSNode } from "#src/access-intent/bash/parser";
|
|
8
|
+
import {
|
|
9
|
+
classifyTokenAsPathCandidate,
|
|
10
|
+
classifyTokenAsRuleCandidate,
|
|
11
|
+
} from "#src/access-intent/bash/token-classification";
|
|
12
|
+
import {
|
|
13
|
+
collectCommandTokens,
|
|
14
|
+
collectPathCandidateTokens,
|
|
15
|
+
collectRedirectTokens,
|
|
16
|
+
extractCommandName,
|
|
17
|
+
} from "#src/access-intent/bash/token-collection";
|
|
18
|
+
import { canonicalizePath } from "#src/canonicalize-path";
|
|
19
|
+
import {
|
|
20
|
+
getPathPolicyValues,
|
|
21
|
+
isPathWithinDirectory,
|
|
22
|
+
isSafeSystemPath,
|
|
23
|
+
normalizePathForComparison,
|
|
24
|
+
normalizePathPolicyLiteral,
|
|
25
|
+
} from "#src/path-utils";
|
|
26
|
+
|
|
27
|
+
// ── Internal types ───────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The working directory in force where a path candidate appears.
|
|
31
|
+
*
|
|
32
|
+
* A `known` base carries an `offset` to be joined with `cwd` at resolution
|
|
33
|
+
* time: a relative-or-absolute path string built by folding the literal targets
|
|
34
|
+
* of current-shell `cd` commands (`""` = `cwd`); an absolute offset (from
|
|
35
|
+
* `cd /abs`) ignores `cwd` at resolution time.
|
|
36
|
+
* An `unknown` base marks a non-literal `cd` target (`cd "$DIR"`, `cd $(…)`,
|
|
37
|
+
* `cd -`, bare `cd`, `cd ~…`) that made the effective directory unresolvable.
|
|
38
|
+
*/
|
|
39
|
+
type EffectiveBase =
|
|
40
|
+
| { readonly kind: "known"; readonly offset: string }
|
|
41
|
+
| { readonly kind: "unknown" };
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A path-candidate token paired with the effective working directory projected
|
|
45
|
+
* onto the point in the command stream where it appears.
|
|
46
|
+
*/
|
|
47
|
+
interface PathCandidate {
|
|
48
|
+
readonly token: string;
|
|
49
|
+
readonly base: EffectiveBase;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ── Public output type ───────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
export interface BashPathRuleCandidate {
|
|
55
|
+
/** Raw path-like token shown in prompts, logs, and session approvals. */
|
|
56
|
+
readonly token: string;
|
|
57
|
+
/** Equivalent values used for permission policy matching. */
|
|
58
|
+
readonly policyValues: readonly string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ── Walk-time constants ──────────────────────────────────────────────────────
|
|
62
|
+
|
|
63
|
+
/** The working directory in force at the start of a program (`cwd`). */
|
|
64
|
+
const CWD_BASE: EffectiveBase = { kind: "known", offset: "" };
|
|
65
|
+
|
|
66
|
+
/** The effective directory after a non-literal or unresolvable `cd`. */
|
|
67
|
+
const UNKNOWN_BASE: EffectiveBase = { kind: "unknown" };
|
|
68
|
+
|
|
69
|
+
// ── AST walk — collect PathCandidates ───────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Walk the AST once, collecting every path-candidate token tagged with the
|
|
73
|
+
* effective working directory projected onto its position.
|
|
74
|
+
*
|
|
75
|
+
* The effective directory is stateful: it starts at `cwd` and each
|
|
76
|
+
* current-shell `cd <literal>` (joined by `&&`, `||`, `;`, or a newline)
|
|
77
|
+
* folds into it for subsequent commands.
|
|
78
|
+
* A `cd` inside a pipeline or a backgrounded command runs in a subshell and
|
|
79
|
+
* does not update the running directory; subshell and brace-group interiors
|
|
80
|
+
* inherit the enclosing base without folding their own `cd`s (a conservative
|
|
81
|
+
* first tier).
|
|
82
|
+
*/
|
|
83
|
+
export function collectPathCandidates(rootNode: TSNode): PathCandidate[] {
|
|
84
|
+
const out: PathCandidate[] = [];
|
|
85
|
+
walkForCandidates(rootNode, CWD_BASE, out);
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Collect a single node's candidates tagged with `base`, returning the
|
|
91
|
+
* effective base in force *after* the node (the input base unless the node is
|
|
92
|
+
* a current-shell `cd <literal>` that folds the running directory).
|
|
93
|
+
*/
|
|
94
|
+
function walkForCandidates(
|
|
95
|
+
node: TSNode,
|
|
96
|
+
base: EffectiveBase,
|
|
97
|
+
out: PathCandidate[],
|
|
98
|
+
): EffectiveBase {
|
|
99
|
+
switch (node.type) {
|
|
100
|
+
case "program":
|
|
101
|
+
case "list":
|
|
102
|
+
case "redirected_statement":
|
|
103
|
+
return walkCurrentShellSequence(node, base, out);
|
|
104
|
+
case "command":
|
|
105
|
+
tagTokens(collectCommandTokens(node), base, out);
|
|
106
|
+
return foldCd(node, base);
|
|
107
|
+
case "pipeline":
|
|
108
|
+
// tree-sitter-bash mis-groups a redirect-bearing `&&`/`;` list as the
|
|
109
|
+
// first stage of a pipeline (`cd a && pnpm x 2>&1 | tail` parses as
|
|
110
|
+
// `(cd a && pnpm x 2>&1) | tail`), burying a current-shell `cd` inside
|
|
111
|
+
// a node the `default` case treats as non-folding. Recover bash operator
|
|
112
|
+
// precedence (`|` binds tighter than `&&`/`||`/`;`): fold the first
|
|
113
|
+
// stage's leading current-shell commands while keeping its terminal
|
|
114
|
+
// command and every downstream stage as non-folding subshells (#454).
|
|
115
|
+
return walkPipeline(node, base, out);
|
|
116
|
+
case "subshell":
|
|
117
|
+
// A subshell runs in a child shell: its interior `cd`s fold within the
|
|
118
|
+
// subshell but reset on exit, so the folded base is discarded.
|
|
119
|
+
walkCurrentShellSequence(node, base, out);
|
|
120
|
+
return base;
|
|
121
|
+
case "compound_statement":
|
|
122
|
+
// A `{ … }` brace group runs in the current shell, so its `cd`s persist
|
|
123
|
+
// to following commands — thread and return the folded base.
|
|
124
|
+
return walkCurrentShellSequence(node, base, out);
|
|
125
|
+
default:
|
|
126
|
+
// Pipelines, control-flow bodies, redirect targets, and command/process
|
|
127
|
+
// substitution interiors: collect every candidate in the subtree tagged
|
|
128
|
+
// with the enclosing base and do not fold their internal `cd`s. (Folding
|
|
129
|
+
// inside substitutions is deferred — conservative, never under-flags.)
|
|
130
|
+
tagTokens(collectPathCandidateTokens(node), base, out);
|
|
131
|
+
return base;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Fold a current-shell sequence (`program` / `list` / `redirected_statement`):
|
|
137
|
+
* thread the effective base left-to-right through the children so a `cd`
|
|
138
|
+
* updates the base for following siblings.
|
|
139
|
+
* A statement immediately followed by the background operator (`&`) runs in a
|
|
140
|
+
* subshell, so its folded base is discarded.
|
|
141
|
+
*/
|
|
142
|
+
function walkCurrentShellSequence(
|
|
143
|
+
seqNode: TSNode,
|
|
144
|
+
base: EffectiveBase,
|
|
145
|
+
out: PathCandidate[],
|
|
146
|
+
): EffectiveBase {
|
|
147
|
+
let current = base;
|
|
148
|
+
for (let i = 0; i < seqNode.childCount; i++) {
|
|
149
|
+
const child = seqNode.child(i);
|
|
150
|
+
if (!child?.isNamed) continue;
|
|
151
|
+
if (SKIP_SUBTREE_TYPES.has(child.type)) continue;
|
|
152
|
+
const after = walkForCandidates(child, current, out);
|
|
153
|
+
current = isBackgrounded(seqNode, i) ? current : after;
|
|
154
|
+
}
|
|
155
|
+
return current;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Walk a `pipeline` node, returning the effective base in force after it.
|
|
160
|
+
*
|
|
161
|
+
* Each stage of a true pipeline (`A | B | C`) runs in a subshell, so a `cd`
|
|
162
|
+
* inside any stage must not leak — the base normally passes through unchanged.
|
|
163
|
+
* The exception is the first stage: tree-sitter-bash wraps a redirect-bearing
|
|
164
|
+
* current-shell `&&`/`;` list (`cd a && pnpm x 2>&1 | tail`) as that stage,
|
|
165
|
+
* and bash precedence makes the list's leading commands current-shell, so they
|
|
166
|
+
* fold and the folded base persists past the pipeline to following siblings.
|
|
167
|
+
*
|
|
168
|
+
* The terminal command of the first stage is the real pipe stage (a subshell)
|
|
169
|
+
* and must not fold; every stage after a `|` is a downstream subshell stage
|
|
170
|
+
* and collects tokens against the folded base without folding (#454).
|
|
171
|
+
*/
|
|
172
|
+
function walkPipeline(
|
|
173
|
+
node: TSNode,
|
|
174
|
+
base: EffectiveBase,
|
|
175
|
+
out: PathCandidate[],
|
|
176
|
+
): EffectiveBase {
|
|
177
|
+
let current = base;
|
|
178
|
+
let first = true;
|
|
179
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
180
|
+
const child = node.child(i);
|
|
181
|
+
if (!child?.isNamed) continue;
|
|
182
|
+
if (SKIP_SUBTREE_TYPES.has(child.type)) continue;
|
|
183
|
+
if (first) {
|
|
184
|
+
current = foldPipelineFirstStage(child, current, out);
|
|
185
|
+
first = false;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
// Downstream stage (after a `|`): subshell — collect against the folded
|
|
189
|
+
// base, do not fold.
|
|
190
|
+
tagTokens(collectPathCandidateTokens(child), current, out);
|
|
191
|
+
}
|
|
192
|
+
return current;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Collect the first pipe stage's candidates, folding its leading current-shell
|
|
197
|
+
* `cd` commands when tree-sitter wrapped a `list` or `redirected_statement`
|
|
198
|
+
* around them.
|
|
199
|
+
* The terminal command of that container is the real pipe stage (a subshell)
|
|
200
|
+
* and is collected without folding.
|
|
201
|
+
* A bare `command` first stage (a true pipeline first stage such as
|
|
202
|
+
* `cd nested | cat ../b`) is a subshell: it collects against the input base
|
|
203
|
+
* and does not fold.
|
|
204
|
+
*/
|
|
205
|
+
function foldPipelineFirstStage(
|
|
206
|
+
node: TSNode,
|
|
207
|
+
base: EffectiveBase,
|
|
208
|
+
out: PathCandidate[],
|
|
209
|
+
): EffectiveBase {
|
|
210
|
+
if (node.type === "list") return foldListExceptTerminal(node, base, out);
|
|
211
|
+
if (node.type === "redirected_statement") {
|
|
212
|
+
let current = base;
|
|
213
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
214
|
+
const child = node.child(i);
|
|
215
|
+
if (!child?.isNamed) continue;
|
|
216
|
+
if (child.type === "file_redirect") {
|
|
217
|
+
// Redirect destinations are part of the piped stage; collect them
|
|
218
|
+
// against the folded base without folding.
|
|
219
|
+
tagTokens(collectRedirectTokens(child), current, out);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
// The inner statement is the `list`/`command` being redirected; fold its
|
|
223
|
+
// leading current-shell commands via the terminal-excluding walk.
|
|
224
|
+
current = foldPipelineFirstStage(child, current, out);
|
|
225
|
+
}
|
|
226
|
+
return current;
|
|
227
|
+
}
|
|
228
|
+
// Bare `command` or any other shape: a true subshell first stage.
|
|
229
|
+
tagTokens(collectPathCandidateTokens(node), base, out);
|
|
230
|
+
return base;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Fold every named, non-skip child of a `list` except the last, threading the
|
|
235
|
+
* effective base left-to-right through the leading current-shell commands; the
|
|
236
|
+
* terminal child is the real pipe stage and is collected without folding.
|
|
237
|
+
*/
|
|
238
|
+
function foldListExceptTerminal(
|
|
239
|
+
node: TSNode,
|
|
240
|
+
base: EffectiveBase,
|
|
241
|
+
out: PathCandidate[],
|
|
242
|
+
): EffectiveBase {
|
|
243
|
+
const namedChildren: TSNode[] = [];
|
|
244
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
245
|
+
const child = node.child(i);
|
|
246
|
+
if (child?.isNamed && !SKIP_SUBTREE_TYPES.has(child.type)) {
|
|
247
|
+
namedChildren.push(child);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
let current = base;
|
|
251
|
+
for (let i = 0; i < namedChildren.length; i++) {
|
|
252
|
+
const child = namedChildren[i];
|
|
253
|
+
if (i < namedChildren.length - 1) {
|
|
254
|
+
current = walkForCandidates(child, current, out);
|
|
255
|
+
} else {
|
|
256
|
+
// Terminal child = the real pipe stage; collect without folding.
|
|
257
|
+
tagTokens(collectPathCandidateTokens(child), current, out);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return current;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* True when the statement at `index` is immediately followed by the background
|
|
265
|
+
* operator (`&`) — distinct from the `&&` / `||` / `;` current-shell
|
|
266
|
+
* separators.
|
|
267
|
+
*/
|
|
268
|
+
function isBackgrounded(seqNode: TSNode, index: number): boolean {
|
|
269
|
+
const next = seqNode.child(index + 1);
|
|
270
|
+
if (!next || next.isNamed) return false;
|
|
271
|
+
return next.type === "&";
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function tagTokens(
|
|
275
|
+
tokens: readonly string[],
|
|
276
|
+
base: EffectiveBase,
|
|
277
|
+
out: PathCandidate[],
|
|
278
|
+
): void {
|
|
279
|
+
for (const token of tokens) out.push({ token, base });
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ── cd-fold helpers ──────────────────────────────────────────────────────────
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Compute the effective base after a command runs.
|
|
286
|
+
* Returns `base` unchanged unless the command is `cd`:
|
|
287
|
+
*
|
|
288
|
+
* - `cd /abs` (absolute literal) → a fresh known base, recovering from an
|
|
289
|
+
* earlier unknown base.
|
|
290
|
+
* - `cd rel` (relative literal) → fold into a known base, or stay unknown if
|
|
291
|
+
* the base was already unknown.
|
|
292
|
+
* - `cd "$DIR"` / `cd $(…)` / `cd -` / bare `cd` / `cd ~…` (non-literal) →
|
|
293
|
+
* unknown.
|
|
294
|
+
*/
|
|
295
|
+
function foldCd(commandNode: TSNode, base: EffectiveBase): EffectiveBase {
|
|
296
|
+
if (extractCommandName(commandNode) !== "cd") return base;
|
|
297
|
+
const target = cdLiteralTarget(commandNode);
|
|
298
|
+
if (target === null) return UNKNOWN_BASE;
|
|
299
|
+
if (isAbsolute(target)) return { kind: "known", offset: target };
|
|
300
|
+
if (base.kind === "unknown") return UNKNOWN_BASE;
|
|
301
|
+
return { kind: "known", offset: join(base.offset, target) };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Resolve the literal target of a `cd` command, or `null` when the first
|
|
306
|
+
* argument is not a static literal (contains an expansion or command
|
|
307
|
+
* substitution) or cannot be resolved against the working directory (`cd -`,
|
|
308
|
+
* `cd ~…`, bare `cd`).
|
|
309
|
+
*/
|
|
310
|
+
function cdLiteralTarget(commandNode: TSNode): string | null {
|
|
311
|
+
for (let i = 0; i < commandNode.childCount; i++) {
|
|
312
|
+
const child = commandNode.child(i);
|
|
313
|
+
if (!child) continue;
|
|
314
|
+
if (child.type === "command_name" || child.type === "variable_assignment")
|
|
315
|
+
continue;
|
|
316
|
+
if (!child.isNamed) continue;
|
|
317
|
+
// Skip the `--` end-of-flags marker; the next argument is the target.
|
|
318
|
+
if (child.type === "word" && child.text === "--") continue;
|
|
319
|
+
if (!ARG_NODE_TYPES.has(child.type)) return null;
|
|
320
|
+
return literalTextOf(child);
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The literal string value of an argument node, or `null` when it contains a
|
|
327
|
+
* variable expansion / command substitution or is a non-resolvable `cd`
|
|
328
|
+
* destination (`-`, `~…`).
|
|
329
|
+
*/
|
|
330
|
+
function literalTextOf(node: TSNode): string | null {
|
|
331
|
+
switch (node.type) {
|
|
332
|
+
case "word": {
|
|
333
|
+
const text = node.text;
|
|
334
|
+
if (text === "-" || text.startsWith("~")) return null;
|
|
335
|
+
return text;
|
|
336
|
+
}
|
|
337
|
+
case "raw_string": {
|
|
338
|
+
const text = node.text;
|
|
339
|
+
return text.length >= 2 && text.startsWith("'") && text.endsWith("'")
|
|
340
|
+
? text.slice(1, -1)
|
|
341
|
+
: text;
|
|
342
|
+
}
|
|
343
|
+
case "concatenation": {
|
|
344
|
+
let result = "";
|
|
345
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
346
|
+
const child = node.child(i);
|
|
347
|
+
if (!child) continue;
|
|
348
|
+
const part = literalTextOf(child);
|
|
349
|
+
if (part === null) return null;
|
|
350
|
+
result += part;
|
|
351
|
+
}
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
case "string": {
|
|
355
|
+
let result = "";
|
|
356
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
357
|
+
const child = node.child(i);
|
|
358
|
+
if (!child) continue;
|
|
359
|
+
if (child.type === '"') continue;
|
|
360
|
+
if (child.type !== "string_content") return null;
|
|
361
|
+
result += child.text;
|
|
362
|
+
}
|
|
363
|
+
return result;
|
|
364
|
+
}
|
|
365
|
+
default:
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// ── Per-candidate helpers ────────────────────────────────────────────────────
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* True when a path candidate is relative (resolved against the effective
|
|
374
|
+
* directory) rather than absolute (`/…`) or home-relative (`~…`), which are
|
|
375
|
+
* base-independent.
|
|
376
|
+
* Used to decide which candidates an unknown base affects.
|
|
377
|
+
*/
|
|
378
|
+
function isRelativeCandidate(candidate: string): boolean {
|
|
379
|
+
return !candidate.startsWith("/") && !candidate.startsWith("~");
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function getPolicyValuesForRuleCandidate(
|
|
383
|
+
candidate: string,
|
|
384
|
+
base: EffectiveBase,
|
|
385
|
+
cwd: string,
|
|
386
|
+
): string[] {
|
|
387
|
+
if (base.kind === "unknown" && isRelativeCandidate(candidate)) {
|
|
388
|
+
const literal = normalizePathPolicyLiteral(candidate);
|
|
389
|
+
return literal ? [literal] : [];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const resolveBase = base.kind === "known" ? resolve(cwd, base.offset) : cwd;
|
|
393
|
+
return getPathPolicyValues(candidate, { cwd, resolveBase });
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// ── Projection functions ─────────────────────────────────────────────────────
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Project a collection of path candidates into deduplicated external paths.
|
|
400
|
+
*
|
|
401
|
+
* Filters candidates through the strict path classifier
|
|
402
|
+
* (`classifyTokenAsPathCandidate`), resolves each against its effective working
|
|
403
|
+
* directory base, and returns only paths that resolve outside `cwd` in their
|
|
404
|
+
* lexical (as-typed, normalized but not symlink-resolved) form.
|
|
405
|
+
*
|
|
406
|
+
* The outside-`cwd` decision and the dedup identity use the canonical
|
|
407
|
+
* (symlink-resolved) form so `external_directory` config patterns match the
|
|
408
|
+
* path as the user typed it (#418).
|
|
409
|
+
*/
|
|
410
|
+
export function projectExternalPaths(
|
|
411
|
+
candidates: readonly PathCandidate[],
|
|
412
|
+
cwd: string,
|
|
413
|
+
): AccessPath[] {
|
|
414
|
+
const normalizedCwd = canonicalizePath(normalizePathForComparison(cwd, cwd));
|
|
415
|
+
|
|
416
|
+
const seen = new Set<string>();
|
|
417
|
+
const externalPaths: AccessPath[] = [];
|
|
418
|
+
|
|
419
|
+
for (const { token, base } of candidates) {
|
|
420
|
+
const candidate = classifyTokenAsPathCandidate(token);
|
|
421
|
+
if (!candidate) continue;
|
|
422
|
+
|
|
423
|
+
// Unknown effective directory: a relative candidate could resolve anywhere,
|
|
424
|
+
// so flag it conservatively (resolving against `cwd` only for a display
|
|
425
|
+
// path). Absolute / `~` candidates are base-independent and resolve below.
|
|
426
|
+
if (base.kind === "unknown" && isRelativeCandidate(candidate)) {
|
|
427
|
+
const lexical = normalizePathForComparison(candidate, cwd);
|
|
428
|
+
const canonical = canonicalizePath(lexical);
|
|
429
|
+
if (
|
|
430
|
+
canonical &&
|
|
431
|
+
normalizedCwd !== "" &&
|
|
432
|
+
!isSafeSystemPath(canonical) &&
|
|
433
|
+
!seen.has(canonical)
|
|
434
|
+
) {
|
|
435
|
+
seen.add(canonical);
|
|
436
|
+
// The factory recomputes the canonical via canonicalNormalizePathForComparison
|
|
437
|
+
// (win32-lowercased, #382) rather than reusing the raw canonicalizePath output.
|
|
438
|
+
externalPaths.push(AccessPath.forExternalDirectory(lexical, cwd));
|
|
439
|
+
}
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const resolveBase = base.kind === "known" ? resolve(cwd, base.offset) : cwd;
|
|
444
|
+
const lexical = normalizePathForComparison(candidate, resolveBase);
|
|
445
|
+
if (!lexical) continue;
|
|
446
|
+
// The boundary decision and dedup identity use the canonical
|
|
447
|
+
// (symlink-resolved) form, but the returned value is the lexical form so
|
|
448
|
+
// config patterns match the path as the user typed it (#418).
|
|
449
|
+
const canonical = canonicalizePath(lexical);
|
|
450
|
+
|
|
451
|
+
if (
|
|
452
|
+
normalizedCwd !== "" &&
|
|
453
|
+
!isSafeSystemPath(canonical) &&
|
|
454
|
+
!isPathWithinDirectory(canonical, normalizedCwd) &&
|
|
455
|
+
!seen.has(canonical)
|
|
456
|
+
) {
|
|
457
|
+
seen.add(canonical);
|
|
458
|
+
// The factory recomputes the canonical via canonicalNormalizePathForComparison
|
|
459
|
+
// (win32-lowercased, #382) rather than reusing the raw canonicalizePath output.
|
|
460
|
+
externalPaths.push(AccessPath.forExternalDirectory(lexical, cwd));
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return externalPaths;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Project a collection of path candidates into rule candidates with their
|
|
469
|
+
* cd-aware policy lookup values.
|
|
470
|
+
*
|
|
471
|
+
* Filters candidates through the broad path classifier
|
|
472
|
+
* (`classifyTokenAsRuleCandidate`) and pairs each qualifying token with its
|
|
473
|
+
* set of policy values (absolute + project-relative + raw).
|
|
474
|
+
* A token after a non-literal `cd` keeps only its literal value so no
|
|
475
|
+
* spurious absolute rule can match (#393).
|
|
476
|
+
*/
|
|
477
|
+
export function projectRuleCandidates(
|
|
478
|
+
candidates: readonly PathCandidate[],
|
|
479
|
+
cwd: string,
|
|
480
|
+
): BashPathRuleCandidate[] {
|
|
481
|
+
const seen = new Set<string>();
|
|
482
|
+
const result: BashPathRuleCandidate[] = [];
|
|
483
|
+
|
|
484
|
+
for (const { token, base } of candidates) {
|
|
485
|
+
const candidate = classifyTokenAsRuleCandidate(token);
|
|
486
|
+
if (!candidate) continue;
|
|
487
|
+
|
|
488
|
+
const policyValues = getPolicyValuesForRuleCandidate(candidate, base, cwd);
|
|
489
|
+
if (policyValues.length === 0) continue;
|
|
490
|
+
|
|
491
|
+
const key = policyValues.join("\0");
|
|
492
|
+
if (seen.has(key)) continue;
|
|
493
|
+
seen.add(key);
|
|
494
|
+
result.push({ token: candidate, policyValues });
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return result;
|
|
498
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { TSNode } from "#src/access-intent/bash/parser";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Node types whose subtrees must never be descended into for
|
|
5
|
+
* path extraction — their text content is not a command argument.
|
|
6
|
+
*/
|
|
7
|
+
export const SKIP_SUBTREE_TYPES = new Set([
|
|
8
|
+
"heredoc_body",
|
|
9
|
+
"heredoc_end",
|
|
10
|
+
"comment",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Node types that represent argument values in the AST
|
|
15
|
+
* (word, concatenation, single-quoted string, double-quoted string).
|
|
16
|
+
*/
|
|
17
|
+
export const ARG_NODE_TYPES = new Set([
|
|
18
|
+
"word",
|
|
19
|
+
"concatenation",
|
|
20
|
+
"string",
|
|
21
|
+
"raw_string",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolve the "shell value" of an argument node — the string the shell
|
|
26
|
+
* would pass to the command after quote removal.
|
|
27
|
+
*
|
|
28
|
+
* - `word` → `.text` (already unquoted)
|
|
29
|
+
* - `raw_string` → strip surrounding single quotes
|
|
30
|
+
* - `string` → strip surrounding double quotes, concatenate children text
|
|
31
|
+
* - `concatenation` → concatenate resolved children
|
|
32
|
+
* - other → `.text` as fallback
|
|
33
|
+
*/
|
|
34
|
+
export function resolveNodeText(node: TSNode): string {
|
|
35
|
+
switch (node.type) {
|
|
36
|
+
case "word":
|
|
37
|
+
return node.text;
|
|
38
|
+
case "raw_string": {
|
|
39
|
+
// Strip surrounding single quotes: 'content' → content
|
|
40
|
+
const t = node.text;
|
|
41
|
+
if (t.length >= 2 && t.startsWith("'") && t.endsWith("'")) {
|
|
42
|
+
return t.slice(1, -1);
|
|
43
|
+
}
|
|
44
|
+
return t;
|
|
45
|
+
}
|
|
46
|
+
case "string": {
|
|
47
|
+
// Double-quoted string: concatenate the resolved text of inner children,
|
|
48
|
+
// skipping the quote-delimiter nodes (literal `"`).
|
|
49
|
+
let result = "";
|
|
50
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
51
|
+
const child = node.child(i);
|
|
52
|
+
if (!child) continue;
|
|
53
|
+
// Skip the literal `"` delimiters
|
|
54
|
+
if (child.type === '"') continue;
|
|
55
|
+
result += resolveNodeText(child);
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
case "string_content":
|
|
60
|
+
case "simple_expansion":
|
|
61
|
+
case "expansion":
|
|
62
|
+
return node.text;
|
|
63
|
+
case "concatenation": {
|
|
64
|
+
let result = "";
|
|
65
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
66
|
+
const child = node.child(i);
|
|
67
|
+
if (!child) continue;
|
|
68
|
+
result += resolveNodeText(child);
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
default:
|
|
73
|
+
return node.text;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { memoizeAsyncWithRetry } from "#src/async-cache";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Minimal subset of web-tree-sitter's SyntaxNode used by the AST walker.
|
|
6
|
+
* Defined locally so callers do not need to import web-tree-sitter types.
|
|
7
|
+
*/
|
|
8
|
+
export interface TSNode {
|
|
9
|
+
readonly type: string;
|
|
10
|
+
readonly text: string;
|
|
11
|
+
readonly childCount: number;
|
|
12
|
+
/** False for anonymous tokens (operators, delimiters); true for named nodes. */
|
|
13
|
+
readonly isNamed: boolean;
|
|
14
|
+
child(index: number): TSNode | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Minimal subset of web-tree-sitter's Parser used by this module.
|
|
19
|
+
*/
|
|
20
|
+
interface TSParser {
|
|
21
|
+
parse(input: string): { rootNode: TSNode; delete(): void } | null;
|
|
22
|
+
delete(): void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function initParser(): Promise<TSParser> {
|
|
26
|
+
// Use named imports — web-tree-sitter exports Parser as a named class.
|
|
27
|
+
const { Parser, Language } = await import("web-tree-sitter");
|
|
28
|
+
const req = createRequire(import.meta.url);
|
|
29
|
+
const treeSitterWasm = req.resolve("web-tree-sitter/web-tree-sitter.wasm");
|
|
30
|
+
await Parser.init({ locateFile: () => treeSitterWasm });
|
|
31
|
+
|
|
32
|
+
const parser = new Parser();
|
|
33
|
+
const bashWasm = req.resolve("tree-sitter-bash/tree-sitter-bash.wasm");
|
|
34
|
+
const bash = await Language.load(bashWasm);
|
|
35
|
+
parser.setLanguage(bash);
|
|
36
|
+
return parser;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Memoize on success but drop a rejected result so a transient init failure
|
|
40
|
+
// (e.g. a slow WASM load) is retried on the next tool call instead of poisoning
|
|
41
|
+
// the parser for the process lifetime.
|
|
42
|
+
export const getParser = memoizeAsyncWithRetry(initParser);
|