@duanz/pi-pi-cmd-guard 0.0.0-stage → 1.0.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.
Files changed (3) hide show
  1. package/index.ts +132 -0
  2. package/package.json +27 -4
  3. package/README.md +0 -3
package/index.ts ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * pi-cmd-guard — 命令级硬拦截
3
+ *
4
+ * 目的:把「会打印明文密钥的命令」变成发不出去,而不是执行完再遮蔽。
5
+ * 触发点:tool_call(bash),返回 { block: true } 后命令根本不执行。
6
+ *
7
+ * 覆盖场景:2026-11-05 事故(sudo systemctl cat caddy 把 Environment=ALI_ACCESS_KEY_SECRET 打进上下文)。
8
+ *
9
+ * 逃生门:PI_CMD_GUARD=off 关闭;或用户自己敲 `!` 命令(user_bash 不经过这里)。
10
+ */
11
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
12
+
13
+ type Rule = {
14
+ name: string;
15
+ re: RegExp;
16
+ why: string;
17
+ alt: string;
18
+ };
19
+
20
+ export const RULES: Rule[] = [
21
+ {
22
+ name: "直接重启 pi-web",
23
+ re: /\bsystemctl\s+(?:--user\s+)?(?:restart|stop|kill)\s+[^|;&\n]*\bpi-web\b/,
24
+ why: "会直接掐断其他正在跑的会话(in-flight 轮次全部丢失)",
25
+ alt: "用 d_restart 工具,或终端跑 d-restart(旧名 safe_restart / piweb-safe-restart);自动快照忙碌会话并在就绪后唤醒接力",
26
+ },
27
+ {
28
+ name: "systemctl cat",
29
+ re: /\bsystemctl\s+cat\b/,
30
+ why: "会把 unit 里的 Environment= 整行打出来,里面常有明文密钥",
31
+ alt: "systemctl show <unit> -p EnvironmentFiles,ExecStart,WorkingDirectory,User",
32
+ },
33
+ {
34
+ name: "systemctl show Environment",
35
+ re: /\bsystemctl\s+show\b[^|;&\n]*(?:-p|--property)[= ]*[^|;&\n]*\bEnvironment\b(?!Files)/,
36
+ why: "直接 dump 环境变量的值",
37
+ alt: "systemctl show <unit> -p EnvironmentFiles(只给文件路径)",
38
+ },
39
+ {
40
+ name: "裸 env / printenv",
41
+ re: /(?:^|[;&|(]\s*|\bsudo\s+)(?:env|printenv)\s*(?:$|[;&|)])/,
42
+ why: "会 dump 当前进程全部环境变量",
43
+ alt: "printenv | cut -d= -f1(只看变量名)",
44
+ },
45
+ {
46
+ name: "printenv 指定敏感变量",
47
+ re: /\bprintenv\s+[A-Za-z0-9_]*(?:SECRET|TOKEN|KEY|PASSWORD|PASSWD|CREDENTIAL)/i,
48
+ why: "直接打印该变量的明文值",
49
+ alt: "safe_inspect action=env + unit 名(只出变量名)",
50
+ },
51
+ {
52
+ name: "cat 密钥文件",
53
+ re: /\bcat\s+[^|;&\n]*?(?:\.env(?:\.[a-z]+)?\b|secrets?\.json|credentials?|\.netrc|id_rsa\b|id_ed25519\b|\.pem\b|\.key\b)/,
54
+ why: "文件内容就是明文密钥",
55
+ alt: "safe_inspect action=secret + 路径(只出 存在/长度/sha8)",
56
+ },
57
+ {
58
+ name: "读 /proc/*/environ",
59
+ re: /\/proc\/[^/\s]+\/environ/,
60
+ why: "进程环境块的原始字节,全是明文",
61
+ alt: "tr '\\0' '\\n' < /proc/PID/environ | cut -d= -f1(只看变量名)",
62
+ },
63
+ {
64
+ name: "grep 全环境",
65
+ re: /\bgrep\b[^|;&\n]*\s(?:\/proc\/self\/environ|-z\s*\/proc)/,
66
+ why: "在环境块里全文搜索会把命中的值一起打出来",
67
+ alt: "先用 cut -d= -f1 只取变量名,再筛",
68
+ },
69
+ ];
70
+
71
+ /** 把引号内的内容抹成空格再匹配:避免「文档里提到某命令」被当成「要执行它」。
72
+ * 代价:`bash -c "..."` 这类引号内真实执行的命令会漏拦,可接受。 */
73
+ export function stripQuoted(s: string): string {
74
+ let out = "";
75
+ let q: string | null = null;
76
+ for (let i = 0; i < s.length; i++) {
77
+ const c = s[i];
78
+ if (q) {
79
+ if (c === "\\" && q === '"') {
80
+ out += " ";
81
+ i++;
82
+ continue;
83
+ }
84
+ if (c === q) {
85
+ q = null;
86
+ out += c;
87
+ continue;
88
+ }
89
+ out += " ";
90
+ continue;
91
+ }
92
+ if (c === "'" || c === '"') {
93
+ q = c;
94
+ out += c;
95
+ continue;
96
+ }
97
+ out += c;
98
+ }
99
+ return out;
100
+ }
101
+
102
+ /** 返回命中的规则;null = 放行。扩展与测试共用同一份判定。 */
103
+ export function checkCommand(cmd: string): Rule | null {
104
+ const probe = stripQuoted(cmd);
105
+ for (const rule of RULES) {
106
+ if (!rule.re.test(probe)) continue;
107
+ // 裸 env/printenv 管道给 cut/head/wc 之类加工时不拦(只出变量名或截断,不 dump 值)
108
+ if (rule.name === "裸 env / printenv" && /\|\s*(?:cut|head|wc|sort|uniq|awk)\b/.test(probe)) continue;
109
+ return rule;
110
+ }
111
+ return null;
112
+ }
113
+
114
+ export default function (pi: ExtensionAPI) {
115
+ pi.on("tool_call", async (event) => {
116
+ if (process.env.PI_CMD_GUARD === "off") return;
117
+ if (event.toolName !== "bash") return;
118
+
119
+ const cmd = String((event.input as any)?.command ?? "");
120
+ if (!cmd) return;
121
+
122
+ const rule = checkCommand(cmd);
123
+ if (!rule) return;
124
+ return {
125
+ block: true,
126
+ reason:
127
+ `[cmd-guard] 已拦下「${rule.name}」:${rule.why}。\n` +
128
+ `改用:${rule.alt}\n` +
129
+ `(这条拦截不依赖模型是否记得规则,命令根本没执行。确需放行时让用户自己敲 ! 命令。)`,
130
+ };
131
+ });
132
+ }
package/package.json CHANGED
@@ -1,6 +1,29 @@
1
1
  {
2
2
  "name": "@duanz/pi-pi-cmd-guard",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "拦住会毁东西的命令:cat .env、裸 env、systemctl restart pi-web",
5
+ "type": "module",
6
+ "keywords": [
7
+ "pi-package",
8
+ "pi-extension"
9
+ ],
10
+ "license": "MIT",
11
+ "author": "DuanZ",
12
+ "homepage": "https://github.com/DuanZGit/D-Hub/tree/master/extensions/pi-cmd-guard",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/DuanZGit/D-Hub.git",
16
+ "directory": "extensions/pi-cmd-guard"
17
+ },
18
+ "files": [
19
+ "index.ts"
20
+ ],
21
+ "pi": {
22
+ "extensions": [
23
+ "./index.ts"
24
+ ]
25
+ },
26
+ "engines": {
27
+ "node": ">=20"
28
+ }
29
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.