@duanz/pi-pi-cmd-guard 1.0.0 → 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.
- package/README.en.md +57 -0
- package/README.md +57 -0
- package/package.json +8 -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
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# pi-cmd-guard
|
|
2
|
+
|
|
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/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duanz/pi-pi-cmd-guard",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "拦住会毁东西的命令:cat .env、裸 env、systemctl restart pi-web",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"pi-package",
|
|
8
|
-
"pi-extension"
|
|
8
|
+
"pi-extension",
|
|
9
|
+
"pi-web"
|
|
9
10
|
],
|
|
10
11
|
"license": "MIT",
|
|
11
12
|
"author": "DuanZ",
|
|
@@ -16,12 +17,15 @@
|
|
|
16
17
|
"directory": "extensions/pi-cmd-guard"
|
|
17
18
|
},
|
|
18
19
|
"files": [
|
|
19
|
-
"index.ts"
|
|
20
|
+
"index.ts",
|
|
21
|
+
"README.md",
|
|
22
|
+
"README.en.md"
|
|
20
23
|
],
|
|
21
24
|
"pi": {
|
|
22
25
|
"extensions": [
|
|
23
26
|
"./index.ts"
|
|
24
|
-
]
|
|
27
|
+
],
|
|
28
|
+
"image": "https://raw.githubusercontent.com/DuanZGit/D-Hub/master/docs/covers/pi-cmd-guard.png"
|
|
25
29
|
},
|
|
26
30
|
"engines": {
|
|
27
31
|
"node": ">=20"
|