@fencesandbox/opencode-fence 0.1.1
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 +201 -0
- package/README.md +123 -0
- package/dist/errors.d.ts +3 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -0
- package/dist/factory.d.ts +64 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +103 -0
- package/dist/factory.js.map +1 -0
- package/dist/fence-runner.d.ts +75 -0
- package/dist/fence-runner.d.ts.map +1 -0
- package/dist/fence-runner.js +82 -0
- package/dist/fence-runner.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Tusk AI, Inc
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# Fence Sandbox Plugin for OpenCode
|
|
2
|
+
|
|
3
|
+
OpenCode plugin that routes the `bash` tool through [Fence](https://github.com/fencesandbox/fence)'s pre-tool-use hook for command-policy enforcement.
|
|
4
|
+
|
|
5
|
+
Fence is a lightweight, container-free process sandbox for running commands with network, filesystem, and command policies.
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
When OpenCode's agent calls the `bash` tool, this plugin intercepts the call and asks Fence to evaluate the command against your policy. Three outcomes:
|
|
10
|
+
|
|
11
|
+
| Fence verdict | Plugin behavior |
|
|
12
|
+
|---|---|
|
|
13
|
+
| **Deny** | Throws an `Error` with Fence's reason. OpenCode surfaces the error in the UI and the command does not run. |
|
|
14
|
+
| **Wrap** | Rewrites the command to run inside `fence -c "..."`. The command then inherits Fence's filesystem and network policy in addition to the command-policy check it just passed. |
|
|
15
|
+
| **Allow** | Leaves the command unchanged. |
|
|
16
|
+
|
|
17
|
+
## Why use this
|
|
18
|
+
|
|
19
|
+
Fence's command policy is enforced at two points:
|
|
20
|
+
|
|
21
|
+
1. **Preflight** — once, on whatever command is given to `fence` (e.g. `fence -- opencode` only preflights `opencode`).
|
|
22
|
+
2. **Runtime exec** — at the kernel exec boundary, against descendant processes.
|
|
23
|
+
|
|
24
|
+
Runtime exec on macOS, and Linux without `runtimeExecPolicy: "argv"`, only handles single-token denies (e.g. `sudo`). Multi-token rules like `gh repo create`, `git push`, and `npm publish` are preflight-only — so when OpenCode spawns one of those after Fence has already preflighted `opencode`, the deny rule does not fire.
|
|
25
|
+
|
|
26
|
+
This plugin closes that gap by re-running preflight on every shell invocation OpenCode's agent issues, before the command runs. See Fence's [Enforcement Across Child Processes](https://github.com/fencesandbox/fence/blob/main/docs/configuration.md#enforcement-across-child-processes) for the full model.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
> [!NOTE]
|
|
31
|
+
> This package moved from `@use-tusk/opencode-fence` to `@fencesandbox/opencode-fence`. If you installed the old package manually, remove it from your OpenCode config and use the new package name below.
|
|
32
|
+
|
|
33
|
+
### Prerequisite
|
|
34
|
+
|
|
35
|
+
Install [Fence](https://github.com/fencesandbox/fence) and confirm it's on your `PATH`:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
fence --version
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
You need Fence v0.1.52 or later for the `--opencode-pre-tool-use` subcommand the plugin spawns.
|
|
42
|
+
|
|
43
|
+
### Add the plugin to OpenCode
|
|
44
|
+
|
|
45
|
+
The easiest way is to let Fence install it for you:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
fence hooks install --opencode
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This adds `@fencesandbox/opencode-fence` to your `~/.config/opencode/opencode.json` (or `opencode.jsonc` if you use that). See [`fence hooks install --opencode`](https://github.com/fencesandbox/fence/blob/main/docs/agents.md#hooks) for details and flags (e.g. `--file` to target a project-local config).
|
|
52
|
+
|
|
53
|
+
If you'd rather edit the config file by hand:
|
|
54
|
+
|
|
55
|
+
```jsonc
|
|
56
|
+
// ~/.config/opencode/opencode.json
|
|
57
|
+
{
|
|
58
|
+
"$schema": "https://opencode.ai/config.json",
|
|
59
|
+
"plugin": ["@fencesandbox/opencode-fence"]
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
OpenCode installs the package via Bun and loads the plugin automatically on next launch.
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
The package's default export is a `Plugin` configured with sensible defaults — it is what gets loaded when you list `@fencesandbox/opencode-fence` in opencode.json's `plugin` array.
|
|
68
|
+
|
|
69
|
+
To customize behavior (custom fence binary, pinned settings file, or template), import the factory from the `/factory` sub-path and construct the plugin yourself in a local plugin shim:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
// ~/.config/opencode/plugins/fence.ts
|
|
73
|
+
import { createFencePlugin } from "@fencesandbox/opencode-fence/factory";
|
|
74
|
+
|
|
75
|
+
export const Fence = createFencePlugin({
|
|
76
|
+
// Pin a specific config file (mutually exclusive with `template`).
|
|
77
|
+
settingsPath: "/Users/me/work/fence.json",
|
|
78
|
+
|
|
79
|
+
// Or pin a built-in Fence template (mutually exclusive with `settingsPath`).
|
|
80
|
+
// template: "code",
|
|
81
|
+
|
|
82
|
+
// Override the fence binary location (default: "fence" on PATH).
|
|
83
|
+
// fenceBinary: "/opt/homebrew/bin/fence",
|
|
84
|
+
|
|
85
|
+
// For local development only. In production this turns the plugin into a no-op
|
|
86
|
+
// when fence is missing or crashes; leave false.
|
|
87
|
+
// failOpenOnRunnerError: false,
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
If you use the local-shim form, **remove `@fencesandbox/opencode-fence` from opencode.json's `plugin` array** to avoid registering the plugin twice. The shim file is auto-loaded by OpenCode; the array entry would also load the no-options version.
|
|
92
|
+
|
|
93
|
+
The factory and the supporting types live on a sub-path so they don't appear in the main entry's exports — OpenCode's plugin loader iterates every export of a package's entry module and tries to register each one as a plugin, so anything other than the `Plugin` itself must live elsewhere.
|
|
94
|
+
|
|
95
|
+
If neither `settingsPath` nor `template` is set, Fence resolves config from the working directory the same way it does for direct invocations: walking upward for `fence.jsonc`/`fence.json`, falling back to `~/.config/fence/`.
|
|
96
|
+
|
|
97
|
+
### Composing with whole-agent wrapping
|
|
98
|
+
|
|
99
|
+
You can (and should) run OpenCode itself under Fence:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
fence -t code -- opencode
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
…and additionally enable this plugin. The plugin detects `FENCE_SANDBOX=1` (set by Fence on the wrapped process) and stops adding `fence -c` wrappers, so commands aren't double-sandboxed. The deny check still runs, which is the whole point of using both: whole-agent wrapping handles filesystem and network policy; the plugin handles multi-token command policy on platforms where Fence's runtime exec deny can't.
|
|
106
|
+
|
|
107
|
+
## Limitations
|
|
108
|
+
|
|
109
|
+
### User-typed `!` commands bypass this plugin
|
|
110
|
+
|
|
111
|
+
OpenCode's plugin lifecycle does **not** currently fire `tool.execute.before` for commands typed directly into the TUI with the `!` prefix. Those commands still run, just outside this plugin's reach.
|
|
112
|
+
|
|
113
|
+
If you need user-typed `!` commands to also be policy-checked today, run OpenCode under Fence (`fence -t code -- opencode`). That gives you Fence's whole-process sandbox for all descendants, with the documented caveat that multi-token denies (which this plugin would catch) remain preflight-only for the `!` path.
|
|
114
|
+
|
|
115
|
+
### Spawn cost per bash call
|
|
116
|
+
|
|
117
|
+
The plugin spawns `fence` once per `bash` tool call. Fence's preflight is fast (sub-100ms in our benchmarks), but cold OS-level process spawn cost adds up if your agent is hammering `bash`. If this becomes a problem in practice, let us know.
|
|
118
|
+
|
|
119
|
+
## Contributing
|
|
120
|
+
|
|
121
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for setup, local-development install patterns, and the release process.
|
|
122
|
+
|
|
123
|
+
Bug reports, feature requests, and PRs welcome.
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAQA,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Re-export the runner error class on a sub-path so it does not appear in the
|
|
2
|
+
// main entry's exports. OpenCode's plugin loader calls every named export of
|
|
3
|
+
// the entry module as `await fn(input)`; a class export there would throw
|
|
4
|
+
// "cannot be invoked without 'new'" or worse, run as a malformed Plugin.
|
|
5
|
+
//
|
|
6
|
+
// Local plugin shims that need to catch this error type should import it
|
|
7
|
+
// from `@fencesandbox/opencode-fence/errors`.
|
|
8
|
+
export { FenceRunnerError } from "./fence-runner.js";
|
|
9
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,6EAA6E;AAC7E,0EAA0E;AAC1E,yEAAyE;AACzE,EAAE;AACF,yEAAyE;AACzE,8CAA8C;AAG9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
export interface FencePluginOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Override the path to the `fence` executable. Defaults to `fence` on PATH.
|
|
5
|
+
*/
|
|
6
|
+
fenceBinary?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Pass `--settings <path>` through to fence so the plugin uses a specific config
|
|
9
|
+
* file regardless of which directory OpenCode is running in. Mutually exclusive
|
|
10
|
+
* with `template`.
|
|
11
|
+
*/
|
|
12
|
+
settingsPath?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Pass `--template <name>` through to fence (e.g. "code", "code-relaxed"). Mutually
|
|
15
|
+
* exclusive with `settingsPath`.
|
|
16
|
+
*/
|
|
17
|
+
template?: string;
|
|
18
|
+
/**
|
|
19
|
+
* If true, fail open when fence cannot be invoked (binary missing, helper crashed).
|
|
20
|
+
* Defaults to false: by default, plugin errors block tool execution.
|
|
21
|
+
*
|
|
22
|
+
* Setting this to true is **only** appropriate for development. In production, a
|
|
23
|
+
* silent fail-open turns a security policy into a no-op.
|
|
24
|
+
*/
|
|
25
|
+
failOpenOnRunnerError?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* createFencePlugin returns an OpenCode `Plugin` that routes the `bash` tool
|
|
29
|
+
* through Fence's pre-tool-use hook, configured with the supplied options.
|
|
30
|
+
*
|
|
31
|
+
* Most users should NOT call this directly. The default export of
|
|
32
|
+
* `@fencesandbox/opencode-fence` is already a `Plugin` with sensible defaults
|
|
33
|
+
* suitable for the `plugin: [...]` array in opencode.json. Use this factory
|
|
34
|
+
* only when you need to override one of the FencePluginOptions (e.g. pinning
|
|
35
|
+
* a specific Fence settings file or template), and write a local plugin shim
|
|
36
|
+
* under .opencode/plugins/ that constructs the Plugin yourself.
|
|
37
|
+
*
|
|
38
|
+
* Why a separate file: OpenCode's plugin loader iterates every export of the
|
|
39
|
+
* package's entry point and calls each one with `(input)` to register hooks.
|
|
40
|
+
* If the entry point exported a factory, OpenCode would call it as
|
|
41
|
+
* `factory(input)` instead of `factory(options)`, silently producing a
|
|
42
|
+
* malformed Plugin instance. Keeping the factory in a sub-path
|
|
43
|
+
* (`@fencesandbox/opencode-fence/factory`) ensures it is never accidentally
|
|
44
|
+
* iterated by the loader.
|
|
45
|
+
*
|
|
46
|
+
* Behavior of the returned Plugin:
|
|
47
|
+
* - For each `tool.execute.before` invocation of the `bash` tool, the plugin
|
|
48
|
+
* sends the command to Fence's hook helper.
|
|
49
|
+
* - If Fence denies the command, the plugin throws an Error with the Fence
|
|
50
|
+
* reason; OpenCode surfaces this in the UI and the command does not run.
|
|
51
|
+
* - If Fence allows the command, the plugin rewrites it to run inside
|
|
52
|
+
* `fence -c "..."`, so the command inherits filesystem and network policy
|
|
53
|
+
* from Fence's sandbox.
|
|
54
|
+
* - If `FENCE_SANDBOX=1` is set in the environment (i.e. the agent is already
|
|
55
|
+
* running inside `fence -- opencode`), the plugin enforces deny rules but
|
|
56
|
+
* skips wrapping — nesting `fence` inside `fence` would double-sandbox
|
|
57
|
+
* without benefit.
|
|
58
|
+
*
|
|
59
|
+
* Limitation: OpenCode's plugin lifecycle does not currently fire
|
|
60
|
+
* `tool.execute.before` for user-typed `!`-prefixed commands. Those bypass
|
|
61
|
+
* this plugin. See https://github.com/fencesandbox/opencode-fence#limitations.
|
|
62
|
+
*/
|
|
63
|
+
export declare const createFencePlugin: (options?: FencePluginOptions) => Plugin;
|
|
64
|
+
//# sourceMappingURL=factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGlD,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;;;OAMG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,iBAAiB,GAAI,UAAS,kBAAuB,KAAG,MAuDpE,CAAC"}
|
package/dist/factory.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { FenceRunnerError, runFence } from "./fence-runner.js";
|
|
2
|
+
/**
|
|
3
|
+
* createFencePlugin returns an OpenCode `Plugin` that routes the `bash` tool
|
|
4
|
+
* through Fence's pre-tool-use hook, configured with the supplied options.
|
|
5
|
+
*
|
|
6
|
+
* Most users should NOT call this directly. The default export of
|
|
7
|
+
* `@fencesandbox/opencode-fence` is already a `Plugin` with sensible defaults
|
|
8
|
+
* suitable for the `plugin: [...]` array in opencode.json. Use this factory
|
|
9
|
+
* only when you need to override one of the FencePluginOptions (e.g. pinning
|
|
10
|
+
* a specific Fence settings file or template), and write a local plugin shim
|
|
11
|
+
* under .opencode/plugins/ that constructs the Plugin yourself.
|
|
12
|
+
*
|
|
13
|
+
* Why a separate file: OpenCode's plugin loader iterates every export of the
|
|
14
|
+
* package's entry point and calls each one with `(input)` to register hooks.
|
|
15
|
+
* If the entry point exported a factory, OpenCode would call it as
|
|
16
|
+
* `factory(input)` instead of `factory(options)`, silently producing a
|
|
17
|
+
* malformed Plugin instance. Keeping the factory in a sub-path
|
|
18
|
+
* (`@fencesandbox/opencode-fence/factory`) ensures it is never accidentally
|
|
19
|
+
* iterated by the loader.
|
|
20
|
+
*
|
|
21
|
+
* Behavior of the returned Plugin:
|
|
22
|
+
* - For each `tool.execute.before` invocation of the `bash` tool, the plugin
|
|
23
|
+
* sends the command to Fence's hook helper.
|
|
24
|
+
* - If Fence denies the command, the plugin throws an Error with the Fence
|
|
25
|
+
* reason; OpenCode surfaces this in the UI and the command does not run.
|
|
26
|
+
* - If Fence allows the command, the plugin rewrites it to run inside
|
|
27
|
+
* `fence -c "..."`, so the command inherits filesystem and network policy
|
|
28
|
+
* from Fence's sandbox.
|
|
29
|
+
* - If `FENCE_SANDBOX=1` is set in the environment (i.e. the agent is already
|
|
30
|
+
* running inside `fence -- opencode`), the plugin enforces deny rules but
|
|
31
|
+
* skips wrapping — nesting `fence` inside `fence` would double-sandbox
|
|
32
|
+
* without benefit.
|
|
33
|
+
*
|
|
34
|
+
* Limitation: OpenCode's plugin lifecycle does not currently fire
|
|
35
|
+
* `tool.execute.before` for user-typed `!`-prefixed commands. Those bypass
|
|
36
|
+
* this plugin. See https://github.com/fencesandbox/opencode-fence#limitations.
|
|
37
|
+
*/
|
|
38
|
+
export const createFencePlugin = (options = {}) => {
|
|
39
|
+
return async () => {
|
|
40
|
+
return {
|
|
41
|
+
"tool.execute.before": async (input, output) => {
|
|
42
|
+
if (input.tool !== "bash")
|
|
43
|
+
return;
|
|
44
|
+
// Best-effort extraction. The "bash" tool ships `command: string` on its
|
|
45
|
+
// input; future schema changes would need a small adapter here.
|
|
46
|
+
const args = output.args;
|
|
47
|
+
const command = typeof args.command === "string" ? args.command : "";
|
|
48
|
+
if (command.trim() === "")
|
|
49
|
+
return;
|
|
50
|
+
const cwd = typeof args.cwd === "string" ? args.cwd : undefined;
|
|
51
|
+
const runnerOpts = buildRunnerOptions(options);
|
|
52
|
+
let result;
|
|
53
|
+
try {
|
|
54
|
+
result = runFence({
|
|
55
|
+
hook_event_name: "PreToolUse",
|
|
56
|
+
tool_name: "Bash",
|
|
57
|
+
tool_input: cwd === undefined ? { command } : { command, cwd },
|
|
58
|
+
}, runnerOpts);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
if (options.failOpenOnRunnerError && err instanceof FenceRunnerError) {
|
|
62
|
+
// Surface a warning so the user knows policy is not being applied. We
|
|
63
|
+
// still run the command (because failOpenOnRunnerError was set), but
|
|
64
|
+
// we want them to notice in the logs.
|
|
65
|
+
console.warn(`[opencode-fence] ${err.message} (continuing because failOpenOnRunnerError=true)`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
if (result.decision === "deny") {
|
|
71
|
+
throw new Error(`Fence denied this command: ${result.reason}`);
|
|
72
|
+
}
|
|
73
|
+
if (result.decision === "wrap" && result.rewrittenCommand) {
|
|
74
|
+
// Honor the same env contract Fence uses for nested invocations: if the
|
|
75
|
+
// agent is already running inside fence, don't double-wrap.
|
|
76
|
+
if ((runnerOpts.env?.FENCE_SANDBOX ?? process.env.FENCE_SANDBOX) === "1") {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
args.command = result.rewrittenCommand;
|
|
80
|
+
}
|
|
81
|
+
// decision === "allow" or wrap was suppressed: leave args.command alone.
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
function buildRunnerOptions(options) {
|
|
87
|
+
if (options.settingsPath && options.template) {
|
|
88
|
+
throw new Error("opencode-fence: settingsPath and template are mutually exclusive");
|
|
89
|
+
}
|
|
90
|
+
const extraArgs = [];
|
|
91
|
+
if (options.settingsPath) {
|
|
92
|
+
extraArgs.push("--settings", options.settingsPath);
|
|
93
|
+
}
|
|
94
|
+
if (options.template) {
|
|
95
|
+
extraArgs.push("--template", options.template);
|
|
96
|
+
}
|
|
97
|
+
const runnerOpts = { extraArgs };
|
|
98
|
+
if (options.fenceBinary !== undefined) {
|
|
99
|
+
runnerOpts.fenceBinary = options.fenceBinary;
|
|
100
|
+
}
|
|
101
|
+
return runnerOpts;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAA2B,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AA4BxF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAA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|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
/**
|
|
3
|
+
* Wire format produced by `fence --opencode-pre-tool-use`. The plugin synthesizes
|
|
4
|
+
* the request, the Fence helper writes the response to stdout.
|
|
5
|
+
*
|
|
6
|
+
* Intentionally close to Claude/Cursor pre-tool-use payloads so a future Fence
|
|
7
|
+
* binary can dispatch all three through one code path.
|
|
8
|
+
*/
|
|
9
|
+
export interface FenceHookRequest {
|
|
10
|
+
/** Stable string identifying the calling agent. */
|
|
11
|
+
hook_event_name: "PreToolUse";
|
|
12
|
+
tool_name: "Bash";
|
|
13
|
+
/** OpenCode-flavored tool input. We always include `command`; `cwd` is best-effort. */
|
|
14
|
+
tool_input: {
|
|
15
|
+
command: string;
|
|
16
|
+
cwd?: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface FenceHookResponse {
|
|
20
|
+
/**
|
|
21
|
+
* "deny": Fence's command policy rejected the command.
|
|
22
|
+
* - `reason` contains a human-readable explanation surfaced to the user.
|
|
23
|
+
* "wrap": Fence allows the command but rewrote it to run inside a `fence -c` shell.
|
|
24
|
+
* - `tool_input.command` is the rewritten command.
|
|
25
|
+
* "allow": Fence allows the command unchanged.
|
|
26
|
+
*/
|
|
27
|
+
decision: "deny" | "wrap" | "allow";
|
|
28
|
+
reason?: string;
|
|
29
|
+
tool_input?: {
|
|
30
|
+
command?: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface FenceRunnerOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Path to the `fence` executable. Defaults to "fence" on PATH.
|
|
36
|
+
*/
|
|
37
|
+
fenceBinary?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Subcommand flag. Today targets a Fence subcommand we still need to add upstream.
|
|
40
|
+
* When that lands in fence we can flip the default; until then, callers can pass
|
|
41
|
+
* `"--claude-pre-tool-use"` to fall back to the existing helper.
|
|
42
|
+
*
|
|
43
|
+
* TODO(opencode-pre-tool-use): wire this up in fence/cmd/fence/hooks_*.go and remove
|
|
44
|
+
* the override path here.
|
|
45
|
+
*/
|
|
46
|
+
subcommand?: string;
|
|
47
|
+
/** Extra args passed through to fence (e.g. `--settings` or `--template`). */
|
|
48
|
+
extraArgs?: readonly string[];
|
|
49
|
+
/**
|
|
50
|
+
* Environment passed to the fence subprocess. Defaults to `process.env`.
|
|
51
|
+
* Tests inject this; production callers should leave it undefined.
|
|
52
|
+
*/
|
|
53
|
+
env?: NodeJS.ProcessEnv;
|
|
54
|
+
/**
|
|
55
|
+
* Timeout in ms for the Fence subprocess. Defaults to 5 seconds; preflight is in-process
|
|
56
|
+
* config evaluation so it is fast, but a hung filesystem/IO operation should not stall
|
|
57
|
+
* a tool call indefinitely.
|
|
58
|
+
*/
|
|
59
|
+
timeoutMs?: number;
|
|
60
|
+
/**
|
|
61
|
+
* Injected spawnSync, for testing. Production callers should leave this undefined.
|
|
62
|
+
*/
|
|
63
|
+
spawn?: typeof spawnSync;
|
|
64
|
+
}
|
|
65
|
+
export interface RunFenceResult {
|
|
66
|
+
decision: "deny" | "wrap" | "allow";
|
|
67
|
+
reason?: string;
|
|
68
|
+
rewrittenCommand?: string;
|
|
69
|
+
}
|
|
70
|
+
export declare class FenceRunnerError extends Error {
|
|
71
|
+
readonly cause?: unknown | undefined;
|
|
72
|
+
constructor(message: string, cause?: unknown | undefined);
|
|
73
|
+
}
|
|
74
|
+
export declare function runFence(request: FenceHookRequest, options?: FenceRunnerOptions): RunFenceResult;
|
|
75
|
+
//# sourceMappingURL=fence-runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fence-runner.d.ts","sourceRoot":"","sources":["../src/fence-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEtE;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,mDAAmD;IACnD,eAAe,EAAE,YAAY,CAAC;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,UAAU,EAAE;QACV,OAAO,EAAE,MAAM,CAAC;QAChB,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;CACH;AAED,MAAM,WAAW,iBAAiB;IAChC;;;;;;OAMG;IACH,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE;QACX,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,CAAC;CACH;AAED,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9B;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IACxB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,SAAS,CAAC;CAC1B;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,qBAAa,gBAAiB,SAAQ,KAAK;aAGd,KAAK,CAAC,EAAE,OAAO;gBADxC,OAAO,EAAE,MAAM,EACU,KAAK,CAAC,EAAE,OAAO,YAAA;CAK3C;AAKD,wBAAgB,QAAQ,CACtB,OAAO,EAAE,gBAAgB,EACzB,OAAO,GAAE,kBAAuB,GAC/B,cAAc,CA2FhB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
export class FenceRunnerError extends Error {
|
|
3
|
+
cause;
|
|
4
|
+
constructor(message, cause) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.cause = cause;
|
|
7
|
+
this.name = "FenceRunnerError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const DEFAULT_TIMEOUT_MS = 5_000;
|
|
11
|
+
const DEFAULT_SUBCOMMAND = "--opencode-pre-tool-use";
|
|
12
|
+
export function runFence(request, options = {}) {
|
|
13
|
+
const { fenceBinary = "fence", subcommand = DEFAULT_SUBCOMMAND, extraArgs = [], env, timeoutMs = DEFAULT_TIMEOUT_MS, spawn = spawnSync, } = options;
|
|
14
|
+
const args = [subcommand, ...extraArgs];
|
|
15
|
+
const stdin = JSON.stringify(request);
|
|
16
|
+
let result;
|
|
17
|
+
try {
|
|
18
|
+
result = spawn(fenceBinary, args, {
|
|
19
|
+
input: stdin,
|
|
20
|
+
encoding: "utf8",
|
|
21
|
+
timeout: timeoutMs,
|
|
22
|
+
env: env ?? process.env,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
throw new FenceRunnerError(`Failed to invoke ${fenceBinary}`, err);
|
|
27
|
+
}
|
|
28
|
+
if (result.error) {
|
|
29
|
+
const code = result.error.code;
|
|
30
|
+
if (code === "ENOENT") {
|
|
31
|
+
throw new FenceRunnerError(`Could not find the \`${fenceBinary}\` executable on PATH. Install Fence (https://github.com/fencesandbox/fence) or set fenceBinary in plugin options.`, result.error);
|
|
32
|
+
}
|
|
33
|
+
throw new FenceRunnerError(`Failed to invoke ${fenceBinary}: ${result.error.message}`, result.error);
|
|
34
|
+
}
|
|
35
|
+
if (result.signal) {
|
|
36
|
+
throw new FenceRunnerError(`${fenceBinary} terminated by signal ${result.signal}`);
|
|
37
|
+
}
|
|
38
|
+
if (typeof result.status !== "number") {
|
|
39
|
+
throw new FenceRunnerError(`${fenceBinary} did not return an exit status`);
|
|
40
|
+
}
|
|
41
|
+
// Non-zero exit: treat as a Fence policy / runtime error. Fence's hook helpers
|
|
42
|
+
// exit non-zero for malformed input or internal failures; in those cases we
|
|
43
|
+
// surface the stderr text rather than silently allowing the command.
|
|
44
|
+
if (result.status !== 0) {
|
|
45
|
+
const stderr = (result.stderr || "").trim();
|
|
46
|
+
const stdout = (result.stdout || "").trim();
|
|
47
|
+
const detail = stderr || stdout || `exit ${result.status}`;
|
|
48
|
+
throw new FenceRunnerError(`Fence hook helper failed: ${detail}`);
|
|
49
|
+
}
|
|
50
|
+
const stdout = (result.stdout || "").trim();
|
|
51
|
+
if (stdout === "") {
|
|
52
|
+
// No-op response: the helper saw nothing it needed to do (e.g. pure `cd`,
|
|
53
|
+
// command already wrapped). Treat as allow-unchanged.
|
|
54
|
+
return { decision: "allow" };
|
|
55
|
+
}
|
|
56
|
+
let parsed;
|
|
57
|
+
try {
|
|
58
|
+
parsed = JSON.parse(stdout);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
throw new FenceRunnerError(`Fence returned non-JSON response: ${stdout.slice(0, 200)}`, err);
|
|
62
|
+
}
|
|
63
|
+
switch (parsed.decision) {
|
|
64
|
+
case "deny":
|
|
65
|
+
return {
|
|
66
|
+
decision: "deny",
|
|
67
|
+
reason: parsed.reason ?? "Command blocked by Fence policy.",
|
|
68
|
+
};
|
|
69
|
+
case "wrap": {
|
|
70
|
+
const rewritten = parsed.tool_input?.command;
|
|
71
|
+
if (!rewritten) {
|
|
72
|
+
throw new FenceRunnerError("Fence returned decision=wrap with no tool_input.command");
|
|
73
|
+
}
|
|
74
|
+
return { decision: "wrap", rewrittenCommand: rewritten };
|
|
75
|
+
}
|
|
76
|
+
case "allow":
|
|
77
|
+
return { decision: "allow" };
|
|
78
|
+
default:
|
|
79
|
+
throw new FenceRunnerError(`Fence returned unknown decision: ${JSON.stringify(parsed.decision)}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=fence-runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fence-runner.js","sourceRoot":"","sources":["../src/fence-runner.ts"],"names":[],"mappings":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,IAAI,CAAC,KAAK,CAAC,MAAM,CAAsB,CAAC;IACnD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,IAAI,gBAAgB,CAAC,qCAAqC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IAC/F,CAAC;IAED,QAAQ,MAAM,CAAC,QAAQ,EAAE,CAAC;QACxB,KAAK,MAAM;YACT,OAAO;gBACL,QAAQ,EAAE,MAAM;gBAChB,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,kCAAkC;aAC5D,CAAC;QACJ,KAAK,MAAM,CAAC,CAAC,CAAC;YACZ,MAAM,SAAS,GAAG,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC;YAC7C,IAAI,CAAC,SAAS,EAAE,CAAC;gBACf,MAAM,IAAI,gBAAgB,CAAC,yDAAyD,CAAC,CAAC;YACxF,CAAC;YACD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,CAAC;QAC3D,CAAC;QACD,KAAK,OAAO;YACV,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;QAC/B;YACE,MAAM,IAAI,gBAAgB,CACxB,oCAAoC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CACtE,CAAC;IACN,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
/**
|
|
3
|
+
* The default `@fencesandbox/opencode-fence` plugin. Routes the `bash` tool
|
|
4
|
+
* through `fence --opencode-pre-tool-use` using the Fence binary on PATH and
|
|
5
|
+
* the active project / user Fence config.
|
|
6
|
+
*
|
|
7
|
+
* Wire it into opencode.json's `plugin` array:
|
|
8
|
+
*
|
|
9
|
+
* {
|
|
10
|
+
* "plugin": ["@fencesandbox/opencode-fence"]
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* For non-default behavior (custom fence binary, pinned settings file or
|
|
14
|
+
* template, fail-open in dev), import `createFencePlugin` from
|
|
15
|
+
* `@fencesandbox/opencode-fence/factory` and construct it yourself in a local
|
|
16
|
+
* plugin shim under `.opencode/plugins/`.
|
|
17
|
+
*
|
|
18
|
+
* Why this entry point is so thin: OpenCode's plugin loader iterates *every*
|
|
19
|
+
* export of the package's entry module and calls each one with the loader's
|
|
20
|
+
* input object to register hooks. Anything else exported here (a factory, an
|
|
21
|
+
* error class, a helper) would be invoked the same way and either crash the
|
|
22
|
+
* loader or silently register a malformed Plugin. So the entry point is
|
|
23
|
+
* deliberately just one Plugin and its default-export alias; everything else
|
|
24
|
+
* lives in sub-paths.
|
|
25
|
+
*/
|
|
26
|
+
export declare const FencePlugin: Plugin;
|
|
27
|
+
export default FencePlugin;
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGlD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,WAAW,EAAE,MAA4B,CAAC;AAEvD,eAAe,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createFencePlugin } from "./factory.js";
|
|
2
|
+
/**
|
|
3
|
+
* The default `@fencesandbox/opencode-fence` plugin. Routes the `bash` tool
|
|
4
|
+
* through `fence --opencode-pre-tool-use` using the Fence binary on PATH and
|
|
5
|
+
* the active project / user Fence config.
|
|
6
|
+
*
|
|
7
|
+
* Wire it into opencode.json's `plugin` array:
|
|
8
|
+
*
|
|
9
|
+
* {
|
|
10
|
+
* "plugin": ["@fencesandbox/opencode-fence"]
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* For non-default behavior (custom fence binary, pinned settings file or
|
|
14
|
+
* template, fail-open in dev), import `createFencePlugin` from
|
|
15
|
+
* `@fencesandbox/opencode-fence/factory` and construct it yourself in a local
|
|
16
|
+
* plugin shim under `.opencode/plugins/`.
|
|
17
|
+
*
|
|
18
|
+
* Why this entry point is so thin: OpenCode's plugin loader iterates *every*
|
|
19
|
+
* export of the package's entry module and calls each one with the loader's
|
|
20
|
+
* input object to register hooks. Anything else exported here (a factory, an
|
|
21
|
+
* error class, a helper) would be invoked the same way and either crash the
|
|
22
|
+
* loader or silently register a malformed Plugin. So the entry point is
|
|
23
|
+
* deliberately just one Plugin and its default-export alias; everything else
|
|
24
|
+
* lives in sub-paths.
|
|
25
|
+
*/
|
|
26
|
+
export const FencePlugin = createFencePlugin();
|
|
27
|
+
export default FencePlugin;
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,MAAM,WAAW,GAAW,iBAAiB,EAAE,CAAC;AAEvD,eAAe,WAAW,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fencesandbox/opencode-fence",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "OpenCode plugin that routes bash tool invocations through Fence for command-policy enforcement.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./factory": {
|
|
14
|
+
"types": "./dist/factory.d.ts",
|
|
15
|
+
"import": "./dist/factory.js"
|
|
16
|
+
},
|
|
17
|
+
"./errors": {
|
|
18
|
+
"types": "./dist/errors.d.ts",
|
|
19
|
+
"import": "./dist/errors.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"registry": "https://registry.npmjs.org/"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.build.json",
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"test:watch": "vitest",
|
|
36
|
+
"lint": "biome check src",
|
|
37
|
+
"lint:fix": "biome check --write src",
|
|
38
|
+
"format": "biome format --write src",
|
|
39
|
+
"prepublishOnly": "npm run lint && npm run typecheck && npm test && npm run build"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"opencode",
|
|
43
|
+
"opencode-plugin",
|
|
44
|
+
"fence",
|
|
45
|
+
"sandbox",
|
|
46
|
+
"security"
|
|
47
|
+
],
|
|
48
|
+
"author": "Fence Sandbox",
|
|
49
|
+
"license": "Apache-2.0",
|
|
50
|
+
"homepage": "https://github.com/fencesandbox/opencode-fence",
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/fencesandbox/opencode-fence.git"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/fencesandbox/opencode-fence/issues"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=18"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"@opencode-ai/plugin": ">=1.14.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@biomejs/biome": "^2.0.0",
|
|
66
|
+
"@opencode-ai/plugin": "^1.14.28",
|
|
67
|
+
"@types/node": "^20.0.0",
|
|
68
|
+
"typescript": "^5.5.0",
|
|
69
|
+
"vitest": "^2.0.0"
|
|
70
|
+
}
|
|
71
|
+
}
|