opencode-any-sandbox 0.0.0-stage → 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/LICENSE +201 -0
- package/README.md +111 -2
- package/dist/config.d.ts +53 -0
- package/dist/config.js +128 -0
- package/dist/engines.d.ts +35 -0
- package/dist/engines.js +161 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +354 -0
- package/dist/rpc.d.ts +43 -0
- package/dist/rpc.js +17 -0
- package/package.json +67 -4
- package/tui.jsx +165 -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 2025 OpenAI
|
|
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
CHANGED
|
@@ -1,3 +1,112 @@
|
|
|
1
|
-
#
|
|
1
|
+
# opencode-any-sandbox
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://opencode.ai)
|
|
5
|
+
[](#engines)
|
|
6
|
+
[](#engines)
|
|
7
|
+
[](https://github.com/carlostapiacl/opencode-any-sandbox/blob/main/evidence/README.md)
|
|
8
|
+
|
|
9
|
+
An [OpenCode](https://opencode.ai) plugin that runs the agent's shell commands inside the sandbox you choose:
|
|
10
|
+
[tartaros](https://github.com/carlostapiacl/tartaros) or Anthropic's
|
|
11
|
+
[sandbox-runtime](https://github.com/anthropic-experimental/sandbox-runtime) (`srt`). Both use the OS itself, with no
|
|
12
|
+
container or VM. It is a community plugin, not part of OpenCode.
|
|
13
|
+
|
|
14
|
+
```mermaid
|
|
15
|
+
flowchart LR
|
|
16
|
+
A[agent] -->|shell tool| H[shell.create.before]
|
|
17
|
+
H --> L[launcher]
|
|
18
|
+
L --> E{engine}
|
|
19
|
+
E -->|tartaros run| S[real shell, confined]
|
|
20
|
+
E -->|srt| S
|
|
21
|
+
A -->|write / edit / patch| P[permission.evaluate]
|
|
22
|
+
P -->|protected path| D[denied]
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```jsonc
|
|
28
|
+
// ~/.config/opencode/opencode.jsonc
|
|
29
|
+
{ "plugins": ["opencode-any-sandbox"] }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Then install the engine: `npm i -g @carlostapiacl/tartaros` (the default) or `npm i -g @anthropic-ai/sandbox-runtime`.
|
|
33
|
+
|
|
34
|
+
## What a command can and cannot do
|
|
35
|
+
|
|
36
|
+
- It writes to the project and to a private `$TMPDIR`, and nowhere else.
|
|
37
|
+
- It cannot touch `.opencode`, `.claude`, `.agents`, `.codex`, `opencode.json(c)`, `.git/hooks` or `.git/config`, so it
|
|
38
|
+
cannot plant a skill, a plugin or a git hook that runs later outside the sandbox. The `write`, `edit` and `patch`
|
|
39
|
+
tools are denied the same paths, also through a symlink or another letter case, and `cd` cannot enter them.
|
|
40
|
+
- It cannot read `~/.ssh`, `~/.aws`, `~/.gnupg`, `~/.kube`, `~/.config/gh`, `~/.netrc`, `~/.npmrc` or OpenCode's own
|
|
41
|
+
data.
|
|
42
|
+
- It has no network, unless you list domains.
|
|
43
|
+
|
|
44
|
+
## In the TUI
|
|
45
|
+
|
|
46
|
+
The prompt bar shows the engine and the network. `/sandbox` (also in `ctrl+p`) lists what the shell can write, read
|
|
47
|
+
and reach. When the plugin refuses a file edit, a `cd` or a command, a toast says so; what the engine denies inside a
|
|
48
|
+
running command shows in that command's output. Here the agent is asked to plant a skill with the `write` tool:
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
If the sandbox cannot start, commands are blocked, the bar turns red and the shell tool says why. Install the engine or
|
|
53
|
+
fix the config and the next command picks it up, without restarting OpenCode. Measurements and tests:
|
|
54
|
+
[evidence](https://github.com/carlostapiacl/opencode-any-sandbox/blob/main/evidence/README.md).
|
|
55
|
+
|
|
56
|
+
## Config
|
|
57
|
+
|
|
58
|
+
Optional, in `~/.config/opencode-any-sandbox/config.json` only. A repository cannot turn the sandbox off or widen it.
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"engine": "srt",
|
|
63
|
+
"write": ["~/.cache/go-build"],
|
|
64
|
+
"extraDenyRead": ["~/work/secrets"],
|
|
65
|
+
"allowDomains": ["registry.npmjs.org", "github.com"]
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
| key | default | what it does |
|
|
70
|
+
|---|---|---|
|
|
71
|
+
| `engine` | `"tartaros"` | `"tartaros"` or `"srt"` |
|
|
72
|
+
| `mode` | `"enforce"` | `"permissive"` runs commands unconfined, and tells the model, when the engine fails |
|
|
73
|
+
| `bin` | found on `PATH` | absolute path to the engine |
|
|
74
|
+
| `write` | `[]` | extra writable directories |
|
|
75
|
+
| `extraDenyRead` | `[]` | added to the default deny list (`denyRead` replaces it) |
|
|
76
|
+
| `denyWrite` | `[]` | extra paths inside a writable directory |
|
|
77
|
+
| `allowDomains` | `[]` | domains the commands can reach |
|
|
78
|
+
| `network` | `"none"` | `"all"` opens the network (tartaros only) |
|
|
79
|
+
| `engineArgs` | `[]` | passed to the engine before the command |
|
|
80
|
+
|
|
81
|
+
Plain JSON, no comments. An unknown key or a bad value blocks every command, with the file and the reason.
|
|
82
|
+
|
|
83
|
+
## Engines
|
|
84
|
+
|
|
85
|
+
| | tartaros | srt |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| macOS | Seatbelt | Seatbelt |
|
|
88
|
+
| Linux | Landlock and seccomp, nothing else to install | bubblewrap; needs `bwrap`, `rg` and `socat` |
|
|
89
|
+
| network | none, a domain allowlist, or all | none or a domain allowlist |
|
|
90
|
+
| environment | scrubbed to a safe allowlist | passed through |
|
|
91
|
+
|
|
92
|
+
Windows is next. Today commands are blocked there.
|
|
93
|
+
|
|
94
|
+
## Limits
|
|
95
|
+
|
|
96
|
+
- Only the shell tool is confined. MCP servers, formatters, LSPs and your own terminal run as before.
|
|
97
|
+
- A plugin that rewrites the same shell invocation after this one could undo it.
|
|
98
|
+
- OpenCode's experimental remote machines are not supported yet.
|
|
99
|
+
|
|
100
|
+
## How it works
|
|
101
|
+
|
|
102
|
+
`shell.create.before` swaps the shell for a small launcher named like the real one, so OpenCode keeps passing the
|
|
103
|
+
arguments that shell expects. The launcher runs `tartaros run <policy> -- <shell> <args>` or
|
|
104
|
+
`srt --settings <file> -- <shell> <args>`. The launcher and the srt settings live in a private directory outside every
|
|
105
|
+
writable root. Swapping the shell instead of the command comes from
|
|
106
|
+
[opencode-sandbox](https://github.com/isanchez31/opencode-sandbox-plugin).
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
npm install && npm test # runs every case through the engines found on PATH
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Apache-2.0.
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type EngineName = "tartaros" | "srt";
|
|
2
|
+
/** What the user writes in the config file. Every field is optional. */
|
|
3
|
+
export interface UserConfig {
|
|
4
|
+
/** Which sandbox runs the commands. */
|
|
5
|
+
engine?: EngineName;
|
|
6
|
+
/** "enforce" blocks a command the sandbox cannot run; "permissive" runs it unconfined with a warning. */
|
|
7
|
+
mode?: "enforce" | "permissive";
|
|
8
|
+
/** Absolute path to the engine's executable. Default: found on PATH. */
|
|
9
|
+
bin?: string;
|
|
10
|
+
/** Extra writable directories besides the project and the session's temp directory. */
|
|
11
|
+
write?: string[];
|
|
12
|
+
/** Paths the commands cannot read. Replaces the default list when set. */
|
|
13
|
+
denyRead?: string[];
|
|
14
|
+
/** Paths added to the default deny-read list. */
|
|
15
|
+
extraDenyRead?: string[];
|
|
16
|
+
/** Paths inside a writable directory the commands cannot write. Added to the protected names. */
|
|
17
|
+
denyWrite?: string[];
|
|
18
|
+
/** Domains the commands may reach. Empty with network "none" means no network. */
|
|
19
|
+
allowDomains?: string[];
|
|
20
|
+
/** "none" (default): only allowDomains. "all": unrestricted network. */
|
|
21
|
+
network?: "none" | "all";
|
|
22
|
+
/** Extra arguments passed to the engine as is, before the command. */
|
|
23
|
+
engineArgs?: string[];
|
|
24
|
+
}
|
|
25
|
+
export interface Config {
|
|
26
|
+
engine: EngineName;
|
|
27
|
+
mode: "enforce" | "permissive";
|
|
28
|
+
bin?: string;
|
|
29
|
+
write: string[];
|
|
30
|
+
denyRead: string[];
|
|
31
|
+
denyWrite: string[];
|
|
32
|
+
allowDomains: string[];
|
|
33
|
+
network: "none" | "all";
|
|
34
|
+
engineArgs: string[];
|
|
35
|
+
/** Where the config was read from, or undefined when the defaults apply. */
|
|
36
|
+
source?: string;
|
|
37
|
+
}
|
|
38
|
+
/** Credential stores and OpenCode's own data. The commands never need these. */
|
|
39
|
+
export declare const DEFAULT_DENY_READ: string[];
|
|
40
|
+
/**
|
|
41
|
+
* Names that OpenCode or another agent loads and runs from a project: config, plugins, skills, agents and git hooks.
|
|
42
|
+
* A sandboxed command must not plant them for the unconfined host to load later. Relative to each writable root.
|
|
43
|
+
*/
|
|
44
|
+
export declare const PROTECTED_NAMES: string[];
|
|
45
|
+
export declare function configDirectory(env?: NodeJS.ProcessEnv): string;
|
|
46
|
+
export declare function expandHome(value: string, home?: string): string;
|
|
47
|
+
/** Parses and validates a config object. Unknown keys are an error, so a typo does not silently weaken the sandbox. */
|
|
48
|
+
export declare function parse(raw: unknown, source?: string): Config;
|
|
49
|
+
/**
|
|
50
|
+
* Reads the config only from the user's config directory. Project files and plugin options are ignored on purpose:
|
|
51
|
+
* a repository must not be able to turn the sandbox off or widen it.
|
|
52
|
+
*/
|
|
53
|
+
export declare function load(env?: NodeJS.ProcessEnv): Promise<Config>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { lstat, readFile } from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
/** Credential stores and OpenCode's own data. The commands never need these. */
|
|
5
|
+
export const DEFAULT_DENY_READ = [
|
|
6
|
+
"~/.ssh",
|
|
7
|
+
"~/.aws",
|
|
8
|
+
"~/.gnupg",
|
|
9
|
+
"~/.kube",
|
|
10
|
+
"~/.docker/config.json",
|
|
11
|
+
"~/.netrc",
|
|
12
|
+
"~/.npmrc",
|
|
13
|
+
"~/.pypirc",
|
|
14
|
+
"~/.config/gh",
|
|
15
|
+
"~/.config/gcloud",
|
|
16
|
+
"~/.azure",
|
|
17
|
+
"~/.local/share/opencode",
|
|
18
|
+
"~/.config/opencode",
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* Names that OpenCode or another agent loads and runs from a project: config, plugins, skills, agents and git hooks.
|
|
22
|
+
* A sandboxed command must not plant them for the unconfined host to load later. Relative to each writable root.
|
|
23
|
+
*/
|
|
24
|
+
export const PROTECTED_NAMES = [
|
|
25
|
+
".opencode",
|
|
26
|
+
".claude",
|
|
27
|
+
".agents",
|
|
28
|
+
".codex",
|
|
29
|
+
"opencode.json",
|
|
30
|
+
"opencode.jsonc",
|
|
31
|
+
".git/hooks",
|
|
32
|
+
".git/config",
|
|
33
|
+
];
|
|
34
|
+
export function configDirectory(env = process.env) {
|
|
35
|
+
const base = env.XDG_CONFIG_HOME && path.isAbsolute(env.XDG_CONFIG_HOME) ? env.XDG_CONFIG_HOME : path.join(os.homedir(), ".config");
|
|
36
|
+
return path.join(base, "opencode-any-sandbox");
|
|
37
|
+
}
|
|
38
|
+
export function expandHome(value, home = os.homedir()) {
|
|
39
|
+
if (value === "~")
|
|
40
|
+
return home;
|
|
41
|
+
if (value.startsWith("~/"))
|
|
42
|
+
return path.join(home, value.slice(2));
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
const stringList = (value, field) => {
|
|
46
|
+
if (value === undefined)
|
|
47
|
+
return undefined;
|
|
48
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
|
|
49
|
+
throw new Error(`opencode-any-sandbox: "${field}" must be a list of strings`);
|
|
50
|
+
return value;
|
|
51
|
+
};
|
|
52
|
+
/** Parses and validates a config object. Unknown keys are an error, so a typo does not silently weaken the sandbox. */
|
|
53
|
+
export function parse(raw, source) {
|
|
54
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
55
|
+
throw new Error("opencode-any-sandbox: config must be a JSON object");
|
|
56
|
+
const input = raw;
|
|
57
|
+
// null is not "use the default": a key that is present must hold a valid value.
|
|
58
|
+
for (const [key, value] of Object.entries(input))
|
|
59
|
+
if (value === null)
|
|
60
|
+
throw new Error(`opencode-any-sandbox: "${key}" cannot be null; remove the key to use its default`);
|
|
61
|
+
const known = new Set(["$schema", "engine", "mode", "bin", "write", "denyRead", "extraDenyRead", "denyWrite", "allowDomains", "network", "engineArgs"]);
|
|
62
|
+
for (const key of Object.keys(input))
|
|
63
|
+
if (!known.has(key))
|
|
64
|
+
throw new Error(`opencode-any-sandbox: unknown config key "${key}"`);
|
|
65
|
+
const engine = input.engine ?? "tartaros";
|
|
66
|
+
if (engine !== "tartaros" && engine !== "srt")
|
|
67
|
+
throw new Error(`opencode-any-sandbox: engine must be "tartaros" or "srt"`);
|
|
68
|
+
const mode = input.mode ?? "enforce";
|
|
69
|
+
if (mode !== "enforce" && mode !== "permissive")
|
|
70
|
+
throw new Error(`opencode-any-sandbox: mode must be "enforce" or "permissive"`);
|
|
71
|
+
const network = input.network ?? "none";
|
|
72
|
+
if (network !== "none" && network !== "all")
|
|
73
|
+
throw new Error(`opencode-any-sandbox: network must be "none" or "all"`);
|
|
74
|
+
if (input.bin !== undefined && (typeof input.bin !== "string" || !path.isAbsolute(expandHome(input.bin))))
|
|
75
|
+
throw new Error(`opencode-any-sandbox: "bin" must be an absolute path`);
|
|
76
|
+
const absolute = (list, field) => list.map((item) => {
|
|
77
|
+
const value = expandHome(item);
|
|
78
|
+
if (!path.isAbsolute(value))
|
|
79
|
+
throw new Error(`opencode-any-sandbox: "${field}" entries must be absolute or start with ~/: ${item}`);
|
|
80
|
+
return path.normalize(value);
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
engine,
|
|
84
|
+
mode,
|
|
85
|
+
bin: input.bin === undefined ? undefined : expandHome(input.bin),
|
|
86
|
+
write: absolute(stringList(input.write, "write") ?? [], "write"),
|
|
87
|
+
denyRead: absolute([...(stringList(input.denyRead, "denyRead") ?? DEFAULT_DENY_READ), ...(stringList(input.extraDenyRead, "extraDenyRead") ?? [])], "denyRead"),
|
|
88
|
+
denyWrite: absolute(stringList(input.denyWrite, "denyWrite") ?? [], "denyWrite"),
|
|
89
|
+
allowDomains: stringList(input.allowDomains, "allowDomains") ?? [],
|
|
90
|
+
network,
|
|
91
|
+
engineArgs: stringList(input.engineArgs, "engineArgs") ?? [],
|
|
92
|
+
source,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Reads the config only from the user's config directory. Project files and plugin options are ignored on purpose:
|
|
97
|
+
* a repository must not be able to turn the sandbox off or widen it.
|
|
98
|
+
*/
|
|
99
|
+
export async function load(env = process.env) {
|
|
100
|
+
const file = path.join(configDirectory(env), "config.json");
|
|
101
|
+
let text;
|
|
102
|
+
try {
|
|
103
|
+
text = await readFile(file, "utf8");
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error.code === "ENOENT") {
|
|
107
|
+
// A symlink whose target is gone is not "no config": the user meant one, so do not fall back to the defaults.
|
|
108
|
+
const link = await lstat(file).catch(() => undefined);
|
|
109
|
+
if (!link)
|
|
110
|
+
return parse({});
|
|
111
|
+
throw new Error(`${file} points to a file that does not exist`);
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`cannot read ${file}: ${error.message}`);
|
|
114
|
+
}
|
|
115
|
+
let raw;
|
|
116
|
+
try {
|
|
117
|
+
raw = JSON.parse(text);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
throw new Error(`${file} is not valid JSON (comments are not allowed): ${error.message}`);
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
return parse(raw, file);
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
throw new Error(`${file}: ${error.message.replace(/^opencode-any-sandbox: /, "")}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Config, type EngineName } from "./config.js";
|
|
2
|
+
/** What the sandbox must enforce for one OpenCode location. Paths are absolute and canonical. */
|
|
3
|
+
export interface Policy {
|
|
4
|
+
project: string;
|
|
5
|
+
/** The session's private temp directory: writable, exported as TMPDIR. */
|
|
6
|
+
scratch: string;
|
|
7
|
+
writable: string[];
|
|
8
|
+
denyWrite: string[];
|
|
9
|
+
denyRead: string[];
|
|
10
|
+
allowDomains: string[];
|
|
11
|
+
network: "none" | "all";
|
|
12
|
+
}
|
|
13
|
+
export interface Engine {
|
|
14
|
+
name: EngineName;
|
|
15
|
+
/** Canonical path of the engine's executable. */
|
|
16
|
+
executable: string;
|
|
17
|
+
/** argv placed before the shell and its arguments. */
|
|
18
|
+
prefix: string[];
|
|
19
|
+
/** Variables the launcher exports for the engine process itself (not a way to pass secrets to the command). */
|
|
20
|
+
env: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
export declare function policy(config: Config, project: string, scratch: string): Promise<Policy>;
|
|
23
|
+
export declare function resolveExecutable(config: Config, env?: NodeJS.ProcessEnv): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Builds the engine's argv for a policy. Every protection is written as an explicit rule, so it does not depend on
|
|
26
|
+
* what the engine happens to protect by default.
|
|
27
|
+
*/
|
|
28
|
+
export declare function build(config: Config, executable: string, policy: Policy, privateDir: string): Promise<Engine>;
|
|
29
|
+
/**
|
|
30
|
+
* Runs `exit 0` through the engine. A sandbox that cannot start a trivial command will not start the agent's. The probe
|
|
31
|
+
* runs in its own process group, killed on timeout and after it exits, so no helper the engine started outlives it.
|
|
32
|
+
*/
|
|
33
|
+
export declare function probe(engine: Engine, cwd: string): Promise<void>;
|
|
34
|
+
/** The launcher OpenCode runs as the shell. It runs outside the sandbox and starts the real shell inside it. */
|
|
35
|
+
export declare function launcher(engine: Engine): string;
|