aegiscode 7.0.31 → 7.0.32
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/LICENSING.md +186 -0
- package/package.json +4 -2
- package/src/app.js +1 -1
- package/src/chatflow.js +1 -1
- package/src/commands.js +3 -1
- package/src/config.js +1 -1
- package/src/engine.js +1 -1
- package/src/history.js +1 -1
- package/src/models.js +1 -1
- package/src/overlays.js +1 -1
- package/src/panels.js +1 -1
- package/vendor/client/brain-catalog.js +1 -1
- package/vendor/client/byok-catalog.js +1 -1
- package/vendor/client/credits.js +45 -0
- package/vendor/client/foreign-sessions.js +3 -0
- package/vendor/client/naming.js +1 -1
- package/vendor/client/prefs.js +1 -1
- package/vendor/client/session-search.js +3 -0
- package/vendor/desktop/lib/local/claude-pro.js +1 -1
- package/vendor/desktop/lib/local/engine.js +1 -1
- package/vendor/desktop/lib/local/integrity.js +42 -2
- package/vendor/desktop/lib/local/perm-rules.js +206 -0
- package/vendor/desktop/lib/local/perm-store.js +298 -0
- package/vendor/desktop/lib/local/perm-write.js +199 -0
- package/vendor/desktop/lib/local/pro-sessions.js +3 -0
- package/vendor/desktop/lib/local/sandbox-appcontainer.js +242 -0
- package/vendor/desktop/lib/local/sandbox-bwrap.js +316 -0
- package/vendor/desktop/lib/local/sandbox-seatbelt.js +380 -0
- package/vendor/desktop/lib/local/sandbox.js +493 -0
- package/vendor/desktop/lib/local/shell.js +155 -20
- package/vendor/desktop/lib/local/tools.js +29 -4
- package/vendor/desktop/lib/settings.js +1 -1
- package/vendor/desktop/main.js +774 -37
- package/vendor/desktop/renderer/usage.js +1 -1
- package/vendor/mcp/admission.js +3 -0
- package/vendor/mcp/server.js +1 -1
- package/vendor/one/src/child.js +1 -1
- package/vendor/one/src/permissions.js +1 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
3
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
4
|
+
// Conveying a modified copy, or running one as a network service, owes the
|
|
5
|
+
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* sandbox-appcontainer.js — the Windows mechanism behind `sandbox.js` (plan §1e).
|
|
10
|
+
*
|
|
11
|
+
* ── The disagreement this file is named after ────────────────────────────────
|
|
12
|
+
*
|
|
13
|
+
* Linux has `bwrap` and macOS has `sandbox-exec`: both are *wrappers* that take
|
|
14
|
+
* a child argv and exec it confined, so a per-command bound is a argv prefix and
|
|
15
|
+
* the whole thing is pure construction. **Windows has no equivalent wrapper.**
|
|
16
|
+
* AppContainer confinement is not something you pass a command to; it is a set
|
|
17
|
+
* of token attributes applied to a process BEFORE it starts, via
|
|
18
|
+
* `CreateProcessAsUser`/`UpdateProcThreadAttribute` with a security-capability
|
|
19
|
+
* SID — a native Win32 sequence with no command-line surface at all.
|
|
20
|
+
*
|
|
21
|
+
* So there are exactly three honest options, and this file picks the third:
|
|
22
|
+
*
|
|
23
|
+
* 1. **Fake it.** Emit an "unavailable"/"degraded" sandwich that prints
|
|
24
|
+
* "sandbox: workspace-write" while nothing is confined. Forbidden — that is
|
|
25
|
+
* the false assurance `sandbox.js`'s header spends a paragraph outlawing.
|
|
26
|
+
* 2. **Shell out to PowerShell to build a job object.** `New-JobObject` does
|
|
27
|
+
* not exist; PowerShell has no AppContainer API. A job object can bound
|
|
28
|
+
* process count, CPU and memory, but NOT filesystem access — which is the
|
|
29
|
+
* entire point of the policy. So this would confine the wrong axis and
|
|
30
|
+
* would be reported as a file bound it does not provide.
|
|
31
|
+
* 3. **Report the truth and refuse to claim the mode.** `detect()` returns
|
|
32
|
+
* unavailable with a specific reason, `sandbox.js`'s existing contract
|
|
33
|
+
* turns that into a loud degrade, and `strict: true` refuses to spawn.
|
|
34
|
+
* Everything below is written so that the *moment* a helper binary exists
|
|
35
|
+
* it can be wired in without touching the policy layer.
|
|
36
|
+
*
|
|
37
|
+
* ── What the helper has to be, when someone builds it ────────────────────────
|
|
38
|
+
*
|
|
39
|
+
* A small signed executable — `aegis-sandbox-host.exe` — that:
|
|
40
|
+
*
|
|
41
|
+
* · creates an AppContainer profile (`CreateAppContainerProfile`) whose
|
|
42
|
+
* security-capability SID is derived per workspace, or reuses a cached one;
|
|
43
|
+
* · grants that SID ACLs on exactly the policy's write grants, and REMOVES it
|
|
44
|
+
* everywhere else (`SetEntriesInAcl` on the workspace root is not enough —
|
|
45
|
+
* the inherited-DACL walk is what people get wrong);
|
|
46
|
+
* · starts the child with `PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES`;
|
|
47
|
+
* · adds a job object with `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE` so a killed
|
|
48
|
+
* turn does not orphan a detached process tree (bwrap's
|
|
49
|
+
* `--die-with-parent`, which Windows does not provide for free);
|
|
50
|
+
* · removes the capability SID from the profile on exit, or the next run
|
|
51
|
+
* reuses a grant nobody re-approved.
|
|
52
|
+
*
|
|
53
|
+
* That is a native project, not a JS one. `helperCandidates()` below is the
|
|
54
|
+
* single place its path would be named, so the wiring is a one-line change to
|
|
55
|
+
* `detect()` rather than a redesign.
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
const fs = require('node:fs');
|
|
59
|
+
const path = require('node:path');
|
|
60
|
+
|
|
61
|
+
/** Where a vendored helper would live, relative to the app dir. */
|
|
62
|
+
const VENDOR_REL = path.join('vendor', 'sandbox');
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The executable a future implementation would wrap.
|
|
66
|
+
*
|
|
67
|
+
* Named `host` rather than `bwrap` deliberately: it is not a wrapper in the
|
|
68
|
+
* bwrap sense (argv prefix), it is a launcher that applies token attributes and
|
|
69
|
+
* then starts the child, so the file+args shape differs. Pretending it is a
|
|
70
|
+
* bwrap drop-in would make the eventual implementation a rewrite of `wrap()`.
|
|
71
|
+
*/
|
|
72
|
+
function helperCandidates(appDir, env = process.env) {
|
|
73
|
+
const out = [];
|
|
74
|
+
if (env && env.AEGIS_SANDBOX_HOST) out.push(String(env.AEGIS_SANDBOX_HOST));
|
|
75
|
+
if (appDir) {
|
|
76
|
+
out.push(path.join(appDir, VENDOR_REL, `${process.arch}-win32`, 'aegis-sandbox-host.exe'));
|
|
77
|
+
out.push(path.join(appDir, VENDOR_REL, 'win32', 'aegis-sandbox-host.exe'));
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** First existing helper, or null. */
|
|
83
|
+
function findHelper(appDir, env) {
|
|
84
|
+
for (const c of helperCandidates(appDir, env)) {
|
|
85
|
+
try {
|
|
86
|
+
const st = fs.statSync(c);
|
|
87
|
+
if (st.isFile()) return c;
|
|
88
|
+
} catch {
|
|
89
|
+
/* try the next candidate */
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Report availability truthfully.
|
|
97
|
+
*
|
|
98
|
+
* Two distinct unavailable reasons, because they mean different things to
|
|
99
|
+
* whoever reads the transcript:
|
|
100
|
+
*
|
|
101
|
+
* · no helper at all → `reason` names the missing mechanism
|
|
102
|
+
* · helper present, unproven → still unavailable, because a present binary
|
|
103
|
+
* that has never been exercised is not evidence it confines. A probe (start
|
|
104
|
+
* a child that must fail to write outside the grant) is the only thing that
|
|
105
|
+
* would make it available, and it is not written yet — this function says
|
|
106
|
+
* so instead of trusting the file's existence.
|
|
107
|
+
*
|
|
108
|
+
* If a helper appears AND a probe is supplied, this returns available. That is
|
|
109
|
+
* the seam: implementation work lands in `probeHelper`, not in this contract.
|
|
110
|
+
*/
|
|
111
|
+
function detect({ appDir, env = process.env, platform = process.platform, probe } = {}) {
|
|
112
|
+
if (platform !== 'win32') {
|
|
113
|
+
return {
|
|
114
|
+
available: false,
|
|
115
|
+
path: null,
|
|
116
|
+
reason: `no win32 mechanism on ${platform}`,
|
|
117
|
+
probed: false,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const bin = findHelper(appDir, env);
|
|
121
|
+
if (!bin) {
|
|
122
|
+
return {
|
|
123
|
+
available: false,
|
|
124
|
+
path: null,
|
|
125
|
+
reason:
|
|
126
|
+
'AppContainer has no command-line wrapper; a signed aegis-sandbox-host.exe is required and is not shipped',
|
|
127
|
+
probed: false,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (typeof probe !== 'function') {
|
|
131
|
+
return {
|
|
132
|
+
available: false,
|
|
133
|
+
path: bin,
|
|
134
|
+
reason: 'aegis-sandbox-host.exe present but unproven — no confinement probe is implemented',
|
|
135
|
+
probed: false,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const res = probe(bin);
|
|
139
|
+
if (res && res.ok) {
|
|
140
|
+
return { available: true, path: bin, reason: null, probed: true, mechanism: 'appcontainer' };
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
available: false,
|
|
144
|
+
path: bin,
|
|
145
|
+
reason: (res && res.reason) || 'aegis-sandbox-host probe failed',
|
|
146
|
+
probed: true,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Build the launcher argv for one command — shaped for the helper described in
|
|
152
|
+
* the header, not for bwrap.
|
|
153
|
+
*
|
|
154
|
+
* Kept here (rather than omitted) so the policy→argv translation is reviewable
|
|
155
|
+
* and testable on any host, and so the eventual native author has the exact
|
|
156
|
+
* contract to implement against. It is NOT reachable from production today:
|
|
157
|
+
* `detect()` cannot return available without a real helper and a real probe, and
|
|
158
|
+
* `sandbox.js` refuses to spawn a wrapped command when the state is unavailable.
|
|
159
|
+
*/
|
|
160
|
+
function wrap(policyInput, command, opts = {}) {
|
|
161
|
+
const { host, workspace, argv = [], cwd } = opts;
|
|
162
|
+
if (!host) return { ok: false, reason: 'no aegis-sandbox-host binary' };
|
|
163
|
+
const policy = policyInput || {};
|
|
164
|
+
if (policy.mode === 'off') return { ok: false, reason: 'sandbox disabled by policy' };
|
|
165
|
+
|
|
166
|
+
const ws = workspace ? path.resolve(workspace) : null;
|
|
167
|
+
const entries = (policy.fileSystem && Array.isArray(policy.fileSystem.entries))
|
|
168
|
+
? policy.fileSystem.entries
|
|
169
|
+
: [];
|
|
170
|
+
|
|
171
|
+
// Root write is refused for the same reason as on the other platforms: honouring
|
|
172
|
+
// it would make the confinement a no-op while the state still said "sandboxed".
|
|
173
|
+
for (const e of entries) {
|
|
174
|
+
if (!e || e.access !== 'write') continue;
|
|
175
|
+
const p = e.path;
|
|
176
|
+
if (p && p.type === 'special' && p.value && p.value.kind === 'root') {
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
policyFault: true,
|
|
180
|
+
reason: 'policy grants write to the filesystem root, which a sandbox cannot honour',
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const grants = [];
|
|
186
|
+
let tmpGrant = false;
|
|
187
|
+
for (const e of entries) {
|
|
188
|
+
if (!e || e.access !== 'write') continue;
|
|
189
|
+
const p = e.path;
|
|
190
|
+
if (!p) continue;
|
|
191
|
+
if (p.type === 'special') {
|
|
192
|
+
if (p.value && p.value.kind === 'slash_tmp') tmpGrant = true;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (p.type !== 'path' || typeof p.path !== 'string' || !p.path) continue;
|
|
196
|
+
grants.push(path.resolve(p.path));
|
|
197
|
+
}
|
|
198
|
+
if (ws && policy.mode === 'workspace-write' && !grants.includes(ws)) grants.push(ws);
|
|
199
|
+
|
|
200
|
+
// The helper takes the grant list explicitly rather than reading the policy
|
|
201
|
+
// itself: the policy vocabulary lives in one module, and a native copy of it
|
|
202
|
+
// would drift the moment `normalize()` changed.
|
|
203
|
+
const out = [host];
|
|
204
|
+
out.push('--mode', String(policy.mode));
|
|
205
|
+
out.push('--network', policy.network === 'open' ? 'open' : 'restricted');
|
|
206
|
+
if (tmpGrant) out.push('--grant-tmp');
|
|
207
|
+
for (const g of grants) out.push('--grant-write', g);
|
|
208
|
+
if (cwd) out.push('--cwd', String(cwd));
|
|
209
|
+
out.push('--');
|
|
210
|
+
for (const a of argv) out.push(String(a));
|
|
211
|
+
out.push(String(command));
|
|
212
|
+
return { ok: true, argv: out, guarantee: 'capability', pending: true };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The sentence the UI prints.
|
|
217
|
+
*
|
|
218
|
+
* Same rule as elsewhere: never claim enforcement that did not run. On Windows
|
|
219
|
+
* this will read to a user as "not supported yet", which is the truth.
|
|
220
|
+
*/
|
|
221
|
+
function describe(state, policy) {
|
|
222
|
+
if (!state || !state.available) {
|
|
223
|
+
return `sandbox: OFF — ${(state && state.reason) || 'unavailable'} (commands run with full user access)`;
|
|
224
|
+
}
|
|
225
|
+
const mode = (policy && policy.mode) || 'workspace-write';
|
|
226
|
+
const net = (policy && policy.network) === 'open' ? 'network open' : 'network restricted';
|
|
227
|
+
return `sandbox: ${mode} via AppContainer — ${net}`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
module.exports = {
|
|
231
|
+
name: 'win32',
|
|
232
|
+
mechanism: 'appcontainer',
|
|
233
|
+
guarantee: 'capability',
|
|
234
|
+
/** True when the mechanism exists in this build. Wired to reality, not hope. */
|
|
235
|
+
implemented: false,
|
|
236
|
+
VENDOR_REL,
|
|
237
|
+
helperCandidates,
|
|
238
|
+
findHelper,
|
|
239
|
+
detect,
|
|
240
|
+
wrap,
|
|
241
|
+
describe,
|
|
242
|
+
};
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
|
|
3
|
+
// AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
|
|
4
|
+
// Conveying a modified copy, or running one as a network service, owes the
|
|
5
|
+
// corresponding source under this licence (§5/§13) — see LICENSING.md.
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* sandbox-bwrap.js — the Linux mechanism behind `sandbox.js` (plan §1d).
|
|
10
|
+
*
|
|
11
|
+
* bubblewrap is a setuid-free user-namespace wrapper: `--ro-bind / /` makes the
|
|
12
|
+
* whole filesystem read-only, and each `--bind` re-opens exactly the paths the
|
|
13
|
+
* policy grants write. That is a capability grant, not a path blacklist — a
|
|
14
|
+
* blacklist is what a determined command walks around.
|
|
15
|
+
*
|
|
16
|
+
* It is NOT a defence against a kernel user-namespace escape, and it is not a
|
|
17
|
+
* network firewall: `--unshare-net` removes the interface, which stops TCP/UDP
|
|
18
|
+
* and leaves nothing to leak through because there is no interface to bind.
|
|
19
|
+
*
|
|
20
|
+
* Unprivileged user namespaces are restricted by AppArmor on Ubuntu 23.10+ and
|
|
21
|
+
* in most container images. When that happens the probe FAILS and the caller
|
|
22
|
+
* gets a loud degrade, not a quiet pass.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { spawnSync } = require('node:child_process');
|
|
26
|
+
const fs = require('node:fs');
|
|
27
|
+
const path = require('node:path');
|
|
28
|
+
|
|
29
|
+
/** Where a vendored bwrap would live, relative to the app dir. */
|
|
30
|
+
const VENDOR_REL = path.join('vendor', 'sandbox');
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Candidate bwrap paths, best first.
|
|
34
|
+
*
|
|
35
|
+
* The vendored copy wins over the system one, for the same reason the
|
|
36
|
+
* comparison set vendors its own: a distro package can be absent, or can be a
|
|
37
|
+
* version whose flags differ, and the sandbox must not vary by distro.
|
|
38
|
+
*/
|
|
39
|
+
function candidates(appDir, env = process.env) {
|
|
40
|
+
const out = [];
|
|
41
|
+
if (appDir) {
|
|
42
|
+
// A PACKAGED appDir ends in `app.asar`, and asar is an ARCHIVE: Electron's
|
|
43
|
+
// fs patch answers `statSync` for a virtual path inside it, but there is no
|
|
44
|
+
// directory entry there for `execve`. `electron-builder.yml`'s `asarUnpack`
|
|
45
|
+
// puts the real bytes beside it under `app.asar.unpacked/`, so the spawnable
|
|
46
|
+
// path is the .unpacked one. Both are offered, unpacked FIRST: the virtual
|
|
47
|
+
// path would pass `findBwrap`'s stat and then fail at spawn, which is the
|
|
48
|
+
// "looks available, fails at use" shape the mechanism must never report.
|
|
49
|
+
const unpacked = /app\.asar[\\/]?$/.test(String(appDir))
|
|
50
|
+
? String(appDir).replace(/app\.asar[\\/]?$/, 'app.asar.unpacked')
|
|
51
|
+
: String(appDir).replace(/app\.asar([\\/])/, 'app.asar.unpacked$1');
|
|
52
|
+
if (unpacked !== appDir) {
|
|
53
|
+
out.push(path.join(unpacked, VENDOR_REL, `${process.arch}-linux`, 'bwrap'));
|
|
54
|
+
out.push(path.join(unpacked, VENDOR_REL, 'linux', 'bwrap'));
|
|
55
|
+
}
|
|
56
|
+
out.push(path.join(appDir, VENDOR_REL, `${process.arch}-linux`, 'bwrap'));
|
|
57
|
+
out.push(path.join(appDir, VENDOR_REL, 'linux', 'bwrap'));
|
|
58
|
+
}
|
|
59
|
+
if (env && env.AEGIS_SANDBOX_BWRAP) out.push(String(env.AEGIS_SANDBOX_BWRAP));
|
|
60
|
+
out.push('/usr/bin/bwrap', '/bin/bwrap', '/usr/local/bin/bwrap');
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** First candidate that exists and is executable, or null. */
|
|
65
|
+
function findBwrap(appDir, env) {
|
|
66
|
+
for (const c of candidates(appDir, env)) {
|
|
67
|
+
try {
|
|
68
|
+
const st = fs.statSync(c);
|
|
69
|
+
if (st.isFile()) {
|
|
70
|
+
fs.accessSync(c, fs.constants.X_OK);
|
|
71
|
+
return c;
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
/* try the next one */
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Execute the probe for real. Never throws.
|
|
82
|
+
*
|
|
83
|
+
* `--ro-bind / /` alone is the cheapest thing that still proves the kernel
|
|
84
|
+
* granted a user namespace: without that grant bwrap cannot even perform the
|
|
85
|
+
* bind and exits non-zero. `spawnSync` with a short timeout because a hung
|
|
86
|
+
* probe would hang the first tool call of every turn.
|
|
87
|
+
*
|
|
88
|
+
* @returns {{ available: boolean, path: string|null, reason: string|null, probed: boolean }}
|
|
89
|
+
*/
|
|
90
|
+
function detect({ appDir, env = process.env, platform = process.platform, probe } = {}) {
|
|
91
|
+
if (platform !== 'linux') {
|
|
92
|
+
return { available: false, path: null, reason: `no linux mechanism on ${platform}`, probed: false };
|
|
93
|
+
}
|
|
94
|
+
const bin = findBwrap(appDir, env);
|
|
95
|
+
if (!bin) {
|
|
96
|
+
return { available: false, path: null, reason: 'bwrap not found', probed: false };
|
|
97
|
+
}
|
|
98
|
+
// Injectable so the "unavailable" branch is testable without breaking the
|
|
99
|
+
// host's own user namespaces.
|
|
100
|
+
const run = typeof probe === 'function' ? probe : defaultProbe;
|
|
101
|
+
const res = run(bin);
|
|
102
|
+
if (res && res.ok) return { available: true, path: bin, reason: null, probed: true, mechanism: 'bwrap' };
|
|
103
|
+
return {
|
|
104
|
+
available: false,
|
|
105
|
+
path: bin,
|
|
106
|
+
reason: (res && res.reason) || 'bwrap probe failed (unprivileged user namespaces unavailable?)',
|
|
107
|
+
probed: true,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function defaultProbe(bin) {
|
|
112
|
+
try {
|
|
113
|
+
const r = spawnSync(bin, ['--ro-bind', '/', '/', '--dev', '/dev', '--', '/bin/true'], {
|
|
114
|
+
timeout: 5000,
|
|
115
|
+
encoding: 'utf8',
|
|
116
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
117
|
+
});
|
|
118
|
+
if (r.error) return { ok: false, reason: `bwrap probe error: ${r.error.message}` };
|
|
119
|
+
if (r.status !== 0) {
|
|
120
|
+
const why = String(r.stderr || '').trim().split('\n')[0];
|
|
121
|
+
return { ok: false, reason: why || `bwrap probe exited ${r.status}` };
|
|
122
|
+
}
|
|
123
|
+
return { ok: true };
|
|
124
|
+
} catch (e) {
|
|
125
|
+
return { ok: false, reason: `bwrap probe threw: ${e.message}` };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Is this a special we know how to map onto bwrap argv? */
|
|
130
|
+
function isSpecial(entry, kind) {
|
|
131
|
+
const p = entry && entry.path;
|
|
132
|
+
return Boolean(p && p.type === 'special' && p.value && p.value.kind === kind);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Resolve an entry's absolute path, or null when it names a special location. */
|
|
136
|
+
function entryPath(entry) {
|
|
137
|
+
const p = entry && entry.path;
|
|
138
|
+
if (!p) return null;
|
|
139
|
+
if (p.type === 'path') return typeof p.path === 'string' && p.path ? p.path : null;
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Build the bwrap argv prefix for one command.
|
|
145
|
+
*
|
|
146
|
+
* Takes the ALREADY-NORMALISED policy from `sandbox.js`, so the mode bound
|
|
147
|
+
* (`read-only` cannot carry a write grant) is enforced in one place.
|
|
148
|
+
*
|
|
149
|
+
* Returns `{ ok: true, argv }` where argv is the full command line (bwrap plus
|
|
150
|
+
* the shell invocation), or `{ ok: false, reason }` when the policy cannot be
|
|
151
|
+
* honoured. Callers must treat `ok: false` as "do not run" and show `reason`.
|
|
152
|
+
*
|
|
153
|
+
* Shape, and why each flag:
|
|
154
|
+
* --ro-bind / / everything read-only first; the grants below re-open
|
|
155
|
+
* --dev /dev --proc /proc a command with no /dev or /proc fails obscurely
|
|
156
|
+
* --bind <ws> <ws> the ONE writable tree, when the mode grants it
|
|
157
|
+
* --ro-bind <p> <p> entries that exist with read access inside a write grant
|
|
158
|
+
* --unshare-net no interface at all when network is restricted
|
|
159
|
+
* --die-with-parent a killed turn must not leave the child running
|
|
160
|
+
* --new-session detach the controlling tty (no TIOCSTI injection back)
|
|
161
|
+
*/
|
|
162
|
+
function wrap(policyInput, command, opts = {}) {
|
|
163
|
+
const { bwrap, workspace, argv = [], cwd } = opts;
|
|
164
|
+
if (!bwrap) return { ok: false, reason: 'no bwrap binary' };
|
|
165
|
+
const policy = policyInput || {};
|
|
166
|
+
if (policy.mode === 'off') return { ok: false, reason: 'sandbox disabled by policy' };
|
|
167
|
+
|
|
168
|
+
const ws = workspace ? path.resolve(workspace) : null;
|
|
169
|
+
const out = [bwrap, '--ro-bind', '/', '/', '--dev', '/dev', '--proc', '/proc'];
|
|
170
|
+
const entries = (policy.fileSystem && Array.isArray(policy.fileSystem.entries))
|
|
171
|
+
? policy.fileSystem.entries
|
|
172
|
+
: [];
|
|
173
|
+
|
|
174
|
+
// Write grants. `root` read is already implied by --ro-bind /; a `root`
|
|
175
|
+
// WRITE grant is refused rather than honoured — it would make the sandbox a
|
|
176
|
+
// no-op while still reporting itself as active, which is the one failure
|
|
177
|
+
// mode this file must not have.
|
|
178
|
+
let rootWrite = false;
|
|
179
|
+
for (const e of entries) {
|
|
180
|
+
if (e.access !== 'write') continue;
|
|
181
|
+
if (isSpecial(e, 'root')) {
|
|
182
|
+
rootWrite = true;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (isSpecial(e, 'slash_tmp')) {
|
|
186
|
+
out.push('--bind', '/tmp', '/tmp');
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const p = entryPath(e);
|
|
190
|
+
if (!p) continue;
|
|
191
|
+
// `missingPathBehavior: 'skip'` means "bind it read-only IF it exists" —
|
|
192
|
+
// `.git` in a non-repo workspace, for instance. bwrap errors on a missing
|
|
193
|
+
// bind source, so existence is decided here, before the spawn.
|
|
194
|
+
let exists = false;
|
|
195
|
+
try {
|
|
196
|
+
fs.statSync(p);
|
|
197
|
+
exists = true;
|
|
198
|
+
} catch {
|
|
199
|
+
exists = false;
|
|
200
|
+
}
|
|
201
|
+
if (!exists) {
|
|
202
|
+
if (e.missingPathBehavior === 'skip') continue;
|
|
203
|
+
return { ok: false, policyFault: true, reason: `sandbox path does not exist: ${p}` };
|
|
204
|
+
}
|
|
205
|
+
out.push('--bind', p, p);
|
|
206
|
+
}
|
|
207
|
+
if (rootWrite) {
|
|
208
|
+
return {
|
|
209
|
+
ok: false,
|
|
210
|
+
policyFault: true,
|
|
211
|
+
reason: 'policy grants write to the filesystem root, which a sandbox cannot honour',
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// The workspace itself, when the mode says so and the policy did not already
|
|
216
|
+
// name it (defaultPolicy does, so this is the belt to that braces).
|
|
217
|
+
// `read-only` never takes this branch: that is the whole meaning of the mode.
|
|
218
|
+
if (ws && policy.mode === 'workspace-write') {
|
|
219
|
+
let already = false;
|
|
220
|
+
for (const e of entries) {
|
|
221
|
+
const p = entryPath(e);
|
|
222
|
+
if (e.access === 'write' && p && path.resolve(p) === ws) already = true;
|
|
223
|
+
}
|
|
224
|
+
if (!already) out.push('--bind', ws, ws);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Read-only re-grants INSIDE a writable tree (e.g. workspace write but .git
|
|
228
|
+
// read). Applied after the bind so it wins — bwrap's later mounts shadow.
|
|
229
|
+
for (const e of entries) {
|
|
230
|
+
if (e.access !== 'read') continue;
|
|
231
|
+
const p = entryPath(e);
|
|
232
|
+
if (!p) continue;
|
|
233
|
+
try {
|
|
234
|
+
fs.statSync(p);
|
|
235
|
+
} catch {
|
|
236
|
+
if (e.missingPathBehavior === 'skip') continue;
|
|
237
|
+
return { ok: false, policyFault: true, reason: `sandbox path does not exist: ${p}` };
|
|
238
|
+
}
|
|
239
|
+
out.push('--ro-bind', p, p);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Denies, LAST — after every bind and every re-grant, because bwrap mounts
|
|
243
|
+
// shadow in argv order and a deny emitted before a bind is silently undone by
|
|
244
|
+
// it. `--tmpfs` over the path empties it in the child's mount namespace while
|
|
245
|
+
// leaving the parent's untouched.
|
|
246
|
+
//
|
|
247
|
+
// Only paths that resolve inside an already-bound tree can be denied: bwrap
|
|
248
|
+
// has nothing to shadow under a path that was never mounted. A missing path
|
|
249
|
+
// is a hard fault rather than a skip, because `--tmpfs` on a nonexistent path
|
|
250
|
+
// exits non-zero and would otherwise be read as "the sandbox refused the
|
|
251
|
+
// command" — the wrong diagnosis for a typo.
|
|
252
|
+
const denies = (policy.fileSystem && Array.isArray(policy.fileSystem.denies))
|
|
253
|
+
? policy.fileSystem.denies
|
|
254
|
+
: [];
|
|
255
|
+
for (const d of denies) {
|
|
256
|
+
const p = d && d.path;
|
|
257
|
+
if (!p) continue;
|
|
258
|
+
let st = null;
|
|
259
|
+
try {
|
|
260
|
+
st = fs.statSync(p);
|
|
261
|
+
} catch {
|
|
262
|
+
return { ok: false, policyFault: true, reason: `sandbox deny path does not exist: ${p}` };
|
|
263
|
+
}
|
|
264
|
+
// A FILE cannot be shadowed by --tmpfs (it mounts a directory). Denying one
|
|
265
|
+
// file inside a writable tree needs --ro-bind of /dev/null over it, which is
|
|
266
|
+
// a different mechanism; refuse rather than silently not denying.
|
|
267
|
+
if (!st.isDirectory()) {
|
|
268
|
+
return {
|
|
269
|
+
ok: false,
|
|
270
|
+
policyFault: true,
|
|
271
|
+
reason: `sandbox deny must name a directory, not a file: ${p}`,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
out.push('--tmpfs', p);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (policy.network === 'restricted') out.push('--unshare-net');
|
|
278
|
+
out.push('--die-with-parent', '--new-session');
|
|
279
|
+
if (cwd) out.push('--chdir', cwd);
|
|
280
|
+
out.push('--');
|
|
281
|
+
for (const a of argv) out.push(String(a));
|
|
282
|
+
out.push(String(command));
|
|
283
|
+
return { ok: true, argv: out, policy, guarantee: 'capability' };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The sentence the UI and the transcript print.
|
|
288
|
+
*
|
|
289
|
+
* Never says "sandboxed" on a host where nothing was enforced — a lie here
|
|
290
|
+
* costs more than the sandbox buys, because it transfers trust the user did not
|
|
291
|
+
* grant.
|
|
292
|
+
*/
|
|
293
|
+
function describe(state, policy) {
|
|
294
|
+
if (!state || !state.available) {
|
|
295
|
+
return `sandbox: OFF — ${(state && state.reason) || 'unavailable'} (commands run with full user access)`;
|
|
296
|
+
}
|
|
297
|
+
const mode = (policy && policy.mode) || 'workspace-write';
|
|
298
|
+
const net = (policy && policy.network) === 'open' ? 'network open' : 'network restricted';
|
|
299
|
+
return `sandbox: ${mode} via bwrap — ${net}`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
module.exports = {
|
|
303
|
+
name: 'linux',
|
|
304
|
+
mechanism: 'bwrap',
|
|
305
|
+
guarantee: 'capability',
|
|
306
|
+
implemented: true,
|
|
307
|
+
VENDOR_REL,
|
|
308
|
+
candidates,
|
|
309
|
+
findBwrap,
|
|
310
|
+
detect,
|
|
311
|
+
defaultProbe,
|
|
312
|
+
wrap,
|
|
313
|
+
describe,
|
|
314
|
+
isSpecial,
|
|
315
|
+
entryPath,
|
|
316
|
+
};
|