@duanz/pi-pi-cmd-guard 0.0.0-stage → 1.0.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.
Files changed (4) hide show
  1. package/README.en.md +57 -0
  2. package/README.md +56 -2
  3. package/index.ts +132 -0
  4. package/package.json +31 -4
package/README.en.md ADDED
@@ -0,0 +1,57 @@
1
+ # pi-cmd-guard
2
+
3
+ <p align="center"><strong>English</strong> | <a href="./README.md">中文</a></p>
4
+
5
+ <p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/pi-cmd-guard.png" alt="pi-cmd-guard cover" width="880"></p>
6
+
7
+ <p align="center"><strong>Commands that would print plaintext secrets never run</strong><br><sub>Blocked at tool_call · with an alternative that works</sub></p>
8
+
9
+ <p align="center">
10
+ <a href="https://www.npmjs.com/package/@duanz/pi-pi-cmd-guard"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-pi-cmd-guard.svg?style=flat-square" alt="npm"></a>
11
+ <a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-pi-cmd-guard.svg?style=flat-square" alt="license"></a>
12
+ <a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
13
+ <img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
14
+ </p>
15
+
16
+ <p align="center">
17
+ <code>pi install npm:@duanz/pi-pi-cmd-guard</code>
18
+ </p>
19
+
20
+ ## Why
21
+
22
+ The incident: one `sudo systemctl cat caddy` printed the unit's `Environment=ALI_ACCESS_KEY_SECRET=...` line straight into context. The command succeeded, and the secret went into the transcript.
23
+
24
+ Masking afterwards is pointless — the value already left. This blocks **before execution**.
25
+
26
+ ## Rules
27
+
28
+ | Rule | Why it is dangerous | Instead |
29
+ | --- | --- | --- |
30
+ | `systemctl restart/stop/kill pi-web` | Kills other running sessions and in-flight turns | `d_restart` tool or `d-restart` |
31
+ | `systemctl cat` | Prints `Environment=` with plaintext values | `systemctl show <unit> -p EnvironmentFiles,ExecStart,User` |
32
+ | `systemctl show ... Environment` | Dumps variable values | `systemctl show <unit> -p EnvironmentFiles` |
33
+ | bare `env` / `printenv` | Dumps the whole process environment | Ask for one variable by name |
34
+ | `printenv <secret>` | Prints that value | Inspect existence / length / hash |
35
+ | `cat <secret file>` | The file *is* the secret | Check permissions and hash |
36
+ | Reading `/proc/*/environ` | Raw process environment bytes | Same as above |
37
+ | `grep` over the environment | Matching lines carry the values | Filter by allowlisted field names |
38
+
39
+ ## Usage
40
+
41
+ Enabled on install, nothing to configure.
42
+
43
+ - Disable: `PI_CMD_GUARD=off`
44
+ - Commands the user types (`!` prefix) bypass this hook and still work
45
+
46
+ ## How
47
+
48
+ A `tool_call` hook matches bash commands and returns `{ block: true, reason, alternative }`, so the command never runs. `RULES` is one regex + one reason + one alternative per entry — adding a rule is one object.
49
+
50
+ ## Limits
51
+
52
+ - Covers the `bash` tool only; user `!` commands and external scripts are out of scope.
53
+ - Patterns are regexes; deliberate obfuscation can slip through. This is anti-accident, not anti-attacker.
54
+
55
+ ## License
56
+
57
+ MIT © DuanZ
package/README.md CHANGED
@@ -1,3 +1,57 @@
1
- # Temporary Holding Version
1
+ # pi-cmd-guard
2
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.
3
+ <p align="center"><a href="./README.en.md">English</a> | <strong>中文</strong></p>
4
+
5
+ <p align="center"><img src="https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/pi-cmd-guard.png" alt="pi-cmd-guard cover" width="880"></p>
6
+
7
+ <p align="center"><strong>会打印明文密钥的命令,根本发不出去</strong><br><sub>tool_call 阶段拦截 · 命令不执行 · 给替代写法</sub></p>
8
+
9
+ <p align="center">
10
+ <a href="https://www.npmjs.com/package/@duanz/pi-pi-cmd-guard"><img src="https://img.shields.io/npm/v/@duanz%2Fpi-pi-cmd-guard.svg?style=flat-square" alt="npm"></a>
11
+ <a href="https://github.com/DuanZGit/D-Hub/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/@duanz%2Fpi-pi-cmd-guard.svg?style=flat-square" alt="license"></a>
12
+ <a href="https://github.com/DuanZGit/D-Hub"><img src="https://img.shields.io/badge/GitHub-DuanZGit%2FD--Hub-181717?style=flat-square&logo=github" alt="GitHub"></a>
13
+ <img src="https://img.shields.io/badge/pi-extension-34d399?style=flat-square" alt="pi extension">
14
+ </p>
15
+
16
+ <p align="center">
17
+ <code>pi install npm:@duanz/pi-pi-cmd-guard</code>
18
+ </p>
19
+
20
+ ## 它解决什么
21
+
22
+ 2026-11-05 的事故:一次 `sudo systemctl cat caddy` 把 unit 里的 `Environment=ALI_ACCESS_KEY_SECRET=...` 整行打进了上下文。命令成功执行,密钥随后进了对话记录。
23
+
24
+ 事后遮蔽没有意义——值已经发出去了。所以要拦在**执行之前**。
25
+
26
+ ## 拦截清单
27
+
28
+ | 规则 | 为什么危险 | 替代写法 |
29
+ | --- | --- | --- |
30
+ | `systemctl restart/stop/kill pi-web` | 掐断其他正在跑的会话,in-flight 轮次全丢 | `d_restart` 工具或 `d-restart` 命令 |
31
+ | `systemctl cat` | unit 里的 `Environment=` 整行带明文 | `systemctl show <unit> -p EnvironmentFiles,ExecStart,User` |
32
+ | `systemctl show ... Environment` | 直接 dump 变量值 | `systemctl show <unit> -p EnvironmentFiles` |
33
+ | 裸 `env` / `printenv` | dump 当前进程全部环境变量 | 只查确定的变量名 |
34
+ | `printenv <敏感名>` | 打印该变量明文 | 用 `safe_inspect` 类工具看存在/长度/哈希 |
35
+ | `cat <密钥文件>` | 文件内容就是明文密钥 | 看权限与哈希,不看值 |
36
+ | 读 `/proc/*/environ` | 进程环境块原始字节 | 同上 |
37
+ | `grep` 全环境 | 命中行把值一起打出来 | 按字段名白名单过滤 |
38
+
39
+ ## 用法
40
+
41
+ 装完即生效,无需配置。
42
+
43
+ - 关闭:`PI_CMD_GUARD=off`
44
+ - 用户自己敲的命令(`!` 开头)不经过这里,仍然可以执行
45
+
46
+ ## 实现
47
+
48
+ 在 `tool_call` 钩子里匹配 bash 命令,返回 `{ block: true, reason, alternative }`,命令根本不会执行。规则表 `RULES` 是一条正则 + 一句原因 + 一个替代写法,加新规则只加一条。
49
+
50
+ ## 限制
51
+
52
+ - 只覆盖 `bash` 工具;其他执行路径(用户 `!` 命令、外部脚本)不在范围内。
53
+ - 规则是正则,刻意的变形写法可能绕过——这是防误触,不是防攻击。
54
+
55
+ ## License
56
+
57
+ MIT © DuanZ
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,33 @@
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.1",
4
+ "description": "拦住会毁东西的命令:cat .env、裸 env、systemctl restart pi-web",
5
+ "type": "module",
6
+ "keywords": [
7
+ "pi-package",
8
+ "pi-extension",
9
+ "pi-web"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "DuanZ",
13
+ "homepage": "https://github.com/DuanZGit/D-Hub/tree/master/extensions/pi-cmd-guard",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/DuanZGit/D-Hub.git",
17
+ "directory": "extensions/pi-cmd-guard"
18
+ },
19
+ "files": [
20
+ "index.ts",
21
+ "README.md",
22
+ "README.en.md"
23
+ ],
24
+ "pi": {
25
+ "extensions": [
26
+ "./index.ts"
27
+ ],
28
+ "image": "https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/pi-cmd-guard.png"
29
+ },
30
+ "engines": {
31
+ "node": ">=20"
32
+ }
33
+ }