pi-subagents-extend 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +133 -2
- package/bin/pi-subagents-extend-helper-linux-arm64 +0 -0
- package/bin/pi-subagents-extend-helper-linux-x64 +0 -0
- package/package.json +63 -4
- package/skills/pi-subagents-extend/SKILL.md +312 -0
- package/skills/pi-subagents-extend-context/SKILL.md +103 -0
- package/skills/pi-subagents-extend-probes/SKILL.md +151 -0
- package/src/bash/env.ts +108 -0
- package/src/bash/index.ts +27 -0
- package/src/bash/network.ts +97 -0
- package/src/bash/programs.ts +367 -0
- package/src/bash/rules.ts +247 -0
- package/src/bash/tokens.ts +169 -0
- package/src/bash/unreachable.ts +429 -0
- package/src/budgets.ts +89 -0
- package/src/child-context.ts +24 -0
- package/src/context-scope.ts +93 -0
- package/src/deliverable.ts +216 -0
- package/src/dependencies.ts +320 -0
- package/src/handoff-hint.ts +69 -0
- package/src/index.ts +181 -0
- package/src/main-context.ts +30 -0
- package/src/main-skills.ts +77 -0
- package/src/notices.ts +299 -0
- package/src/paths/index.ts +165 -0
- package/src/policy/glob.ts +146 -0
- package/src/policy/index.ts +36 -0
- package/src/policy/payload.ts +312 -0
- package/src/probe.ts +495 -0
- package/src/role-config.ts +434 -0
- package/src/sandbox/grants.ts +339 -0
- package/src/sandbox/helper.ts +137 -0
- package/src/sandbox/index.ts +12 -0
- package/src/sandbox/spec.ts +285 -0
- package/src/sandbox-note.ts +194 -0
- package/src/wiring/agent-start.ts +371 -0
- package/src/wiring/context.ts +216 -0
- package/src/wiring/misc.ts +55 -0
- package/src/wiring/session-start.ts +69 -0
- package/src/wiring/tool-call/anchor.ts +138 -0
- package/src/wiring/tool-call/bash-path.ts +0 -0
- package/src/wiring/tool-call/dispatch-preflight.ts +0 -0
- package/src/wiring/tool-call/judge.ts +585 -0
- package/src/wiring/tool-call/original-args.ts +64 -0
- package/src/wiring/tool-call/paths.ts +235 -0
- package/src/wiring/tool-result.ts +68 -0
- package/src/worktree-default.ts +27 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 程序的读写归类:直通包装器剥离、git / 包管理器的子命令定位、单程序的写判定。
|
|
3
|
+
*
|
|
4
|
+
* 从 bash/index.ts 拆出来的归类层:给 `allow` 列判读写。这里的表是白名单,
|
|
5
|
+
* 漏一个条目就是把会写的命令判成 read —— 条目逐条照搬,不做「清理」。
|
|
6
|
+
* bash/index.ts 仍是唯一入口,这里的东西由它转出口。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { basename } from "node:path";
|
|
10
|
+
import { ASSIGNMENT_LIKE, leadingAssignments } from "./env.ts";
|
|
11
|
+
import { commandSegments, shellTokens } from "./tokens.ts";
|
|
12
|
+
|
|
13
|
+
// 直通包装器会吃掉自己的旗标和旗标值;只认下面列出的,遇到不认识的旗标一律判不准而放行。
|
|
14
|
+
interface WrapperFlags {
|
|
15
|
+
noArg?: string[];
|
|
16
|
+
withArg?: string[];
|
|
17
|
+
assignments?: boolean;
|
|
18
|
+
duration?: boolean;
|
|
19
|
+
}
|
|
20
|
+
const WRAPPER_FLAGS: Record<string, WrapperFlags> = {
|
|
21
|
+
env: {
|
|
22
|
+
noArg: ["-i", "-0", "--ignore-environment", "--null"],
|
|
23
|
+
withArg: ["-u", "--unset", "-C", "--chdir", "-S", "--split-string", "--default-signal", "--skip-default-signal"],
|
|
24
|
+
assignments: true,
|
|
25
|
+
},
|
|
26
|
+
sudo: {
|
|
27
|
+
noArg: [
|
|
28
|
+
"-n", "--non-interactive", "-b", "--background", "-E", "--preserve-env", "-H", "--set-home",
|
|
29
|
+
"-i", "--login", "-s", "--shell", "-S", "--stdin", "-v", "--validate", "-k", "--reset-timestamp",
|
|
30
|
+
"-K", "--remove-timestamp", "-l", "--list", "-h", "--help", "-V", "--version",
|
|
31
|
+
],
|
|
32
|
+
withArg: [
|
|
33
|
+
"-u", "--user", "-g", "--group", "-G", "--group-list", "-p", "--prompt", "-r", "--role",
|
|
34
|
+
"-t", "--type", "-C", "--close-from", "-D", "--chdir", "-R", "--chroot", "-T", "--command-timeout",
|
|
35
|
+
"-A", "--askpass", "-P", "--preserve-fd",
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
nice: {
|
|
39
|
+
noArg: ["--version", "--help"],
|
|
40
|
+
withArg: ["-n", "--adjustment"],
|
|
41
|
+
},
|
|
42
|
+
timeout: {
|
|
43
|
+
noArg: ["--foreground", "--preserve-status", "--version", "--help"],
|
|
44
|
+
withArg: ["-k", "--kill-after", "-s", "--signal"],
|
|
45
|
+
duration: true,
|
|
46
|
+
},
|
|
47
|
+
command: {
|
|
48
|
+
noArg: ["-p"],
|
|
49
|
+
},
|
|
50
|
+
stdbuf: {
|
|
51
|
+
withArg: ["-i", "-o", "-e", "--input", "--output", "--error"],
|
|
52
|
+
},
|
|
53
|
+
// xargs 的旗标按「吃不吃下一个 token」分组;判不准就剥不到程序位,宁可按 write 处理。
|
|
54
|
+
xargs: {
|
|
55
|
+
noArg: ["-t", "-r", "-0"],
|
|
56
|
+
withArg: [
|
|
57
|
+
"-n", "--max-args", "-I", "--replace", "-P", "--max-procs",
|
|
58
|
+
"-d", "--delimiter", "-E", "-L", "-s", "-a", "--arg-file",
|
|
59
|
+
],
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
const WRAPPERS = new Set(Object.keys(WRAPPER_FLAGS));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 剥掉前置的直通包装器,返回从真正程序开始的 token;判不准返回 null。
|
|
66
|
+
*
|
|
67
|
+
* 带值旗标吃掉下一个 token(或等号右边),没列出的旗标一律当判不准而放行。
|
|
68
|
+
* `command -v/-V` 只查询不执行,直接放弃判断。套得太深也放弃。
|
|
69
|
+
*/
|
|
70
|
+
export function peelWrappers(words: string[], seenWrappers?: Set<string>): string[] | null {
|
|
71
|
+
let rest = words;
|
|
72
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
73
|
+
const wrapper = rest[0];
|
|
74
|
+
if (!wrapper || !WRAPPERS.has(wrapper)) return rest;
|
|
75
|
+
seenWrappers?.add(wrapper);
|
|
76
|
+
// `command -v curl` / `command -V curl` 只报路径不运行,不能算它要联网。
|
|
77
|
+
if (wrapper === "command" && (rest[1] === "-v" || rest[1] === "-V")) return null;
|
|
78
|
+
const flags = WRAPPER_FLAGS[wrapper] as WrapperFlags;
|
|
79
|
+
const args = rest.slice(1);
|
|
80
|
+
let i = 0;
|
|
81
|
+
while (i < args.length) {
|
|
82
|
+
const word = args[i] as string;
|
|
83
|
+
if (flags.assignments && ASSIGNMENT_LIKE.test(word)) {
|
|
84
|
+
i += 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (word === "--") {
|
|
88
|
+
i += 1;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
if (word.startsWith("--")) {
|
|
92
|
+
const eq = word.indexOf("=");
|
|
93
|
+
const name = eq < 0 ? word : word.slice(0, eq);
|
|
94
|
+
if (flags.withArg?.includes(name)) i += eq < 0 ? 2 : 1;
|
|
95
|
+
else if (flags.noArg?.includes(name) && eq < 0) i += 1;
|
|
96
|
+
else return null; // 没见过的长旗标:吃不吃值不知道,放行
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (word.startsWith("-") && word.length > 1) {
|
|
100
|
+
const short = word.slice(0, 2);
|
|
101
|
+
if (flags.withArg?.includes(short)) i += word.length > 2 ? 1 : 2; // -n5 / -n 5
|
|
102
|
+
else if (flags.noArg?.includes(short) && word.length === 2) i += 1;
|
|
103
|
+
else return null; // 没见过的短旗标:放行
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
break; // 第一个非旗标 token 是 DURATION 或程序名
|
|
107
|
+
}
|
|
108
|
+
// timeout 的位置参数先是 DURATION,再是程序名。
|
|
109
|
+
if (flags.duration) {
|
|
110
|
+
const duration = args[i];
|
|
111
|
+
if (!duration || !/^(?:infinity|[0-9]+(?:\.[0-9]+)?[smhd]?)$/.test(duration)) return null;
|
|
112
|
+
i += 1;
|
|
113
|
+
}
|
|
114
|
+
rest = args.slice(i);
|
|
115
|
+
if (rest.length === 0) return null;
|
|
116
|
+
}
|
|
117
|
+
return null; // 包装器套太深,判不准
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface GitSubcommand {
|
|
121
|
+
/** 子命令所在的 token 下标。 */
|
|
122
|
+
index: number;
|
|
123
|
+
/** 全局旗标都认得、位置可靠时为 true;认不全时调用方必须按 write 处理。 */
|
|
124
|
+
certain: boolean;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* git 的全局旗标排在子命令前面;跳过它们才知道真正要跑的子命令。
|
|
129
|
+
*
|
|
130
|
+
* 调用方用 quote-aware 的 shellTokens 切词:带引号的旗标值(`core.pager='rm x'`)是单个 token,
|
|
131
|
+
* 吃掉值之后直接就到子命令,不必再把引号残片拼回去。
|
|
132
|
+
* 没列进来的旗标(例如带参数的 `--namespace`)吃不吃值判不准,返回 `certain: false`:
|
|
133
|
+
* 此时子命令位置不可靠,写判定必须落到 write,而不是顺着错位置把它当 read。
|
|
134
|
+
*/
|
|
135
|
+
export function gitSubcommandIndex(words: string[]): GitSubcommand {
|
|
136
|
+
let i = 1;
|
|
137
|
+
for (;;) {
|
|
138
|
+
const word = words[i];
|
|
139
|
+
if (word === undefined) return { index: i, certain: true };
|
|
140
|
+
if (word === "-C" || word === "--git-dir" || word === "--work-tree" || word === "-c") {
|
|
141
|
+
i += 2; // 吃一个参数
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (
|
|
145
|
+
word.startsWith("--git-dir=") ||
|
|
146
|
+
word.startsWith("--work-tree=") ||
|
|
147
|
+
word === "--no-pager" ||
|
|
148
|
+
word === "--progress" ||
|
|
149
|
+
// 这些没有参数(或只用 = 形式带参数),吃一个 token 就到子命令。
|
|
150
|
+
word === "-p" ||
|
|
151
|
+
word === "--paginate" ||
|
|
152
|
+
word === "-P" ||
|
|
153
|
+
word === "--exec-path" ||
|
|
154
|
+
word === "--literal-pathspecs" ||
|
|
155
|
+
word === "--no-replace-objects" ||
|
|
156
|
+
word === "--no-optional-locks" ||
|
|
157
|
+
word === "--bare" ||
|
|
158
|
+
word === "--version" ||
|
|
159
|
+
word === "--help" ||
|
|
160
|
+
word === "-h"
|
|
161
|
+
) {
|
|
162
|
+
i += 1;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
// 没见过的短/长旗标:不知道它吃不吃下一个 token,子命令位置判不准。
|
|
166
|
+
if (word.startsWith("-") && word !== "-") return { index: i, certain: false };
|
|
167
|
+
return { index: i, certain: true };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 命令的固有写能力判定:给 `allow` 列归类用,只看命令本身,不看路径与白名单。
|
|
173
|
+
*
|
|
174
|
+
* 只认下面这些写得确定的程序与子命令,其余一律 read。把拿不准的程序判成 write 是误授权,
|
|
175
|
+
* 反方向(判成 read)只是少给一点权限,所以这里一律取后者。
|
|
176
|
+
*
|
|
177
|
+
* 四种容易漏的形态都在这一层剥掉或判掉:
|
|
178
|
+
* - 前置赋值(`FOO=1 rm x`):先剥赋值,程序位是 `rm`,不是 `FOO=1`;
|
|
179
|
+
* - 直通包装器(`env` / `sudo` / `nice` / `timeout` / `xargs`):剥到真正跑的程序;
|
|
180
|
+
* - 程序写成路径(`/usr/bin/rm`、`./rm`):取 basename,和裸程序名落同一张表;
|
|
181
|
+
* - `;` / `|` / `&&` 链:逐段判,任一段的程序会写,整条就算写 —— 只看第一段会把
|
|
182
|
+
* `cat x; rm y` 放过去,而它后半句确实会删文件。
|
|
183
|
+
*
|
|
184
|
+
* `writableCeiling` 是外部的写上限(role 的 `bash.writable`):false 时表里怎么写都返回
|
|
185
|
+
* read —— 没有写授权,判成 write 也拿不到写权限。要问「这个程序本身会不会写」时传 true。
|
|
186
|
+
*/
|
|
187
|
+
const WRITE_PROGRAMS = new Set([
|
|
188
|
+
"mkdir", "cp", "mv", "rm", "touch", "chmod", "chown", "ln", "tee", "dd",
|
|
189
|
+
"truncate", "install", "rsync",
|
|
190
|
+
]);
|
|
191
|
+
const READ_PROGRAMS = new Set([
|
|
192
|
+
"cat", "ls", "rg", "find", "grep", "head", "tail", "wc", "file", "stat",
|
|
193
|
+
"diff", "cmp", "du", "df", "echo", "pwd", "which", "type",
|
|
194
|
+
]);
|
|
195
|
+
const WRITE_GIT_SUBCOMMANDS = new Set([
|
|
196
|
+
"add", "commit", "switch", "stash", "init", "worktree", "rm", "mv", "apply",
|
|
197
|
+
"cherry-pick", "merge", "rebase", "tag",
|
|
198
|
+
// 会改写工作树、索引或仓库历史的子命令:漏一个就会静默按 read 跑。
|
|
199
|
+
"checkout", "restore", "reset", "clean", "revert", "pull", "fetch", "clone",
|
|
200
|
+
"gc", "prune", "update-index", "submodule", "notes", "am", "sparse-checkout",
|
|
201
|
+
]);
|
|
202
|
+
const READ_GIT_SUBCOMMANDS = new Set([
|
|
203
|
+
"status", "diff", "log", "show", "rev-parse", "branch", "remote", "blame", "describe",
|
|
204
|
+
]);
|
|
205
|
+
// branch / remote 平时只读,带上这些参数就改写分支或远端配置。
|
|
206
|
+
const WRITE_GIT_BRANCH_FLAGS = new Set(["-d", "-D", "-m", "-M", "-C", "-c", "-f", "--move", "--delete", "--copy", "--force", "--unset-upstream", "--set-upstream-to", "--edit-description"]);
|
|
207
|
+
const WRITE_GIT_REMOTE_ARGS = new Set(["add", "remove", "rename", "set-url", "set-head", "prune", "set-branches", "update"]);
|
|
208
|
+
// tag 平时算写(会打标签),纯列举的旗标与无标签名时才是读。
|
|
209
|
+
const GIT_TAG_LISTING_FLAGS = new Set(["-l", "--list", "--contains", "--points-at", "--sort", "--format"]);
|
|
210
|
+
const GIT_TAG_WRITE_FLAGS = new Set(["-d", "-a", "-f"]);
|
|
211
|
+
// find 默认只读,这些动作参数会改文件(find 自己就能删、能跑任意命令)。
|
|
212
|
+
const WRITE_FIND_ARGS = new Set(["-delete", "-exec", "-execdir", "-ok", "-okdir", "-fprint", "-fprint0", "-fprintf", "-fls"]);
|
|
213
|
+
const ACCESS_PACKAGE_MANAGERS = new Set(["pnpm", "npm", "yarn", "bun"]);
|
|
214
|
+
// 包管理器只有这些子命令只读;run/exec/test/...
|
|
215
|
+
// 都会执行任意代码,没列进来的按 write(少一个误放比少一个误拒严重)。
|
|
216
|
+
const READ_ONLY_PACKAGE_SUBCOMMANDS = new Set(["list", "ls", "why", "view", "info", "show", "outdated"]);
|
|
217
|
+
// 这些子命令会执行代码或改文件;位置不确定时定位到它们,名字本身仍是子命令的强证据。
|
|
218
|
+
const WRITE_PACKAGE_SUBCOMMANDS = new Set(["run", "exec", "dlx", "test", "start", "create", "install", "add", "remove", "uninstall", "update", "ci", "publish"]);
|
|
219
|
+
// config / store 本身不写,但第二 token 决定动作:get/path 查询,set/prune 会改文件。
|
|
220
|
+
const READ_ONLY_PACKAGE_CONFIG_ARGS = new Set(["get", "list", "ls"]);
|
|
221
|
+
const READ_ONLY_PACKAGE_STORE_ARGS = new Set(["path", "status"]);
|
|
222
|
+
const INTERPRETERS = new Set(["node", "python", "python3", "sh", "bash", "zsh", "ruby", "perl"]);
|
|
223
|
+
|
|
224
|
+
type WriteVerdict = "write" | "read" | "unknown";
|
|
225
|
+
|
|
226
|
+
interface PackageSubcommand {
|
|
227
|
+
/** 子命令所在的 token 下标。 */
|
|
228
|
+
index: number;
|
|
229
|
+
/** 位置可靠时为 true(前置旗标都认得,或定位到已知子命令);否则调用方必须按 write 处理。 */
|
|
230
|
+
certain: boolean;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// 包管理器的全局旗标里,这些要吃掉下一个 token 才到子命令。等号形式自己带值,另按单 token 处理。
|
|
234
|
+
const PACKAGE_VALUE_FLAGS = new Set(["-C", "--dir", "--prefix", "-F", "--filter", "--registry", "--reporter", "--loglevel"]);
|
|
235
|
+
// 这些旗标自己不带值(workspace-root 是布尔旗标;global 是 config 的作用域开关),跳一个就到子命令。
|
|
236
|
+
const PACKAGE_NO_ARG_FLAGS = new Set(["--version", "-v", "--help", "-h", "--global", "-g", "--workspace-root"]);
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 跳过包管理器的全局旗标,定位真正的子命令。没列进来的旗标只跳它自己,并记下位置已不确定:
|
|
240
|
+
* 若其后定位到的是已知子命令(只读表或会写的那张),名字本身就是位置的强证据,照它判;
|
|
241
|
+
* 若定位到的 token 也不认识,就不猜 read,交给调用方按 write 处理。
|
|
242
|
+
*/
|
|
243
|
+
function packageSubcommandIndex(words: string[]): PackageSubcommand {
|
|
244
|
+
let i = 1;
|
|
245
|
+
let uncertain = false;
|
|
246
|
+
for (;;) {
|
|
247
|
+
const word = words[i];
|
|
248
|
+
if (word === undefined) return { index: i, certain: true };
|
|
249
|
+
if (!word.startsWith("-") || word === "-") {
|
|
250
|
+
// 位置不确定时只有已知子命令的名字能坐实它,否则维持「不可靠」。
|
|
251
|
+
const known = READ_ONLY_PACKAGE_SUBCOMMANDS.has(word) || WRITE_PACKAGE_SUBCOMMANDS.has(word);
|
|
252
|
+
return { index: i, certain: !uncertain || known };
|
|
253
|
+
}
|
|
254
|
+
// `--dir=x`、`--config.foo=bar` 这类值就在同一个 token 里。
|
|
255
|
+
if (word.includes("=")) {
|
|
256
|
+
i += 1;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (PACKAGE_VALUE_FLAGS.has(word)) {
|
|
260
|
+
i += 2;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (PACKAGE_NO_ARG_FLAGS.has(word)) {
|
|
264
|
+
i += 1;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
// 没见过的旗标:不知道它吃不吃下一个 token,只跳它自己并记下位置不确定。
|
|
268
|
+
i += 1;
|
|
269
|
+
uncertain = true;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** 单个程序的写判定:只认表里的,子命令按子命令表,其余 unknown。 */
|
|
274
|
+
function programWriteVerdict(words: string[]): WriteVerdict {
|
|
275
|
+
const program = basename(words[0] as string);
|
|
276
|
+
const args = words.slice(1);
|
|
277
|
+
if (WRITE_PROGRAMS.has(program) || INTERPRETERS.has(program)) return "write";
|
|
278
|
+
if (program === "sed") {
|
|
279
|
+
return args.some((arg) => arg === "-i" || arg.startsWith("-i") || arg.startsWith("--in-place")) ? "write" : "read";
|
|
280
|
+
}
|
|
281
|
+
if (program === "find") {
|
|
282
|
+
return args.some((arg) => WRITE_FIND_ARGS.has(arg)) ? "write" : "read";
|
|
283
|
+
}
|
|
284
|
+
if (program === "git") {
|
|
285
|
+
const located = gitSubcommandIndex(words);
|
|
286
|
+
// 全局旗标认不全就定位不到子命令:按 write 处理,不能顺着错位置静默判 read。
|
|
287
|
+
if (!located.certain) return "write";
|
|
288
|
+
const sub = words[located.index];
|
|
289
|
+
const subArgs = words.slice(located.index + 1);
|
|
290
|
+
// tag 的例外要先于整张写表:列举标签不写任何东西。
|
|
291
|
+
if (sub === "tag") {
|
|
292
|
+
if (subArgs.some((arg) => GIT_TAG_WRITE_FLAGS.has(arg))) return "write";
|
|
293
|
+
const listing = subArgs.some((arg) =>
|
|
294
|
+
GIT_TAG_LISTING_FLAGS.has(arg) || [...GIT_TAG_LISTING_FLAGS].some((flag) => arg.startsWith(`${flag}=`))
|
|
295
|
+
);
|
|
296
|
+
if (listing) return "read";
|
|
297
|
+
// 没有标签名也没有列举旗标(`git tag`)只是列全部。
|
|
298
|
+
return subArgs.some((arg) => !arg.startsWith("-")) ? "write" : "read";
|
|
299
|
+
}
|
|
300
|
+
if (sub && WRITE_GIT_SUBCOMMANDS.has(sub)) return "write";
|
|
301
|
+
if (sub === "branch" && subArgs.some((arg) => WRITE_GIT_BRANCH_FLAGS.has(arg))) return "write";
|
|
302
|
+
if (sub === "remote" && subArgs.some((arg) => WRITE_GIT_REMOTE_ARGS.has(arg))) return "write";
|
|
303
|
+
if (sub && READ_GIT_SUBCOMMANDS.has(sub)) return "read";
|
|
304
|
+
return "unknown";
|
|
305
|
+
}
|
|
306
|
+
if (ACCESS_PACKAGE_MANAGERS.has(program)) {
|
|
307
|
+
// 前置旗标可能吃掉自己的值;位置认不全时按 write,不能猜一个 token 当子命令。
|
|
308
|
+
const located = packageSubcommandIndex(words);
|
|
309
|
+
if (!located.certain) return "write";
|
|
310
|
+
const sub = words[located.index];
|
|
311
|
+
// 只有旗标(`pnpm --version`、`npm --help`):打印完就退出,不写。
|
|
312
|
+
if (sub === undefined) return "read";
|
|
313
|
+
if (READ_ONLY_PACKAGE_SUBCOMMANDS.has(sub)) return "read";
|
|
314
|
+
// config / store / version 第二 token 才定读写:`npm version patch` 会改包文件。
|
|
315
|
+
const second = words.slice(located.index + 1).find((arg) => !arg.startsWith("-"));
|
|
316
|
+
if (sub === "config") return second !== undefined && READ_ONLY_PACKAGE_CONFIG_ARGS.has(second) ? "read" : "write";
|
|
317
|
+
if (sub === "store") return second !== undefined && READ_ONLY_PACKAGE_STORE_ARGS.has(second) ? "read" : "write";
|
|
318
|
+
if (sub === "version") return second === undefined ? "read" : "write";
|
|
319
|
+
return "write";
|
|
320
|
+
}
|
|
321
|
+
if (READ_PROGRAMS.has(program)) return "read";
|
|
322
|
+
return "unknown";
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 一段命令的写判定。`xargs` 是拿来跑程序的:剥完旗标仍判不出程序时按 write,
|
|
327
|
+
* 不能因为表外就静默按 read 跑(那样它真的会写,而模型什么都不知道)。
|
|
328
|
+
*/
|
|
329
|
+
function segmentWriteVerdict(segment: string): WriteVerdict {
|
|
330
|
+
const words = shellTokens(leadingAssignments(segment).rest);
|
|
331
|
+
if (words.length === 0) return "unknown";
|
|
332
|
+
const seen = new Set<string>();
|
|
333
|
+
const effective = peelWrappers(words, seen);
|
|
334
|
+
if (!effective || effective.length === 0) return seen.has("xargs") ? "write" : "unknown";
|
|
335
|
+
const verdict = programWriteVerdict(effective);
|
|
336
|
+
if (verdict === "unknown" && seen.has("xargs")) return "write";
|
|
337
|
+
return verdict;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function commandAccess(command: string, writableCeiling: boolean): "read" | "write" {
|
|
341
|
+
if (!writableCeiling) return "read";
|
|
342
|
+
for (const segment of commandSegments(command)) {
|
|
343
|
+
if (segmentWriteVerdict(segment) === "write") return "write";
|
|
344
|
+
}
|
|
345
|
+
return "read";
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 任一段的程序不在任何表里就算 unknown:写越界注解要说清「它按 read 跑是因为判不出来」。 */
|
|
349
|
+
export function isUnknownProgram(command: string): boolean {
|
|
350
|
+
const segments = commandSegments(command);
|
|
351
|
+
if (segments.length === 0) return false;
|
|
352
|
+
return segments.some((segment) => segmentWriteVerdict(segment) === "unknown");
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 包管理器与「换包目录」的旗标。
|
|
357
|
+
*
|
|
358
|
+
* 建议里必须用**这家自己的**旗标:给 npm 建议 `--dir`,那条建议自己就跑不起来。
|
|
359
|
+
*/
|
|
360
|
+
export const PACKAGE_MANAGERS: Record<string, string | undefined> = {
|
|
361
|
+
pnpm: "--dir",
|
|
362
|
+
npm: "--prefix",
|
|
363
|
+
yarn: "--cwd",
|
|
364
|
+
bun: "--cwd",
|
|
365
|
+
};
|
|
366
|
+
/** 能把包目录改掉的旗标。判定看全部,建议给各家自己的那个。 */
|
|
367
|
+
export const PACKAGE_DIR_FLAGS = ["--dir", "--prefix", "-C", "--cwd"];
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bash 白名单的匹配与读写归类。
|
|
3
|
+
*
|
|
4
|
+
* 条目两种写法:
|
|
5
|
+
* - 字面量:按空白切词的前缀匹配。`"pnpm test"` 放行 `pnpm test -t foo`,不放行 `pnpm publish`。
|
|
6
|
+
* - `re:` 前缀:正则,整串锚定(不写 ^$ 也整串)。用来表达一个命令族的多种参数。
|
|
7
|
+
*
|
|
8
|
+
* 不自动探测正则:命令参数本身就常含 `*`、`.`、`(`(glob 输入、shell 分支),
|
|
9
|
+
* 猜错方向是误授权,比误拒绝危险得多。
|
|
10
|
+
*
|
|
11
|
+
* 读写归类供探针使用:legacy 的 read / write 列按列定读写(两侧都命中以 write 为准,
|
|
12
|
+
* 保守方向);新的 allow 单列用 commandAccess 从命令本身判读写,read / write 留作兼容列。
|
|
13
|
+
*
|
|
14
|
+
* 从 bash/index.ts 拆出来的规则层:白名单三列的匹配、归类与 bash 配置的解析。
|
|
15
|
+
* 两层接线(角色 md 基线逐字段补进派单载荷、bash.deny 并集作硬底线)在
|
|
16
|
+
* wiring/context.ts 的 applyRoleBaseline。
|
|
17
|
+
* bash/index.ts 仍是唯一入口,这里的东西由它转出口。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { leadingAssignments } from "./env.ts";
|
|
21
|
+
import { commandAccess } from "./programs.ts";
|
|
22
|
+
import { RE_PREFIX, tokens } from "./tokens.ts";
|
|
23
|
+
|
|
24
|
+
export type BashAccess = "read" | "write";
|
|
25
|
+
|
|
26
|
+
export interface BashRule {
|
|
27
|
+
read: string[];
|
|
28
|
+
write: string[];
|
|
29
|
+
/** 新的单列白名单:读写由 commandAccess 从命令本身判,不再预先分列。 */
|
|
30
|
+
allow: string[];
|
|
31
|
+
/** 允许网络(默认关闭,沙箱内使用私有网络命名空间)。 */
|
|
32
|
+
network: boolean;
|
|
33
|
+
/** scope 命中的目录在沙箱里可写(默认只读根)。 */
|
|
34
|
+
writable: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 额外给予写权限的路径(绝对路径、`~` 开头或相对 cwd)。
|
|
37
|
+
* pnpm store、mise 的 state 目录这类全局可写区,不在 scope 里,但工具链要写。
|
|
38
|
+
*/
|
|
39
|
+
writablePaths: string[];
|
|
40
|
+
/** 注入到沙箱里的环境变量(写进 spec 的 env 字段)。 */
|
|
41
|
+
env: Record<string, string>;
|
|
42
|
+
/**
|
|
43
|
+
* 项目内构建/测试产物目录。默认按工具链 marker 判断后放行(并创建),
|
|
44
|
+
* `false` 关闭整份内置清单,数组在内置清单之上追加。
|
|
45
|
+
*/
|
|
46
|
+
scratchPaths: string[] | false;
|
|
47
|
+
/**
|
|
48
|
+
* 结构性禁掉的命令,**不可被 bindings 放宽**。来自项目 profile 的 bash.deny,
|
|
49
|
+
* 与调用方写的 read/write 取并集:即便调用方把同一条命令列进白名单也进不来。
|
|
50
|
+
*/
|
|
51
|
+
deny: string[];
|
|
52
|
+
/**
|
|
53
|
+
* 启动前的依赖完整性闸门(默认开):有 lockfile 却没有 node_modules/.bin 的目录,
|
|
54
|
+
* 让 bash 直接失败并点名,而不是让第一条构建命令在沙箱里去装依赖、然后挂住。
|
|
55
|
+
*
|
|
56
|
+
* 数组是「这些目录不必装」的前缀清单:树里常年躺着没人安装的 lockfile
|
|
57
|
+
* (fixtures/、vendored 的样例工程)时,只关掉整道闸门等于把真包也一起交出去。
|
|
58
|
+
* `false` 才是整道闸门的开关。
|
|
59
|
+
*/
|
|
60
|
+
dependencyGate: string[] | false;
|
|
61
|
+
/**
|
|
62
|
+
* 命令树停止消耗 CPU 超过这么多秒就杀掉(默认关)。
|
|
63
|
+
*
|
|
64
|
+
* 墙钟超时分不开「死锁」和「长编译」,累计 CPU 分得开:干活的树每次采样都在涨,卡住的
|
|
65
|
+
* 树一动不动。判据由 helper 实现 —— 它是子进程 PID 命名空间的 PID 1,看得见整棵子树。
|
|
66
|
+
*/
|
|
67
|
+
cpuStallSeconds: number | false;
|
|
68
|
+
/**
|
|
69
|
+
* 是否给沙箱里的 git 注入全局排除规则(node_modules/、.pnpm-store/)。
|
|
70
|
+
* 默认开:防止子代理 `git add -A` 把依赖目录吸进索引。
|
|
71
|
+
*/
|
|
72
|
+
gitExcludes: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface BashMatch {
|
|
76
|
+
access: BashAccess;
|
|
77
|
+
/** 命中哪一列:legacy 的 read / write,或是新的 allow。 */
|
|
78
|
+
column: "read" | "write" | "allow";
|
|
79
|
+
/** 命中的条目原文,用于拒绝文案与诊断。 */
|
|
80
|
+
entry: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 单条模式是否匹配整条命令。`re:` 条目按正则整串匹配;否则按 token 前缀匹配。
|
|
85
|
+
* 空命令永不匹配。
|
|
86
|
+
*/
|
|
87
|
+
export function commandMatches(pattern: string, command: string): boolean {
|
|
88
|
+
const cmd = command.trim();
|
|
89
|
+
if (!cmd) return false;
|
|
90
|
+
const entry = pattern.trim();
|
|
91
|
+
if (entry.startsWith(RE_PREFIX)) {
|
|
92
|
+
const source = entry.slice(RE_PREFIX.length).trim();
|
|
93
|
+
if (!source) return false;
|
|
94
|
+
try {
|
|
95
|
+
return new RegExp(`^(?:${source})$`).test(cmd);
|
|
96
|
+
} catch {
|
|
97
|
+
// 写坏的正则不匹配任何命令(fail closed),而不是让整条规则消失。
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const want = tokens(entry);
|
|
102
|
+
if (want.length === 0) return false;
|
|
103
|
+
// 赋值前缀先剥掉再比:条目的第一个 token 指的是程序名,不是它前面的环境变量。
|
|
104
|
+
const have = tokens(leadingAssignments(cmd).rest);
|
|
105
|
+
return want.every((token, i) => have[i] === token);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 归类一条命令。legacy 两列先判且各按原义:write 命中 = write,read 命中 = read;
|
|
110
|
+
* allow 列最后判,读写由 commandAccess 从命令本身推出来。
|
|
111
|
+
*
|
|
112
|
+
* allow 排在 legacy 两列之后是刻意的:同一条命令同时列进 write 与 allow 时,legacy 语义
|
|
113
|
+
* 必须原样保留(这个兼容阶段不改老配置的行为);先判 allow 会把它重新判成可能的 read。
|
|
114
|
+
*/
|
|
115
|
+
export function classifyCommand(rule: BashRule, command: string): BashMatch | undefined {
|
|
116
|
+
const write = rule.write.find((entry) => commandMatches(entry, command));
|
|
117
|
+
if (write !== undefined) return { access: "write", column: "write", entry: write };
|
|
118
|
+
const read = rule.read.find((entry) => commandMatches(entry, command));
|
|
119
|
+
if (read !== undefined) return { access: "read", column: "read", entry: read };
|
|
120
|
+
const allow = rule.allow.find((entry) => commandMatches(entry, command));
|
|
121
|
+
if (allow !== undefined) return { access: commandAccess(command, rule.writable), column: "allow", entry: allow };
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 全部条目(供拒绝文案与诊断),三列都要列。 */
|
|
126
|
+
export function allEntries(rule: BashRule): string[] {
|
|
127
|
+
return [...rule.read, ...rule.write, ...rule.allow];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** 命中禁止列表时返回该条目原文,否则 undefined。 */
|
|
131
|
+
export function deniedEntry(rule: BashRule, command: string): string | undefined {
|
|
132
|
+
return rule.deny.find((entry) => commandMatches(entry, command));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function stringList(value: unknown): string[] {
|
|
136
|
+
if (!Array.isArray(value)) return [];
|
|
137
|
+
return (value as unknown[])
|
|
138
|
+
.filter((v): v is string => typeof v === "string" && v.trim() !== "")
|
|
139
|
+
.map((v) => v.trim());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function stringMap(value: unknown): Record<string, string> {
|
|
143
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
|
144
|
+
const out: Record<string, string> = {};
|
|
145
|
+
for (const [key, raw] of Object.entries(value as Record<string, unknown>)) {
|
|
146
|
+
if (!key.trim()) continue;
|
|
147
|
+
if (typeof raw !== "string" && typeof raw !== "number") continue;
|
|
148
|
+
out[key.trim()] = String(raw);
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 解析 bindings 里的 bash 字段。没写、写坏、三列都空都返回 undefined(= 拒绝 bash)。
|
|
155
|
+
* `read` / `write` 是兼容列,含义照旧;`allow` 是新的单列,读写留给 commandAccess 判。
|
|
156
|
+
*/
|
|
157
|
+
export function parseBashRule(raw: unknown): BashRule | undefined {
|
|
158
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
159
|
+
const cfg = raw as Record<string, unknown>;
|
|
160
|
+
const read = stringList(cfg.read);
|
|
161
|
+
const write = stringList(cfg.write);
|
|
162
|
+
const allow = stringList(cfg.allow);
|
|
163
|
+
if (read.length === 0 && write.length === 0 && allow.length === 0) return undefined;
|
|
164
|
+
return {
|
|
165
|
+
read,
|
|
166
|
+
write,
|
|
167
|
+
allow,
|
|
168
|
+
network: cfg.network === true,
|
|
169
|
+
writable: cfg.writable === true,
|
|
170
|
+
writablePaths: stringList(cfg.writablePaths),
|
|
171
|
+
env: stringMap(cfg.env),
|
|
172
|
+
scratchPaths: cfg.scratchPaths === false ? false : stringList(cfg.scratchPaths),
|
|
173
|
+
gitExcludes: cfg.gitExcludes !== false,
|
|
174
|
+
deny: stringList(cfg.deny),
|
|
175
|
+
dependencyGate: cfg.dependencyGate === false ? false : stringList(cfg.dependencyGate),
|
|
176
|
+
// 未写 = 关。这条看门狗杀掉整棵进程树,默认打开会误杀刻意等待的命令(sleep、退避轮询),
|
|
177
|
+
// 而 helper 看不到输出(stdio 是继承的,不走管道),没法用「无输出」佐证。
|
|
178
|
+
cpuStallSeconds: stallSeconds(cfg.cpuStallSeconds),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** `cpuStallSeconds`:未写、写坏、`false`、小于等于 0 一律当关闭。 */
|
|
183
|
+
function stallSeconds(value: unknown): number | false {
|
|
184
|
+
// 先 floor 再判断:否则 0.5 会得到 0,而 0 !== false 会把 `stallSeconds: 0` 写进 spec ——
|
|
185
|
+
// 旧版 helper 的 deny_unknown_fields 会因此整份拒绝(退出码 77),正好是「关掉时不写键」
|
|
186
|
+
// 想避免的那种失败。也不接受非数字:`true` 经 Number() 会变成 1,即「静默 1 秒就杀」。
|
|
187
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return false;
|
|
188
|
+
const n = Math.floor(value);
|
|
189
|
+
if (n <= 0) return false;
|
|
190
|
+
return n;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 「授权写了却永远命中不了」的判定结果。
|
|
195
|
+
*/
|
|
196
|
+
export interface UnreachableEntry {
|
|
197
|
+
/** 条目原文。 */
|
|
198
|
+
entry: string;
|
|
199
|
+
/** 为什么命中不了:一句可核对的事实。 */
|
|
200
|
+
why: string;
|
|
201
|
+
/**
|
|
202
|
+
* 判定时实际查看的目录:cwd 的**解析后**路径(`realpathSync`),解不出时才是字面路径。
|
|
203
|
+
*
|
|
204
|
+
* 文案要印这个而不是字面 cwd:cwd 经软链进来时,读者按字面链一查就能找到 manifest,
|
|
205
|
+
* 那条「永远命中不了」的结论于是看起来是错的。
|
|
206
|
+
*/
|
|
207
|
+
lookedIn?: string;
|
|
208
|
+
/** 改成什么:拿不到可靠改法时没有这个字段。 */
|
|
209
|
+
fix?: string;
|
|
210
|
+
/**
|
|
211
|
+
* cwd 下面所有带 package.json 的目录(相对 cwd 的路径)。
|
|
212
|
+
*
|
|
213
|
+
* 有多个候选时给不出可靠的改法:这条授权原本指哪个包只有父代理知道。
|
|
214
|
+
*/
|
|
215
|
+
candidates?: string[];
|
|
216
|
+
/**
|
|
217
|
+
* 恰好一个候选包、但那个包里没有条目点名的脚本(`pnpm run typecheck` 而包里只有 `lint`)。
|
|
218
|
+
*
|
|
219
|
+
* 这是「脚本不存在」,不是「授权不可达」:命令跑起来会得到 pnpm 自己的
|
|
220
|
+
* "Command not found",改包目录也治不好。有了这个标记,文案才能说清「没有候选包带这个
|
|
221
|
+
* 脚本」,而不是让读者去几个候选里「挑一个」—— 挑哪一个都不行。
|
|
222
|
+
*/
|
|
223
|
+
scriptMissing?: string;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** 首个词:条目与命令的程序位。 */
|
|
227
|
+
export function firstToken(text: string): string {
|
|
228
|
+
const head = tokens(text.trim())[0];
|
|
229
|
+
return head && head !== "" ? head : "";
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 一条文案里最多列几项。monorepo 下候选包可能有几百个,一条告警不该被它撑爆。 */
|
|
233
|
+
export const MAX_LISTED = 8;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 把一组路径拼进一条文案,超过上限就截断并说还剩几条。
|
|
237
|
+
*
|
|
238
|
+
* 截断放在**渲染**这一层,不放在收集处:`candidates.length !== 1` 决定「给不给改法」,
|
|
239
|
+
* 收集时就截断会把「12 个候选,判不了」写成「8 个候选,判不了」—— 结论没变,但读者
|
|
240
|
+
* 以为只有 8 个,而且 `… and N more` 里的 N 也没地方算。收集保持完整,只有给人看的
|
|
241
|
+
* 那串字有上限。
|
|
242
|
+
*/
|
|
243
|
+
export function listedJoin(items: string[]): string {
|
|
244
|
+
if (items.length <= MAX_LISTED) return items.join(", ");
|
|
245
|
+
const rest = items.length - MAX_LISTED;
|
|
246
|
+
return `${items.slice(0, MAX_LISTED).join(", ")}, … and ${rest} more`;
|
|
247
|
+
}
|