@gotgenes/pi-permission-system 36.1.0 → 36.2.1
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 +26 -0
- package/docs/configuration.md +13 -3
- package/package.json +1 -1
- package/schemas/permissions.schema.json +1 -1
- package/src/access-intent/bash/bash-path-resolver.ts +22 -10
- package/src/access-intent/bash/command-enumeration.ts +48 -20
- package/src/access-intent/bash/node-text.ts +87 -31
- package/src/access-intent/bash/program.ts +8 -2
- package/src/access-intent/bash/shell-variable-expansion.ts +207 -10
- package/src/access-intent/bash/sync-commands.ts +11 -4
- package/src/access-intent/bash/token-collection.ts +66 -45
- package/src/authority/authorizer.ts +4 -0
- package/src/authority/local-user-authorizer.ts +15 -3
- package/src/authority/permission-prompt-component.ts +7 -2
- package/src/config/config-schema.ts +1 -1
- package/src/logging/command-redaction.ts +15 -9
- package/src/path/expand-home.ts +23 -7
- package/src/presentation/prompt-notification.ts +71 -10
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,32 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [36.2.1](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v36.2.0...pi-permission-system-v36.2.1) (2026-10-01)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* **pi-permission-system:** a reassigned $HOME or $PWD is no longer resolved to its startup value ([#995](https://github.com/gotgenes/pi-packages/issues/995)) ([956fa32](https://github.com/gotgenes/pi-packages/commit/956fa32ecbe6ee3b2954cebbfed8d9a44857c617)), closes [#995](https://github.com/gotgenes/pi-packages/issues/995)
|
|
14
|
+
* **pi-permission-system:** a name read, printf -v, nameref, eval, or source counts as reassigning $HOME ([#995](https://github.com/gotgenes/pi-packages/issues/995)) ([a05a6d3](https://github.com/gotgenes/pi-packages/commit/a05a6d391eb0281d5977a2264aa69958ce8d7549))
|
|
15
|
+
* **pi-permission-system:** ~ is no longer read as the home directory after the command reassigns HOME ([#995](https://github.com/gotgenes/pi-packages/issues/995)) ([998d51b](https://github.com/gotgenes/pi-packages/commit/998d51b36989bf0397beb4028d76e75d3e37666d))
|
|
16
|
+
* **pi-permission-system:** ~ may spell an option when the inherited HOME begins with a dash ([#995](https://github.com/gotgenes/pi-packages/issues/995)) ([f1ee69a](https://github.com/gotgenes/pi-packages/commit/f1ee69a19b3f6880228da88ad2683f9b1f79a980))
|
|
17
|
+
* **pi-permission-system:** a quoted export, let, trap, or quoted eval counts as reassigning $HOME ([#995](https://github.com/gotgenes/pi-packages/issues/995)) ([4406ef4](https://github.com/gotgenes/pi-packages/commit/4406ef429706aa0b7cd77ba9608d0bcb1c2982bc))
|
|
18
|
+
|
|
19
|
+
### Documentation
|
|
20
|
+
|
|
21
|
+
* **pi-permission-system:** record that a reassigned $HOME or $PWD is not resolved ([#995](https://github.com/gotgenes/pi-packages/issues/995)) ([e7c8b88](https://github.com/gotgenes/pi-packages/commit/e7c8b88167f05531507cdfaf584dd9e4905a0387))
|
|
22
|
+
|
|
23
|
+
## [36.2.0](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v36.1.0...pi-permission-system-v36.2.0) (2026-09-30)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
* name the session and the requested tool in prompt notifications ([#996](https://github.com/gotgenes/pi-packages/issues/996)) ([eb49c64](https://github.com/gotgenes/pi-packages/commit/eb49c6494d12749f71130d74b75c578530c89afa)), closes [#996](https://github.com/gotgenes/pi-packages/issues/996)
|
|
29
|
+
|
|
30
|
+
### Documentation
|
|
31
|
+
|
|
32
|
+
* describe the session and tool in prompt notifications ([#996](https://github.com/gotgenes/pi-packages/issues/996)) ([c5c74db](https://github.com/gotgenes/pi-packages/commit/c5c74db43235297a39bbe94303225ac50baf0ca4))
|
|
33
|
+
|
|
8
34
|
## [36.1.0](https://github.com/gotgenes/pi-packages/compare/pi-permission-system-v36.0.0...pi-permission-system-v36.1.0) (2026-09-30)
|
|
9
35
|
|
|
10
36
|
|
package/docs/configuration.md
CHANGED
|
@@ -208,7 +208,17 @@ The two OSC forms raise a desktop notification in terminals that implement them;
|
|
|
208
208
|
|
|
209
209
|
Channels are written in the order listed.
|
|
210
210
|
The list is empty by default, so nothing is emitted until you set it; an unknown channel name is a validation error like any other malformed field.
|
|
211
|
-
The notification
|
|
211
|
+
The notification names the session and what is being asked, so it tells you which pane to go to.
|
|
212
|
+
Its title is `pi — <session name>`, or `pi — <directory name>` for a session without a name.
|
|
213
|
+
Its body is the dialog title followed by the tool (or, for an ask that is not a tool call, the gate surface) and the requesting agent when there is one:
|
|
214
|
+
|
|
215
|
+
| Ask | `osc777` title | `osc777` body |
|
|
216
|
+
| ------------------------------------------- | -------------------- | ---------------------------------------------- |
|
|
217
|
+
| local `bash` ask in session `refactor-auth` | `pi — refactor-auth` | `Permission Required: bash` |
|
|
218
|
+
| `read` forwarded from subagent `scout` | `pi — refactor-auth` | `Permission Required (Subagent): read (scout)` |
|
|
219
|
+
|
|
220
|
+
OSC 9 has a single text field, so `osc9` writes the title ahead of the body: `pi — refactor-auth: Permission Required: bash`.
|
|
221
|
+
The notification carries labels only, never the command, path, MCP target, or skill being decided, so nothing the request holds lands in your notification history.
|
|
212
222
|
|
|
213
223
|
The signal fires once per prompt, when the prompt is actually shown.
|
|
214
224
|
A second ask that waits behind an open dialog signals when its own turn comes, and a forwarded subagent ask signals in the parent session that shows it.
|
|
@@ -775,11 +785,11 @@ Quoting is understood, so `ls "$HOME/x"` and `ls $HOME/x` are treated alike.
|
|
|
775
785
|
What the bash projection resolves:
|
|
776
786
|
|
|
777
787
|
- Absolute, home-relative (`~/`), parent-traversal (`../`), and separator-bearing tokens, plus redirect targets (`> out.txt`, including a file the redirect creates) and values embedded in long options (`--file=/tmp/patterns`).
|
|
778
|
-
- The plain shell variables `$HOME` / `${HOME}` and `$PWD` / `${PWD}`, so `$HOME/x` is gated exactly as `~/x` and the literal absolute spelling, whether or not the target exists.
|
|
788
|
+
- The plain shell variables `$HOME` / `${HOME}` and `$PWD` / `${PWD}`, so `$HOME/x` is gated exactly as `~/x` and the literal absolute spelling, whether or not the target exists, as long as the command does not reassign them.
|
|
779
789
|
- Relative tokens, against the working directory produced by folding literal current-shell `cd` commands.
|
|
780
790
|
- A bare token (`cat id_rsa`) when it names an existing filesystem entry.
|
|
781
791
|
|
|
782
|
-
What it deliberately does not resolve: any other variable (`$CONFIG_DIR`), a command substitution (`$(cmd)`), an expansion carrying an operator (`${HOME:-/tmp}`),
|
|
792
|
+
What it deliberately does not resolve: any other variable (`$CONFIG_DIR`), a command substitution (`$(cmd)`), an expansion carrying an operator (`${HOME:-/tmp}`), a variable reached through an assignment (`CURRENT="$HOME"; ls "$CURRENT"`), and `$HOME`, `$PWD`, or a leading `~` in a command that reassigns the variable anywhere (`HOME=/etc; cat "$HOME/shadow"`), including through `read`, `unset`, `eval`, or `source`.
|
|
783
793
|
A non-literal `cd` (`cd "$DIR"`) makes the working directory unknown, after which relative tokens are kept literal rather than resolved against a guess.
|
|
784
794
|
Commands whose payload is opaque (`bash -c`, `eval`, `sudo`, `xargs`) are floored to `ask` instead of projected.
|
|
785
795
|
The governing record is [ADR 0009](https://github.com/gotgenes/pi-packages/blob/main/packages/pi-permission-system/docs/decisions/0009-bash-path-projection-completeness-contract.md), which states what the projection guarantees and which gaps are accepted residuals rather than bugs.
|
package/package.json
CHANGED
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
},
|
|
71
71
|
"promptNotifications": {
|
|
72
72
|
"description": "Terminal notifications to emit when the inline permission dialog opens: bell (BEL), osc9 (OSC 9 notification), osc777 (OSC 777 notification). Omit or leave empty for none. TUI sessions only.",
|
|
73
|
-
"markdownDescription": "Terminal notifications to emit when the inline permission dialog opens, so a terminal or multiplexer can flag a session waiting on a decision.\n\n- `\"bell\"` writes a BEL — what tmux `monitor-bell` and most terminals react to.\n- `\"osc9\"` writes an OSC 9 desktop notification.\n- `\"osc777\"` writes an OSC 777 desktop notification.\n\nOmit or leave empty for none (the default). Applies to interactive **TUI** sessions only
|
|
73
|
+
"markdownDescription": "Terminal notifications to emit when the inline permission dialog opens, so a terminal or multiplexer can flag a session waiting on a decision.\n\n- `\"bell\"` writes a BEL — what tmux `monitor-bell` and most terminals react to.\n- `\"osc9\"` writes an OSC 9 desktop notification.\n- `\"osc777\"` writes an OSC 777 desktop notification.\n\nOmit or leave empty for none (the default). Applies to interactive **TUI** sessions only. The notification names the session and the requested tool and agent, never the command, path, or other value being decided.",
|
|
74
74
|
"default": [],
|
|
75
75
|
"type": "array",
|
|
76
76
|
"items": {
|
|
@@ -7,7 +7,11 @@ import {
|
|
|
7
7
|
import { normalizePathPolicyLiteral } from "#src/access-intent/path-normalization";
|
|
8
8
|
import type { PathNormalizer } from "#src/path/path-normalizer";
|
|
9
9
|
import { isSafeSystemPath } from "#src/path/safe-system-paths";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
ARG_NODE_TYPES,
|
|
12
|
+
SKIP_SUBTREE_TYPES,
|
|
13
|
+
type WordReader,
|
|
14
|
+
} from "./node-text";
|
|
11
15
|
import type { TSNode } from "./parser";
|
|
12
16
|
import { REDIRECT_NODE_TYPES } from "./redirect-analysis";
|
|
13
17
|
import {
|
|
@@ -121,6 +125,7 @@ const UNKNOWN_BASE: EffectiveBase = { kind: "unknown" };
|
|
|
121
125
|
export class BashPathResolver {
|
|
122
126
|
constructor(
|
|
123
127
|
private readonly normalizer: PathNormalizer,
|
|
128
|
+
private readonly words: WordReader,
|
|
124
129
|
private readonly workdir?: string,
|
|
125
130
|
) {}
|
|
126
131
|
|
|
@@ -146,6 +151,10 @@ export class BashPathResolver {
|
|
|
146
151
|
* could then allow this access. #393's unknown base declines the claim
|
|
147
152
|
* instead, keeping an absolute token literal-only and unconditionally
|
|
148
153
|
* external while a relative one is not projected at all.
|
|
154
|
+
*
|
|
155
|
+
* A token spelled from a `HOME` the program rebinds leaves both slices before
|
|
156
|
+
* projection: path normalization would otherwise expand its `~`/`$HOME` prefix
|
|
157
|
+
* to the startup home, naming a directory the shell never touches.
|
|
149
158
|
*/
|
|
150
159
|
resolve(
|
|
151
160
|
rootNode: TSNode,
|
|
@@ -155,10 +164,13 @@ export class BashPathResolver {
|
|
|
155
164
|
this.workdir === undefined
|
|
156
165
|
? CWD_BASE
|
|
157
166
|
: this.deriveBaseFromCdTarget(CWD_BASE, this.workdir);
|
|
158
|
-
const
|
|
167
|
+
const collected = this.collectPathCandidates(rootNode, initialBase);
|
|
159
168
|
for (const salvaged of salvagedRoots) {
|
|
160
|
-
this.walkForCandidates(salvaged, UNKNOWN_BASE,
|
|
169
|
+
this.walkForCandidates(salvaged, UNKNOWN_BASE, collected);
|
|
161
170
|
}
|
|
171
|
+
const candidates = collected.filter(
|
|
172
|
+
({ token }) => !this.words.spellsReboundHome(token),
|
|
173
|
+
);
|
|
162
174
|
return {
|
|
163
175
|
externalAccesses: this.withWorkdirExternal(
|
|
164
176
|
this.projectExternalPaths(candidates),
|
|
@@ -236,7 +248,7 @@ export class BashPathResolver {
|
|
|
236
248
|
case "redirected_statement":
|
|
237
249
|
return this.walkCurrentShellSequence(node, base, out);
|
|
238
250
|
case "command":
|
|
239
|
-
tagTokens(collectCommandTokens(node), base, out);
|
|
251
|
+
tagTokens(collectCommandTokens(node, this.words), base, out);
|
|
240
252
|
return this.foldCd(node, base);
|
|
241
253
|
case "pipeline":
|
|
242
254
|
// tree-sitter-bash mis-groups a redirect-bearing `&&`/`;` list as the
|
|
@@ -261,7 +273,7 @@ export class BashPathResolver {
|
|
|
261
273
|
// substitution interiors: collect every candidate in the subtree tagged
|
|
262
274
|
// with the enclosing base and do not fold their internal `cd`s. (Folding
|
|
263
275
|
// inside substitutions is deferred — conservative, never under-flags.)
|
|
264
|
-
tagTokens(collectPathCandidateTokens(node), base, out);
|
|
276
|
+
tagTokens(collectPathCandidateTokens(node, this.words), base, out);
|
|
265
277
|
return base;
|
|
266
278
|
}
|
|
267
279
|
}
|
|
@@ -321,7 +333,7 @@ export class BashPathResolver {
|
|
|
321
333
|
}
|
|
322
334
|
// Downstream stage (after a `|`): subshell — collect against the folded
|
|
323
335
|
// base, do not fold.
|
|
324
|
-
tagTokens(collectPathCandidateTokens(child), current, out);
|
|
336
|
+
tagTokens(collectPathCandidateTokens(child, this.words), current, out);
|
|
325
337
|
}
|
|
326
338
|
return current;
|
|
327
339
|
}
|
|
@@ -351,7 +363,7 @@ export class BashPathResolver {
|
|
|
351
363
|
if (child.type === "file_redirect") {
|
|
352
364
|
// Redirect destinations are part of the piped stage; collect them
|
|
353
365
|
// against the folded base without folding.
|
|
354
|
-
tagTokens(collectRedirectTokens(child), current, out);
|
|
366
|
+
tagTokens(collectRedirectTokens(child, this.words), current, out);
|
|
355
367
|
continue;
|
|
356
368
|
}
|
|
357
369
|
// The inner statement is the `list`/`command` being redirected; fold its
|
|
@@ -361,7 +373,7 @@ export class BashPathResolver {
|
|
|
361
373
|
return current;
|
|
362
374
|
}
|
|
363
375
|
// Bare `command` or any other shape: a true subshell first stage.
|
|
364
|
-
tagTokens(collectPathCandidateTokens(node), base, out);
|
|
376
|
+
tagTokens(collectPathCandidateTokens(node, this.words), base, out);
|
|
365
377
|
return base;
|
|
366
378
|
}
|
|
367
379
|
|
|
@@ -389,7 +401,7 @@ export class BashPathResolver {
|
|
|
389
401
|
current = this.walkForCandidates(child, current, out);
|
|
390
402
|
} else {
|
|
391
403
|
// Terminal child = the real pipe stage; collect without folding.
|
|
392
|
-
tagTokens(collectPathCandidateTokens(child), current, out);
|
|
404
|
+
tagTokens(collectPathCandidateTokens(child, this.words), current, out);
|
|
393
405
|
}
|
|
394
406
|
}
|
|
395
407
|
return current;
|
|
@@ -412,7 +424,7 @@ export class BashPathResolver {
|
|
|
412
424
|
* {@link PathNormalizer}; this method owns only the base-folding state.
|
|
413
425
|
*/
|
|
414
426
|
private foldCd(commandNode: TSNode, base: EffectiveBase): EffectiveBase {
|
|
415
|
-
if (extractCommandName(commandNode) !== "cd") return base;
|
|
427
|
+
if (extractCommandName(commandNode, this.words) !== "cd") return base;
|
|
416
428
|
const target = cdLiteralTarget(commandNode);
|
|
417
429
|
if (target === null) return UNKNOWN_BASE;
|
|
418
430
|
return this.deriveBaseFromCdTarget(base, target);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BashCommandContext, FloorExemption } from "#src/types";
|
|
2
2
|
import { EXECUTION_HOST_TYPES, forEachExecutionIn } from "./nested-execution";
|
|
3
|
-
import {
|
|
3
|
+
import type { WordReader } from "./node-text";
|
|
4
4
|
import { parseUnresolvedWithin } from "./parse-health";
|
|
5
5
|
import type { TSNode } from "./parser";
|
|
6
6
|
import { REDIRECT_NODE_TYPES, redirectMayWriteFile } from "./redirect-analysis";
|
|
@@ -105,14 +105,22 @@ interface UnitScope {
|
|
|
105
105
|
* everything found inside a salvaged region is salvaged.
|
|
106
106
|
*/
|
|
107
107
|
readonly salvaged: boolean;
|
|
108
|
+
/**
|
|
109
|
+
* How the program's shell expands an argument word. Relayed unchanged: a
|
|
110
|
+
* program's variables are the same wherever in it a word sits.
|
|
111
|
+
*/
|
|
112
|
+
readonly words: WordReader;
|
|
108
113
|
}
|
|
109
114
|
|
|
110
115
|
/** A top-level command in the current shell, writing no file, fully parsed. */
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
+
function topLevelScope(words: WordReader): UnitScope {
|
|
117
|
+
return {
|
|
118
|
+
writesViaRedirect: false,
|
|
119
|
+
parseUnresolved: false,
|
|
120
|
+
salvaged: false,
|
|
121
|
+
words,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
116
124
|
|
|
117
125
|
/**
|
|
118
126
|
* The scope a salvaged region's own units run under.
|
|
@@ -123,11 +131,14 @@ const TOP_LEVEL_SCOPE: UnitScope = {
|
|
|
123
131
|
* {@link collectHostedCommands} resets it: a redirect established outside the
|
|
124
132
|
* region is the enclosing statement's, not the region's.
|
|
125
133
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
134
|
+
function salvagedScope(words: WordReader): UnitScope {
|
|
135
|
+
return {
|
|
136
|
+
writesViaRedirect: false,
|
|
137
|
+
parseUnresolved: true,
|
|
138
|
+
salvaged: true,
|
|
139
|
+
words,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
131
142
|
|
|
132
143
|
// ── Node-type vocabulary ─────────────────────────────────────────────────────
|
|
133
144
|
|
|
@@ -244,9 +255,12 @@ const STATEMENT_TYPES = new Set([
|
|
|
244
255
|
* wrapper unit (`bash -c`/`eval`, or an indirection wrapper such as `sudo`) is
|
|
245
256
|
* tagged with a {@link WrapperKind} so its decision is later floored to `ask`.
|
|
246
257
|
*/
|
|
247
|
-
export function collectCommands(
|
|
258
|
+
export function collectCommands(
|
|
259
|
+
node: TSNode,
|
|
260
|
+
words: WordReader,
|
|
261
|
+
): BashCommand[] {
|
|
248
262
|
const out: BashCommand[] = [];
|
|
249
|
-
collectCommandsInto(node,
|
|
263
|
+
collectCommandsInto(node, topLevelScope(words), out);
|
|
250
264
|
return out;
|
|
251
265
|
}
|
|
252
266
|
|
|
@@ -260,9 +274,12 @@ export function collectCommands(node: TSNode): BashCommand[] {
|
|
|
260
274
|
* explicit `deny` fires — while its `allow` is still floored to `ask` by the
|
|
261
275
|
* verdict fold (#840).
|
|
262
276
|
*/
|
|
263
|
-
export function collectSalvagedCommands(
|
|
277
|
+
export function collectSalvagedCommands(
|
|
278
|
+
node: TSNode,
|
|
279
|
+
words: WordReader,
|
|
280
|
+
): BashCommand[] {
|
|
264
281
|
const out: BashCommand[] = [];
|
|
265
|
-
collectCommandsInto(node,
|
|
282
|
+
collectCommandsInto(node, salvagedScope(words), out);
|
|
266
283
|
return out;
|
|
267
284
|
}
|
|
268
285
|
|
|
@@ -282,11 +299,14 @@ export function collectSalvagedCommands(node: TSNode): BashCommand[] {
|
|
|
282
299
|
* (`python3 -c`, `node -e`) out: its payload is another language, so re-parsing
|
|
283
300
|
* it as bash would read a secret out of embedded Python.
|
|
284
301
|
*/
|
|
285
|
-
export function inlineShellPayloadNode(
|
|
302
|
+
export function inlineShellPayloadNode(
|
|
303
|
+
command: TSNode,
|
|
304
|
+
words: WordReader,
|
|
305
|
+
): TSNode | null {
|
|
286
306
|
const nodes = commandWordNodes(command);
|
|
287
307
|
const index = inlineShellPayloadIndex(
|
|
288
308
|
nodes.map((node) => ({
|
|
289
|
-
...
|
|
309
|
+
...words.argWord(node),
|
|
290
310
|
text: node.text,
|
|
291
311
|
offset: node.startIndex,
|
|
292
312
|
})),
|
|
@@ -426,7 +446,7 @@ function makeUnit(
|
|
|
426
446
|
* surely as one on the enclosing statement.
|
|
427
447
|
*/
|
|
428
448
|
function makeCommandUnit(node: TSNode, scope: UnitScope): BashCommand {
|
|
429
|
-
const { text, words } = readCommandUnit(node);
|
|
449
|
+
const { text, words } = readCommandUnit(node, scope.words);
|
|
430
450
|
return makeUnit(text, scope, {
|
|
431
451
|
wrapperKind: classifyWrapperWords(words),
|
|
432
452
|
executedUnit: executedUnitOf(text, words) ?? undefined,
|
|
@@ -482,7 +502,10 @@ function redirectedScope(node: TSNode, scope: UnitScope): UnitScope {
|
|
|
482
502
|
* A pure assignment (`FOO=bar`, no `command_name`) runs no command, has no
|
|
483
503
|
* words, and keeps its whole text.
|
|
484
504
|
*/
|
|
485
|
-
function readCommandUnit(
|
|
505
|
+
function readCommandUnit(
|
|
506
|
+
node: TSNode,
|
|
507
|
+
reader: WordReader,
|
|
508
|
+
): {
|
|
486
509
|
text: string;
|
|
487
510
|
words: CommandWord[];
|
|
488
511
|
} {
|
|
@@ -495,7 +518,11 @@ function readCommandUnit(node: TSNode): {
|
|
|
495
518
|
let previous: TSNode | undefined;
|
|
496
519
|
for (const word of nodes) {
|
|
497
520
|
if (previous) text += gapBetween(node, previous, word, redirects);
|
|
498
|
-
words.push({
|
|
521
|
+
words.push({
|
|
522
|
+
...reader.argWord(word),
|
|
523
|
+
text: word.text,
|
|
524
|
+
offset: text.length,
|
|
525
|
+
});
|
|
499
526
|
text += word.text;
|
|
500
527
|
previous = word;
|
|
501
528
|
}
|
|
@@ -626,6 +653,7 @@ function collectHostedCommands(
|
|
|
626
653
|
writesViaRedirect: false,
|
|
627
654
|
parseUnresolved: false,
|
|
628
655
|
salvaged: scope.salvaged,
|
|
656
|
+
words: scope.words,
|
|
629
657
|
},
|
|
630
658
|
out,
|
|
631
659
|
);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSNode } from "./parser";
|
|
2
|
-
import {
|
|
2
|
+
import type { ShellVariables } from "./shell-variable-expansion";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Node types whose text content is never a command argument, so no path
|
|
@@ -35,13 +35,13 @@ export const ARG_NODE_TYPES = new Set([
|
|
|
35
35
|
* since a word only the shell decides can spell any option at all.
|
|
36
36
|
*/
|
|
37
37
|
export interface ArgWord {
|
|
38
|
-
/** The string the shell passes after quote removal ({@link
|
|
38
|
+
/** The string the shell passes after quote removal ({@link resolveText}). */
|
|
39
39
|
readonly value: string;
|
|
40
40
|
/**
|
|
41
41
|
* Whether `value` may differ from what the program receives: a part only
|
|
42
|
-
* running the command decides ({@link
|
|
42
|
+
* running the command decides ({@link computedPart}), or a spelling the
|
|
43
43
|
* shell rewrites before the program sees it — an escape, a glob, a brace
|
|
44
|
-
* expansion, an ANSI-C string — which {@link
|
|
44
|
+
* expansion, an ANSI-C string — which {@link resolveText} passes through as written.
|
|
45
45
|
*/
|
|
46
46
|
readonly computed: boolean;
|
|
47
47
|
/**
|
|
@@ -53,17 +53,67 @@ export interface ArgWord {
|
|
|
53
53
|
readonly mayLeadWithDash: boolean;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
/**
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Reads argument nodes as one program's shell expands them: the node-text
|
|
58
|
+
* reads bound to the {@link ShellVariables} that program rebinds.
|
|
59
|
+
*/
|
|
60
|
+
export class WordReader {
|
|
61
|
+
constructor(private readonly variables: ShellVariables) {}
|
|
62
|
+
|
|
63
|
+
/** Read an argument node into the word the program receives. */
|
|
64
|
+
argWord(node: TSNode): ArgWord {
|
|
65
|
+
const value = this.text(node);
|
|
66
|
+
const tilde = this.variables.readTilde(leadingUnquotedText(node));
|
|
67
|
+
if (tilde?.known === false) {
|
|
68
|
+
return { value, computed: true, mayLeadWithDash: true };
|
|
69
|
+
}
|
|
70
|
+
const computed =
|
|
71
|
+
this.isComputed(node) || !isSpelledExactly(node, this.variables);
|
|
72
|
+
if (tilde) {
|
|
73
|
+
// The spelling `~` is not what the program receives; it is exact enough
|
|
74
|
+
// to prove with only while the home it stands for cannot be an option.
|
|
75
|
+
return {
|
|
76
|
+
value,
|
|
77
|
+
computed: computed || tilde.leadsWithDash,
|
|
78
|
+
mayLeadWithDash:
|
|
79
|
+
tilde.leadsWithDash || (computed && maySplitIntoWords(node, false)),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
value,
|
|
84
|
+
computed,
|
|
85
|
+
mayLeadWithDash: computed
|
|
86
|
+
? mayExpandToDashWord(node)
|
|
87
|
+
: value.startsWith("-"),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The string the shell passes after quote removal ({@link resolveText}). */
|
|
92
|
+
text(node: TSNode): string {
|
|
93
|
+
return resolveText(node, this.variables);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Whether the node's value is decided at run time ({@link computedPart}). */
|
|
97
|
+
isComputed(node: TSNode): boolean {
|
|
98
|
+
return computedPart(node, this.variables);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Whether a collected token is spelled from a rebound `HOME` ({@link ShellVariables.spellsReboundHome}). */
|
|
102
|
+
spellsReboundHome(token: string): boolean {
|
|
103
|
+
return this.variables.spellsReboundHome(token);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The unquoted literal an argument opens with, where bash expands a tilde
|
|
109
|
+
* prefix: the word itself, or a concatenation's first part when that is a
|
|
110
|
+
* word. A tilde after any other part, or inside quotes, stays literal.
|
|
111
|
+
*/
|
|
112
|
+
function leadingUnquotedText(node: TSNode): string {
|
|
113
|
+
if (node.type === "word") return node.text;
|
|
114
|
+
if (node.type !== "concatenation") return "";
|
|
115
|
+
const first = node.child(0);
|
|
116
|
+
return first?.type === "word" ? first.text : "";
|
|
67
117
|
}
|
|
68
118
|
|
|
69
119
|
/**
|
|
@@ -182,12 +232,12 @@ function quotedLiteralMayLeadWithDash(text: string): boolean | undefined {
|
|
|
182
232
|
}
|
|
183
233
|
|
|
184
234
|
/**
|
|
185
|
-
* Whether {@link
|
|
235
|
+
* Whether {@link resolveText} returns exactly the string the shell passes.
|
|
186
236
|
*
|
|
187
237
|
* Answers `false` for any node type it does not know, which is the
|
|
188
238
|
* fail-closed direction for a caller proving what a word cannot be.
|
|
189
239
|
*/
|
|
190
|
-
function isSpelledExactly(node: TSNode): boolean {
|
|
240
|
+
function isSpelledExactly(node: TSNode, variables: ShellVariables): boolean {
|
|
191
241
|
switch (node.type) {
|
|
192
242
|
case "raw_string":
|
|
193
243
|
return true;
|
|
@@ -197,27 +247,33 @@ function isSpelledExactly(node: TSNode): boolean {
|
|
|
197
247
|
return !node.text.includes("\\");
|
|
198
248
|
case "simple_expansion":
|
|
199
249
|
case "expansion":
|
|
200
|
-
return
|
|
250
|
+
return variables.resolveReference(node) !== null;
|
|
201
251
|
case "string":
|
|
202
252
|
// A digit run is an ordinary word to the shell; the grammar's `10#$x` form
|
|
203
253
|
// carries an expansion child, which answers for itself.
|
|
204
254
|
case "number":
|
|
205
|
-
return childrenSpelledExactly(node);
|
|
255
|
+
return childrenSpelledExactly(node, variables);
|
|
206
256
|
case "concatenation":
|
|
207
257
|
// The grammar splits `{-i,-n}` into plain words, so the expansion is
|
|
208
258
|
// visible only across the whole concatenation's text.
|
|
209
|
-
return
|
|
259
|
+
return (
|
|
260
|
+
!BRACE_EXPANSION.test(node.text) &&
|
|
261
|
+
childrenSpelledExactly(node, variables)
|
|
262
|
+
);
|
|
210
263
|
default:
|
|
211
264
|
return false;
|
|
212
265
|
}
|
|
213
266
|
}
|
|
214
267
|
|
|
215
268
|
/** A `"` delimiter is spelled exactly; every named child must be too. */
|
|
216
|
-
function childrenSpelledExactly(
|
|
269
|
+
function childrenSpelledExactly(
|
|
270
|
+
node: TSNode,
|
|
271
|
+
variables: ShellVariables,
|
|
272
|
+
): boolean {
|
|
217
273
|
for (let i = 0; i < node.childCount; i++) {
|
|
218
274
|
const child = node.child(i);
|
|
219
275
|
if (!child || child.type === '"') continue;
|
|
220
|
-
if (!isSpelledExactly(child)) return false;
|
|
276
|
+
if (!isSpelledExactly(child, variables)) return false;
|
|
221
277
|
}
|
|
222
278
|
return true;
|
|
223
279
|
}
|
|
@@ -236,22 +292,22 @@ const BRACE_EXPANSION = /\{[^}]*(,|\.\.)[^}]*\}/;
|
|
|
236
292
|
/**
|
|
237
293
|
* Whether an argument node's value is decided at run time: it contains a
|
|
238
294
|
* command or process substitution, an arithmetic expansion, or a variable
|
|
239
|
-
* expansion {@link
|
|
295
|
+
* expansion {@link ShellVariables.resolveReference} cannot resolve.
|
|
240
296
|
*
|
|
241
|
-
* The complement of what {@link
|
|
297
|
+
* The complement of what {@link resolveText} can spell exactly. A plain
|
|
242
298
|
* `$HOME` / `$PWD` reference resolves, so `"$HOME/out"` is not computed; any
|
|
243
299
|
* other expansion falls back to its own source text there, which names a file
|
|
244
300
|
* that is not the one the shell will touch (ADR 0009's computed-path residual).
|
|
245
301
|
* A single-quoted `'$x'` is a literal.
|
|
246
302
|
*/
|
|
247
|
-
|
|
303
|
+
function computedPart(node: TSNode, variables: ShellVariables): boolean {
|
|
248
304
|
if (COMPUTED_NODE_TYPES.has(node.type)) return true;
|
|
249
305
|
if (VARIABLE_EXPANSION_TYPES.has(node.type)) {
|
|
250
|
-
return
|
|
306
|
+
return variables.resolveReference(node) === null;
|
|
251
307
|
}
|
|
252
308
|
for (let i = 0; i < node.childCount; i++) {
|
|
253
309
|
const child = node.child(i);
|
|
254
|
-
if (child &&
|
|
310
|
+
if (child && computedPart(child, variables)) return true;
|
|
255
311
|
}
|
|
256
312
|
return false;
|
|
257
313
|
}
|
|
@@ -281,7 +337,7 @@ const VARIABLE_EXPANSION_TYPES: ReadonlySet<string> = new Set([
|
|
|
281
337
|
* else `.text` (see `shell-variable-expansion.ts`)
|
|
282
338
|
* - other → `.text` as fallback
|
|
283
339
|
*/
|
|
284
|
-
|
|
340
|
+
function resolveText(node: TSNode, variables: ShellVariables): string {
|
|
285
341
|
switch (node.type) {
|
|
286
342
|
case "word":
|
|
287
343
|
return node.text;
|
|
@@ -302,7 +358,7 @@ export function resolveNodeText(node: TSNode): string {
|
|
|
302
358
|
if (!child) continue;
|
|
303
359
|
// Skip the literal `"` delimiters
|
|
304
360
|
if (child.type === '"') continue;
|
|
305
|
-
result +=
|
|
361
|
+
result += resolveText(child, variables);
|
|
306
362
|
}
|
|
307
363
|
return result;
|
|
308
364
|
}
|
|
@@ -310,13 +366,13 @@ export function resolveNodeText(node: TSNode): string {
|
|
|
310
366
|
return node.text;
|
|
311
367
|
case "simple_expansion":
|
|
312
368
|
case "expansion":
|
|
313
|
-
return
|
|
369
|
+
return variables.resolveReference(node) ?? node.text;
|
|
314
370
|
case "concatenation": {
|
|
315
371
|
let result = "";
|
|
316
372
|
for (let i = 0; i < node.childCount; i++) {
|
|
317
373
|
const child = node.child(i);
|
|
318
374
|
if (!child) continue;
|
|
319
|
-
result +=
|
|
375
|
+
result += resolveText(child, variables);
|
|
320
376
|
}
|
|
321
377
|
return result;
|
|
322
378
|
}
|
|
@@ -9,7 +9,9 @@ import {
|
|
|
9
9
|
collectCommands,
|
|
10
10
|
collectSalvagedCommands,
|
|
11
11
|
} from "./command-enumeration";
|
|
12
|
+
import { WordReader } from "./node-text";
|
|
12
13
|
import { getParser } from "./parser";
|
|
14
|
+
import { ShellVariables } from "./shell-variable-expansion";
|
|
13
15
|
import { withSalvagedRoots } from "./unresolved-salvage";
|
|
14
16
|
|
|
15
17
|
export type { BashCommand, BashExternalPath, BashPathRuleCandidate };
|
|
@@ -62,15 +64,19 @@ export class BashProgram {
|
|
|
62
64
|
|
|
63
65
|
try {
|
|
64
66
|
return withSalvagedRoots(tree.rootNode, parser, (salvaged) => {
|
|
67
|
+
const words = new WordReader(
|
|
68
|
+
ShellVariables.scan([tree.rootNode, ...salvaged]),
|
|
69
|
+
);
|
|
65
70
|
const { externalAccesses, ruleCandidates } = new BashPathResolver(
|
|
66
71
|
normalizer,
|
|
72
|
+
words,
|
|
67
73
|
options?.workdir,
|
|
68
74
|
).resolve(tree.rootNode, salvaged);
|
|
69
75
|
return new BashProgram(
|
|
70
76
|
command,
|
|
71
77
|
[
|
|
72
|
-
...collectCommands(tree.rootNode),
|
|
73
|
-
...salvaged.flatMap(collectSalvagedCommands),
|
|
78
|
+
...collectCommands(tree.rootNode, words),
|
|
79
|
+
...salvaged.flatMap((root) => collectSalvagedCommands(root, words)),
|
|
74
80
|
],
|
|
75
81
|
externalAccesses,
|
|
76
82
|
ruleCandidates,
|