permiscope-opencode-plugin 0.0.0-stage → 2.2.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 +47 -2
- package/dist/index.js +181 -0
- package/package.json +20 -4
package/README.md
CHANGED
|
@@ -1,3 +1,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# permiscope-opencode-plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
OpenCode V2 plugin that applies [Permiscope](https://github.com/shubhusion/Permiscope) policy
|
|
4
|
+
to tool permissions. It uses the shared evaluator and never upgrades an approval request to
|
|
5
|
+
an allow.
|
|
6
|
+
|
|
7
|
+
> This package is prepared for the unreleased v2.2.0 line. Publishing requires explicit
|
|
8
|
+
> approval; until then, build it with `npm run build:opencode` from the repository and copy
|
|
9
|
+
> this directory into `.opencode/plugins/permiscope` in your project.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install permiscope@latest permiscope-opencode-plugin@latest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`opencode.json`:
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
{
|
|
21
|
+
"plugins": [
|
|
22
|
+
{
|
|
23
|
+
"package": "permiscope-opencode-plugin",
|
|
24
|
+
"options": {
|
|
25
|
+
"approvalMode": "deny",
|
|
26
|
+
"policyFile": ".permiscope/policy.json"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`approvalMode` may be `deny` (default), `ask` or `passthrough`. See
|
|
34
|
+
[ADR-0028](../../docs/adr/0028-approval-modes.md). Do not use `ask` or `passthrough` with
|
|
35
|
+
`opencode run --auto`: the host auto-approves `ask`, so sensitive reads can proceed without a
|
|
36
|
+
human. Keep `deny` for unattended sessions.
|
|
37
|
+
|
|
38
|
+
## Build
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm install
|
|
42
|
+
npm run build
|
|
43
|
+
npm run build:opencode
|
|
44
|
+
npm run typecheck:plugins
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The build bundles only this plugin's own files. All Permiscope functions come from the public
|
|
48
|
+
`permiscope` package, so no repository paths are needed at runtime.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// plugins/opencode/src/index.ts
|
|
2
|
+
import { createHookDecisionRecorder, resolvePolicy } from "permiscope";
|
|
3
|
+
|
|
4
|
+
// plugins/opencode/src/approval.ts
|
|
5
|
+
var APPROVAL_MODES = /* @__PURE__ */ new Set(["deny", "ask", "passthrough"]);
|
|
6
|
+
function resolveApprovalMode(option, env = process.env) {
|
|
7
|
+
const raw = option !== void 0 ? option : env.PERMISCOPE_APPROVAL_MODE;
|
|
8
|
+
if (raw === void 0) return { mode: "deny" };
|
|
9
|
+
if (typeof raw === "string" && APPROVAL_MODES.has(raw)) {
|
|
10
|
+
return { mode: raw };
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
mode: "deny",
|
|
14
|
+
warning: `Unknown approval mode ${String(raw)}; failing closed with deny`
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// plugins/opencode/src/permission.ts
|
|
19
|
+
import {
|
|
20
|
+
defaultPolicy,
|
|
21
|
+
evaluateHookAction,
|
|
22
|
+
finalizeHookDecision,
|
|
23
|
+
unmappedDenyVerdict
|
|
24
|
+
} from "permiscope";
|
|
25
|
+
function decisionRecord(event, action, verdict, effect) {
|
|
26
|
+
return {
|
|
27
|
+
host: "opencode",
|
|
28
|
+
tool: event.action,
|
|
29
|
+
sessionId: event.sessionID,
|
|
30
|
+
action,
|
|
31
|
+
verdict: verdict.decision,
|
|
32
|
+
effect,
|
|
33
|
+
reason: verdict.reason,
|
|
34
|
+
reasonCode: verdict.reasonCode
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function mapPermission(action, resources, now = /* @__PURE__ */ new Date()) {
|
|
38
|
+
const resource = resources[0];
|
|
39
|
+
const base = { agentId: "opencode-hook", timestamp: now };
|
|
40
|
+
if (!resource) return null;
|
|
41
|
+
switch (action.toLowerCase()) {
|
|
42
|
+
case "bash":
|
|
43
|
+
case "shell":
|
|
44
|
+
case "execute":
|
|
45
|
+
return { ...base, actionName: "run_command", parameters: { command: resource } };
|
|
46
|
+
case "read":
|
|
47
|
+
case "grep":
|
|
48
|
+
case "glob":
|
|
49
|
+
return { ...base, actionName: "read_file", parameters: { path: resource } };
|
|
50
|
+
case "write":
|
|
51
|
+
case "edit":
|
|
52
|
+
case "patch":
|
|
53
|
+
case "apply_patch":
|
|
54
|
+
return { ...base, actionName: "write_file", parameters: { path: resource } };
|
|
55
|
+
case "webfetch":
|
|
56
|
+
case "fetch":
|
|
57
|
+
return { ...base, actionName: "call_api", parameters: { url: resource } };
|
|
58
|
+
case "external_directory":
|
|
59
|
+
return {
|
|
60
|
+
...base,
|
|
61
|
+
actionName: "external_directory",
|
|
62
|
+
parameters: { path: resource.replace(/\/\*$/, "") }
|
|
63
|
+
};
|
|
64
|
+
case "subagent":
|
|
65
|
+
case "skill":
|
|
66
|
+
case "glob":
|
|
67
|
+
case "grep":
|
|
68
|
+
case "websearch":
|
|
69
|
+
case "question":
|
|
70
|
+
return { ...base, actionName: "host_capability", parameters: { action } };
|
|
71
|
+
default:
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function evaluatePermission(event, policy = defaultPolicy, now = /* @__PURE__ */ new Date(), options = {}) {
|
|
76
|
+
if (event.effect === "deny") return null;
|
|
77
|
+
const mode = resolveApprovalMode(options.approvalMode, {}).mode;
|
|
78
|
+
const recorder = options.recorder ?? null;
|
|
79
|
+
const finalize = (record, result) => finalizeHookDecision({
|
|
80
|
+
decision: record,
|
|
81
|
+
output: result,
|
|
82
|
+
recorder,
|
|
83
|
+
isDeny: (value) => value !== null && value.effect === "deny",
|
|
84
|
+
deny: (reason) => ({ effect: "deny", message: reason })
|
|
85
|
+
});
|
|
86
|
+
const action = mapPermission(event.action, event.resources ?? [], now);
|
|
87
|
+
if (!action) {
|
|
88
|
+
if (event.effect === "allow") {
|
|
89
|
+
const verdict2 = unmappedDenyVerdict();
|
|
90
|
+
return finalize(decisionRecord(event, void 0, verdict2, "deny"), {
|
|
91
|
+
effect: "deny",
|
|
92
|
+
message: verdict2.reason
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const verdict = await evaluateHookAction(action, policy);
|
|
98
|
+
if (verdict.decision === "deny") {
|
|
99
|
+
return finalize(decisionRecord(event, action, verdict, "deny"), {
|
|
100
|
+
effect: "deny",
|
|
101
|
+
message: verdict.reason
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (verdict.decision === "ask") {
|
|
105
|
+
if (mode === "ask") {
|
|
106
|
+
return finalize(decisionRecord(event, action, verdict, "ask"), {
|
|
107
|
+
effect: "ask",
|
|
108
|
+
message: verdict.reason
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
if (mode === "passthrough") {
|
|
112
|
+
return finalize(decisionRecord(event, action, verdict, event.effect), null);
|
|
113
|
+
}
|
|
114
|
+
return finalize(decisionRecord(event, action, verdict, "deny"), {
|
|
115
|
+
effect: "deny",
|
|
116
|
+
message: `${verdict.reason}. Rerun without --auto or use an approval-capable host`
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return finalize(decisionRecord(event, action, verdict, event.effect), null);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// plugins/opencode/src/index.ts
|
|
123
|
+
var plugin = {
|
|
124
|
+
id: "permiscope",
|
|
125
|
+
async setup(ctx) {
|
|
126
|
+
const directory = ctx.location?.directory ?? process.cwd();
|
|
127
|
+
const recorder = createHookDecisionRecorder({ directory });
|
|
128
|
+
const { mode, warning } = resolveApprovalMode(ctx.options?.approvalMode);
|
|
129
|
+
if (warning) console.error(`[permiscope] ${warning}`);
|
|
130
|
+
if ((mode === "ask" || mode === "passthrough") && process.argv.includes("--auto")) {
|
|
131
|
+
console.error(
|
|
132
|
+
`[permiscope] approval mode "${mode}" with --auto: approval requests may be auto-approved by OpenCode`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
let policy = null;
|
|
136
|
+
let policyError;
|
|
137
|
+
const optionFile = ctx.options?.policyFile;
|
|
138
|
+
try {
|
|
139
|
+
policy = resolvePolicy({
|
|
140
|
+
policyFile: typeof optionFile === "string" ? optionFile : void 0,
|
|
141
|
+
cwd: directory,
|
|
142
|
+
baseDir: directory
|
|
143
|
+
}).policy;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
policyError = error instanceof Error ? error.message : String(error);
|
|
146
|
+
console.error(`[permiscope] ${policyError}; denying allow/ask decisions`);
|
|
147
|
+
}
|
|
148
|
+
await ctx.permission.hook("evaluate", async (event) => {
|
|
149
|
+
if (!policy) {
|
|
150
|
+
if (event.effect !== "deny") {
|
|
151
|
+
const reason = `Permiscope blocked: POLICY_FILE_INVALID: ${policyError ?? "policy unavailable"}`;
|
|
152
|
+
event.effect = "deny";
|
|
153
|
+
event.message = reason;
|
|
154
|
+
if (recorder) {
|
|
155
|
+
await recorder.record({
|
|
156
|
+
host: "opencode",
|
|
157
|
+
tool: event.action,
|
|
158
|
+
sessionId: event.sessionID,
|
|
159
|
+
verdict: "deny",
|
|
160
|
+
effect: "deny",
|
|
161
|
+
reason,
|
|
162
|
+
reasonCode: "POLICY_FILE_INVALID"
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const verdict = await evaluatePermission(event, policy, /* @__PURE__ */ new Date(), {
|
|
169
|
+
recorder,
|
|
170
|
+
approvalMode: mode
|
|
171
|
+
});
|
|
172
|
+
if (!verdict) return;
|
|
173
|
+
event.effect = verdict.effect;
|
|
174
|
+
event.message = verdict.message;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
var index_default = plugin;
|
|
179
|
+
export {
|
|
180
|
+
index_default as default
|
|
181
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "permiscope-opencode-plugin",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "OpenCode plugin that applies Permiscope policy to tool permissions",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=24"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"permiscope": "^2.2.0"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT"
|
|
22
|
+
}
|