@jalaxy33/pi-permission 0.0.0-stage → 0.5.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 +164 -2
- package/docs/configuration.md +244 -0
- package/index.ts +99 -0
- package/package.json +54 -4
- package/rules/index.json +76 -0
- package/rules/packs/bulk-delete.json +83 -0
- package/rules/packs/database.json +30 -0
- package/rules/packs/dynamic-exec.json +105 -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 +77 -0
- package/src/config.ts +204 -0
- package/src/decide.ts +200 -0
- package/src/gate.ts +233 -0
- package/src/match.ts +59 -0
- package/src/ops.ts +252 -0
- package/src/outside.ts +87 -0
- package/src/paths.ts +212 -0
- package/src/prefixes.ts +83 -0
- package/src/program.ts +19 -0
- package/src/prompt.ts +275 -0
- package/src/rulesets.ts +454 -0
- package/src/scripts.ts +31 -0
- package/src/tokenize.ts +460 -0
- package/src/types.ts +167 -0
- package/src/unlayer.ts +48 -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,165 @@
|
|
|
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, and a command the splitter cannot resolve (an unterminated quote, `$(` or heredoc) asks instead of guessed at.
|
|
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 exact path, 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
|
+
| `dynamic-exec` | programs whose real command cannot be read: `env` with flags, `eval`, `source`, `exec`, `strace`, `xargs`, `busybox`, `find -exec`/`-delete`, `python -c`, `node -e`, `deno eval`, `perl -e`, `awk 'BEGIN{system(...)}'`, `pwsh` |
|
|
104
|
+
|
|
105
|
+
## Presets
|
|
106
|
+
|
|
107
|
+
| Preset | Level | Adds |
|
|
108
|
+
|---|---|---|
|
|
109
|
+
| `minimal` | 1 | `irreversible`, `secrets` |
|
|
110
|
+
| `standard` | 2 | `minimal` + `privilege` |
|
|
111
|
+
| `cautious` | 3 | `standard` + `bulk-delete`, `database`, `infra`, `git-remote`, `publish` |
|
|
112
|
+
| `paranoid` | 4 | `cautious` + `git-local` |
|
|
113
|
+
|
|
114
|
+
Every preset also includes `dynamic-exec`. It does not add risk levels, it closes a hole in reading the command: a program can hide the real command behind another one, and asking about the wrapper is the only answer that works for the ones with no closed option table.
|
|
115
|
+
|
|
116
|
+
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.
|
|
117
|
+
|
|
118
|
+
Wrappers with a closed option table are peeled before matching, so the inner command decides and the prompt names it: `timeout 60 git push` asks because it is a push, `time ls` runs because it is `ls`. Peeling covers `time`, `nice`, `nohup`, `setsid`, `timeout`, `stdbuf`, `command`, and `env` in its `VAR=VAL` form. `env -S`, `xargs`, `find -exec`, `busybox`, `eval` and `source` stay whole: their command exists only at runtime.
|
|
119
|
+
|
|
120
|
+
Shell keywords are peeled the same way, so `if …; then rm -rf x; fi` and `for f in *; do rm -rf "$f"; done` are judged by the command they run.
|
|
121
|
+
|
|
122
|
+
If you do not want the remaining prompts, drop the ruleset for your config and accept that those programs run unguarded:
|
|
123
|
+
|
|
124
|
+
```json
|
|
125
|
+
{ "preset": { "id": "paranoid", "remove": ["dynamic-exec"] } }
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Upgrading to 0.5.0
|
|
129
|
+
|
|
130
|
+
The release is a fix for commands that slipped past the gate, and it changes what you see:
|
|
131
|
+
|
|
132
|
+
- Programs are normalized before matching: `bash.exe`, `git.exe`, `C:/bin/bash.exe` are now the programs they name.
|
|
133
|
+
- `if`, `for`, `then`, `do`, `!` and `{` no longer hide the command they precede.
|
|
134
|
+
- Wrappers with a closed option table are peeled, so `timeout 60 git push` asks because it is a push and `time ls` runs. The same wrapper approvals no longer cover other commands.
|
|
135
|
+
- A program name built from a substitution, escape or glob (`"$CMD"`, `\rm`, `r[m]`) asks instead of falling through.
|
|
136
|
+
- A program's script input is judged: `bash deploy.sh` and `bash <<EOF` are read, `python job.py` asks once per file content, and stdin (`curl … | bash`) asks without a grant.
|
|
137
|
+
- The `powershell` tool asks and cannot be granted, rather than being read as bash.
|
|
138
|
+
- A config file that is not valid JSON is fatal instead of dropping its rules.
|
|
139
|
+
- An outside-workspace grant names the exact path, not its directory.
|
|
140
|
+
|
|
141
|
+
## Limits
|
|
142
|
+
|
|
143
|
+
A guard, not a sandbox. The strongest thing it can promise is that nothing the command text leaves unreadable runs silently. It cannot promise to block everything that should be blocked: a program resolves its own behaviour once it runs.
|
|
144
|
+
|
|
145
|
+
- It only reads the command text. A program name built from a substitution, escape or glob asks instead of matching (`"$CMD"`, `\rm`, `r[m]`); a name in quotes is still a name.
|
|
146
|
+
- A shell script is read and judged like a command line: `bash deploy.sh`, `bash <<EOF` and `sh -c '...'` (three levels) are followed. `-n` is not followed, since it runs nothing.
|
|
147
|
+
- Another language is asked about, not parsed: `python job.py` asks once per file content, and `python -c`, `node -e`, `awk 'BEGIN{system(...)}'` are covered by `dynamic-exec`. Once allowed, the script can still read anything the user can, `.env` included.
|
|
148
|
+
- A script on stdin is not visible in the command text, so `curl … | bash`, `bash < deploy.sh`, `sh -s` and `<<<` ask and cannot be granted for the session.
|
|
149
|
+
- Not covered at all: downloaded code run later (`npx some-package`, `at`, `cron`), remote or nested execution (`ssh host '…'`, `docker exec`, `flock`), project-defined scripts (`make`, `npm run`, `bun run`), and `rm` without `-r`.
|
|
150
|
+
- Shell aliases and functions are not expanded; `shellCommandPrefix` from Pi's settings is executed but never judged, and the gate warns when it is set.
|
|
151
|
+
- Reads outside the workspace are not gated: a path rule is the only check on what the agent may read.
|
|
152
|
+
- 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.
|
|
153
|
+
- Only `bash`, `powershell` and the file tools (`read`, `grep`, `find`, `ls`, `write`, `edit`) are inspected. The `codemode` script itself is not judged — its nested tool calls are — and `models.classify` inside a script never reaches the gate.
|
|
154
|
+
|
|
155
|
+
## Development
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
bun test
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
`pi-tui`, `pi-coding-agent` and `ajv` come from symlinks in `node_modules/`. The first two are peer dependencies that pi provides at runtime.
|
|
162
|
+
|
|
163
|
+
## Similar extensions
|
|
164
|
+
|
|
165
|
+
- [`@aliou/pi-guardrails`](https://pi.dev/packages/%40aliou/pi-guardrails): which inspires this plugin.
|
|
@@ -0,0 +1,244 @@
|
|
|
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. A deeper script, and a script that reads a variable (`sh -c "$DEPLOY"`), asks.
|
|
119
|
+
- `$(...)`, backticks, `<(...)` and `>(...)` are parsed as commands and checked with the same rules, including inside double quotes. Single-quoted text stays text.
|
|
120
|
+
- Text the splitter cannot resolve — an unterminated quote, `$(` or heredoc — asks with the reason `gate cannot inspect this command`. It is never treated as an allow.
|
|
121
|
+
- Wrappers with a closed option table are peeled before matching, so the inner command decides: `time`, `nice`, `nohup`, `setsid`, `timeout`, `stdbuf`, `command`, and `env` when it only carries `VAR=VAL` words. A shape the table does not cover (`env -C dir`, `env -S`, `timeout -v`, `strace -o file`) is left whole and reaches the `dynamic-exec` rules, which ask.
|
|
122
|
+
- Shell keywords are peeled too: `then`, `do`, `else`, `elif`, `if`, `while`, `until`, `!` and `{` precede a command; `for`, `in`, `case`, `fi`, `done`, `esac` and `}` never carry one. Word-exact and position zero only, so `cp a in` keeps its operand.
|
|
123
|
+
- A word containing an expansion, escape or glob cannot name a program: `"$CMD"`, `\rm` and `r[m]` ask instead of matching. A quoted name (`'git'`) is still a name.
|
|
124
|
+
- A program's script input is judged when the gate can see it: a shell script in a file (`bash deploy.sh`) or a heredoc body on a shell (`bash <<EOF`) is tokenized and judged like the command line; a script in another language (`python job.py`) asks once per file content, granted as `opaque:<program>:file:<hash>`; stdin (`curl … | bash`, `bash < f`, `sh -s`, `<<<`) asks and carries no grant key, because that text does not exist when the decision is made.
|
|
125
|
+
- `-c` names inline code and is followed instead of read; `-n` parses without running, so it is not followed.
|
|
126
|
+
- Precedence inside one simple command: `deny` → `allow` → `ask` → `defaultPermission.commands`.
|
|
127
|
+
- "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.
|
|
128
|
+
|
|
129
|
+
## Path rules
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"paths": [
|
|
134
|
+
{
|
|
135
|
+
"id": "secrets-env",
|
|
136
|
+
"patterns": [".env", ".env.*"],
|
|
137
|
+
"except": [".env.example"],
|
|
138
|
+
"action": "ask",
|
|
139
|
+
"ops": ["read", "write", "delete"],
|
|
140
|
+
"onlyIfExists": false,
|
|
141
|
+
"message": "ask the user to handle .env files"
|
|
142
|
+
}
|
|
143
|
+
]
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
| Field | Required | Meaning |
|
|
148
|
+
|---|---|---|
|
|
149
|
+
| `id` | yes | identity for overrides, removals and messages |
|
|
150
|
+
| `patterns` | yes | globs; matching rules below |
|
|
151
|
+
| `except` | no | patterns that cancel the rule |
|
|
152
|
+
| `action` | no | `deny` (default), `ask`, `allow` |
|
|
153
|
+
| `ops` | no | operations covered: `read`, `write`, `delete`; default all three |
|
|
154
|
+
| `onlyIfExists` | no | protect existing files only; default `false` |
|
|
155
|
+
| `message` | no | text returned when the rule denies or asks |
|
|
156
|
+
|
|
157
|
+
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.
|
|
158
|
+
|
|
159
|
+
Matching:
|
|
160
|
+
|
|
161
|
+
| Pattern | Compared against |
|
|
162
|
+
|---|---|
|
|
163
|
+
| `**` | every path under the project; never `../outside` |
|
|
164
|
+
| no slash (`.env`, `*.pem`) | the basename, at any depth |
|
|
165
|
+
| starts with `~` or `/` | the absolute path |
|
|
166
|
+
| anything else (`config/*.yml`) | the path relative to the project; never `../outside` |
|
|
167
|
+
|
|
168
|
+
- 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.
|
|
169
|
+
- 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.
|
|
170
|
+
- `*` does not cross `/`; `**` does.
|
|
171
|
+
|
|
172
|
+
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, with or without a file extension.
|
|
173
|
+
|
|
174
|
+
"Allow for this session" remembers `path:<rule id>:<op>`: allowing a read leaves writes and deletes asking.
|
|
175
|
+
|
|
176
|
+
## `defaultPermission`
|
|
177
|
+
|
|
178
|
+
```json
|
|
179
|
+
{ "defaultPermission": { "commands": "allow", "paths": "allow" } }
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
What happens when no rule matches:
|
|
183
|
+
|
|
184
|
+
| Value | Commands | Paths |
|
|
185
|
+
|---|---|---|
|
|
186
|
+
| `allow` | run (default) | allowed (default) |
|
|
187
|
+
| `ask` | prompt for the program | prompt for the path |
|
|
188
|
+
| `deny` | only `allow` rules run | only `allow` path rules are reachable |
|
|
189
|
+
|
|
190
|
+
- Config-level only; ignored in rulesets and presets.
|
|
191
|
+
- Grants: `default:<program>:<flags>` for commands, `path:default:<op>:<absolute path>` for paths. A path fallback grant covers exactly one path.
|
|
192
|
+
- The path fallback does not bypass `outsideCwd`; that gate keeps its own `mode`.
|
|
193
|
+
- With `paths: "deny"`, use an allow rule for the workspace: `{ "id": "workspace", "patterns": ["**"], "action": "allow" }`.
|
|
194
|
+
|
|
195
|
+
## `outsideCwd`
|
|
196
|
+
|
|
197
|
+
```json
|
|
198
|
+
{ "outsideCwd": { "mode": "ask", "allowPaths": ["~/work/shared/**"] } }
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
| Field | Meaning |
|
|
202
|
+
|---|---|
|
|
203
|
+
| `mode` | what writes and deletes outside the workspace do: `ask` (default), `block`, `allow` |
|
|
204
|
+
| `allowPaths` | extra globs allowed outside the workspace; relative patterns compare against the project |
|
|
205
|
+
|
|
206
|
+
Built-in allow list: `/tmp/**`, `/dev/**`, `~/.pi/agent/**`, `~/.cache/**`, `~/.bun/**`, `~/.npm/**`, `~/.local/share/**`.
|
|
207
|
+
|
|
208
|
+
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.
|
|
209
|
+
|
|
210
|
+
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 exact path that was approved. A directory grant belongs in `outsideCwd.allowPaths`, where it is written down and visible.
|
|
211
|
+
|
|
212
|
+
## `enabled`
|
|
213
|
+
|
|
214
|
+
```json
|
|
215
|
+
{ "enabled": { "commands": true, "paths": true } }
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
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.
|
|
219
|
+
|
|
220
|
+
## Diagnostics
|
|
221
|
+
|
|
222
|
+
Problems are reported as notifications at load time and listed by `/permission`.
|
|
223
|
+
|
|
224
|
+
Fatal — the config cannot be honored and every gated tool is blocked:
|
|
225
|
+
|
|
226
|
+
- a `ruleset` that contains `include`, `preset`, `rulesets` or another config field;
|
|
227
|
+
- an unknown `include` or `preset` id, or two equal ids in one `include` list;
|
|
228
|
+
- the same rule id defined twice inside one ruleset or one config file;
|
|
229
|
+
- the same rule id in two selected rulesets;
|
|
230
|
+
- a selected ruleset file that is missing or invalid.
|
|
231
|
+
|
|
232
|
+
Warnings:
|
|
233
|
+
|
|
234
|
+
- a `remove` id that matches nothing;
|
|
235
|
+
- a shadowed ruleset id;
|
|
236
|
+
- a config rule that moves a bundled `deny` rule to `ask` or `allow`;
|
|
237
|
+
- an unknown field name;
|
|
238
|
+
- a bad `argvRegex` (the rule is dropped);
|
|
239
|
+
- invalid JSON in a user file (that file is skipped);
|
|
240
|
+
- `presets` in a user file (ignored; presets are shipped only).
|
|
241
|
+
|
|
242
|
+
## Schema
|
|
243
|
+
|
|
244
|
+
`$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,99 @@
|
|
|
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
|
+
// Grants name decisions made under the old rules; they cannot outlive a reload.
|
|
12
|
+
sessionGrants.clear();
|
|
13
|
+
config = loadConfig(cwd, trusted);
|
|
14
|
+
for (const warning of config.warnings) notify(`pi-permission: ${warning}`);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
pi.on("session_start", (_event, ctx) => {
|
|
18
|
+
sessionGrants.clear();
|
|
19
|
+
reload(ctx.cwd, ctx.isProjectTrusted(), (message) => ctx.ui.notify(message, "warning"));
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const confirm = (
|
|
23
|
+
ctx: { hasUI: boolean; mode: string; ui: any; abort: () => void },
|
|
24
|
+
request: { command: string; title: string; reason: string; grantKeys: string[] },
|
|
25
|
+
) =>
|
|
26
|
+
ask({
|
|
27
|
+
...request,
|
|
28
|
+
hasUI: ctx.hasUI,
|
|
29
|
+
grants: sessionGrants,
|
|
30
|
+
// RPC has a no-op custom(); its dialogs go through select().
|
|
31
|
+
custom:
|
|
32
|
+
ctx.mode === "tui" && typeof ctx.ui.custom === "function"
|
|
33
|
+
? (factory: unknown) => ctx.ui.custom(factory)
|
|
34
|
+
: undefined,
|
|
35
|
+
select: (title, options) => ctx.ui.select(title, options),
|
|
36
|
+
abort: () => ctx.abort(),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
40
|
+
const decision = evaluate(
|
|
41
|
+
config,
|
|
42
|
+
event.toolName,
|
|
43
|
+
event.input as Record<string, unknown>,
|
|
44
|
+
ctx.cwd,
|
|
45
|
+
(key) => sessionGrants.has(key),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
if (decision.kind === "allow") return;
|
|
49
|
+
if (decision.kind === "block") {
|
|
50
|
+
return { block: true, reason: decision.reason, terminate: decision.terminate };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const outcome = await confirm(ctx, {
|
|
54
|
+
command: decision.label,
|
|
55
|
+
title: decision.title,
|
|
56
|
+
reason: decision.reason,
|
|
57
|
+
grantKeys: decision.grantKeys,
|
|
58
|
+
});
|
|
59
|
+
return outcome.block ? { block: true, reason: outcome.reason } : undefined;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
pi.registerCommand("permission", {
|
|
63
|
+
description: "Show pi-permission config, composition and session grants",
|
|
64
|
+
handler: async (_args, ctx) => {
|
|
65
|
+
const { deny, ask: askRules, allow } = config.commands;
|
|
66
|
+
const preset = config.preset ? `${config.preset.name} (level ${config.preset.level})` : "none";
|
|
67
|
+
const removed = config.rulesets.flatMap((entry) =>
|
|
68
|
+
entry.removed.map((id) => `${entry.id}:${id}`),
|
|
69
|
+
);
|
|
70
|
+
const rulesets = config.rulesets
|
|
71
|
+
.map((entry) => `${entry.id} (${entry.rules} rules, ${entry.paths} paths)`)
|
|
72
|
+
.join(", ");
|
|
73
|
+
ctx.ui.notify(
|
|
74
|
+
[
|
|
75
|
+
`pi-permission: preset ${preset} · ${deny.length} deny / ${askRules.length} ask / ${allow.length} allow rules, ${config.paths.length} path rules`,
|
|
76
|
+
`defaults: commands=${config.defaultPermission.commands}, paths=${config.defaultPermission.paths}`,
|
|
77
|
+
`rulesets: ${rulesets || "none"}`,
|
|
78
|
+
...(removed.length > 0 ? [`removed: ${removed.join(", ")}`] : []),
|
|
79
|
+
`sources: ${config.sources.length > 0 ? config.sources.join(", ") : "defaults only"}`,
|
|
80
|
+
`session grants: ${sessionGrants.size}`,
|
|
81
|
+
...(config.warnings.length > 0 ? [`warnings:\n ${config.warnings.join("\n ")}`] : []),
|
|
82
|
+
...(config.fatal ? [config.fatal] : []),
|
|
83
|
+
].join("\n"),
|
|
84
|
+
config.fatal ? "error" : config.warnings.length > 0 ? "warning" : "info",
|
|
85
|
+
);
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
pi.registerCommand("permission:reload", {
|
|
90
|
+
description: "Reload permission.json from disk",
|
|
91
|
+
handler: async (_args, ctx) => {
|
|
92
|
+
reload(ctx.cwd, ctx.isProjectTrusted(), (message) => ctx.ui.notify(message, "warning"));
|
|
93
|
+
ctx.ui.notify(
|
|
94
|
+
`pi-permission: ${config.commands.deny.length + config.commands.ask.length} rules loaded`,
|
|
95
|
+
"info",
|
|
96
|
+
);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
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.5.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,76 @@
|
|
|
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
|
+
"dynamic-exec": "./packs/dynamic-exec.json"
|
|
15
|
+
},
|
|
16
|
+
"presets": {
|
|
17
|
+
"minimal": {
|
|
18
|
+
"level": 1,
|
|
19
|
+
"description": "Unrecoverable damage, secrets, and commands the gate cannot read.",
|
|
20
|
+
"include": [
|
|
21
|
+
"irreversible",
|
|
22
|
+
"secrets",
|
|
23
|
+
"dynamic-exec"
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"standard": {
|
|
27
|
+
"level": 2,
|
|
28
|
+
"description": "Adds privilege escalation. Quiet during normal work.",
|
|
29
|
+
"include": [
|
|
30
|
+
"irreversible",
|
|
31
|
+
"secrets",
|
|
32
|
+
"privilege",
|
|
33
|
+
"dynamic-exec"
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
"cautious": {
|
|
37
|
+
"level": 3,
|
|
38
|
+
"description": "Adds bulk deletes, databases, infrastructure and remote writes.",
|
|
39
|
+
"include": [
|
|
40
|
+
"irreversible",
|
|
41
|
+
"secrets",
|
|
42
|
+
"privilege",
|
|
43
|
+
"bulk-delete",
|
|
44
|
+
"database",
|
|
45
|
+
"infra",
|
|
46
|
+
"git-remote",
|
|
47
|
+
"publish",
|
|
48
|
+
"dynamic-exec"
|
|
49
|
+
]
|
|
50
|
+
},
|
|
51
|
+
"paranoid": {
|
|
52
|
+
"level": 4,
|
|
53
|
+
"description": "Adds local git state changes: commit, merge, discard.",
|
|
54
|
+
"include": [
|
|
55
|
+
"irreversible",
|
|
56
|
+
"secrets",
|
|
57
|
+
"privilege",
|
|
58
|
+
"bulk-delete",
|
|
59
|
+
"database",
|
|
60
|
+
"infra",
|
|
61
|
+
"git-remote",
|
|
62
|
+
"publish",
|
|
63
|
+
"git-local",
|
|
64
|
+
"dynamic-exec"
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"preset": "standard",
|
|
69
|
+
"defaultPermission": {
|
|
70
|
+
"commands": "allow",
|
|
71
|
+
"paths": "allow"
|
|
72
|
+
},
|
|
73
|
+
"outsideCwd": {
|
|
74
|
+
"mode": "ask"
|
|
75
|
+
}
|
|
76
|
+
}
|