@yaosu/pi-path-guard 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.
- package/LICENSE +21 -0
- package/README.md +86 -0
- package/extensions/path-guard.ts +1287 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yaosu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# pi-path-guard
|
|
2
|
+
|
|
3
|
+
**Path Guard — pi extension: prevents accidental deletes / overwrites / edits**
|
|
4
|
+
**Path Guard — pi 扩展:防误删 / 防误覆盖 / 防误改**
|
|
5
|
+
|
|
6
|
+
Intercepts destructive operations in tool calls (`bash`, `write`, `edit`): protected paths (`.env`, `.ssh`, keys, credentials, …), system-destructive commands (`mkfs`/`reboot`/block-device writes/bulk deletes, …), overwrites/deletes outside the project, and `>` truncation of existing files — deciding **block / confirm / pass** per guard mode.
|
|
7
|
+
|
|
8
|
+
拦截 `bash` / `write` / `edit` 等工具调用中的破坏性操作:受保护路径(`.env`、`.ssh`、密钥、凭据等)、系统级破坏命令(mkfs/reboot/写块设备/批量删除等)、项目外覆盖/删除、`>` 截断已有文件等,按防护模式决定 **阻止 / 询问 / 放行**。
|
|
9
|
+
|
|
10
|
+
> ⚠️ **Security notice / 安全提示**: pi extensions run with full system permissions and can execute arbitrary code. Review the source before installing (this project is open source — see `extensions/path-guard.ts`).
|
|
11
|
+
> pi 扩展拥有完整系统权限,可执行任意代码。安装前请审阅源码(本项目源码开源,见 `extensions/path-guard.ts`)。
|
|
12
|
+
|
|
13
|
+
## Install / 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# From git (recommended / 推荐)
|
|
17
|
+
pi install git:github.com/yaodashanren/pi-path-guard@v1
|
|
18
|
+
|
|
19
|
+
# Local directory (development / 本地目录,开发用)
|
|
20
|
+
pi install /path/to/pi-path-guard
|
|
21
|
+
|
|
22
|
+
# Try without installing (no settings change / 临时试用,不写入 settings)
|
|
23
|
+
pi -e ./pi-path-guard
|
|
24
|
+
|
|
25
|
+
# npm (after publishing / 发布后): scope package
|
|
26
|
+
# pi install npm:@yaosu/pi-path-guard
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
After installing, run `/reload` or restart pi. 安装后 `/reload` 或重启 pi 生效。
|
|
30
|
+
|
|
31
|
+
## Usage / 使用
|
|
32
|
+
|
|
33
|
+
### `/guard` command
|
|
34
|
+
|
|
35
|
+
- `/guard` — interactive mode picker (title shows the full decision matrix; choices are bilingual) 交互式选择防护模式(标题展示完整判定矩阵,选项中英双语)
|
|
36
|
+
- `/guard <strict|normal|loose|trusted>` — quick switch (trusted requires a warning confirmation) 快捷切换(trusted 需警告确认)
|
|
37
|
+
- Invalid argument → falls back to the interactive picker 非法参数 → 兜底弹出交互选择
|
|
38
|
+
- Every new session resets to `normal` 每次新会话自动回到 `normal`
|
|
39
|
+
|
|
40
|
+
### Guard mode matrix / 防护模式矩阵
|
|
41
|
+
|
|
42
|
+
| Checkpoint / 判定点 | strict | normal | loose | trusted |
|
|
43
|
+
| --- | --- | --- | --- | --- |
|
|
44
|
+
| Protected paths (.env/.ssh/keys/credentials) / 受保护路径 | block | block | block | block |
|
|
45
|
+
| Block group (mkfs/reboot/block-device writes/bulk delete) / Block 组危险命令 | block | block | block | block |
|
|
46
|
+
| Confirm group (sudo/ssh/chmod 777 …) / Confirm 组 | block | confirm | confirm | confirm |
|
|
47
|
+
| git destructive (reset --hard/clean -f …) / git 破坏性 | confirm | confirm | confirm | confirm |
|
|
48
|
+
| In-project write/edit/new / 项目内写/改/新建 | confirm | pass | pass | pass |
|
|
49
|
+
| In-project delete / 项目内删除 | confirm | confirm | pass | pass |
|
|
50
|
+
| Outside write (new file) / 项目外写新文件 | confirm | confirm | pass | pass |
|
|
51
|
+
| Outside overwrite existing / 项目外覆盖已存在 | block | block | confirm | pass |
|
|
52
|
+
| Outside delete ordinary / 项目外删除普通文件 | block | block | confirm | pass |
|
|
53
|
+
| `>` truncate existing file / 截断已有文件 | confirm | confirm | confirm | confirm |
|
|
54
|
+
| cwd=HOME write / HOME 目录写 | confirm | confirm | pass | pass |
|
|
55
|
+
| No UI (headless) / 无交互界面 | block* | block* | block* | block* |
|
|
56
|
+
|
|
57
|
+
*block = denied directly, no confirmation opportunity / 直接阻止,无确认机会;confirm = prompt / 弹窗询问;pass = allow / 放行;\*headless: items that would be confirmed are blocked instead / 无 UI 时需确认项一律阻止
|
|
58
|
+
|
|
59
|
+
### Core capabilities / 核心能力
|
|
60
|
+
|
|
61
|
+
- **Protected-path interception / 受保护路径拦截**: `.env` / `.ssh` / `.aws` / `.kube` / private keys (`*.pem`/`*.key`) / credentials / shell configs (`.bashrc` …) / `node_modules` / `dist` / `build` … blocked hard in every mode — 任何模式下硬性阻止
|
|
62
|
+
- **Block group / Block 组危险命令**: `mkfs.*` / `mkswap` / `poweroff` / `reboot` / `shutdown` / `dd` to block devices / `> /dev/sdX` / `find -delete` / `find -exec rm` / `xargs rm`
|
|
63
|
+
- **Confirm group / Confirm 组**: `sudo` / `doas` / `pkexec` / `chmod 777` / `ssh` / `scp` / `sftp` / `rsh` / `telnet` / `wget -O /dev/null`
|
|
64
|
+
- **Overwrite detection / 覆盖检测**: `mv` / `cp` / `install` / `tee` / `ln -f` / `rsync --delete` on existing targets, classified by in/out project — 目标已存在时按内外策略处理
|
|
65
|
+
- **Redirect truncation / 重定向截断**: `> existing file` (incl. `2>` / `&>`, excluding `>>` and devices) → confirm
|
|
66
|
+
- **Shell wrapper recursion / shell 包装器递归**: strips `sudo`/`nohup`/`timeout`/`env` … prefixes, recurses into `bash -c` / `eval`; quote-aware tokenization — 前缀剥除后分析真实命令;引号感知分词
|
|
67
|
+
- **git destructive commands / git 破坏性命令**: `clean -f` / `reset --hard` / `checkout -- .` / `branch -D` / `push --force` / `stash drop`
|
|
68
|
+
- **Bypass resistance / 防绕过**: variable/wildcard paths that can't be statically resolved always confirm; any hard block in a compound command blocks the whole thing — 变量/通配符路径一律 confirm;复合命令任一段硬性阻止则整体阻止
|
|
69
|
+
|
|
70
|
+
## Development / 开发与测试
|
|
71
|
+
|
|
72
|
+
Automated tests (83 assertions) load the real extension with a mocked pi API, covering the 4 modes × protected paths / dangerous commands / truncation / git destructive matrix, plus `/guard` command interaction and trusted-mode confirmation flow:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
cd tests && node --experimental-strip-types test-pathguard.ts
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
自动化测试(83 断言)模拟 pi API 加载真实扩展,覆盖 4 种模式 × 受保护路径 / 危险命令 / 截断 / git 破坏性等判定矩阵,以及 `/guard` 命令交互与 trusted 确认流程:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
cd tests && node --experimental-strip-types test-pathguard.ts
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT © yaosu
|