@andares/pi-toolkits 1.2.5 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +29 -14
- package/README.zh-CN.md +20 -10
- package/package.json +1 -1
- package/src/features/permission/policy.test.ts +11 -0
- package/src/features/permission/units.test.ts +168 -6
- package/src/features/permission/units.ts +441 -34
- package/src/features/uat/assets/index-CPTHXggT.js +1 -0
- package/src/features/uat/assets/index-D9S_sCVr.css +1 -0
- package/src/features/uat/assets/index.html +3 -3
- package/src/features/uat/index.test.ts +96 -3
- package/src/features/uat/index.ts +42 -12
- package/src/features/uat/item-tool.ts +36 -8
- package/src/features/uat/ledger.test.ts +170 -14
- package/src/features/uat/ledger.ts +127 -44
- package/src/features/uat/prompt.ts +41 -8
- package/src/features/uat/records.test.ts +14 -0
- package/src/features/uat/records.ts +13 -1
- package/src/features/uat/assets/index-D-W9zj0w.css +0 -1
- package/src/features/uat/assets/index-DCBQCsli.js +0 -1
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* `ask` for an opaque unit regardless of mode (fail closed).
|
|
18
18
|
*/
|
|
19
19
|
import { homedir } from "node:os";
|
|
20
|
-
import {
|
|
20
|
+
import { resolve } from "node:path";
|
|
21
21
|
import {
|
|
22
22
|
type ControlOperator,
|
|
23
23
|
type GlobPattern,
|
|
@@ -57,6 +57,19 @@ export interface CommandUnit {
|
|
|
57
57
|
|
|
58
58
|
const SENTINEL = "\u0000";
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Shell keywords and grouping words: structure, not commands. Left in place
|
|
62
|
+
* they parsed as unknown commands (`if`, `then`, `fi`, `{`, `}`), which is a
|
|
63
|
+
* write with the base directory as target — an outside-project ask for a
|
|
64
|
+
* read-only loop (audit 2026-11-20/2026-12-01: 2 asks). They are stripped at
|
|
65
|
+
* segment edges only, so an operand that happens to be `{` or `!` in the
|
|
66
|
+
* middle of a command is untouched.
|
|
67
|
+
*/
|
|
68
|
+
const STRUCTURAL_LEADING = new Set([
|
|
69
|
+
"if", "then", "else", "elif", "fi", "while", "until", "!", "{", "}",
|
|
70
|
+
]);
|
|
71
|
+
const STRUCTURAL_TRAILING = new Set(["}", "]]"]);
|
|
72
|
+
|
|
60
73
|
/**
|
|
61
74
|
* Device sinks: writing to them is a no-op, not a boundary decision. They are
|
|
62
75
|
* excluded whenever they appear as a plain operand (`curl -o /dev/null`),
|
|
@@ -89,6 +102,61 @@ const DATA_VALUE_OPTS: Record<string, ReadonlySet<string>> = {
|
|
|
89
102
|
]),
|
|
90
103
|
};
|
|
91
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Flags that name a file the command writes. Their value is a target even when
|
|
107
|
+
* it does not look like a path, and an unresolved value must fail closed:
|
|
108
|
+
* `curl -o "$X"` used to be dropped on the floor and judged as a write to the
|
|
109
|
+
* working directory, which is the wrong directory whenever `$X` points
|
|
110
|
+
* elsewhere.
|
|
111
|
+
*/
|
|
112
|
+
const WRITE_VALUE_OPTS: Record<string, ReadonlySet<string>> = {
|
|
113
|
+
curl: new Set([
|
|
114
|
+
"-o", "--output", "--output-dir", "-D", "--dump-header", "-c",
|
|
115
|
+
"--cookie-jar", "--stderr", "--trace", "--trace-ascii",
|
|
116
|
+
"--etag-save", "--libcurl",
|
|
117
|
+
]),
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Curl options whose value is a local file (or socket), not a URL operand.
|
|
122
|
+
* They need an explicit pass because the URL rule below skips unresolved
|
|
123
|
+
* operands: without it, `--cacert "$X/ca.pem"` or `--upload-file "$X/f"`
|
|
124
|
+
* would lose the fail-closed behaviour they had before that rule. The target
|
|
125
|
+
* keeps the command's existing effect, so `-T` does not silently change the
|
|
126
|
+
* policy it had before this table was added.
|
|
127
|
+
*/
|
|
128
|
+
const CURL_LOCAL_FILE_OPTS = new Set([
|
|
129
|
+
"-T", "--upload-file", "-K", "--config", "-E", "--cert",
|
|
130
|
+
"--proxy-cert", "--key", "--proxy-key", "--cacert", "--proxy-cacert",
|
|
131
|
+
"--capath", "--proxy-capath", "--crlfile", "--proxy-crlfile",
|
|
132
|
+
"--pubkey", "--pinnedpubkey", "--proxy-pinnedpubkey", "--netrc-file",
|
|
133
|
+
"--random-file", "--egd-file", "--unix-socket",
|
|
134
|
+
"--abstract-unix-socket", "--etag-compare",
|
|
135
|
+
]);
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Commands whose operands are URLs, not filesystem paths. A curl operand is
|
|
139
|
+
* fetched, never opened, so an unresolved one (`curl "$API$path"`,
|
|
140
|
+
* `curl "${H[@]}" …`) is an unknown URL rather than a path the boundary could
|
|
141
|
+
* judge (audit 2026-11-15 → 2026-11-25: 17 asks, all of them `allow`). The
|
|
142
|
+
* file side of curl is declared in {@link WRITE_VALUE_OPTS} (`-o`, `-D`, `-c`)
|
|
143
|
+
* and in {@link DATA_VALUE_OPTS}; `curl file:///…` reads are the same read
|
|
144
|
+
* blind spot as a bare filename operand.
|
|
145
|
+
*/
|
|
146
|
+
const URL_OPERAND_COMMANDS = new Set(["curl"]);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* `-o` / `--output` is an output path for most commands, but a plain flag for
|
|
150
|
+
* these: `find … -o -name x` and `grep -o PATTERN` were both treated as writes
|
|
151
|
+
* to an "output file" named after the next word (audit 2026-11-20/2026-12-02;
|
|
152
|
+
* the pattern-looking value even resolved through the glob fallback). The ssh
|
|
153
|
+
* family uses `-o Key=value` for config options.
|
|
154
|
+
*/
|
|
155
|
+
const DASH_O_IS_FLAG = new Set([
|
|
156
|
+
"find", "fd", "grep", "egrep", "fgrep", "rg", "ag", "ps", "mount",
|
|
157
|
+
"umount", "ssh", "scp", "sftp", "rsync", "tar",
|
|
158
|
+
]);
|
|
159
|
+
|
|
92
160
|
/* -------------------------------------------------------------------------- */
|
|
93
161
|
/* Read/write classification */
|
|
94
162
|
/* -------------------------------------------------------------------------- */
|
|
@@ -103,6 +171,7 @@ const READ_COMMANDS = new Set([
|
|
|
103
171
|
"readlink", "test", "[", "true", "false", "sleep", "seq", "bc", "expr",
|
|
104
172
|
"tput", "jq", "yq", "sort", "sed", "awk", "find", "fd", "nl", "rev",
|
|
105
173
|
"comm", "paste", "join", "split", "fold", "fmt", "pr", "od", "xxd",
|
|
174
|
+
"[[",
|
|
106
175
|
]);
|
|
107
176
|
|
|
108
177
|
/** Commands that mutate by name. */
|
|
@@ -671,14 +740,18 @@ function resolveTargetPath(
|
|
|
671
740
|
if (hasSentinel(word)) return undefined;
|
|
672
741
|
const expanded = expandHome(word);
|
|
673
742
|
if (/[*?[\]]/.test(expanded)) {
|
|
674
|
-
// A glob
|
|
675
|
-
//
|
|
743
|
+
// A glob names a set of children, so the decision target is the literal
|
|
744
|
+
// prefix it expands from, not the directory that contains that prefix:
|
|
745
|
+
// `rm -f /tmp/snap*.txt` used to resolve to `/tmp` and hit the
|
|
746
|
+
// "recursive delete of a temp root" table. A pattern with no literal part
|
|
747
|
+
// (`*.log`) still resolves to the directory itself, which is the
|
|
748
|
+
// conservative answer for a bare-name glob.
|
|
676
749
|
const prefix = expanded.slice(0, expanded.search(/[*?[\]]/));
|
|
677
|
-
const
|
|
678
|
-
if (!
|
|
750
|
+
const literal = prefix.includes("/") ? prefix : ".";
|
|
751
|
+
if (!literal.startsWith("/") && !literal.startsWith("~") && base === null) {
|
|
679
752
|
return undefined;
|
|
680
753
|
}
|
|
681
|
-
return resolve(base ?? "/", expandHome(
|
|
754
|
+
return resolve(base ?? "/", expandHome(literal));
|
|
682
755
|
}
|
|
683
756
|
if (expanded.startsWith("/")) return resolve(expanded);
|
|
684
757
|
if (base === null) return undefined;
|
|
@@ -716,6 +789,49 @@ const WRITE_OPERAND_VALUE_OPTS = new Set([
|
|
|
716
789
|
"--reference", "--mode",
|
|
717
790
|
]);
|
|
718
791
|
|
|
792
|
+
/**
|
|
793
|
+
* Extra flags whose next word is a value, per command. Without these, `scp -P
|
|
794
|
+
* 2158 src host:dst` collected `2158` as a local write target (audit
|
|
795
|
+
* 2026-11-20). Only flags that really take a value belong here: listing a
|
|
796
|
+
* boolean flag would swallow the following operand and hide a real target.
|
|
797
|
+
*/
|
|
798
|
+
const WRITE_COMMAND_VALUE_OPTS: Record<string, ReadonlySet<string>> = {
|
|
799
|
+
scp: new Set(["-P", "-i", "-F", "-o", "-c", "-l", "-S", "-J"]),
|
|
800
|
+
sftp: new Set(["-P", "-b", "-F", "-i", "-o", "-S", "-D", "-R", "-l"]),
|
|
801
|
+
rsync: new Set([
|
|
802
|
+
"-e", "--rsh", "--port", "--timeout", "--bwlimit", "--exclude",
|
|
803
|
+
"--include", "--filter", "--files-from", "--exclude-from",
|
|
804
|
+
"--include-from", "--log-file", "--partial-dir", "--temp-dir",
|
|
805
|
+
"--link-dest", "--compare-dest", "--copy-dest",
|
|
806
|
+
]),
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Copies normally read their sources: only the last operand (or the `-t`
|
|
811
|
+
* directory) is written. `mv` is excluded because it removes the source;
|
|
812
|
+
* `rsync --remove-source-files` and `install -d` receive the same exception.
|
|
813
|
+
*/
|
|
814
|
+
const SOURCE_IS_READ = new Set(["cp", "ln", "install", "rsync", "scp"]);
|
|
815
|
+
|
|
816
|
+
/** Commands where `-t DIR` names the destination directory. */
|
|
817
|
+
const TARGET_DIRECTORY_COMMANDS = new Set(["cp", "ln", "install", "mv"]);
|
|
818
|
+
const TARGET_DIRECTORY_OPTS = new Set(["-t", "--target-directory"]);
|
|
819
|
+
|
|
820
|
+
/** Commands whose operands may name a path on another machine. */
|
|
821
|
+
const REMOTE_COMMANDS = new Set(["scp", "sftp", "rsync", "ssh"]);
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* `host:path` (and `user@host:path`) is a path on the *remote* machine. Judging
|
|
825
|
+
* it as a local path made every `scp … host:/tmp/x` a write to a directory that
|
|
826
|
+
* does not exist; nothing on this filesystem is touched by that operand. Only
|
|
827
|
+
* the ssh family uses this form, so the check stays scoped to those commands.
|
|
828
|
+
*/
|
|
829
|
+
function isRemotePath(word: string): boolean {
|
|
830
|
+
if (/^[A-Za-z]:[\\/]/.test(word)) return false; // Windows drive
|
|
831
|
+
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(word)) return false; // URL
|
|
832
|
+
return /^[^/\s:]+:/.test(word) || /^\[[^\]]+\]:/.test(word);
|
|
833
|
+
}
|
|
834
|
+
|
|
719
835
|
/**
|
|
720
836
|
* grep-family commands whose **first positional operand is the pattern**, not a
|
|
721
837
|
* path. `grep -n '</div>\s*$' src/a.vue` used to collect the pattern as a file
|
|
@@ -773,6 +889,80 @@ function splitRedirects(tokens: string[]): {
|
|
|
773
889
|
return { words, redirects, processSubstitution };
|
|
774
890
|
}
|
|
775
891
|
|
|
892
|
+
/** The value of a declared option, including `--opt=value` and `-ovalue`. */
|
|
893
|
+
function declaredOptionValue(
|
|
894
|
+
args: readonly string[],
|
|
895
|
+
index: number,
|
|
896
|
+
options: ReadonlySet<string>,
|
|
897
|
+
): { value: string | undefined; next: number; option: string } | undefined {
|
|
898
|
+
const arg = args[index];
|
|
899
|
+
if (options.has(arg)) {
|
|
900
|
+
return { value: args[index + 1], next: index + 1, option: arg };
|
|
901
|
+
}
|
|
902
|
+
for (const option of options) {
|
|
903
|
+
if (option.startsWith("--") && arg.startsWith(`${option}=`)) {
|
|
904
|
+
return { value: arg.slice(option.length + 1), next: index, option };
|
|
905
|
+
}
|
|
906
|
+
if (option.length === 2 && arg.startsWith(option) && arg.length > 2) {
|
|
907
|
+
return { value: arg.slice(2), next: index, option };
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
return undefined;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/**
|
|
914
|
+
* Targets of a path-writing command (`cp`, `rm`, `mkdir`, `scp`, …), or the
|
|
915
|
+
* fail-closed note when one of its operands cannot be resolved.
|
|
916
|
+
*/
|
|
917
|
+
function writePathTargets(
|
|
918
|
+
command: string,
|
|
919
|
+
args: readonly string[],
|
|
920
|
+
base: string | null,
|
|
921
|
+
): UnitTarget[] | string {
|
|
922
|
+
const extra = WRITE_COMMAND_VALUE_OPTS[command];
|
|
923
|
+
const valueOpts = extra
|
|
924
|
+
? new Set([...WRITE_OPERAND_VALUE_OPTS, ...extra])
|
|
925
|
+
: WRITE_OPERAND_VALUE_OPTS;
|
|
926
|
+
const remote = REMOTE_COMMANDS.has(command);
|
|
927
|
+
const ops = operands(args, valueOpts);
|
|
928
|
+
// `cp -t DIR src…` / `ln -t DIR target`: the destination is named by the
|
|
929
|
+
// flag and every operand is a source.
|
|
930
|
+
let targetDirectory: ReturnType<typeof declaredOptionValue>;
|
|
931
|
+
if (TARGET_DIRECTORY_COMMANDS.has(command)) {
|
|
932
|
+
for (let i = 0; i < args.length; i++) {
|
|
933
|
+
targetDirectory = declaredOptionValue(args, i, TARGET_DIRECTORY_OPTS);
|
|
934
|
+
if (targetDirectory) break;
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
const targets: UnitTarget[] = [];
|
|
938
|
+
if (targetDirectory?.value !== undefined) {
|
|
939
|
+
const pathValue = resolveTargetPath(base, targetDirectory.value);
|
|
940
|
+
if (pathValue === undefined) return "unresolved path operand";
|
|
941
|
+
targets.push({ path: pathValue, effect: "write", source: "operand" });
|
|
942
|
+
}
|
|
943
|
+
// A copy reads its sources and writes one destination; `mv` also removes
|
|
944
|
+
// the source, so it stays a write everywhere.
|
|
945
|
+
const removesSources = command === "rsync" && hasFlag(args, "--remove-source-files");
|
|
946
|
+
const allAreTargets = command === "install" && hasFlag(args, "-d", "--directory");
|
|
947
|
+
for (let i = 0; i < ops.length; i++) {
|
|
948
|
+
const operand = ops[i];
|
|
949
|
+
if (remote && isRemotePath(operand)) continue;
|
|
950
|
+
const pathValue = resolveTargetPath(base, operand);
|
|
951
|
+
if (pathValue === undefined) return "unresolved path operand";
|
|
952
|
+
const source =
|
|
953
|
+
SOURCE_IS_READ.has(command) &&
|
|
954
|
+
!removesSources &&
|
|
955
|
+
!allAreTargets &&
|
|
956
|
+
(targetDirectory !== undefined || i < ops.length - 1);
|
|
957
|
+
targets.push({
|
|
958
|
+
path: pathValue,
|
|
959
|
+
effect: source ? "read" : "write",
|
|
960
|
+
source: "operand",
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
return targets;
|
|
964
|
+
}
|
|
965
|
+
|
|
776
966
|
/**
|
|
777
967
|
* Turn one simple command (already split on separators) into units.
|
|
778
968
|
* Recursion depth is bounded so a pathological substitution cannot loop.
|
|
@@ -971,16 +1161,14 @@ function analyzeSegment(
|
|
|
971
1161
|
|
|
972
1162
|
// Command-specific target extraction.
|
|
973
1163
|
if (WRITE_PATH_COMMANDS.has(command)) {
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
targets.push({ path: pathValue, effect: "write", source: "operand" });
|
|
1164
|
+
const result = writePathTargets(command, args, commandBase);
|
|
1165
|
+
if (typeof result === "string") {
|
|
1166
|
+
return {
|
|
1167
|
+
units: [opaqueUnit(raw, headWord, command, base, result)],
|
|
1168
|
+
nextBase: base,
|
|
1169
|
+
};
|
|
983
1170
|
}
|
|
1171
|
+
targets.push(...result);
|
|
984
1172
|
} else if (
|
|
985
1173
|
command === "sed" &&
|
|
986
1174
|
effect === "write"
|
|
@@ -1023,10 +1211,42 @@ function analyzeSegment(
|
|
|
1023
1211
|
}
|
|
1024
1212
|
}
|
|
1025
1213
|
|
|
1214
|
+
// Declared file-writing flags (`curl -o`, `curl -D`, …): the value is a
|
|
1215
|
+
// target even when it does not look like a path, and an unresolved one is
|
|
1216
|
+
// opaque rather than silently dropped.
|
|
1217
|
+
const writeOpts = WRITE_VALUE_OPTS[command];
|
|
1218
|
+
if (writeOpts) {
|
|
1219
|
+
for (let i = 0; i < args.length; i++) {
|
|
1220
|
+
const matched = declaredOptionValue(args, i, writeOpts);
|
|
1221
|
+
if (!matched) continue;
|
|
1222
|
+
const { value, next, option } = matched;
|
|
1223
|
+
if (value === undefined) {
|
|
1224
|
+
i = next;
|
|
1225
|
+
continue;
|
|
1226
|
+
}
|
|
1227
|
+
// `-` is stdout for curl's file outputs, but `--output-dir -` names
|
|
1228
|
+
// a directory literally called `-`.
|
|
1229
|
+
if (DEVICE_SINKS.has(value) || (value === "-" && option !== "--output-dir")) {
|
|
1230
|
+
i = next;
|
|
1231
|
+
continue;
|
|
1232
|
+
}
|
|
1233
|
+
const pathValue = resolveTargetPath(commandBase, value);
|
|
1234
|
+
if (pathValue === undefined) {
|
|
1235
|
+
return {
|
|
1236
|
+
units: [opaqueUnit(raw, headWord, command, base, "unresolved output target")],
|
|
1237
|
+
nextBase: base,
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
targets.push({ path: pathValue, effect: "write", source: "operand" });
|
|
1241
|
+
i = next;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1026
1245
|
// Path-like arguments are targets for every command, except for commands
|
|
1027
1246
|
// whose arguments are data (`echo`, `printf`), declared data flags, and
|
|
1028
|
-
// device sinks.
|
|
1029
|
-
|
|
1247
|
+
// device sinks. Path-writing commands collected their operands above; the
|
|
1248
|
+
// blanket pass would re-add a copy's source as a write.
|
|
1249
|
+
if (!NO_PATH_OPERANDS.has(command) && !WRITE_PATH_COMMANDS.has(command)) {
|
|
1030
1250
|
const dataOpts = DATA_VALUE_OPTS[command];
|
|
1031
1251
|
const patternIndex =
|
|
1032
1252
|
GREP_FAMILY.has(command) &&
|
|
@@ -1035,12 +1255,49 @@ function analyzeSegment(
|
|
|
1035
1255
|
: -1;
|
|
1036
1256
|
for (let i = 0; i < args.length; i++) {
|
|
1037
1257
|
const arg = args[i];
|
|
1258
|
+
const declaredWrite = writeOpts
|
|
1259
|
+
? declaredOptionValue(args, i, writeOpts)
|
|
1260
|
+
: undefined;
|
|
1261
|
+
if (declaredWrite) {
|
|
1262
|
+
// The write pass above owns both the option and its value.
|
|
1263
|
+
i = declaredWrite.next;
|
|
1264
|
+
continue;
|
|
1265
|
+
}
|
|
1266
|
+
const localFile =
|
|
1267
|
+
command === "curl"
|
|
1268
|
+
? declaredOptionValue(args, i, CURL_LOCAL_FILE_OPTS)
|
|
1269
|
+
: undefined;
|
|
1270
|
+
if (localFile) {
|
|
1271
|
+
const value = localFile.value;
|
|
1272
|
+
if (value !== undefined && value !== "-") {
|
|
1273
|
+
const pathValue = resolveTargetPath(commandBase, value);
|
|
1274
|
+
if (pathValue === undefined) {
|
|
1275
|
+
return {
|
|
1276
|
+
units: [opaqueUnit(raw, headWord, command, base, "unresolved path argument")],
|
|
1277
|
+
nextBase: base,
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
targets.push({ path: pathValue, effect, source: "operand" });
|
|
1281
|
+
}
|
|
1282
|
+
i = localFile.next;
|
|
1283
|
+
continue;
|
|
1284
|
+
}
|
|
1038
1285
|
if (dataOpts?.has(arg)) {
|
|
1039
1286
|
i++;
|
|
1040
1287
|
continue;
|
|
1041
1288
|
}
|
|
1042
1289
|
if (i === patternIndex) continue;
|
|
1043
1290
|
if (DEVICE_SINKS.has(arg)) continue;
|
|
1291
|
+
// A curl operand is a URL: an unresolved one is an unknown address,
|
|
1292
|
+
// not a path this layer could judge (see URL_OPERAND_COMMANDS).
|
|
1293
|
+
// Option-shaped words are never operands: an unhandled inline option
|
|
1294
|
+
// must keep failing closed rather than ride on the URL exemption.
|
|
1295
|
+
if (
|
|
1296
|
+
hasSentinel(arg) &&
|
|
1297
|
+
URL_OPERAND_COMMANDS.has(command) &&
|
|
1298
|
+
!arg.startsWith("-")
|
|
1299
|
+
) continue;
|
|
1300
|
+
if (REMOTE_COMMANDS.has(command) && isRemotePath(arg)) continue;
|
|
1044
1301
|
if (!looksLikePath(arg)) continue;
|
|
1045
1302
|
// `sed -i` operands were already classified above; its script is not a path.
|
|
1046
1303
|
if (command === "sed" && effect === "write") continue;
|
|
@@ -1056,14 +1313,30 @@ function analyzeSegment(
|
|
|
1056
1313
|
}
|
|
1057
1314
|
|
|
1058
1315
|
// `-o FILE` / `--output FILE` writes whatever the command is. `echo` and
|
|
1059
|
-
// `printf` do not have an output option, so their `-o` is data too
|
|
1060
|
-
|
|
1316
|
+
// `printf` do not have an output option, so their `-o` is data too; the
|
|
1317
|
+
// commands in DASH_O_IS_FLAG use `-o` as a plain flag and are skipped.
|
|
1318
|
+
if (!NO_PATH_OPERANDS.has(command) && !DASH_O_IS_FLAG.has(command)) {
|
|
1061
1319
|
for (let i = 0; i < args.length; i++) {
|
|
1062
1320
|
if (args[i] !== "-o" && args[i] !== "--output") continue;
|
|
1063
1321
|
const value = args[i + 1];
|
|
1064
1322
|
if (value === undefined || DEVICE_SINKS.has(value)) continue;
|
|
1323
|
+
if (hasSentinel(value)) {
|
|
1324
|
+
return {
|
|
1325
|
+
units: [opaqueUnit(raw, headWord, command, base, "unresolved output target")],
|
|
1326
|
+
nextBase: base,
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
// A non-path value is an option argument (`ssh -o BatchMode=yes`); the
|
|
1330
|
+
// ssh family is exempt above, so a bare filename still counts here
|
|
1331
|
+
// (`sort -o out.txt f`).
|
|
1065
1332
|
const pathValue = resolveTargetPath(commandBase, value);
|
|
1066
|
-
if (pathValue)
|
|
1333
|
+
if (pathValue === undefined) {
|
|
1334
|
+
return {
|
|
1335
|
+
units: [opaqueUnit(raw, headWord, command, base, "unresolved output target")],
|
|
1336
|
+
nextBase: base,
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
targets.push({ path: pathValue, effect: "write", source: "operand" });
|
|
1067
1340
|
}
|
|
1068
1341
|
}
|
|
1069
1342
|
|
|
@@ -1084,14 +1357,30 @@ function analyzeSegment(
|
|
|
1084
1357
|
/* Program parsing */
|
|
1085
1358
|
/* -------------------------------------------------------------------------- */
|
|
1086
1359
|
|
|
1087
|
-
|
|
1088
|
-
|
|
1360
|
+
/**
|
|
1361
|
+
* One simple command plus the separators that bound it. The separators are kept
|
|
1362
|
+
* because they decide whether a statement is unconditional: an assignment
|
|
1363
|
+
* behind `&&`/`||` may never run, and one in a pipeline runs in a subshell.
|
|
1364
|
+
*/
|
|
1365
|
+
interface Segment {
|
|
1366
|
+
readonly words: string[];
|
|
1367
|
+
readonly previous: string | undefined;
|
|
1368
|
+
readonly next: string | undefined;
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
function segmentTokens(entries: ParseEntry[]): Segment[] {
|
|
1372
|
+
const segments: Segment[] = [];
|
|
1089
1373
|
let current: string[] = [];
|
|
1374
|
+
let previous: string | undefined;
|
|
1375
|
+
const flush = (next: string | undefined): void => {
|
|
1376
|
+
if (current.length > 0) segments.push({ words: current, previous, next });
|
|
1377
|
+
current = [];
|
|
1378
|
+
previous = next;
|
|
1379
|
+
};
|
|
1090
1380
|
for (const entry of entries) {
|
|
1091
1381
|
if (isControl(entry)) {
|
|
1092
1382
|
if (SEPARATORS.has(entry.op)) {
|
|
1093
|
-
|
|
1094
|
-
current = [];
|
|
1383
|
+
flush(entry.op);
|
|
1095
1384
|
continue;
|
|
1096
1385
|
}
|
|
1097
1386
|
if (entry.op === "(" || entry.op === ")") continue;
|
|
@@ -1101,7 +1390,7 @@ function segmentTokens(entries: ParseEntry[]): string[][] {
|
|
|
1101
1390
|
const word = wordOf(entry);
|
|
1102
1391
|
if (word !== undefined) current.push(word);
|
|
1103
1392
|
}
|
|
1104
|
-
|
|
1393
|
+
flush(undefined);
|
|
1105
1394
|
return segments;
|
|
1106
1395
|
}
|
|
1107
1396
|
|
|
@@ -1117,11 +1406,14 @@ const PLACEHOLDER = /\u0000SUBST(\d+)\u0000/g;
|
|
|
1117
1406
|
* such command opaque (audit 2026-11-12: 29 of 61 asks). Expanding the literal
|
|
1118
1407
|
* loop values is what bash does, so the per-unit judgement sees real paths.
|
|
1119
1408
|
*
|
|
1120
|
-
* This deliberately does **not** persist ordinary `NAME=value` assignments
|
|
1121
|
-
* without a control-flow graph,
|
|
1122
|
-
* `d=/safe; read d; rm "$d/x"` would be
|
|
1123
|
-
*
|
|
1124
|
-
*
|
|
1409
|
+
* This deliberately does **not** persist ordinary `NAME=value` assignments from
|
|
1410
|
+
* a command that might not run them in order: without a control-flow graph,
|
|
1411
|
+
* `false && d=/safe; rm "$d/x"` or `d=/safe; read d; rm "$d/x"` would be
|
|
1412
|
+
* judged against the wrong path. Assignments are substituted only inside a
|
|
1413
|
+
* plain sequential command (see {@link canPersistAssignments}), and even then
|
|
1414
|
+
* only the ones that are provably literal and unconditional. Loop expansion
|
|
1415
|
+
* likewise stops for a variable reassigned or passed to a shell builtin that
|
|
1416
|
+
* can mutate it.
|
|
1125
1417
|
*
|
|
1126
1418
|
* Anything not provably literal — a value from `$(…)`, a loop over an unknown
|
|
1127
1419
|
* list, more than {@link MAX_EXPANSIONS} combinations, a value with whitespace
|
|
@@ -1131,6 +1423,20 @@ const MAX_EXPANSIONS = 64;
|
|
|
1131
1423
|
const ASSIGNMENT_RE = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/;
|
|
1132
1424
|
const SUBST_PLACEHOLDER_RE = /\u0000SUBST\d+\u0000/;
|
|
1133
1425
|
|
|
1426
|
+
/**
|
|
1427
|
+
* Words that make assignment substitution unsound. Each one can run a command
|
|
1428
|
+
* conditionally, more than once, in a subshell, or reassign a name behind the
|
|
1429
|
+
* parser's back; a command containing any of them keeps the old fail-closed
|
|
1430
|
+
* behavior (no cross-segment substitution at all).
|
|
1431
|
+
*/
|
|
1432
|
+
const ASSIGNMENT_HOSTILE = new Set([
|
|
1433
|
+
"if", "then", "else", "elif", "fi", "case", "esac", "while", "until",
|
|
1434
|
+
"for", "do", "done", "select", "function", "source", ".", "eval",
|
|
1435
|
+
"exec", "read", "readarray", "mapfile", "unset", "export", "declare",
|
|
1436
|
+
"local", "typeset", "let", "return", "exit", "trap", "set", "shift",
|
|
1437
|
+
"builtin", "command", "printf", "getopts", "coproc", "fc",
|
|
1438
|
+
]);
|
|
1439
|
+
|
|
1134
1440
|
interface LoopFrame {
|
|
1135
1441
|
readonly name: string;
|
|
1136
1442
|
/** Literal values; empty means "cannot expand, keep failing closed". */
|
|
@@ -1140,6 +1446,10 @@ interface LoopFrame {
|
|
|
1140
1446
|
interface ExpansionState {
|
|
1141
1447
|
/** Open `for` loops, innermost last. */
|
|
1142
1448
|
readonly loops: LoopFrame[];
|
|
1449
|
+
/** Literal `NAME=value` assignments in force, text order. */
|
|
1450
|
+
readonly assignments: Map<string, string>;
|
|
1451
|
+
/** `false` disables cross-segment assignment substitution. */
|
|
1452
|
+
readonly persist: boolean;
|
|
1143
1453
|
}
|
|
1144
1454
|
|
|
1145
1455
|
function variableMarker(name: string): string {
|
|
@@ -1196,6 +1506,81 @@ function disableMutatedLoopVariables(
|
|
|
1196
1506
|
}
|
|
1197
1507
|
}
|
|
1198
1508
|
|
|
1509
|
+
/** Remove shell keywords and grouping words at the edges of a segment. */
|
|
1510
|
+
function stripStructure(words: readonly string[]): string[] {
|
|
1511
|
+
let start = 0;
|
|
1512
|
+
let end = words.length;
|
|
1513
|
+
while (start < end && STRUCTURAL_LEADING.has(words[start])) start++;
|
|
1514
|
+
while (end > start && STRUCTURAL_TRAILING.has(words[end - 1])) end--;
|
|
1515
|
+
return words.slice(start, end);
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
/**
|
|
1519
|
+
* Whether a statement always runs exactly where the text says it does. An
|
|
1520
|
+
* assignment behind `&&` / `||` may be skipped, and one inside a pipeline runs
|
|
1521
|
+
* in a subshell, so neither may be remembered for later commands.
|
|
1522
|
+
*/
|
|
1523
|
+
function isUnconditional(segment: Segment): boolean {
|
|
1524
|
+
const before = segment.previous;
|
|
1525
|
+
const after = segment.next;
|
|
1526
|
+
return (
|
|
1527
|
+
(before === undefined || before === ";") &&
|
|
1528
|
+
(after === undefined || after === ";")
|
|
1529
|
+
);
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
/**
|
|
1533
|
+
* Whether cross-segment assignment substitution is sound for this command.
|
|
1534
|
+
*
|
|
1535
|
+
* Conditional constructs, loops, subshells, function definitions and the
|
|
1536
|
+
* builtins that can reassign a name behind the parser's back all make the text
|
|
1537
|
+
* order differ from the execution order, and a wrong substitution answers for
|
|
1538
|
+
* a path the command does not actually touch. Those commands keep every
|
|
1539
|
+
* `$VAR` fail-closed; a plain `PI=/opt/x; grep … $PI` is the case worth
|
|
1540
|
+
* resolving, because it is how a long install path is spelled in practice
|
|
1541
|
+
* (audit 2026-11-25 → 2026-12-02: 6 asks, all `allow`).
|
|
1542
|
+
*/
|
|
1543
|
+
function canPersistAssignments(
|
|
1544
|
+
entries: readonly ParseEntry[],
|
|
1545
|
+
payloads: readonly string[],
|
|
1546
|
+
): boolean {
|
|
1547
|
+
// `$((NAME=value))` is an arithmetic expansion, but it reaches this walker
|
|
1548
|
+
// as a substitution payload just like `$(command)`. Any such payload may
|
|
1549
|
+
// reassign a shell variable, so keep later `$NAME` references fail-closed.
|
|
1550
|
+
if (payloads.some((payload) => payload.trimStart().startsWith("("))) {
|
|
1551
|
+
return false;
|
|
1552
|
+
}
|
|
1553
|
+
for (const entry of entries) {
|
|
1554
|
+
const op = opOf(entry);
|
|
1555
|
+
if (op === "(" || op === ")" || op === "&") return false;
|
|
1556
|
+
const word = wordOf(entry);
|
|
1557
|
+
if (word !== undefined && ASSIGNMENT_HOSTILE.has(word)) return false;
|
|
1558
|
+
}
|
|
1559
|
+
return true;
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
/**
|
|
1563
|
+
* Record (or, for a non-literal value, forget) the assignments of a
|
|
1564
|
+
* standalone `NAME=value` statement. Only called for statements that are known
|
|
1565
|
+
* to run unconditionally; anything else leaves the variable unresolved.
|
|
1566
|
+
*/
|
|
1567
|
+
function rememberAssignments(
|
|
1568
|
+
words: readonly string[],
|
|
1569
|
+
state: ExpansionState,
|
|
1570
|
+
unconditional: boolean,
|
|
1571
|
+
): void {
|
|
1572
|
+
if (!state.persist) return;
|
|
1573
|
+
for (const word of words) {
|
|
1574
|
+
const match = ASSIGNMENT_RE.exec(word);
|
|
1575
|
+
if (!match) continue;
|
|
1576
|
+
if (unconditional && isLiteralValue(match[2])) {
|
|
1577
|
+
state.assignments.set(match[1], match[2]);
|
|
1578
|
+
} else {
|
|
1579
|
+
state.assignments.delete(match[1]);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1199
1584
|
/**
|
|
1200
1585
|
* Apply loop keywords and literal expansion to one segment.
|
|
1201
1586
|
*
|
|
@@ -1205,6 +1590,7 @@ function disableMutatedLoopVariables(
|
|
|
1205
1590
|
function expandSegment(
|
|
1206
1591
|
words: readonly string[],
|
|
1207
1592
|
state: ExpansionState,
|
|
1593
|
+
unconditional = false,
|
|
1208
1594
|
): string[][] {
|
|
1209
1595
|
let rest = [...words];
|
|
1210
1596
|
if (rest[0] === "do") rest = rest.slice(1);
|
|
@@ -1217,6 +1603,8 @@ function expandSegment(
|
|
|
1217
1603
|
state.loops.push(parseForHeader(rest));
|
|
1218
1604
|
return [];
|
|
1219
1605
|
}
|
|
1606
|
+
rest = stripStructure(rest);
|
|
1607
|
+
if (rest.length === 0) return [];
|
|
1220
1608
|
|
|
1221
1609
|
// A reassignment shadows the loop value from this point on. Do not substitute
|
|
1222
1610
|
// the old literal into later commands; leave their variable marker opaque.
|
|
@@ -1226,7 +1614,10 @@ function expandSegment(
|
|
|
1226
1614
|
disableLoopVariable(state, match[1]);
|
|
1227
1615
|
rest = rest.slice(1);
|
|
1228
1616
|
}
|
|
1229
|
-
if (rest.length === 0)
|
|
1617
|
+
if (rest.length === 0) {
|
|
1618
|
+
rememberAssignments(words, state, unconditional);
|
|
1619
|
+
return [];
|
|
1620
|
+
}
|
|
1230
1621
|
disableMutatedLoopVariables(rest, state);
|
|
1231
1622
|
|
|
1232
1623
|
let variants: string[][] = [rest];
|
|
@@ -1243,6 +1634,17 @@ function expandSegment(
|
|
|
1243
1634
|
}
|
|
1244
1635
|
variants = next;
|
|
1245
1636
|
}
|
|
1637
|
+
if (state.assignments.size > 0) {
|
|
1638
|
+
variants = variants.map((variant) =>
|
|
1639
|
+
variant.map((word) => {
|
|
1640
|
+
let out = word;
|
|
1641
|
+
for (const [name, value] of state.assignments) {
|
|
1642
|
+
out = out.split(variableMarker(name)).join(value);
|
|
1643
|
+
}
|
|
1644
|
+
return out;
|
|
1645
|
+
}),
|
|
1646
|
+
);
|
|
1647
|
+
}
|
|
1246
1648
|
return variants;
|
|
1247
1649
|
}
|
|
1248
1650
|
|
|
@@ -1264,10 +1666,14 @@ function parseText(
|
|
|
1264
1666
|
}
|
|
1265
1667
|
const units: CommandUnit[] = [];
|
|
1266
1668
|
let currentBase = base;
|
|
1267
|
-
const expansion: ExpansionState = {
|
|
1669
|
+
const expansion: ExpansionState = {
|
|
1670
|
+
loops: [],
|
|
1671
|
+
assignments: new Map(),
|
|
1672
|
+
persist: canPersistAssignments(entries, payloads),
|
|
1673
|
+
};
|
|
1268
1674
|
for (const segment of segmentTokens(entries)) {
|
|
1269
1675
|
const cleaned: string[] = [];
|
|
1270
|
-
for (const word of segment) {
|
|
1676
|
+
for (const word of segment.words) {
|
|
1271
1677
|
let rest = word;
|
|
1272
1678
|
for (const match of word.matchAll(PLACEHOLDER)) {
|
|
1273
1679
|
const payload = payloads[Number(match[1])];
|
|
@@ -1279,7 +1685,8 @@ function parseText(
|
|
|
1279
1685
|
if (rest !== "") cleaned.push(rest);
|
|
1280
1686
|
}
|
|
1281
1687
|
if (cleaned.length === 0) continue;
|
|
1282
|
-
|
|
1688
|
+
const unconditional = isUnconditional(segment);
|
|
1689
|
+
for (const words of expandSegment(cleaned, expansion, unconditional)) {
|
|
1283
1690
|
const analyzed = analyzeSegment(words, currentBase, depth);
|
|
1284
1691
|
units.push(...analyzed.units);
|
|
1285
1692
|
currentBase = analyzed.nextBase;
|