@aliou/pi-guardrails 0.9.5 → 0.11.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/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![banner](https://assets.aliou.me/pi-extensions/banners/pi-guardrails.png)
2
+
1
3
  # Guardrails
2
4
 
3
5
  Security hooks for Pi to reduce accidental destructive actions and secret-file access.
@@ -18,10 +20,16 @@ Or from git:
18
20
  pi install git:github.com/aliou/pi-guardrails
19
21
  ```
20
22
 
23
+ ## Documentation
24
+
25
+ - [Default configuration](docs/defaults.md) — built-in policy rules and permission gate patterns
26
+ - [Example presets](docs/examples.md) — pre-configured presets available in settings
27
+
21
28
  ## What it does
22
29
 
23
30
  - **policies**: named file-protection rules with per-rule protection levels.
24
31
  - **permission-gate**: detects dangerous bash commands and asks for confirmation.
32
+ - **path-access**: restricts tool access to the current working directory with allow/ask/block modes.
25
33
  - **optional command explainer**: can call a small LLM to explain a dangerous command inline in the confirmation dialog.
26
34
 
27
35
  ## Config locations
@@ -43,7 +51,12 @@ Use `/guardrails:settings` to edit config interactively.
43
51
  "enabled": true,
44
52
  "features": {
45
53
  "policies": true,
46
- "permissionGate": true
54
+ "permissionGate": true,
55
+ "pathAccess": false
56
+ },
57
+ "pathAccess": {
58
+ "mode": "ask",
59
+ "allowedPaths": []
47
60
  },
48
61
  "policies": {
49
62
  "rules": [
@@ -116,6 +129,31 @@ Use:
116
129
 
117
130
  This starts a subagent that helps build and save one policy rule.
118
131
 
132
+ ## Path access
133
+
134
+ Restrict tool access to the current working directory. When enabled, any tool call targeting a path outside `cwd` is checked against the configured mode:
135
+
136
+ - **allow**: no restrictions
137
+ - **ask**: prompt with options to grant access (file or directory, for session or always)
138
+ - **block**: deny all outside access
139
+
140
+ ```jsonc
141
+ {
142
+ "features": { "pathAccess": true },
143
+ "pathAccess": {
144
+ "mode": "ask",
145
+ "allowedPaths": ["~/code/shared-libs/", "~/.config/myapp"]
146
+ }
147
+ }
148
+ ```
149
+
150
+ Grants are stored in project config (always) or session memory (session). The `allowedPaths` array is merged across all config scopes.
151
+
152
+ Limitations:
153
+ - Symlinks are not resolved (lexical path comparison only).
154
+ - Bash path extraction is best-effort (AST-based heuristics).
155
+ - In non-interactive mode, `ask` mode degrades to `block`.
156
+
119
157
  ## Permission gate
120
158
 
121
159
  Detects dangerous bash commands and prompts user confirmation.
@@ -156,6 +194,16 @@ Also note:
156
194
 
157
195
  - `preventBrew`, `preventPython`, `enforcePackageManager`, `packageManager` were removed from guardrails and moved to `@aliou/pi-toolchain`.
158
196
 
197
+ ## Development
198
+
199
+ ```bash
200
+ pnpm test # Run tests
201
+ pnpm test:watch # Run tests in watch mode
202
+ pnpm typecheck # Type check
203
+ pnpm lint # Lint
204
+ pnpm format # Format
205
+ ```
206
+
159
207
  ## Events
160
208
 
161
209
  Guardrails emits events for other extensions:
@@ -164,7 +212,7 @@ Guardrails emits events for other extensions:
164
212
 
165
213
  ```ts
166
214
  interface GuardrailsBlockedEvent {
167
- feature: "policies" | "permissionGate";
215
+ feature: "policies" | "permissionGate" | "pathAccess";
168
216
  toolName: string;
169
217
  input: Record<string, unknown>;
170
218
  reason: string;
@@ -180,4 +228,4 @@ interface GuardrailsDangerousEvent {
180
228
  description: string;
181
229
  pattern: string;
182
230
  }
183
- ```
231
+ ```
@@ -0,0 +1,140 @@
1
+ # Default Configuration
2
+
3
+ These are the built-in defaults that ship with guardrails. Rules marked as disabled are included but inactive by default — enable them in your config or via `/guardrails:settings`.
4
+
5
+ Source: [`src/config.ts`](../src/config.ts)
6
+
7
+
8
+ Home-directory defaults use `~` in patterns. During policy evaluation, guardrails expands `~` to the current user's home directory before checking whether a file exists or should be blocked.
9
+ ## Default Policy Rules
10
+
11
+ ### `secret-files` — Files containing secrets
12
+
13
+ Blocks access to dotenv files and similar secret-bearing files.
14
+
15
+ | Protection | Only if exists |
16
+ |------------|---------------|
17
+ | `noAccess` | yes |
18
+
19
+ **Patterns:**
20
+
21
+ | Pattern | Type |
22
+ |--------------------|------|
23
+ | `.env` | glob |
24
+ | `.env.local` | glob |
25
+ | `.env.production` | glob |
26
+ | `.env.prod` | glob |
27
+ | `.dev.vars` | glob |
28
+
29
+ **Allowed exceptions:**
30
+
31
+ | Pattern | Type |
32
+ |--------------------|------|
33
+ | `*.example.env` | glob |
34
+ | `*.sample.env` | glob |
35
+ | `*.test.env` | glob |
36
+ | `.env.example` | glob |
37
+ | `.env.sample` | glob |
38
+ | `.env.test` | glob |
39
+
40
+ ---
41
+
42
+ ### `home-ssh` — SSH directory and keys
43
+
44
+ Blocks access to SSH configuration, private keys, and related files. Disabled by default.
45
+
46
+ | Protection | Only if exists | Enabled by default |
47
+ |------------|---------------|-------------------|
48
+ | `noAccess` | yes | no |
49
+
50
+ **Patterns:**
51
+
52
+ | Pattern | Type |
53
+ |-----------------------|------|
54
+ | `~/.ssh/**` | glob |
55
+ | `~/.ssh/*_rsa` | glob |
56
+ | `~/.ssh/*_ed25519` | glob |
57
+ | `~/.ssh/*.pem` | glob |
58
+
59
+ **Allowed exceptions:**
60
+
61
+ | Pattern | Type |
62
+ |----------|------|
63
+ | `~/.ssh/*.pub` | glob |
64
+
65
+ ---
66
+
67
+ ### `home-config` — Sensitive user configuration directories
68
+
69
+ Blocks access to a small set of known sensitive config directories that commonly store credentials, tokens, or encrypted material. Disabled by default — enable it if these tools are installed and you want to protect them.
70
+
71
+ | Protection | Only if exists | Enabled by default |
72
+ |------------|---------------|-------------------|
73
+ | `noAccess` | yes | no |
74
+
75
+ **Patterns:**
76
+
77
+ | Pattern | Type |
78
+ |-----------------------|------|
79
+ | `~/.config/gh/**` | glob |
80
+ | `~/.config/gcloud/**` | glob |
81
+ | `~/.config/op/**` | glob |
82
+ | `~/.config/sops/**` | glob |
83
+
84
+ ---
85
+
86
+ ### `home-gpg` — GPG keys and configuration
87
+
88
+ Blocks access to GPG/GnuPG private keys, keyrings, and configuration. Disabled by default.
89
+
90
+ | Protection | Only if exists | Enabled by default |
91
+ |------------|---------------|-------------------|
92
+ | `noAccess` | yes | no |
93
+
94
+ **Patterns:**
95
+
96
+ | Pattern | Type |
97
+ |--------------------|------|
98
+ | `~/.gnupg/**` | glob |
99
+ | `~/*.gpg` | glob |
100
+ | `~/.gpg-agent.conf` | glob |
101
+
102
+ ---
103
+
104
+ ## Path Access
105
+
106
+ | Setting | Default |
107
+ |---|---|
108
+ | `features.pathAccess` | `false` |
109
+ | `pathAccess.mode` | `"ask"` |
110
+ | `pathAccess.allowedPaths` | `[]` |
111
+
112
+ Modes:
113
+ - `allow` — no path restrictions
114
+ - `ask` — prompt when accessing paths outside working directory
115
+ - `block` — deny all access outside working directory
116
+
117
+ Allowed paths use trailing-slash convention:
118
+ - `/path/to/file` — exact file match
119
+ - `/path/to/dir/` — directory and all descendants
120
+ - Supports `~/` for home directory
121
+
122
+ Limitations:
123
+ - Bash path extraction is best-effort (AST-based heuristics). Tokens like `application/json` may trigger false-positive prompts.
124
+ - Symlinks are not resolved. Lexical path comparison only.
125
+ - In non-interactive mode (--print), `ask` mode degrades to `block`.
126
+
127
+ ---
128
+
129
+ ## Default Permission Gate Patterns
130
+
131
+ These commands are detected using AST-based structural matching for accuracy.
132
+
133
+ | Pattern | Description |
134
+ |-----------------|--------------------------------|
135
+ | `rm -rf` | Recursive force delete |
136
+ | `sudo` | Superuser command |
137
+ | `dd of=` | Disk write operation |
138
+ | `mkfs.` | Filesystem format |
139
+ | `chmod -R 777` | Insecure recursive permissions |
140
+ | `chown -R` | Recursive ownership change |
@@ -0,0 +1,170 @@
1
+ # Example Presets
2
+
3
+ Pre-configured presets available in the `/guardrails:settings` Examples tab. These can be applied to any config scope (global, local, or memory).
4
+
5
+ Source: [`src/commands/settings-command.ts`](../src/commands/settings-command.ts)
6
+
7
+ ## File Policy Presets
8
+
9
+ ### Secrets (.env)
10
+
11
+ Block dotenv-like files using glob patterns.
12
+
13
+ | Field | Value |
14
+ |------------|------------------------------------|
15
+ | ID | `example-secret-env-files` |
16
+ | Protection | `noAccess` |
17
+ | Patterns | `.env`, `.env.*` |
18
+ | Exceptions | `.env.example`, `*.sample.env` |
19
+
20
+ ---
21
+
22
+ ### Logs (*.log)
23
+
24
+ Mark log files as read-only to prevent accidental modification.
25
+
26
+ | Field | Value |
27
+ |------------|---------------------------|
28
+ | ID | `example-log-files` |
29
+ | Protection | `readOnly` |
30
+ | Patterns | `*.log`, `*.out` |
31
+
32
+ ---
33
+
34
+ ### Regex env
35
+
36
+ Regex-based matching for `.env` and `.env.*` files. Demonstrates regex mode.
37
+
38
+ | Field | Value |
39
+ |------------|------------------------------------------|
40
+ | ID | `example-regex-env` |
41
+ | Protection | `noAccess` |
42
+ | Patterns | `^\.env(\..+)?$` (regex) |
43
+ | Exceptions | `^\.env\.example$` (regex) |
44
+
45
+ ---
46
+
47
+ ### SSH keys
48
+
49
+ Block access to SSH private key files.
50
+
51
+ | Field | Value |
52
+ |------------|--------------------------------------|
53
+ | ID | `example-ssh-keys` |
54
+ | Protection | `noAccess` |
55
+ | Patterns | `*.pem`, `*_rsa`, `*_ed25519` |
56
+ | Exceptions | `*.pub` |
57
+
58
+ ---
59
+
60
+ ### AWS credentials
61
+
62
+ Block AWS CLI credentials and config files.
63
+
64
+ | Field | Value |
65
+ |------------|----------------------------------------|
66
+ | ID | `example-aws-credentials` |
67
+ | Protection | `noAccess` |
68
+ | Patterns | `.aws/credentials`, `.aws/config` |
69
+
70
+ ---
71
+
72
+ ### Database files
73
+
74
+ Mark SQLite and database files as read-only.
75
+
76
+ | Field | Value |
77
+ |------------|----------------------------------------|
78
+ | ID | `example-database-files` |
79
+ | Protection | `readOnly` |
80
+ | Patterns | `*.db`, `*.sqlite`, `*.sqlite3` |
81
+
82
+ ---
83
+
84
+ ### Kubernetes secrets
85
+
86
+ Block kubeconfig and Kubernetes secret files.
87
+
88
+ | Field | Value |
89
+ |------------|----------------------------------------|
90
+ | ID | `example-k8s-secrets` |
91
+ | Protection | `noAccess` |
92
+ | Patterns | `.kube/config`, `*kubeconfig*` |
93
+
94
+ ---
95
+
96
+ ### Certificates
97
+
98
+ Block SSL/TLS certificate and key files.
99
+
100
+ | Field | Value |
101
+ |------------|----------------------------------------|
102
+ | ID | `example-certificates` |
103
+ | Protection | `noAccess` |
104
+ | Patterns | `*.crt`, `*.key`, `*.p12` |
105
+ | Exceptions | `*.csr` |
106
+
107
+ ---
108
+
109
+ ## Dangerous Command Presets
110
+
111
+ ### General
112
+
113
+ | Label | Pattern | Description |
114
+ |--------------------|----------------------|----------------------------------------|
115
+ | Homebrew | `brew` | Homebrew package manager |
116
+ | git push --force | `git push --force` | Git force push |
117
+ | npm publish | `npm publish` | NPM package publishing |
118
+ | yarn publish | `yarn publish` | Yarn package publishing |
119
+ | pnpm publish | `pnpm publish` | PNPM package publishing |
120
+ | drop database | `DROP DATABASE` | SQL database drop |
121
+ | drop table | `DROP TABLE` | SQL table drop |
122
+
123
+ ### dbt
124
+
125
+ | Label | Pattern | Description |
126
+ |----------|------------|------------------------|
127
+ | dbt run | `dbt run` | dbt model execution |
128
+ | dbt seed | `dbt seed` | dbt seed data loading |
129
+
130
+ ### AWS
131
+
132
+ | Label | Pattern | Description |
133
+ |----------------------|--------------------------------|------------------------------|
134
+ | aws s3 rm | `aws s3 rm` | AWS S3 object deletion |
135
+ | aws iam | `aws iam` | AWS IAM permission changes |
136
+ | aws ec2 terminate | `aws ec2 terminate-instances` | AWS EC2 instance termination |
137
+
138
+ ### Kubernetes
139
+
140
+ | Label | Pattern | Description |
141
+ |----------------|------------------|--------------------------------|
142
+ | kubectl delete | `kubectl delete` | Kubernetes resource deletion |
143
+ | kubectl apply | `kubectl apply` | Kubernetes resource application|
144
+ | kubectl scale | `kubectl scale` | Kubernetes scaling operation |
145
+
146
+ ### Docker
147
+
148
+ | Label | Pattern | Description |
149
+ |----------------------|------------------------|------------------------------------------|
150
+ | Docker secrets | `docker inspect` | Docker inspect (may expose env vars) |
151
+ | docker rm | `docker rm` | Docker container removal |
152
+ | docker rmi | `docker rmi` | Docker image removal |
153
+ | docker system prune | `docker system prune` | Docker system cleanup |
154
+ | docker compose down | `docker compose down` | Docker Compose service teardown |
155
+
156
+ ### Terraform
157
+
158
+ | Label | Pattern | Description |
159
+ |--------------------|----------------------|------------------------------------|
160
+ | Terraform apply | `terraform apply` | Terraform infrastructure changes |
161
+ | Terraform destroy | `terraform destroy` | Terraform infrastructure destruction|
162
+ | terraform import | `terraform import` | Terraform resource import |
163
+
164
+ ### Google Cloud
165
+
166
+ | Label | Pattern | Description |
167
+ |------------------------|------------------------------------|----------------------------------|
168
+ | gcloud compute delete | `gcloud compute instances delete` | GCP compute instance deletion |
169
+ | gcloud iam | `gcloud iam` | GCP IAM permission changes |
170
+ | gcloud sql delete | `gcloud sql instances delete` | GCP Cloud SQL instance deletion |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aliou/pi-guardrails",
3
- "version": "0.9.5",
3
+ "version": "0.11.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "private": false,
@@ -26,10 +26,11 @@
26
26
  },
27
27
  "files": [
28
28
  "src",
29
+ "docs",
29
30
  "README.md"
30
31
  ],
31
32
  "dependencies": {
32
- "@aliou/pi-utils-settings": "^0.10.1",
33
+ "@aliou/pi-utils-settings": "^0.11.2",
33
34
  "@aliou/sh": "^0.1.0"
34
35
  },
35
36
  "peerDependencies": {
@@ -48,7 +49,8 @@
48
49
  "@sinclair/typebox": "^0.34.48",
49
50
  "@types/node": "^25.0.10",
50
51
  "husky": "^9.1.7",
51
- "typescript": "^5.9.3"
52
+ "typescript": "^5.9.3",
53
+ "vitest": "^4.1.4"
52
54
  },
53
55
  "peerDependenciesMeta": {
54
56
  "@mariozechner/pi-agent-core": {
@@ -66,6 +68,8 @@
66
68
  },
67
69
  "scripts": {
68
70
  "typecheck": "tsc --noEmit",
71
+ "test": "vitest run",
72
+ "test:watch": "vitest",
69
73
  "lint": "biome check",
70
74
  "format": "biome check --write",
71
75
  "check:lockfile": "pnpm install --frozen-lockfile --ignore-scripts",
@@ -0,0 +1,76 @@
1
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
2
+ import { configLoader, type GuardrailsConfig } from "../config";
3
+ import {
4
+ buildOnboardedConfig,
5
+ createOnboardingWizard,
6
+ isOnboardingPending,
7
+ type OnboardingResult,
8
+ } from "./onboarding";
9
+
10
+ function mergeOnboarding(
11
+ base: GuardrailsConfig | null,
12
+ applyBuiltinDefaults: boolean,
13
+ pathAccessEnabled?: boolean | null,
14
+ ): GuardrailsConfig {
15
+ const next = structuredClone(base ?? {});
16
+ const onboarded = buildOnboardedConfig(
17
+ applyBuiltinDefaults,
18
+ pathAccessEnabled,
19
+ );
20
+ next.applyBuiltinDefaults = onboarded.applyBuiltinDefaults;
21
+ next.version = onboarded.version;
22
+ next.onboarding = onboarded.onboarding;
23
+ if (onboarded.features?.pathAccess !== undefined) {
24
+ next.features = {
25
+ ...next.features,
26
+ pathAccess: onboarded.features.pathAccess,
27
+ };
28
+ }
29
+ if (onboarded.pathAccess) {
30
+ next.pathAccess = onboarded.pathAccess;
31
+ }
32
+ return next;
33
+ }
34
+
35
+ export function registerGuardrailsOnboardingCommand(
36
+ pi: ExtensionAPI,
37
+ onCompleted?: () => void,
38
+ ): void {
39
+ pi.registerCommand("guardrails:onboarding", {
40
+ description: "Run guardrails onboarding",
41
+ handler: async (_args, ctx) => {
42
+ if (!ctx.hasUI) return;
43
+
44
+ const globalConfig = configLoader.getRawConfig("global");
45
+ if (!isOnboardingPending(globalConfig)) {
46
+ ctx.ui.notify(
47
+ "[Guardrails] onboarding already completed. Use /guardrails:settings to update behavior.",
48
+ "info",
49
+ );
50
+ return;
51
+ }
52
+
53
+ const result = await ctx.ui.custom<OnboardingResult>(
54
+ (_tui, theme, _keybindings, done) =>
55
+ createOnboardingWizard(theme, done),
56
+ { overlay: true },
57
+ );
58
+
59
+ if (!result.completed || result.applyBuiltinDefaults === null) {
60
+ ctx.ui.notify("[Guardrails] onboarding cancelled.", "warning");
61
+ return;
62
+ }
63
+
64
+ const merged = mergeOnboarding(
65
+ globalConfig,
66
+ result.applyBuiltinDefaults,
67
+ result.pathAccessEnabled,
68
+ );
69
+ await configLoader.save("global", merged);
70
+ await configLoader.load();
71
+
72
+ onCompleted?.();
73
+ ctx.ui.notify("[Guardrails] onboarding completed.", "info");
74
+ },
75
+ });
76
+ }