@jalaxy33/pi-permission 0.0.0-stage → 0.4.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 +134 -2
- package/docs/configuration.md +237 -0
- package/index.ts +97 -0
- package/package.json +54 -4
- package/rules/index.json +71 -0
- package/rules/packs/bulk-delete.json +83 -0
- package/rules/packs/database.json +30 -0
- package/rules/packs/git-local.json +81 -0
- package/rules/packs/git-remote.json +27 -0
- package/rules/packs/infra.json +45 -0
- package/rules/packs/irreversible.json +93 -0
- package/rules/packs/privilege.json +50 -0
- package/rules/packs/publish.json +33 -0
- package/rules/packs/secrets.json +43 -0
- package/schema.json +348 -0
- package/src/argv.ts +67 -0
- package/src/config.ts +201 -0
- package/src/decide.ts +90 -0
- package/src/gate.ts +208 -0
- package/src/match.ts +58 -0
- package/src/ops.ts +244 -0
- package/src/outside.ts +87 -0
- package/src/paths.ts +194 -0
- package/src/prompt.ts +275 -0
- package/src/rulesets.ts +444 -0
- package/src/tokenize.ts +222 -0
- package/src/types.ts +167 -0
- package/src/validate.ts +125 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 jalaxy33
|
|
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
CHANGED
|
@@ -1,3 +1,135 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-permission
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A permission gate for pi. Every tool call gets one of three answers: run it, ask the user, or block it.
|
|
4
|
+
|
|
5
|
+
Three gates, checked in order:
|
|
6
|
+
|
|
7
|
+
1. **Path rules** — file tools and bash paths are matched by operation: `read`, `write`, `delete`. A match can deny, ask, or allow.
|
|
8
|
+
2. **Outside the workspace** — a write or delete outside the session `cwd` asks first, unless the path is on the allow list.
|
|
9
|
+
3. **Commands** — each command in a bash string is matched against command rules. Text inside heredocs, quotes and comments is never matched.
|
|
10
|
+
|
|
11
|
+
When nothing matches, `defaultPermission` decides: `allow` (default), `ask`, or `deny`.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pi install npm:@jalaxy33/pi-permission
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
From the repository:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pi install git:github.com/jalaxy33/pi-permission
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Then run `/reload`.
|
|
26
|
+
|
|
27
|
+
## Keys
|
|
28
|
+
|
|
29
|
+
| Key | Action |
|
|
30
|
+
|---|---|
|
|
31
|
+
| `y` / `Enter` | allow once |
|
|
32
|
+
| `a` | allow this rule with these flags, this rule and operation, or this directory, for the rest of the session |
|
|
33
|
+
| `n` / `Esc` / `Ctrl+C` | deny; the turn continues |
|
|
34
|
+
| `s` | deny and stop the turn |
|
|
35
|
+
| `j` / `k` / `↑` / `↓` · `o` | scroll · expand the command view |
|
|
36
|
+
|
|
37
|
+
Without a UI the gate fails closed: a call that would ask is blocked instead.
|
|
38
|
+
|
|
39
|
+
## Commands
|
|
40
|
+
|
|
41
|
+
| Command | Action |
|
|
42
|
+
|---|---|
|
|
43
|
+
| `/permission` | show the active preset, selected rulesets, removals, defaults, sources and warnings |
|
|
44
|
+
| `/permission:reload` | re-read the config without restarting pi |
|
|
45
|
+
|
|
46
|
+
## Concepts
|
|
47
|
+
|
|
48
|
+
| Concept | What it is |
|
|
49
|
+
|---|---|
|
|
50
|
+
| **ruleset** | A leaf bundle of rules. It never includes another ruleset, so references cannot form a cycle. |
|
|
51
|
+
| **preset** | A permission level: `minimal`, `standard`, `cautious`, `paranoid`. A flat list of rulesets. |
|
|
52
|
+
| **config file** | Picks one preset, adds rulesets, and may override rules. |
|
|
53
|
+
|
|
54
|
+
Presets and include lists are single values: the last config file that declares one replaces the value below it. Rules merge, and a config rule overrides a bundled rule with the same `id`.
|
|
55
|
+
|
|
56
|
+
## Configuration
|
|
57
|
+
|
|
58
|
+
| File | Scope |
|
|
59
|
+
|---|---|
|
|
60
|
+
| `~/.pi/agent/permission.json` | all projects |
|
|
61
|
+
| `<project>/.pi/permission.json` | one project, only after the project is trusted |
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"preset": { "id": "cautious", "remove": ["bulk-delete"] },
|
|
66
|
+
"include": [
|
|
67
|
+
"git-local",
|
|
68
|
+
{ "id": "git-remote", "remove": ["gh-delete"] }
|
|
69
|
+
],
|
|
70
|
+
"defaultPermission": { "commands": "allow", "paths": "allow" },
|
|
71
|
+
"outsideCwd": { "mode": "ask", "allowPaths": ["~/work/shared/**"] },
|
|
72
|
+
"rulesets": {
|
|
73
|
+
"local": "./rules/local.json"
|
|
74
|
+
},
|
|
75
|
+
"commands": {
|
|
76
|
+
"allow": [{ "id": "git-push", "program": "git", "args": ["push"] }]
|
|
77
|
+
},
|
|
78
|
+
"paths": [
|
|
79
|
+
{ "id": "prod-config", "patterns": ["config/prod*.yml"], "action": "ask" }
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- `remove` on a `preset` entry drops rulesets; on an `include` entry it drops rule ids from that ruleset. It never reaches another ruleset.
|
|
85
|
+
- A ruleset file contains only `description`, `commands` and `paths`. To replace a shipped ruleset, define it under the same name in `rulesets`; `/permission` reports the shadow.
|
|
86
|
+
- A config rule with the same `id` as a bundled rule replaces it. Changing a `deny` rule into `ask` or `allow` is reported as a warning.
|
|
87
|
+
|
|
88
|
+
Field-by-field reference and matching rules: [docs/configuration.md](docs/configuration.md).
|
|
89
|
+
|
|
90
|
+
## Shipped rulesets
|
|
91
|
+
|
|
92
|
+
| Ruleset | Contents |
|
|
93
|
+
|---|---|
|
|
94
|
+
| `irreversible` | git history rewrites and ref deletion (deny), `dd of=`, `mkfs*`, `wipefs`, `blkdiscard`, `fdisk` family, `shred` |
|
|
95
|
+
| `secrets` | `.env` files and private keys (path rules, deny) |
|
|
96
|
+
| `privilege` | `sudo`/`doas`/`pkexec`, privileged containers, recursive `chmod 777`/`chown` |
|
|
97
|
+
| `bulk-delete` | `rm -r`, `find -delete`, `xargs rm`, `truncate`, `rsync --delete`, `git clean`, stash drop, worktree remove |
|
|
98
|
+
| `git-local` | `git add`, `commit`, `merge`, `reset --hard`, discard, branch/tag delete |
|
|
99
|
+
| `git-remote` | `git push`; `--force-with-lease` stays allowed |
|
|
100
|
+
| `publish` | package publishing (`npm`, `bun`, `pnpm`, `yarn`, `cargo`, `gem`), `gh delete` |
|
|
101
|
+
| `database` | `dropdb`, SQL `DROP`/`TRUNCATE`, Redis `FLUSH` |
|
|
102
|
+
| `infra` | terraform apply/destroy, kubectl delete/drain, helm uninstall, docker volume/prune |
|
|
103
|
+
|
|
104
|
+
## Presets
|
|
105
|
+
|
|
106
|
+
| Preset | Level | Adds |
|
|
107
|
+
|---|---|---|
|
|
108
|
+
| `minimal` | 1 | `irreversible`, `secrets` |
|
|
109
|
+
| `standard` | 2 | `minimal` + `privilege` |
|
|
110
|
+
| `cautious` | 3 | `standard` + `bulk-delete`, `database`, `infra`, `git-remote`, `publish` |
|
|
111
|
+
| `paranoid` | 4 | `cautious` + `git-local` |
|
|
112
|
+
|
|
113
|
+
The shipped default is `standard`: `git add`/`commit` and `rm -rf node_modules` stay quiet, while `sudo`, `.env`, device writes and git history rewrites are still gated. Move up a level for more prompts, or list the rulesets you want.
|
|
114
|
+
|
|
115
|
+
## Limits
|
|
116
|
+
|
|
117
|
+
A guard, not a sandbox. It sees the command text and the paths in it, so anything a program resolves on its own is out of reach.
|
|
118
|
+
|
|
119
|
+
- `python -c "open('.env')"` passes.
|
|
120
|
+
- Shell aliases, functions and `eval` are not expanded. `sh -c '...'` is followed, but a wrapper in front of a command (`env`, `command`, `nice`, `nohup`, …) is not unwrapped.
|
|
121
|
+
- Reads outside the workspace are not gated: a path rule is the only check on what the agent may read.
|
|
122
|
+
- Symlinks are followed: a link inside the workspace that points outside is treated as outside, and a path rule matches the file the link resolves to.
|
|
123
|
+
- Tools other than `bash`, `powershell` and the file tools (`read`, `grep`, `find`, `ls`, `write`, `edit`) are not inspected.
|
|
124
|
+
|
|
125
|
+
## Development
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
bun test
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`pi-tui`, `pi-coding-agent` and `ajv` come from symlinks in `node_modules/`. The first two are peer dependencies that pi provides at runtime.
|
|
132
|
+
|
|
133
|
+
## Similar extensions
|
|
134
|
+
|
|
135
|
+
- [`@aliou/pi-guardrails`](https://pi.dev/packages/%40aliou/pi-guardrails): which inspires this plugin.
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# Configuration reference
|
|
2
|
+
|
|
3
|
+
All config is JSON.
|
|
4
|
+
|
|
5
|
+
## Files and precedence
|
|
6
|
+
|
|
7
|
+
| File | Scope |
|
|
8
|
+
|---|---|
|
|
9
|
+
| `~/.pi/agent/permission.json` | all projects |
|
|
10
|
+
| `<project>/.pi/permission.json` | one project, only after the project is trusted |
|
|
11
|
+
|
|
12
|
+
Files load in that order on top of the shipped `rules/index.json`. `PI_PERMISSION_HOME` replaces the home directory used for the global file (handy for tests and portable setups).
|
|
13
|
+
|
|
14
|
+
What merges how:
|
|
15
|
+
|
|
16
|
+
| Data | Merge |
|
|
17
|
+
|---|---|
|
|
18
|
+
| `rulesets` definitions | by id; a later file replaces an earlier one |
|
|
19
|
+
| `preset` | single value; the last file that declares it wins |
|
|
20
|
+
| `include` | single list; the last file that declares it replaces the whole list |
|
|
21
|
+
| config `commands` / `paths` | by rule id; a later file replaces an earlier rule |
|
|
22
|
+
| `defaultPermission`, `outsideCwd`, `enabled` | by key; a later file wins |
|
|
23
|
+
|
|
24
|
+
A config rule with the same `id` as a bundled rule replaces it. Moving a `deny` rule to `ask` or `allow` is reported.
|
|
25
|
+
|
|
26
|
+
Config is re-read on session start; `/permission:reload` reloads it without restarting pi.
|
|
27
|
+
|
|
28
|
+
## Concepts
|
|
29
|
+
|
|
30
|
+
### Preset
|
|
31
|
+
|
|
32
|
+
A permission level. Only the shipped index defines presets; a user config selects one.
|
|
33
|
+
|
|
34
|
+
```json
|
|
35
|
+
{ "preset": "cautious" }
|
|
36
|
+
{ "preset": { "id": "cautious", "remove": ["bulk-delete"] } }
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`remove` lists **ruleset ids** to drop from that preset. Dropping a ruleset the preset does not contain is a warning.
|
|
40
|
+
|
|
41
|
+
A preset never contains rules and never includes another preset. That keeps the reference graph two levels deep, so a cycle cannot be written.
|
|
42
|
+
|
|
43
|
+
### Ruleset
|
|
44
|
+
|
|
45
|
+
A leaf bundle of rules:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"description": "local tweaks",
|
|
50
|
+
"commands": { "ask": [{ "id": "x", "program": "x" }] },
|
|
51
|
+
"paths": [{ "id": "my-env", "patterns": [".env"] }]
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
A ruleset file may contain only `description`, `commands` and `paths` (plus `$schema`). A config field such as `include` in a ruleset file is an error. A ruleset never references another ruleset.
|
|
56
|
+
|
|
57
|
+
Named rulesets are defined in a config file, either inline or as a path:
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"rulesets": {
|
|
62
|
+
"local": "./rules/local.json",
|
|
63
|
+
"mine": { "commands": { "ask": [{ "id": "x", "program": "x" }] } }
|
|
64
|
+
},
|
|
65
|
+
"include": ["local", "mine"]
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
A file path resolves from the file that names it. Redefining a shipped ruleset id is allowed and reported; use it to replace a pack.
|
|
70
|
+
|
|
71
|
+
### include
|
|
72
|
+
|
|
73
|
+
`include` adds rulesets to the active preset:
|
|
74
|
+
|
|
75
|
+
```json
|
|
76
|
+
{ "preset": "standard", "include": ["git-local", { "id": "git-remote", "remove": ["gh-delete"] }] }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`remove` lists **rule ids of that ruleset**. It cannot reach another ruleset, so equal ids in unrelated packs cannot cause a cross-pack deletion. Removing an id the ruleset does not define is a warning.
|
|
80
|
+
|
|
81
|
+
The same ruleset id cannot appear twice in one `include` list. To turn one rule of a preset member off, list that member in `include` with `remove`; the include entry replaces the preset's copy of that ruleset.
|
|
82
|
+
|
|
83
|
+
## Shipped rulesets and presets
|
|
84
|
+
|
|
85
|
+
See [README](../README.md#shipped-rulesets) for what each pack gates and [README](../README.md#presets) for the four levels. The shipped default is `preset: "standard"`.
|
|
86
|
+
|
|
87
|
+
## Command rules
|
|
88
|
+
|
|
89
|
+
```json
|
|
90
|
+
{
|
|
91
|
+
"commands": {
|
|
92
|
+
"deny": [{ "id": "git-history", "program": "git", "subcommand": ["filter-repo"], "terminate": true }],
|
|
93
|
+
"ask": [{ "id": "git-push", "program": "git", "subcommand": ["push"], "reason": "updates the remote" }],
|
|
94
|
+
"allow": [{ "id": "push-lease", "program": "git", "subcommand": ["push"], "argvRegex": "--force-with-lease" }]
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
| Field | Required | Meaning |
|
|
100
|
+
|---|---|---|
|
|
101
|
+
| `id` | yes | identity for overrides, removals and diagnostics |
|
|
102
|
+
| `program` | yes | program basename, or a list; `*` wildcard allowed (`mkfs.*`) |
|
|
103
|
+
| `args` | no | matches when any of these is a positional argument |
|
|
104
|
+
| `subcommand` | no | matches the first positional, after global options that take a value (`git -C dir push`) |
|
|
105
|
+
| `anyFlag` | no | matches when any of these flags is present |
|
|
106
|
+
| `allFlags` | no | matches only when all of these flags are present |
|
|
107
|
+
| `unlessFlag` | no | never matches when any of these flags is present |
|
|
108
|
+
| `argvRegex` | no | regex over the joined argv of one simple command |
|
|
109
|
+
| `reason` | no | shown in the prompt and in block messages |
|
|
110
|
+
| `terminate` | no | `deny` only: end the turn instead of letting the agent retry |
|
|
111
|
+
|
|
112
|
+
Matching notes:
|
|
113
|
+
|
|
114
|
+
- `-rf` is expanded to `-r` and `-f`; `--hard` is also available as `hard`.
|
|
115
|
+
- `subcommand` is the safer choice for command families: a commit message or a flag value cannot pass for the subcommand. For git, `-C`, `-c`, `--git-dir`, `--work-tree`, `--namespace`, `--exec-path` and `--super-prefix` are skipped with their value, so `git -C dir push` still matches `subcommand: ["push"]`.
|
|
116
|
+
- `args` matches any positional, so for programs without a value-flag table it also matches flag values. Use it for operands, not for subcommands.
|
|
117
|
+
- One string can hold several commands (`&&`, `;`, `|`, substitutions); the strictest verdict wins.
|
|
118
|
+
- `sh -c '...'` and other shell `-c` scripts are checked like top-level commands (three levels deep).
|
|
119
|
+
- Precedence inside one simple command: `deny` → `allow` → `ask` → `defaultPermission.commands`.
|
|
120
|
+
- "Allow for this session" remembers the rule and the flags: after allowing `rm -r build`, `rm -r dist` runs without asking, while `rm -rf dist` asks again. Flag order does not matter. A deny rule still blocks a granted command.
|
|
121
|
+
|
|
122
|
+
## Path rules
|
|
123
|
+
|
|
124
|
+
```json
|
|
125
|
+
{
|
|
126
|
+
"paths": [
|
|
127
|
+
{
|
|
128
|
+
"id": "secrets-env",
|
|
129
|
+
"patterns": [".env", ".env.*"],
|
|
130
|
+
"except": [".env.example"],
|
|
131
|
+
"action": "ask",
|
|
132
|
+
"ops": ["read", "write", "delete"],
|
|
133
|
+
"onlyIfExists": false,
|
|
134
|
+
"message": "ask the user to handle .env files"
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
| Field | Required | Meaning |
|
|
141
|
+
|---|---|---|
|
|
142
|
+
| `id` | yes | identity for overrides, removals and messages |
|
|
143
|
+
| `patterns` | yes | globs; matching rules below |
|
|
144
|
+
| `except` | no | patterns that cancel the rule |
|
|
145
|
+
| `action` | no | `deny` (default), `ask`, `allow` |
|
|
146
|
+
| `ops` | no | operations covered: `read`, `write`, `delete`; default all three |
|
|
147
|
+
| `onlyIfExists` | no | protect existing files only; default `false` |
|
|
148
|
+
| `message` | no | text returned when the rule denies or asks |
|
|
149
|
+
|
|
150
|
+
Precedence for one path: **deny → allow → ask → `defaultPermission.paths`**. A deny rule on the same path beats an allow or ask rule wherever either sits in the array. A rule that an `allow` rule matches is not asked about.
|
|
151
|
+
|
|
152
|
+
Matching:
|
|
153
|
+
|
|
154
|
+
| Pattern | Compared against |
|
|
155
|
+
|---|---|
|
|
156
|
+
| `**` | every path under the project; never `../outside` |
|
|
157
|
+
| no slash (`.env`, `*.pem`) | the basename, at any depth |
|
|
158
|
+
| starts with `~` or `/` | the absolute path |
|
|
159
|
+
| anything else (`config/*.yml`) | the path relative to the project; never `../outside` |
|
|
160
|
+
|
|
161
|
+
- The target is resolved to an absolute path first: `~` expanded, `..` normalized, and a path reached through `cd` in the same command pinned to that directory.
|
|
162
|
+
- If the path exists, its real path is matched as well, so symlinks do not bypass a rule. A symlink whose target does not exist yet is followed too.
|
|
163
|
+
- `*` does not cross `/`; `**` does.
|
|
164
|
+
|
|
165
|
+
For bash commands the gate reads redirections, path arguments of known programs, path-looking arguments of unknown ones, `sed -i`, `mv` sources (as deletes), and nested `sh -c` (three levels); `cd` inside the string changes the directory for later commands. Globs are expanded before matching, so `cat .env*` is caught for files that exist; `$VAR` operands match by basename only.
|
|
166
|
+
|
|
167
|
+
"Allow for this session" remembers `path:<rule id>:<op>`: allowing a read leaves writes and deletes asking.
|
|
168
|
+
|
|
169
|
+
## `defaultPermission`
|
|
170
|
+
|
|
171
|
+
```json
|
|
172
|
+
{ "defaultPermission": { "commands": "allow", "paths": "allow" } }
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
What happens when no rule matches:
|
|
176
|
+
|
|
177
|
+
| Value | Commands | Paths |
|
|
178
|
+
|---|---|---|
|
|
179
|
+
| `allow` | run (default) | allowed (default) |
|
|
180
|
+
| `ask` | prompt for the program | prompt for the path |
|
|
181
|
+
| `deny` | only `allow` rules run | only `allow` path rules are reachable |
|
|
182
|
+
|
|
183
|
+
- Config-level only; ignored in rulesets and presets.
|
|
184
|
+
- Grants: `default:<program>:<flags>` for commands, `path:default:<op>:<absolute path>` for paths. A path fallback grant covers exactly one path.
|
|
185
|
+
- The path fallback does not bypass `outsideCwd`; that gate keeps its own `mode`.
|
|
186
|
+
- With `paths: "deny"`, use an allow rule for the workspace: `{ "id": "workspace", "patterns": ["**"], "action": "allow" }`.
|
|
187
|
+
|
|
188
|
+
## `outsideCwd`
|
|
189
|
+
|
|
190
|
+
```json
|
|
191
|
+
{ "outsideCwd": { "mode": "ask", "allowPaths": ["~/work/shared/**"] } }
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
| Field | Meaning |
|
|
195
|
+
|---|---|
|
|
196
|
+
| `mode` | what writes and deletes outside the workspace do: `ask` (default), `block`, `allow` |
|
|
197
|
+
| `allowPaths` | extra globs allowed outside the workspace; relative patterns compare against the project |
|
|
198
|
+
|
|
199
|
+
Built-in allow list: `/tmp/**`, `/dev/**`, `~/.pi/agent/**`, `~/.cache/**`, `~/.bun/**`, `~/.npm/**`, `~/.local/share/**`.
|
|
200
|
+
|
|
201
|
+
Only writes and deletes are checked. Reads outside the workspace are not gated, and `mode: block` does not stop them either; use a path rule for files that must never be read.
|
|
202
|
+
|
|
203
|
+
Paths are resolved to their real location first, so a symlink inside the workspace that points outside is outside, and the allow list is matched against where the write lands rather than the link itself. A symlinked workspace stays inside. "Allow for this session" remembers the directory of the approved path, including everything below it.
|
|
204
|
+
|
|
205
|
+
## `enabled`
|
|
206
|
+
|
|
207
|
+
```json
|
|
208
|
+
{ "enabled": { "commands": true, "paths": true } }
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Both default to `true`. Turning `paths` off skips path rules and the path fallback but leaves `outsideCwd` active. Turning `commands` off skips command rules and the command fallback.
|
|
212
|
+
|
|
213
|
+
## Diagnostics
|
|
214
|
+
|
|
215
|
+
Problems are reported as notifications at load time and listed by `/permission`.
|
|
216
|
+
|
|
217
|
+
Fatal — the config cannot be honored and every gated tool is blocked:
|
|
218
|
+
|
|
219
|
+
- a `ruleset` that contains `include`, `preset`, `rulesets` or another config field;
|
|
220
|
+
- an unknown `include` or `preset` id, or two equal ids in one `include` list;
|
|
221
|
+
- the same rule id defined twice inside one ruleset or one config file;
|
|
222
|
+
- the same rule id in two selected rulesets;
|
|
223
|
+
- a selected ruleset file that is missing or invalid.
|
|
224
|
+
|
|
225
|
+
Warnings:
|
|
226
|
+
|
|
227
|
+
- a `remove` id that matches nothing;
|
|
228
|
+
- a shadowed ruleset id;
|
|
229
|
+
- a config rule that moves a bundled `deny` rule to `ask` or `allow`;
|
|
230
|
+
- an unknown field name;
|
|
231
|
+
- a bad `argvRegex` (the rule is dropped);
|
|
232
|
+
- invalid JSON in a user file (that file is skipped);
|
|
233
|
+
- `presets` in a user file (ignored; presets are shipped only).
|
|
234
|
+
|
|
235
|
+
## Schema
|
|
236
|
+
|
|
237
|
+
`$schema` is optional and only helps editors. The loader reports field names it does not read as warnings. A ruleset file uses the smaller `$defs/ruleSet` shape from [`schema.json`](../schema.json).
|
package/index.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { loadConfig, type ResolvedConfig } from "./src/config";
|
|
3
|
+
import { evaluate } from "./src/gate";
|
|
4
|
+
import { ask, SessionGrants } from "./src/prompt";
|
|
5
|
+
|
|
6
|
+
export default async function permission(pi: ExtensionAPI) {
|
|
7
|
+
let config: ResolvedConfig = loadConfig(process.cwd(), false);
|
|
8
|
+
const sessionGrants = new SessionGrants();
|
|
9
|
+
|
|
10
|
+
const reload = (cwd: string, trusted: boolean, notify: (message: string) => void) => {
|
|
11
|
+
config = loadConfig(cwd, trusted);
|
|
12
|
+
for (const warning of config.warnings) notify(`pi-permission: ${warning}`);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
pi.on("session_start", (_event, ctx) => {
|
|
16
|
+
sessionGrants.clear();
|
|
17
|
+
reload(ctx.cwd, ctx.isProjectTrusted(), (message) => ctx.ui.notify(message, "warning"));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const confirm = (
|
|
21
|
+
ctx: { hasUI: boolean; mode: string; ui: any; abort: () => void },
|
|
22
|
+
request: { command: string; title: string; reason: string; grantKeys: string[] },
|
|
23
|
+
) =>
|
|
24
|
+
ask({
|
|
25
|
+
...request,
|
|
26
|
+
hasUI: ctx.hasUI,
|
|
27
|
+
grants: sessionGrants,
|
|
28
|
+
// RPC has a no-op custom(); its dialogs go through select().
|
|
29
|
+
custom:
|
|
30
|
+
ctx.mode === "tui" && typeof ctx.ui.custom === "function"
|
|
31
|
+
? (factory: unknown) => ctx.ui.custom(factory)
|
|
32
|
+
: undefined,
|
|
33
|
+
select: (title, options) => ctx.ui.select(title, options),
|
|
34
|
+
abort: () => ctx.abort(),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
38
|
+
const decision = evaluate(
|
|
39
|
+
config,
|
|
40
|
+
event.toolName,
|
|
41
|
+
event.input as Record<string, unknown>,
|
|
42
|
+
ctx.cwd,
|
|
43
|
+
(key) => sessionGrants.has(key),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (decision.kind === "allow") return;
|
|
47
|
+
if (decision.kind === "block") {
|
|
48
|
+
return { block: true, reason: decision.reason, terminate: decision.terminate };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const outcome = await confirm(ctx, {
|
|
52
|
+
command: decision.label,
|
|
53
|
+
title: decision.title,
|
|
54
|
+
reason: decision.reason,
|
|
55
|
+
grantKeys: decision.grantKeys,
|
|
56
|
+
});
|
|
57
|
+
return outcome.block ? { block: true, reason: outcome.reason } : undefined;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
pi.registerCommand("permission", {
|
|
61
|
+
description: "Show pi-permission config, composition and session grants",
|
|
62
|
+
handler: async (_args, ctx) => {
|
|
63
|
+
const { deny, ask: askRules, allow } = config.commands;
|
|
64
|
+
const preset = config.preset ? `${config.preset.name} (level ${config.preset.level})` : "none";
|
|
65
|
+
const removed = config.rulesets.flatMap((entry) =>
|
|
66
|
+
entry.removed.map((id) => `${entry.id}:${id}`),
|
|
67
|
+
);
|
|
68
|
+
const rulesets = config.rulesets
|
|
69
|
+
.map((entry) => `${entry.id} (${entry.rules} rules, ${entry.paths} paths)`)
|
|
70
|
+
.join(", ");
|
|
71
|
+
ctx.ui.notify(
|
|
72
|
+
[
|
|
73
|
+
`pi-permission: preset ${preset} · ${deny.length} deny / ${askRules.length} ask / ${allow.length} allow rules, ${config.paths.length} path rules`,
|
|
74
|
+
`defaults: commands=${config.defaultPermission.commands}, paths=${config.defaultPermission.paths}`,
|
|
75
|
+
`rulesets: ${rulesets || "none"}`,
|
|
76
|
+
...(removed.length > 0 ? [`removed: ${removed.join(", ")}`] : []),
|
|
77
|
+
`sources: ${config.sources.length > 0 ? config.sources.join(", ") : "defaults only"}`,
|
|
78
|
+
`session grants: ${sessionGrants.size}`,
|
|
79
|
+
...(config.warnings.length > 0 ? [`warnings:\n ${config.warnings.join("\n ")}`] : []),
|
|
80
|
+
...(config.fatal ? [config.fatal] : []),
|
|
81
|
+
].join("\n"),
|
|
82
|
+
config.fatal ? "error" : config.warnings.length > 0 ? "warning" : "info",
|
|
83
|
+
);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
pi.registerCommand("permission:reload", {
|
|
88
|
+
description: "Reload permission.json from disk",
|
|
89
|
+
handler: async (_args, ctx) => {
|
|
90
|
+
reload(ctx.cwd, ctx.isProjectTrusted(), (message) => ctx.ui.notify(message, "warning"));
|
|
91
|
+
ctx.ui.notify(
|
|
92
|
+
`pi-permission: ${config.commands.deny.length + config.commands.ask.length} rules loaded`,
|
|
93
|
+
"info",
|
|
94
|
+
);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jalaxy33/pi-permission",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Permission gate for pi: command, path and workspace rules",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.ts",
|
|
9
|
+
"./schema.json": "./schema.json"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.ts",
|
|
13
|
+
"src",
|
|
14
|
+
"rules",
|
|
15
|
+
"schema.json",
|
|
16
|
+
"docs",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"pi-package",
|
|
22
|
+
"pi-extension",
|
|
23
|
+
"permission",
|
|
24
|
+
"permissions",
|
|
25
|
+
"security",
|
|
26
|
+
"guardrails"
|
|
27
|
+
],
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/jalaxy33/pi-permission.git"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/jalaxy33/pi-permission",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/jalaxy33/pi-permission/issues"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"registry": "https://registry.npmjs.org"
|
|
39
|
+
},
|
|
40
|
+
"pi": {
|
|
41
|
+
"extensions": [
|
|
42
|
+
"./index.ts"
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "bun test",
|
|
47
|
+
"prepublishOnly": "bun test"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
51
|
+
"@earendil-works/pi-tui": "*"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"ajv": "^8.17.1"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/rules/index.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../schema.json",
|
|
3
|
+
"description": "Default configuration: rulesets, permission levels, and the shipped defaults.",
|
|
4
|
+
"rulesets": {
|
|
5
|
+
"irreversible": "./packs/irreversible.json",
|
|
6
|
+
"secrets": "./packs/secrets.json",
|
|
7
|
+
"privilege": "./packs/privilege.json",
|
|
8
|
+
"bulk-delete": "./packs/bulk-delete.json",
|
|
9
|
+
"git-local": "./packs/git-local.json",
|
|
10
|
+
"git-remote": "./packs/git-remote.json",
|
|
11
|
+
"publish": "./packs/publish.json",
|
|
12
|
+
"database": "./packs/database.json",
|
|
13
|
+
"infra": "./packs/infra.json"
|
|
14
|
+
},
|
|
15
|
+
"presets": {
|
|
16
|
+
"minimal": {
|
|
17
|
+
"level": 1,
|
|
18
|
+
"description": "Unrecoverable damage and secrets only.",
|
|
19
|
+
"include": [
|
|
20
|
+
"irreversible",
|
|
21
|
+
"secrets"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"standard": {
|
|
25
|
+
"level": 2,
|
|
26
|
+
"description": "Adds privilege escalation. Quiet during normal work.",
|
|
27
|
+
"include": [
|
|
28
|
+
"irreversible",
|
|
29
|
+
"secrets",
|
|
30
|
+
"privilege"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"cautious": {
|
|
34
|
+
"level": 3,
|
|
35
|
+
"description": "Adds bulk deletes, databases, infrastructure and remote writes.",
|
|
36
|
+
"include": [
|
|
37
|
+
"irreversible",
|
|
38
|
+
"secrets",
|
|
39
|
+
"privilege",
|
|
40
|
+
"bulk-delete",
|
|
41
|
+
"database",
|
|
42
|
+
"infra",
|
|
43
|
+
"git-remote",
|
|
44
|
+
"publish"
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"paranoid": {
|
|
48
|
+
"level": 4,
|
|
49
|
+
"description": "Adds local git state changes: commit, merge, discard.",
|
|
50
|
+
"include": [
|
|
51
|
+
"irreversible",
|
|
52
|
+
"secrets",
|
|
53
|
+
"privilege",
|
|
54
|
+
"bulk-delete",
|
|
55
|
+
"database",
|
|
56
|
+
"infra",
|
|
57
|
+
"git-remote",
|
|
58
|
+
"publish",
|
|
59
|
+
"git-local"
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"preset": "standard",
|
|
64
|
+
"defaultPermission": {
|
|
65
|
+
"commands": "allow",
|
|
66
|
+
"paths": "allow"
|
|
67
|
+
},
|
|
68
|
+
"outsideCwd": {
|
|
69
|
+
"mode": "ask"
|
|
70
|
+
}
|
|
71
|
+
}
|