@justfortytwo/gate 0.1.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/.claude-plugin/plugin.json +5 -0
- package/LICENSE +21 -0
- package/README.md +196 -0
- package/dist/approval-store.d.ts +84 -0
- package/dist/approval-store.js +172 -0
- package/dist/approval-store.js.map +1 -0
- package/dist/bash-allowlist.d.ts +28 -0
- package/dist/bash-allowlist.js +98 -0
- package/dist/bash-allowlist.js.map +1 -0
- package/dist/cli-bin.d.ts +2 -0
- package/dist/cli-bin.js +21 -0
- package/dist/cli-bin.js.map +1 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +60 -0
- package/dist/cli.js.map +1 -0
- package/dist/gate-hook.d.ts +2 -0
- package/dist/gate-hook.js +73 -0
- package/dist/gate-hook.js.map +1 -0
- package/dist/gate.d.ts +47 -0
- package/dist/gate.js +279 -0
- package/dist/gate.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/policy.d.ts +76 -0
- package/dist/policy.js +253 -0
- package/dist/policy.js.map +1 -0
- package/examples/bash-allowlist.jsonl +30 -0
- package/examples/capabilities.toml +70 -0
- package/hooks/hooks.json +15 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Enrico Deleo
|
|
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,196 @@
|
|
|
1
|
+
# @justfortytwo/gate
|
|
2
|
+
|
|
3
|
+
A standalone, agent-agnostic **PreToolUse safety gate** for Claude Code.
|
|
4
|
+
|
|
5
|
+
It is the piece that decides, *before any tool runs*, whether a call is safe to
|
|
6
|
+
auto-run, must be **deferred for a one-shot human approval**, or should be
|
|
7
|
+
**denied outright** — and it does so **fail-closed**: anything it doesn't
|
|
8
|
+
recognise, and anything that errors, is blocked rather than waved through.
|
|
9
|
+
|
|
10
|
+
It is built from three independent parts you can use together or à la carte:
|
|
11
|
+
|
|
12
|
+
1. **A capability policy engine** — every tool is tagged with an *autonomy tier*
|
|
13
|
+
(`read`, `internal`, `draft`, `external`, `irreversible`) in a small TOML
|
|
14
|
+
manifest. Auto tiers pass; outward/destructive tiers defer for approval.
|
|
15
|
+
2. **A fail-closed bash allowlist** — `Bash` defers by default; only commands you
|
|
16
|
+
explicitly allowlist (anchored, chaining-safe globs in a JSONL file) fast-path.
|
|
17
|
+
**Deny always wins**: a dangerous command (`git push`, `rm -rf`, `curl --data`,
|
|
18
|
+
`sudo`, …) can never be laundered past the gate by a broad allowlist glob.
|
|
19
|
+
3. **A PreToolUse hook** — reads the Claude Code hook event on stdin and emits the
|
|
20
|
+
permission decision on stdout.
|
|
21
|
+
|
|
22
|
+
No database, no network, no agent-specific code. It runs as a Claude Code plugin,
|
|
23
|
+
a hook wired by hand, or a plain library.
|
|
24
|
+
|
|
25
|
+
## Why
|
|
26
|
+
|
|
27
|
+
LLM agents are happy to run whatever a tool, a web page, or a chat message tells
|
|
28
|
+
them to. A blocklist ("never run `rm -rf`") fails open: anything you didn't think
|
|
29
|
+
of slips through. This gate inverts that — the dangerous and the *unknown* both
|
|
30
|
+
stop and wait for you, while genuinely safe, known operations stay fast.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
npm install @justfortytwo/gate
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`@justfortytwo/gate` is a leaf package — it has **no `@justfortytwo/*` peer
|
|
39
|
+
dependencies**.
|
|
40
|
+
|
|
41
|
+
## Use as a Claude Code plugin (recommended)
|
|
42
|
+
|
|
43
|
+
This repo ships a plugin manifest (`.claude-plugin/plugin.json`) and a hook
|
|
44
|
+
(`hooks/hooks.json`) that wires a `PreToolUse` matcher `"*"` to the compiled
|
|
45
|
+
`dist/gate-hook.js` via `${CLAUDE_PLUGIN_ROOT}`. Plugin hooks **compose** with your
|
|
46
|
+
project and user hooks, so the gate adds to — never replaces — your existing setup.
|
|
47
|
+
|
|
48
|
+
Then, in the project the agent operates in, provide:
|
|
49
|
+
|
|
50
|
+
- `.claude/policy/capabilities.toml` — your capability manifest
|
|
51
|
+
(copy `examples/capabilities.toml`).
|
|
52
|
+
- `rules/bash-allowlist.jsonl` — your bash allowlist
|
|
53
|
+
(copy `examples/bash-allowlist.jsonl`).
|
|
54
|
+
|
|
55
|
+
Paths are configurable via environment variables (all resolved relative to the
|
|
56
|
+
project root the agent runs in):
|
|
57
|
+
|
|
58
|
+
| Env var | Default | Purpose |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| `GATE_MANIFEST` | `.claude/policy/capabilities.toml` | capability manifest TOML |
|
|
61
|
+
| `GATE_BASH_ALLOWLIST` | `rules/bash-allowlist.jsonl` | bash allowlist JSONL |
|
|
62
|
+
| `GATE_APPROVALS` | `.gate/approvals.jsonl` | durable one-shot approval store |
|
|
63
|
+
|
|
64
|
+
## Use as a hook without the plugin
|
|
65
|
+
|
|
66
|
+
Add to `.claude/settings.json`:
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"hooks": {
|
|
71
|
+
"PreToolUse": [
|
|
72
|
+
{
|
|
73
|
+
"matcher": "*",
|
|
74
|
+
"hooks": [
|
|
75
|
+
{ "type": "command", "command": "node node_modules/@justfortytwo/gate/dist/gate-hook.js" }
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The hook emits a standard Claude Code `permissionDecision` (`allow` | `deny` and a
|
|
84
|
+
`defer`-style hold) with a human-readable reason. The gate is the safety
|
|
85
|
+
authority; with `deny > defer > ask > allow`, a `defer`/`deny` from the gate still
|
|
86
|
+
overrides a permissive `permissions.allow` list.
|
|
87
|
+
|
|
88
|
+
### How an approval flows
|
|
89
|
+
|
|
90
|
+
1. The agent calls an `external`/`irreversible` tool. The gate **defers** and
|
|
91
|
+
stages a pending one-shot keyed by the call's `tool_use_id`.
|
|
92
|
+
2. You clear it out of band with the bundled CLI — **`fortytwo-gate approve <tool_use_id>`**
|
|
93
|
+
(or `fortytwo-gate deny <tool_use_id>`). A host integration can instead call
|
|
94
|
+
`setDecisionByToolUseId(...)` on its own store.
|
|
95
|
+
3. The agent re-fires the same call. The gate **consumes the one-shot exactly
|
|
96
|
+
once** and allows it; any later re-fire is denied.
|
|
97
|
+
|
|
98
|
+
## Clearing approvals — the `fortytwo-gate` CLI
|
|
99
|
+
|
|
100
|
+
When the gate defers a call it stages a one-shot in the approvals store
|
|
101
|
+
(`GATE_APPROVALS`, default `.gate/approvals.jsonl`). The bundled `fortytwo-gate` command
|
|
102
|
+
lets you clear it standalone — no host required:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
fortytwo-gate list # what's waiting: status, tier, tool_use_id, target
|
|
106
|
+
fortytwo-gate approve <tool_use_id> # allow it once (the agent's next re-fire consumes it)
|
|
107
|
+
fortytwo-gate deny <tool_use_id> # block it
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Run it from the **same project root** the agent runs in: the default
|
|
111
|
+
`.gate/approvals.jsonl` is resolved relative to the current directory, so a
|
|
112
|
+
different cwd points at a different (or empty) store. `fortytwo-gate list` prints the
|
|
113
|
+
resolved store path so a mismatch is obvious; set `GATE_APPROVALS` to an absolute
|
|
114
|
+
path to remove the ambiguity. With the package installed, invoke it as `fortytwo-gate`
|
|
115
|
+
(or `npx @justfortytwo/gate`).
|
|
116
|
+
|
|
117
|
+
`approve`/`deny` only act on a **pending** call — an already-consumed (executed) or
|
|
118
|
+
denied one-shot is immutable, so an approval can never be resurrected into a second
|
|
119
|
+
run.
|
|
120
|
+
|
|
121
|
+
## Use as a library
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import {
|
|
125
|
+
loadManifest,
|
|
126
|
+
decide,
|
|
127
|
+
JsonlApprovalStore,
|
|
128
|
+
} from '@justfortytwo/gate';
|
|
129
|
+
|
|
130
|
+
const manifest = loadManifest('.claude/policy/capabilities.toml');
|
|
131
|
+
const store = new JsonlApprovalStore('.gate/approvals.jsonl');
|
|
132
|
+
|
|
133
|
+
const decision = await decide(manifest, {
|
|
134
|
+
toolName: 'Bash',
|
|
135
|
+
toolInput: { command: 'git push origin main' },
|
|
136
|
+
toolUseId: 'toolu_123',
|
|
137
|
+
cwd: process.cwd(),
|
|
138
|
+
}, { store });
|
|
139
|
+
|
|
140
|
+
// decision.permission -> 'allow' | 'defer' | 'deny'
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Bring your own durable approval store / audit log by implementing the
|
|
144
|
+
`ApprovalStore` / `AuditLogger` interfaces and passing them to `decide(...)`. The
|
|
145
|
+
package ships an `InMemoryApprovalStore` (the library default) and a file-backed
|
|
146
|
+
`JsonlApprovalStore` (used by the hook); a richer host integration can supply a
|
|
147
|
+
transactional store instead.
|
|
148
|
+
|
|
149
|
+
There is also a content-trust policy engine (`decidePolicy`, `classifyAuthority`,
|
|
150
|
+
`canUseAsInstruction`, …) for the *"may this source instruct the agent?"* question —
|
|
151
|
+
useful if you ingest untrusted content (web pages, messages, tool output) and want
|
|
152
|
+
a principled trusted/untrusted boundary.
|
|
153
|
+
|
|
154
|
+
## The `policySchema` contract: `POLICY_SCHEMA_VERSION`
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import { POLICY_SCHEMA_VERSION } from '@justfortytwo/gate'; // 1
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`POLICY_SCHEMA_VERSION` is the version of the **`policySchema` contract**: the
|
|
161
|
+
shape and meaning of the `Tier` model (the capability manifest) and of the
|
|
162
|
+
`Authority` / `MemoryClass` / `SourceKind` types and `decidePolicy()` outcomes (the
|
|
163
|
+
content-trust engine).
|
|
164
|
+
|
|
165
|
+
Other `@justfortytwo/*` packages depend on this contract via a **caret range** on
|
|
166
|
+
this package. Any change to the *meaning* of these types or decisions is a breaking
|
|
167
|
+
change: it bumps `POLICY_SCHEMA_VERSION` **and** the package major version (a major
|
|
168
|
+
bump signals a contract break to every consumer).
|
|
169
|
+
|
|
170
|
+
## Manifest format
|
|
171
|
+
|
|
172
|
+
A small TOML file (see `examples/capabilities.toml`):
|
|
173
|
+
|
|
174
|
+
- `default_tier` — tier for any tool not listed (recommended: `external`).
|
|
175
|
+
- `[tiers]` — exact tool name → tier, plus trailing-`*` **prefix globs**
|
|
176
|
+
(e.g. `"mcp__messaging__*" = "external"`).
|
|
177
|
+
- `[bash]` — JS-regex command patterns that **elevate** Bash above its base tier
|
|
178
|
+
(and double as the denylist). Highest match wins; patterns only ever raise.
|
|
179
|
+
- `[guard].files` — project-relative paths whose edit is `irreversible`; point this
|
|
180
|
+
at the manifest, settings, and allowlist so the gate can't be silently weakened.
|
|
181
|
+
|
|
182
|
+
## Bash allowlist format
|
|
183
|
+
|
|
184
|
+
JSONL, one entry per line (see `examples/bash-allowlist.jsonl`). Fields:
|
|
185
|
+
`command_glob` (required, anchored, matched against the whole command),
|
|
186
|
+
`cwd_glob` (optional, default `*`), `expires_at` (optional ISO 8601),
|
|
187
|
+
`approved_by`, `note`. Prefer **exact** commands; a trailing `*` re-enables
|
|
188
|
+
chaining and should be used sparingly.
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
MIT — Copyright (c) 2026 Enrico Deleo.
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
Created and maintained by [**Enrico Deleo**](https://enricodeleo.com).
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type ApprovalStatus = 'pending' | 'approved' | 'denied' | 'executed' | 'expired';
|
|
2
|
+
export interface PendingApproval {
|
|
3
|
+
id: string;
|
|
4
|
+
tool: string;
|
|
5
|
+
target: string;
|
|
6
|
+
payload: Record<string, unknown>;
|
|
7
|
+
tier: string;
|
|
8
|
+
tool_use_id: string;
|
|
9
|
+
session_id?: string | null;
|
|
10
|
+
status: ApprovalStatus;
|
|
11
|
+
created_at: string;
|
|
12
|
+
updated_at: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AddPendingInput {
|
|
15
|
+
tool: string;
|
|
16
|
+
target: string;
|
|
17
|
+
payload: Record<string, unknown>;
|
|
18
|
+
tier: string;
|
|
19
|
+
tool_use_id: string;
|
|
20
|
+
session_id?: string | null;
|
|
21
|
+
}
|
|
22
|
+
export interface ApprovalStore {
|
|
23
|
+
/** Stage a new pending approval; returns its id. */
|
|
24
|
+
addPending(input: AddPendingInput): Promise<string>;
|
|
25
|
+
/** Look up the current record for a tool_use_id (most recent wins). */
|
|
26
|
+
getByToolUseId(toolUseId: string): Promise<PendingApproval | undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* Atomically consume an `approved` record: flip it to `executed` and return
|
|
29
|
+
* true exactly once. A second call (already executed) returns false. This is
|
|
30
|
+
* what makes the approval a one-shot.
|
|
31
|
+
*/
|
|
32
|
+
markExecutedByToolUseId(toolUseId: string): Promise<boolean>;
|
|
33
|
+
/** Record an out-of-band decision (approve/deny) against a staged approval. */
|
|
34
|
+
setDecisionByToolUseId(toolUseId: string, status: 'approved' | 'denied', by?: string): Promise<boolean>;
|
|
35
|
+
/** All staged approvals with their current status (most recent state per tool_use_id). */
|
|
36
|
+
list(): Promise<PendingApproval[]>;
|
|
37
|
+
}
|
|
38
|
+
export interface AuditEntry {
|
|
39
|
+
actor: string;
|
|
40
|
+
kind: string;
|
|
41
|
+
content: string;
|
|
42
|
+
approval_status?: ApprovalStatus;
|
|
43
|
+
meta?: Record<string, unknown>;
|
|
44
|
+
}
|
|
45
|
+
export interface AuditLogger {
|
|
46
|
+
log(entry: AuditEntry): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
export declare class NullAuditLogger implements AuditLogger {
|
|
49
|
+
log(): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Process-local store. Good enough for a single hook invocation that both stages
|
|
53
|
+
* and (on a later turn) consumes — but only when the hook process is long-lived.
|
|
54
|
+
* Most Claude Code hooks are one-shot processes, so prefer JsonlApprovalStore for
|
|
55
|
+
* the actual hook; this is the safe library default.
|
|
56
|
+
*/
|
|
57
|
+
export declare class InMemoryApprovalStore implements ApprovalStore {
|
|
58
|
+
private readonly byToolUse;
|
|
59
|
+
addPending(input: AddPendingInput): Promise<string>;
|
|
60
|
+
getByToolUseId(toolUseId: string): Promise<PendingApproval | undefined>;
|
|
61
|
+
markExecutedByToolUseId(toolUseId: string): Promise<boolean>;
|
|
62
|
+
setDecisionByToolUseId(toolUseId: string, status: 'approved' | 'denied', _by?: string): Promise<boolean>;
|
|
63
|
+
list(): Promise<PendingApproval[]>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* File-backed store: appends approval events as JSONL and replays them to derive
|
|
67
|
+
* current state. Survives across separate one-shot hook processes, which is what a
|
|
68
|
+
* PreToolUse hook needs (stage on turn N, consume on turn N+1). Approvals are
|
|
69
|
+
* recorded out of band by appending a decision line (or via setDecisionByToolUseId
|
|
70
|
+
* from a companion tool). This is intentionally simple and single-writer; a
|
|
71
|
+
* concurrent multi-writer host should supply a transactional store instead.
|
|
72
|
+
*/
|
|
73
|
+
export declare class JsonlApprovalStore implements ApprovalStore {
|
|
74
|
+
private readonly filePath;
|
|
75
|
+
constructor(filePath: string);
|
|
76
|
+
private readAll;
|
|
77
|
+
private writeAll;
|
|
78
|
+
private append;
|
|
79
|
+
addPending(input: AddPendingInput): Promise<string>;
|
|
80
|
+
getByToolUseId(toolUseId: string): Promise<PendingApproval | undefined>;
|
|
81
|
+
markExecutedByToolUseId(toolUseId: string): Promise<boolean>;
|
|
82
|
+
setDecisionByToolUseId(toolUseId: string, status: 'approved' | 'denied', _by?: string): Promise<boolean>;
|
|
83
|
+
list(): Promise<PendingApproval[]>;
|
|
84
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// The durable one-shot approval store: the seam between the agent-agnostic gate
|
|
2
|
+
// and whatever records human approvals out of band. The original host backed this
|
|
3
|
+
// with a transactional database and surfaced approvals over a chat bridge; the
|
|
4
|
+
// gate only needs the small contract below.
|
|
5
|
+
//
|
|
6
|
+
// TODO(wire): a host integration (e.g. @justfortytwo/memory) may provide a
|
|
7
|
+
// richer, transactional ApprovalStore + an AuditLogger that persists to its own
|
|
8
|
+
// store and notifies an approver channel. Pass it to decide() via opts.store /
|
|
9
|
+
// opts.audit. The two implementations here keep the gate fully standalone.
|
|
10
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from 'node:fs';
|
|
11
|
+
import { dirname } from 'node:path';
|
|
12
|
+
export class NullAuditLogger {
|
|
13
|
+
async log() {
|
|
14
|
+
/* no-op */
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
let counter = 0;
|
|
18
|
+
function nextId() {
|
|
19
|
+
counter += 1;
|
|
20
|
+
return `pa_${Date.now().toString(36)}_${counter.toString(36)}`;
|
|
21
|
+
}
|
|
22
|
+
function nowIso() {
|
|
23
|
+
return new Date().toISOString();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Fail-closed transition guard for out-of-band decisions. Only an undecided
|
|
27
|
+
* (pending) call may be approved or denied; an approval may still be revoked
|
|
28
|
+
* (-> denied) before it is consumed. Terminal states (executed/denied/expired)
|
|
29
|
+
* are immutable, so `approve` can never resurrect a spent or denied one-shot.
|
|
30
|
+
*/
|
|
31
|
+
function canDecide(current, next) {
|
|
32
|
+
if (current === 'pending')
|
|
33
|
+
return true;
|
|
34
|
+
if (current === 'approved' && next === 'denied')
|
|
35
|
+
return true;
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Process-local store. Good enough for a single hook invocation that both stages
|
|
40
|
+
* and (on a later turn) consumes — but only when the hook process is long-lived.
|
|
41
|
+
* Most Claude Code hooks are one-shot processes, so prefer JsonlApprovalStore for
|
|
42
|
+
* the actual hook; this is the safe library default.
|
|
43
|
+
*/
|
|
44
|
+
export class InMemoryApprovalStore {
|
|
45
|
+
byToolUse = new Map();
|
|
46
|
+
async addPending(input) {
|
|
47
|
+
const id = nextId();
|
|
48
|
+
const ts = nowIso();
|
|
49
|
+
this.byToolUse.set(input.tool_use_id, {
|
|
50
|
+
id,
|
|
51
|
+
tool: input.tool,
|
|
52
|
+
target: input.target,
|
|
53
|
+
payload: input.payload,
|
|
54
|
+
tier: input.tier,
|
|
55
|
+
tool_use_id: input.tool_use_id,
|
|
56
|
+
session_id: input.session_id ?? null,
|
|
57
|
+
status: 'pending',
|
|
58
|
+
created_at: ts,
|
|
59
|
+
updated_at: ts,
|
|
60
|
+
});
|
|
61
|
+
return id;
|
|
62
|
+
}
|
|
63
|
+
async getByToolUseId(toolUseId) {
|
|
64
|
+
return this.byToolUse.get(toolUseId);
|
|
65
|
+
}
|
|
66
|
+
async markExecutedByToolUseId(toolUseId) {
|
|
67
|
+
const row = this.byToolUse.get(toolUseId);
|
|
68
|
+
if (!row || row.status !== 'approved')
|
|
69
|
+
return false;
|
|
70
|
+
row.status = 'executed';
|
|
71
|
+
row.updated_at = nowIso();
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
async setDecisionByToolUseId(toolUseId, status, _by) {
|
|
75
|
+
const row = this.byToolUse.get(toolUseId);
|
|
76
|
+
if (!row || !canDecide(row.status, status))
|
|
77
|
+
return false;
|
|
78
|
+
row.status = status;
|
|
79
|
+
row.updated_at = nowIso();
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
async list() {
|
|
83
|
+
return [...this.byToolUse.values()];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* File-backed store: appends approval events as JSONL and replays them to derive
|
|
88
|
+
* current state. Survives across separate one-shot hook processes, which is what a
|
|
89
|
+
* PreToolUse hook needs (stage on turn N, consume on turn N+1). Approvals are
|
|
90
|
+
* recorded out of band by appending a decision line (or via setDecisionByToolUseId
|
|
91
|
+
* from a companion tool). This is intentionally simple and single-writer; a
|
|
92
|
+
* concurrent multi-writer host should supply a transactional store instead.
|
|
93
|
+
*/
|
|
94
|
+
export class JsonlApprovalStore {
|
|
95
|
+
filePath;
|
|
96
|
+
constructor(filePath) {
|
|
97
|
+
this.filePath = filePath;
|
|
98
|
+
}
|
|
99
|
+
readAll() {
|
|
100
|
+
if (!existsSync(this.filePath))
|
|
101
|
+
return [];
|
|
102
|
+
const byId = new Map();
|
|
103
|
+
for (const line of readFileSync(this.filePath, 'utf8').split(/\r?\n/)) {
|
|
104
|
+
const trimmed = line.trim();
|
|
105
|
+
if (!trimmed || trimmed.startsWith('#'))
|
|
106
|
+
continue;
|
|
107
|
+
try {
|
|
108
|
+
const rec = JSON.parse(trimmed);
|
|
109
|
+
if (rec && typeof rec.tool_use_id === 'string')
|
|
110
|
+
byId.set(rec.tool_use_id, rec);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// Ignore malformed lines rather than crash the gate.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return [...byId.values()];
|
|
117
|
+
}
|
|
118
|
+
writeAll(rows) {
|
|
119
|
+
mkdirSync(dirname(this.filePath), { recursive: true });
|
|
120
|
+
const tmp = `${this.filePath}.tmp`;
|
|
121
|
+
writeFileSync(tmp, rows.map((r) => JSON.stringify(r)).join('\n') + (rows.length ? '\n' : ''), 'utf8');
|
|
122
|
+
renameSync(tmp, this.filePath);
|
|
123
|
+
}
|
|
124
|
+
append(row) {
|
|
125
|
+
mkdirSync(dirname(this.filePath), { recursive: true });
|
|
126
|
+
appendFileSync(this.filePath, `${JSON.stringify(row)}\n`, 'utf8');
|
|
127
|
+
}
|
|
128
|
+
async addPending(input) {
|
|
129
|
+
const id = nextId();
|
|
130
|
+
const ts = nowIso();
|
|
131
|
+
this.append({
|
|
132
|
+
id,
|
|
133
|
+
tool: input.tool,
|
|
134
|
+
target: input.target,
|
|
135
|
+
payload: input.payload,
|
|
136
|
+
tier: input.tier,
|
|
137
|
+
tool_use_id: input.tool_use_id,
|
|
138
|
+
session_id: input.session_id ?? null,
|
|
139
|
+
status: 'pending',
|
|
140
|
+
created_at: ts,
|
|
141
|
+
updated_at: ts,
|
|
142
|
+
});
|
|
143
|
+
return id;
|
|
144
|
+
}
|
|
145
|
+
async getByToolUseId(toolUseId) {
|
|
146
|
+
return this.readAll().find((r) => r.tool_use_id === toolUseId);
|
|
147
|
+
}
|
|
148
|
+
async markExecutedByToolUseId(toolUseId) {
|
|
149
|
+
const rows = this.readAll();
|
|
150
|
+
const row = rows.find((r) => r.tool_use_id === toolUseId);
|
|
151
|
+
if (!row || row.status !== 'approved')
|
|
152
|
+
return false;
|
|
153
|
+
row.status = 'executed';
|
|
154
|
+
row.updated_at = nowIso();
|
|
155
|
+
this.writeAll(rows);
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
async setDecisionByToolUseId(toolUseId, status, _by) {
|
|
159
|
+
const rows = this.readAll();
|
|
160
|
+
const row = rows.find((r) => r.tool_use_id === toolUseId);
|
|
161
|
+
if (!row || !canDecide(row.status, status))
|
|
162
|
+
return false;
|
|
163
|
+
row.status = status;
|
|
164
|
+
row.updated_at = nowIso();
|
|
165
|
+
this.writeAll(rows);
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
async list() {
|
|
169
|
+
return this.readAll();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=approval-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approval-store.js","sourceRoot":"","sources":["../src/approval-store.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,kFAAkF;AAClF,+EAA+E;AAC/E,4CAA4C;AAC5C,EAAE;AACF,6EAA6E;AAC7E,kFAAkF;AAClF,iFAAiF;AACjF,6EAA6E;AAE7E,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACzG,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAuDpC,MAAM,OAAO,eAAe;IAC1B,KAAK,CAAC,GAAG;QACP,WAAW;IACb,CAAC;CACF;AAED,IAAI,OAAO,GAAG,CAAC,CAAC;AAChB,SAAS,MAAM;IACb,OAAO,IAAI,CAAC,CAAC;IACb,OAAO,MAAM,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;AACjE,CAAC;AAED,SAAS,MAAM;IACb,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;A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AAC,MAAM,EAAE,MAAM,CAAC;YAAE,OAAO,KAAK,CAAC;QACzD,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;QACpB,GAAG,CAAC,UAAU,GAAG,MAAM,EAAE,CAAC;QAC1B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,IAAI;QACR,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface BashAllowlistEntry {
|
|
2
|
+
command_glob: string;
|
|
3
|
+
cwd_glob?: string;
|
|
4
|
+
expires_at?: string;
|
|
5
|
+
approved_by?: string;
|
|
6
|
+
created_at?: string;
|
|
7
|
+
source_tool_use_id?: string;
|
|
8
|
+
note?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const DEFAULT_BASH_ALLOW_TTL_HOURS = 24;
|
|
11
|
+
export declare function escapeGlobLiteral(s: string): string;
|
|
12
|
+
export declare function globToRegExp(glob: string): RegExp;
|
|
13
|
+
export declare function readBashAllowlist(filePath: string, now?: Date): BashAllowlistEntry[];
|
|
14
|
+
/**
|
|
15
|
+
* Default allowlist location, relative to a project/repo root. Override via the
|
|
16
|
+
* `bashAllowlistPath` field on GateContext to point anywhere you like.
|
|
17
|
+
*/
|
|
18
|
+
export declare function bashAllowlistPath(projectRoot: string): string;
|
|
19
|
+
export declare function findBashAllowlistMatch(filePath: string, command: string, cwd: string, now?: Date): BashAllowlistEntry | undefined;
|
|
20
|
+
export declare function appendExactBashAllowlistEntry(args: {
|
|
21
|
+
filePath: string;
|
|
22
|
+
command: string;
|
|
23
|
+
cwd: string;
|
|
24
|
+
approvedBy: string;
|
|
25
|
+
ttlHours?: number;
|
|
26
|
+
sourceToolUseId?: string;
|
|
27
|
+
now?: Date;
|
|
28
|
+
}): BashAllowlistEntry;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Bash allowlist: the one place known-safe shell commands are permitted past the
|
|
2
|
+
// gate. Human-editable JSONL (one entry per line); malformed or expired lines are
|
|
3
|
+
// silently ignored so a typo can never crash the gate (it would fail closed and
|
|
4
|
+
// block everything). Globs are anchored (^...$) against the WHOLE command, so an
|
|
5
|
+
// exact entry like "npm test" will NOT match "npm test && curl evil" — that is the
|
|
6
|
+
// point: chaining-safety by default. The gate's deny-wins rule still applies — a
|
|
7
|
+
// command flagged by any policy elevation pattern can never be satisfied here.
|
|
8
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
9
|
+
import { dirname, resolve } from 'node:path';
|
|
10
|
+
export const DEFAULT_BASH_ALLOW_TTL_HOURS = 24;
|
|
11
|
+
function escapeRegexChar(c) {
|
|
12
|
+
return /[|\\{}()[\]^$+?.]/.test(c) ? `\\${c}` : c;
|
|
13
|
+
}
|
|
14
|
+
export function escapeGlobLiteral(s) {
|
|
15
|
+
return s.replace(/[\\*?]/g, '\\$&');
|
|
16
|
+
}
|
|
17
|
+
export function globToRegExp(glob) {
|
|
18
|
+
let out = '^';
|
|
19
|
+
for (let i = 0; i < glob.length; i++) {
|
|
20
|
+
const c = glob[i];
|
|
21
|
+
if (c === '\\') {
|
|
22
|
+
const next = glob[++i];
|
|
23
|
+
out += next === undefined ? '\\\\' : escapeRegexChar(next);
|
|
24
|
+
}
|
|
25
|
+
else if (c === '*') {
|
|
26
|
+
out += '.*';
|
|
27
|
+
}
|
|
28
|
+
else if (c === '?') {
|
|
29
|
+
out += '.';
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
out += escapeRegexChar(c);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return new RegExp(`${out}$`);
|
|
36
|
+
}
|
|
37
|
+
function isExpired(e, now) {
|
|
38
|
+
if (!e.expires_at)
|
|
39
|
+
return false;
|
|
40
|
+
const t = Date.parse(e.expires_at);
|
|
41
|
+
return Number.isFinite(t) ? t <= now.getTime() : true;
|
|
42
|
+
}
|
|
43
|
+
export function readBashAllowlist(filePath, now = new Date()) {
|
|
44
|
+
if (!existsSync(filePath))
|
|
45
|
+
return [];
|
|
46
|
+
const entries = [];
|
|
47
|
+
for (const line of readFileSync(filePath, 'utf8').split(/\r?\n/)) {
|
|
48
|
+
const trimmed = line.trim();
|
|
49
|
+
if (!trimmed || trimmed.startsWith('#'))
|
|
50
|
+
continue;
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(trimmed);
|
|
53
|
+
if (typeof parsed.command_glob !== 'string' || parsed.command_glob.length === 0)
|
|
54
|
+
continue;
|
|
55
|
+
if (parsed.cwd_glob !== undefined && typeof parsed.cwd_glob !== 'string')
|
|
56
|
+
continue;
|
|
57
|
+
if (isExpired(parsed, now))
|
|
58
|
+
continue;
|
|
59
|
+
entries.push(parsed);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// Human-editable JSONL: ignore malformed lines instead of breaking the gate.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return entries;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Default allowlist location, relative to a project/repo root. Override via the
|
|
69
|
+
* `bashAllowlistPath` field on GateContext to point anywhere you like.
|
|
70
|
+
*/
|
|
71
|
+
export function bashAllowlistPath(projectRoot) {
|
|
72
|
+
return resolve(projectRoot, 'rules', 'bash-allowlist.jsonl');
|
|
73
|
+
}
|
|
74
|
+
export function findBashAllowlistMatch(filePath, command, cwd, now = new Date()) {
|
|
75
|
+
const absCwd = resolve(cwd);
|
|
76
|
+
return readBashAllowlist(filePath, now).find((entry) => {
|
|
77
|
+
const cwdGlob = entry.cwd_glob ?? '*';
|
|
78
|
+
return globToRegExp(entry.command_glob).test(command) && globToRegExp(cwdGlob).test(absCwd);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export function appendExactBashAllowlistEntry(args) {
|
|
82
|
+
const now = args.now ?? new Date();
|
|
83
|
+
const ttlHours = args.ttlHours ?? DEFAULT_BASH_ALLOW_TTL_HOURS;
|
|
84
|
+
const expires = new Date(now.getTime() + ttlHours * 60 * 60 * 1000);
|
|
85
|
+
const entry = {
|
|
86
|
+
command_glob: escapeGlobLiteral(args.command),
|
|
87
|
+
cwd_glob: escapeGlobLiteral(resolve(args.cwd)),
|
|
88
|
+
expires_at: expires.toISOString(),
|
|
89
|
+
approved_by: args.approvedBy,
|
|
90
|
+
created_at: now.toISOString(),
|
|
91
|
+
};
|
|
92
|
+
if (args.sourceToolUseId)
|
|
93
|
+
entry.source_tool_use_id = args.sourceToolUseId;
|
|
94
|
+
mkdirSync(dirname(args.filePath), { recursive: true });
|
|
95
|
+
appendFileSync(args.filePath, `${JSON.stringify(entry)}\n`, 'utf8');
|
|
96
|
+
return entry;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=bash-allowlist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bash-allowlist.js","sourceRoot":"","sources":["../src/bash-allowlist.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,kFAAkF;AAClF,gFAAgF;AAChF,iFAAiF;AACjF,mFAAmF;AACnF,iFAAiF;AACjF,+EAA+E;AAE/E,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAY7C,MAAM,CAAC,MAAM,4BAA4B,GAAG,EAAE,CAAC;AAE/C,SAAS,eAAe,CAAC,CAAS;IAChC,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,CAAS;IACzC,OAAO,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,Y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|
|
3
|
+
// env/defaults the PreToolUse hook uses, then dispatches to runCli. Kept tiny so
|
|
4
|
+
// the testable logic lives in cli.ts.
|
|
5
|
+
import { resolve } from 'node:path';
|
|
6
|
+
import { JsonlApprovalStore } from './approval-store.js';
|
|
7
|
+
import { runCli } from './cli.js';
|
|
8
|
+
const root = process.cwd();
|
|
9
|
+
const approvalsPath = process.env.GATE_APPROVALS
|
|
10
|
+
? resolve(root, process.env.GATE_APPROVALS)
|
|
11
|
+
: resolve(root, '.gate', 'approvals.jsonl');
|
|
12
|
+
runCli(process.argv.slice(2), {
|
|
13
|
+
store: new JsonlApprovalStore(approvalsPath),
|
|
14
|
+
storeLabel: approvalsPath,
|
|
15
|
+
})
|
|
16
|
+
.then((code) => process.exit(code))
|
|
17
|
+
.catch((e) => {
|
|
18
|
+
process.stderr.write(`gate error (fail-closed): ${e instanceof Error ? e.message : String(e)}\n`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
});
|
|
21
|
+
//# sourceMappingURL=cli-bin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli-bin.js","sourceRoot":"","sources":["../src/cli-bin.ts"],"names":[],"mappings":";AACA,iFAAiF;AACjF,iFAAiF;AACjF,sCAAsC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;AAC3B,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc;IAC9C,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;IAC3C,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC;AAE9C,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;IAC5B,KAAK,EAAE,IAAI,kBAAkB,CAAC,aAAa,CAAC;IAC5C,UAAU,EAAE,aAAa;CAC1B,CAAC;KACC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;KAClC,KAAK,CAAC,CAAC,CAAU,EAAE,EAAE;IACpB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,6BAA6B,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAClG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ApprovalStore } from './approval-store.js';
|
|
2
|
+
export interface CliOptions {
|
|
3
|
+
store: ApprovalStore;
|
|
4
|
+
/** Human-readable label for the store (e.g. its resolved path), shown by `list`. */
|
|
5
|
+
storeLabel?: string;
|
|
6
|
+
out?: (line: string) => void;
|
|
7
|
+
err?: (line: string) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function runCli(argv: string[], opts: CliOptions): Promise<number>;
|