@commonlyai/cli 0.1.340 → 0.1.342
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/node_modules/@commonlyai/seats/src/wire/provider-proxy.js +76 -7
- package/package.json +1 -1
- package/src/lib/adapters/dsh.js +8 -2
- package/src/lib/adapters/goose.js +1 -1
- package/src/lib/adapters/opencode.js +12 -5
- package/src/lib/adapters/pi.js +38 -30
- package/src/lib/adapters/qwen.js +1 -1
- package/src/lib/provider-proxy.js +41 -0
- package/src/lib/sandbox/bwrap.js +17 -0
- package/src/lib/sandbox/seatbelt.js +53 -0
|
@@ -2,11 +2,13 @@ import { Transform } from 'node:stream';
|
|
|
2
2
|
import { StringDecoder } from 'node:string_decoder';
|
|
3
3
|
/** The per-spawn loopback proxy that holds a provider key for a CLI (OpenAI chat, Anthropic Messages, OpenAI Responses). */
|
|
4
4
|
import { randomBytes, timingSafeEqual } from 'crypto';
|
|
5
|
-
import { constants as fsConstants, realpathSync } from 'fs';
|
|
5
|
+
import { constants as fsConstants, realpathSync, statSync } from 'fs';
|
|
6
6
|
import { createServer, request as httpRequest } from 'http';
|
|
7
7
|
import { request as httpsRequest } from 'https';
|
|
8
8
|
import { chmod, lstat, open, unlink, writeFile } from 'fs/promises';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
dirname, isAbsolute, relative, sep,
|
|
11
|
+
} from 'path';
|
|
10
12
|
|
|
11
13
|
// Providers sometimes echo request headers in an error response. Redact the
|
|
12
14
|
// material across arbitrary stream chunk boundaries before any model sees it.
|
|
@@ -112,8 +114,64 @@ const isPathWithin = (parent, candidate) => {
|
|
|
112
114
|
&& !fromParent.startsWith(`..${sep}`)
|
|
113
115
|
&& !isAbsolute(fromParent));
|
|
114
116
|
};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
+
// What the file system says a path IS, not how it was spelled: one device and
|
|
118
|
+
// inode, whatever the letter case on a case-insensitive volume, a macOS
|
|
119
|
+
// firmlink (/System/Volumes/Data/opt is /opt) or a symlinked directory.
|
|
120
|
+
// BigInt: inodes on the macOS sealed system volume exceed 2^53, and as numbers
|
|
121
|
+
// /usr and /usr/bin round to the same value.
|
|
122
|
+
const identityOf = (path) => {
|
|
123
|
+
try {
|
|
124
|
+
const details = statSync(path, { bigint: true });
|
|
125
|
+
return `${details.dev}:${details.ino}`;
|
|
126
|
+
} catch {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const realOrSelf = (path) => {
|
|
132
|
+
try { return realpathSync(path); } catch { return path; }
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The root a sandbox could read this key file through, or null. A subpath root
|
|
137
|
+
* holds the file when it is the file's directory or one of its ancestors; a
|
|
138
|
+
* literal root only when it is the file itself. Either of two tests refuses:
|
|
139
|
+
* the paths as written and real-pathed (which also covers a root that doesn't
|
|
140
|
+
* exist yet), and file identity, so any spelling the file system resolves to
|
|
141
|
+
* the same place is treated as that place. A second hard link is refused by
|
|
142
|
+
* resolveProviderKeyFile (one link only), not here.
|
|
143
|
+
*/
|
|
144
|
+
export const keyFileReadableRoot = (keyFile, { subpaths = [], literals = [] } = {}) => {
|
|
145
|
+
const ancestors = new Set();
|
|
146
|
+
for (let current = keyFile; ; current = dirname(current)) {
|
|
147
|
+
const id = identityOf(current);
|
|
148
|
+
if (id) ancestors.add(id);
|
|
149
|
+
if (dirname(current) === current) break;
|
|
150
|
+
}
|
|
151
|
+
for (const root of subpaths) {
|
|
152
|
+
if (isPathWithin(root, keyFile) || isPathWithin(realOrSelf(root), keyFile)) return root;
|
|
153
|
+
const id = identityOf(root);
|
|
154
|
+
if (id && ancestors.has(id)) return root;
|
|
155
|
+
}
|
|
156
|
+
const own = identityOf(keyFile);
|
|
157
|
+
for (const root of literals) {
|
|
158
|
+
if (root === keyFile || realOrSelf(root) === keyFile) return root;
|
|
159
|
+
if (own && identityOf(root) === own) return root;
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Validate the key path without reading the key. The proxy reads it at spawn.
|
|
166
|
+
* `readableRoots` ({ subpaths, literals }) is what a sandboxed seat can read;
|
|
167
|
+
* the proxy reads the key outside the sandbox, so a key file there is refused.
|
|
168
|
+
*/
|
|
169
|
+
export const resolveProviderKeyFile = async (
|
|
170
|
+
provider,
|
|
171
|
+
workspacePath,
|
|
172
|
+
adapterLabel = 'OpenCode',
|
|
173
|
+
{ readableRoots } = {},
|
|
174
|
+
) => {
|
|
117
175
|
if (!provider) return null;
|
|
118
176
|
const keyFile = provider.keyFile;
|
|
119
177
|
if (typeof keyFile !== 'string' || !isAbsolute(keyFile)) {
|
|
@@ -134,6 +192,11 @@ export const resolveProviderKeyFile = async (provider, workspacePath, adapterLab
|
|
|
134
192
|
if ((details.mode & 0o777) !== 0o600) {
|
|
135
193
|
throw new Error(`${adapterLabel} provider.keyFile must have mode 0600`);
|
|
136
194
|
}
|
|
195
|
+
// One name only: a second link elsewhere is the same file under a path the
|
|
196
|
+
// checks below never look at.
|
|
197
|
+
if (details.nlink !== 1) {
|
|
198
|
+
throw new Error(`${adapterLabel} provider.keyFile must have exactly one link (no hard links)`);
|
|
199
|
+
}
|
|
137
200
|
if (typeof workspacePath !== 'string' || !isAbsolute(workspacePath)) {
|
|
138
201
|
throw new Error(`${adapterLabel} provider.keyFile requires an absolute workspace path`);
|
|
139
202
|
}
|
|
@@ -145,9 +208,15 @@ export const resolveProviderKeyFile = async (provider, workspacePath, adapterLab
|
|
|
145
208
|
} catch (err) {
|
|
146
209
|
throw new Error(`${adapterLabel} provider key path could not be resolved: ${err.message}`);
|
|
147
210
|
}
|
|
148
|
-
if (isPathWithin(realWorkspace, realKeyFile)) {
|
|
211
|
+
if (isPathWithin(realWorkspace, realKeyFile) || keyFileReadableRoot(realKeyFile, { subpaths: [realWorkspace] })) {
|
|
149
212
|
throw new Error(`${adapterLabel} provider.keyFile must be outside the workspace`);
|
|
150
213
|
}
|
|
214
|
+
const readableRoot = readableRoots ? keyFileReadableRoot(realKeyFile, readableRoots) : null;
|
|
215
|
+
if (readableRoot) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
`${adapterLabel} provider.keyFile ${realKeyFile} is inside the seat's sandbox; move it outside ${readableRoot}`,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
151
220
|
return realKeyFile;
|
|
152
221
|
};
|
|
153
222
|
|
|
@@ -156,8 +225,8 @@ const readProviderKey = async (keyFile, adapterLabel) => {
|
|
|
156
225
|
const handle = await open(keyFile, fsConstants.O_RDONLY | noFollow);
|
|
157
226
|
try {
|
|
158
227
|
const details = await handle.stat();
|
|
159
|
-
if (!details.isFile() || (details.mode & 0o777) !== 0o600) {
|
|
160
|
-
throw new Error(`${adapterLabel} provider.keyFile changed after validation; expected a regular mode-0600 file`);
|
|
228
|
+
if (!details.isFile() || (details.mode & 0o777) !== 0o600 || details.nlink !== 1) {
|
|
229
|
+
throw new Error(`${adapterLabel} provider.keyFile changed after validation; expected a regular mode-0600 file with one link`);
|
|
161
230
|
}
|
|
162
231
|
const value = (await handle.readFile()).toString('utf8').trim();
|
|
163
232
|
if (!value) throw new Error(`${adapterLabel} provider.keyFile must not be empty`);
|
package/package.json
CHANGED
package/src/lib/adapters/dsh.js
CHANGED
|
@@ -358,7 +358,10 @@ export default {
|
|
|
358
358
|
if (!TESTED_DSH_VERSIONS.includes(detected?.version)) {
|
|
359
359
|
throw new Error(`dsh adapter: installed version ${detected?.version || 'unknown'} is not smoke-tested (${TESTED_DSH_VERSIONS.join(', ')})`);
|
|
360
360
|
}
|
|
361
|
-
|
|
361
|
+
// A confined dsh is a private seat under Seatbelt, so it is checked too.
|
|
362
|
+
return resolveProviderKeyFile(environment.provider, workspacePath, 'dsh', {
|
|
363
|
+
environment, sandboxed: isConfinedDsh(environment),
|
|
364
|
+
});
|
|
362
365
|
},
|
|
363
366
|
|
|
364
367
|
async detect() {
|
|
@@ -396,7 +399,10 @@ export default {
|
|
|
396
399
|
if (!TESTED_DSH_VERSIONS.includes(version)) {
|
|
397
400
|
throw new Error(`dsh adapter: installed version ${version || 'unknown'} is not smoke-tested (${TESTED_DSH_VERSIONS.join(', ')})`);
|
|
398
401
|
}
|
|
399
|
-
|
|
402
|
+
// The seat home is the confined run's statePath: stable and writable in the jail.
|
|
403
|
+
const keyFile = await resolveProviderKeyFile(ctx.environment.provider, ctx.cwd, 'dsh', {
|
|
404
|
+
environment: ctx.environment, platform: ctx._platform, sandboxed: confined, seatDirectories: [home],
|
|
405
|
+
});
|
|
400
406
|
const tempDir = await mkdtemp(join(tmpdir(), 'commonly-dsh-'));
|
|
401
407
|
await chmod(tempDir, 0o700);
|
|
402
408
|
let proxy;
|
|
@@ -312,7 +312,7 @@ export default {
|
|
|
312
312
|
assertSandboxSupported(environment);
|
|
313
313
|
const validation = validateEnvironmentSpec(environment || {});
|
|
314
314
|
if (!validation.ok) throw new Error(validation.errors.join('; '));
|
|
315
|
-
return resolveProviderKeyFile(environment?.provider, workspacePath, 'Goose');
|
|
315
|
+
return resolveProviderKeyFile(environment?.provider, workspacePath, 'Goose', { environment });
|
|
316
316
|
},
|
|
317
317
|
|
|
318
318
|
async detect() {
|
|
@@ -716,7 +716,7 @@ const buildProviderConfig = (provider, model, keyFile) => {
|
|
|
716
716
|
};
|
|
717
717
|
};
|
|
718
718
|
|
|
719
|
-
const validateProviderEnvironment = async (environment, workspacePath) => {
|
|
719
|
+
const validateProviderEnvironment = async (environment, workspacePath, options = {}) => {
|
|
720
720
|
if (environment?.provider === undefined) return null;
|
|
721
721
|
const validation = validateEnvironmentSpec({
|
|
722
722
|
provider: environment.provider,
|
|
@@ -725,7 +725,7 @@ const validateProviderEnvironment = async (environment, workspacePath) => {
|
|
|
725
725
|
if (!validation.ok) {
|
|
726
726
|
throw new Error(`Invalid OpenCode provider configuration: ${validation.errors.join('; ')}`);
|
|
727
727
|
}
|
|
728
|
-
return resolveProviderKeyFile(environment.provider, workspacePath);
|
|
728
|
+
return resolveProviderKeyFile(environment.provider, workspacePath, undefined, { environment, ...options });
|
|
729
729
|
};
|
|
730
730
|
|
|
731
731
|
const STALE_SESSION_PATTERN = /session not found/i;
|
|
@@ -939,6 +939,10 @@ const seatIdentity = (ctx) => [
|
|
|
939
939
|
ctx.metadata?.event?.podId || 'default-pod',
|
|
940
940
|
].join('\0');
|
|
941
941
|
|
|
942
|
+
/** A public seat's stable data home: Seatbelt's statePath, bwrap's write-outside. */
|
|
943
|
+
const openCodeSeatRoot = (ctx) => ctx._opencodeHomeRoot
|
|
944
|
+
|| join(homedir(), '.commonly', 'opencode-homes', createHash('sha256').update(seatIdentity(ctx)).digest('hex').slice(0, 24));
|
|
945
|
+
|
|
942
946
|
const prepareOpenCodeDataHome = async (ctx, { publicSeat = false } = {}) => {
|
|
943
947
|
const operatorDataHome = ctx.env?.XDG_DATA_HOME
|
|
944
948
|
|| process.env.XDG_DATA_HOME
|
|
@@ -951,8 +955,7 @@ const prepareOpenCodeDataHome = async (ctx, { publicSeat = false } = {}) => {
|
|
|
951
955
|
authPath: null,
|
|
952
956
|
};
|
|
953
957
|
}
|
|
954
|
-
const
|
|
955
|
-
const root = ctx._opencodeHomeRoot || join(homedir(), '.commonly', 'opencode-homes', identity);
|
|
958
|
+
const root = openCodeSeatRoot(ctx);
|
|
956
959
|
const operatorAppData = join(pathResolve(operatorDataHome), 'opencode');
|
|
957
960
|
if (pathResolve(root) === pathResolve(operatorAppData)) {
|
|
958
961
|
throw new Error('OpenCode seat data must be isolated from the operator OpenCode data directory');
|
|
@@ -1181,7 +1184,11 @@ export default {
|
|
|
1181
1184
|
if (ctx.providerKey && (!isPublic || !['workspace', 'read-only', 'bwrap'].includes(sandboxMode))) {
|
|
1182
1185
|
throw new Error('Own provider keys require a confined OpenCode runtime');
|
|
1183
1186
|
}
|
|
1184
|
-
|
|
1187
|
+
// The public seat home is readable and writable in the sandbox, so a key
|
|
1188
|
+
// file there is refused with the platform roots, before anything opens it.
|
|
1189
|
+
const providerKeyFile = ctx.providerKey ? null : await validateProviderEnvironment(ctx.environment, ctx.cwd, {
|
|
1190
|
+
platform: ctx._platform, seatDirectories: isPublic ? [openCodeSeatRoot(ctx)] : [],
|
|
1191
|
+
});
|
|
1185
1192
|
|
|
1186
1193
|
const sourceEnv = ctx.env || process.env;
|
|
1187
1194
|
const childSourceEnv = Object.fromEntries(Object.entries(sourceEnv)
|
package/src/lib/adapters/pi.js
CHANGED
|
@@ -53,7 +53,7 @@ import {
|
|
|
53
53
|
} from 'fs/promises';
|
|
54
54
|
import { homedir } from 'os';
|
|
55
55
|
import {
|
|
56
|
-
dirname, isAbsolute, join,
|
|
56
|
+
dirname, isAbsolute, join,
|
|
57
57
|
} from 'path';
|
|
58
58
|
import { fileURLToPath } from 'url';
|
|
59
59
|
import {
|
|
@@ -71,8 +71,8 @@ import { deliverSeatCredential, withholdRuntimeCredential } from '../mcp-credent
|
|
|
71
71
|
import { prepareMcpSpawn, publicMcpUnavailableError, selectedMcpVersionDir } from '../mcp-home.js';
|
|
72
72
|
import { removeCredentialFile, writeCredentialFile } from '../credential-file.js';
|
|
73
73
|
import { resolvePublicSandboxMode } from '../sandbox/mode.js';
|
|
74
|
-
import { wrapArgvWithSeatbelt } from '../sandbox/seatbelt.js';
|
|
75
|
-
import { resolveProviderKeyFile, startProviderProxy } from '../provider-proxy.js';
|
|
74
|
+
import { seatbeltSystemReadRoots, wrapArgvWithSeatbelt } from '../sandbox/seatbelt.js';
|
|
75
|
+
import { keyFileReadableRoot, resolveProviderKeyFile, startProviderProxy } from '../provider-proxy.js';
|
|
76
76
|
import { publicSeatWithheldTools } from '../public-withheld-tools.js';
|
|
77
77
|
import { promptTokens } from '../session-recycle.js';
|
|
78
78
|
|
|
@@ -335,25 +335,32 @@ const SHARED_PROVIDER_KEYS = ['id', 'baseURL', 'keyFile', 'models'];
|
|
|
335
335
|
const isSharedProvider = (spec) => Boolean(spec) && SHARED_PROVIDER_KEYS.some((key) => spec[key] !== undefined);
|
|
336
336
|
|
|
337
337
|
/** The real key file of a shared provider, validated without reading it; null for any other seat. */
|
|
338
|
-
const sharedProviderKeyFile = async (environment, workspacePath) => {
|
|
338
|
+
const sharedProviderKeyFile = async (environment, workspacePath, platform) => {
|
|
339
339
|
if (!isSharedProvider(environment?.provider)) return null;
|
|
340
340
|
const validation = validateEnvironmentSpec(environment);
|
|
341
341
|
if (!validation.ok) throw new Error(validation.errors.join('; '));
|
|
342
|
-
return resolveProviderKeyFile(environment.provider, workspacePath, 'pi');
|
|
342
|
+
return resolveProviderKeyFile(environment.provider, workspacePath, 'pi', { environment, platform });
|
|
343
343
|
};
|
|
344
344
|
|
|
345
345
|
/**
|
|
346
346
|
* The proxy reads a public seat's key file from this process, so the file stays
|
|
347
|
-
* out of the jail, and nothing here admits it. The jail does admit
|
|
348
|
-
* directories
|
|
349
|
-
*
|
|
347
|
+
* out of the jail, and nothing here admits it. The jail does admit the seat's
|
|
348
|
+
* own directories (the seat home is a stable path a key file could be put in),
|
|
349
|
+
* and the Seatbelt baseline admits system trees such as /etc, /opt/homebrew/lib
|
|
350
|
+
* and /usr/local/lib (seatbeltSystemReadRoots, read from the profile itself).
|
|
351
|
+
* A key under any of them would be readable from inside, so it is refused. The
|
|
352
|
+
* comparison is the shared keyFileReadableRoot (F453): as written, real-pathed
|
|
353
|
+
* and by identity, so another spelling of the same path is caught too. The
|
|
354
|
+
* system roots are also checked when the key file is first resolved; this is
|
|
355
|
+
* where the per-turn directories join them.
|
|
350
356
|
*/
|
|
351
|
-
const refuseKeyFileInJail = (keyFile, readableDirectories) => {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
+
export const refuseKeyFileInJail = (keyFile, readableDirectories, systemRoots = seatbeltSystemReadRoots()) => {
|
|
358
|
+
const root = keyFileReadableRoot(keyFile, {
|
|
359
|
+
subpaths: [...readableDirectories, ...systemRoots.subpaths],
|
|
360
|
+
literals: systemRoots.literals,
|
|
361
|
+
});
|
|
362
|
+
if (root) {
|
|
363
|
+
throw new Error(`pi adapter: provider.keyFile ${keyFile} is inside the sandbox of a public seat; move it outside ${root}`);
|
|
357
364
|
}
|
|
358
365
|
};
|
|
359
366
|
|
|
@@ -775,7 +782,7 @@ export default {
|
|
|
775
782
|
assertSandboxSupported(ctx.environment, platform);
|
|
776
783
|
const sandbox = normalizeSandboxTrust(ctx.environment?.sandbox);
|
|
777
784
|
const isPublic = effectiveSandboxTrust(sandbox?.trust) === 'public';
|
|
778
|
-
const keyFile = await sharedProviderKeyFile(ctx.environment, ctx.cwd);
|
|
785
|
+
const keyFile = await sharedProviderKeyFile(ctx.environment, ctx.cwd, platform);
|
|
779
786
|
const provider = resolveProvider(ctx.environment);
|
|
780
787
|
const model = seatModel;
|
|
781
788
|
const thinking = thinkingFor(ctx.environment?.effort);
|
|
@@ -861,21 +868,6 @@ export default {
|
|
|
861
868
|
});
|
|
862
869
|
let proxy = null;
|
|
863
870
|
try {
|
|
864
|
-
// A public pi never holds the provider key: it talks to a loopback proxy
|
|
865
|
-
// with a per-turn token, and the proxy, in this process, adds the key and
|
|
866
|
-
// pins the model (the same shape as Goose and Qwen).
|
|
867
|
-
if (isPublic) {
|
|
868
|
-
proxy = await (ctx._startProviderProxy || startProviderProxy)({
|
|
869
|
-
provider: { baseURL: provider.baseUrl },
|
|
870
|
-
...(keyFile ? { keyFile } : { keyMaterial: baseEnv[provider.apiKeyEnv] }),
|
|
871
|
-
adapterLabel: 'pi',
|
|
872
|
-
pinnedModel: model,
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
await writeFile(join(agentDir, 'models.json'), `${JSON.stringify(buildModelsJson(
|
|
876
|
-
proxy ? { ...provider, baseUrl: proxy.baseURL } : provider, model,
|
|
877
|
-
), null, 2)}\n`, { mode: 0o600 });
|
|
878
|
-
|
|
879
871
|
const unavailableMcp = [];
|
|
880
872
|
const mcpReadDirectories = [];
|
|
881
873
|
const grantBrokers = [];
|
|
@@ -889,9 +881,25 @@ export default {
|
|
|
889
881
|
grantBrokers,
|
|
890
882
|
});
|
|
891
883
|
if (unavailableMcp.length) throw publicMcpUnavailableError(unavailableMcp);
|
|
884
|
+
// Every refusal comes before the proxy opens the key file (F453): nothing
|
|
885
|
+
// touches a refused key file, this process included.
|
|
892
886
|
if (keyFile && isPublic) {
|
|
893
887
|
refuseKeyFileInJail(keyFile, [home, packageRoot, EXTENSION_CODE_ROOT, ...mcpReadDirectories]);
|
|
894
888
|
}
|
|
889
|
+
// A public pi never holds the provider key: it talks to a loopback proxy
|
|
890
|
+
// with a per-turn token, and the proxy, in this process, adds the key and
|
|
891
|
+
// pins the model (the same shape as Goose and Qwen).
|
|
892
|
+
if (isPublic) {
|
|
893
|
+
proxy = await (ctx._startProviderProxy || startProviderProxy)({
|
|
894
|
+
provider: { baseURL: provider.baseUrl },
|
|
895
|
+
...(keyFile ? { keyFile } : { keyMaterial: baseEnv[provider.apiKeyEnv] }),
|
|
896
|
+
adapterLabel: 'pi',
|
|
897
|
+
pinnedModel: model,
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
await writeFile(join(agentDir, 'models.json'), `${JSON.stringify(buildModelsJson(
|
|
901
|
+
proxy ? { ...provider, baseUrl: proxy.baseURL } : provider, model,
|
|
902
|
+
), null, 2)}\n`, { mode: 0o600 });
|
|
895
903
|
const childEnv = isPublic ? {
|
|
896
904
|
// A scrubbed environment: nothing of the operator's crosses into the
|
|
897
905
|
// jail, and the provider variable holds the proxy's token, not the key.
|
package/src/lib/adapters/qwen.js
CHANGED
|
@@ -294,7 +294,7 @@ export default {
|
|
|
294
294
|
if (!environment?.provider || !environment?.model) {
|
|
295
295
|
throw new Error('Qwen requires environment.provider and environment.model');
|
|
296
296
|
}
|
|
297
|
-
return resolveProviderKeyFile(environment.provider, workspacePath, 'Qwen');
|
|
297
|
+
return resolveProviderKeyFile(environment.provider, workspacePath, 'Qwen', { environment });
|
|
298
298
|
},
|
|
299
299
|
|
|
300
300
|
async detect() {
|
|
@@ -1 +1,42 @@
|
|
|
1
|
+
import { resolveProviderKeyFile as resolveKeyFileShared } from '@commonlyai/seats/wire/provider-proxy';
|
|
2
|
+
import { effectiveSandboxTrust } from './environment.js';
|
|
3
|
+
import { bwrapReadRoots } from './sandbox/bwrap.js';
|
|
4
|
+
import { seatbeltSystemReadRoots } from './sandbox/seatbelt.js';
|
|
5
|
+
|
|
1
6
|
export * from '@commonlyai/seats/wire/provider-proxy';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* F453. What a sandboxed seat can read on this platform, as roots, or
|
|
10
|
+
* undefined for a spawn that runs in no sandbox. macOS: the Seatbelt
|
|
11
|
+
* baseline's read grants, read from the profile itself. Linux: bwrap's read
|
|
12
|
+
* binds, including the seat's declared read-outside and write-outside.
|
|
13
|
+
* `sandboxed` is the adapter's word on whether this spawn is confined; left
|
|
14
|
+
* out, a public seat is confined and any other is not.
|
|
15
|
+
*/
|
|
16
|
+
export const sandboxReadableRoots = (environment, platform = process.platform, { sandboxed } = {}) => {
|
|
17
|
+
const confined = sandboxed ?? effectiveSandboxTrust(environment?.sandbox?.trust) === 'public';
|
|
18
|
+
if (!confined) return undefined;
|
|
19
|
+
if (platform === 'darwin') return seatbeltSystemReadRoots();
|
|
20
|
+
if (platform === 'linux') return bwrapReadRoots(environment);
|
|
21
|
+
return { subpaths: [], literals: [] };
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The shared key-file validation, with the sandbox check every adapter gets:
|
|
26
|
+
* the proxy reads a confined seat's key file outside the sandbox, so a key
|
|
27
|
+
* file the sandbox can read is refused before the seat starts, and before
|
|
28
|
+
* anything opens the file. Adapters pass their environment; `seatDirectories`
|
|
29
|
+
* names stable directories the adapter itself gives the sandbox (a seat home),
|
|
30
|
+
* checked whenever the spawn is confined. Without options nothing changes.
|
|
31
|
+
*/
|
|
32
|
+
export const resolveProviderKeyFile = (provider, workspacePath, adapterLabel, {
|
|
33
|
+
environment, platform, sandboxed, seatDirectories = [],
|
|
34
|
+
} = {}) => {
|
|
35
|
+
const roots = sandboxReadableRoots(environment, platform, { sandboxed });
|
|
36
|
+
return resolveKeyFileShared(provider, workspacePath, adapterLabel, {
|
|
37
|
+
readableRoots: roots && {
|
|
38
|
+
subpaths: [...roots.subpaths, ...seatDirectories.filter(Boolean)],
|
|
39
|
+
literals: roots.literals,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
};
|
package/src/lib/sandbox/bwrap.js
CHANGED
|
@@ -55,6 +55,23 @@ const DEFAULT_RO_BINDS = [
|
|
|
55
55
|
'/lib64',
|
|
56
56
|
];
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* What a bwrap sandbox binds for reading, as roots (F453): the default binds
|
|
60
|
+
* plus the seat's declared read-outside and write-outside, tilde-expanded the
|
|
61
|
+
* way wrapArgvWithBwrap expands them. A caller keeping a secret out of the
|
|
62
|
+
* sandbox (a public seat's provider key file) checks against this rather than
|
|
63
|
+
* a copy of the bind list. Per-turn paths (workspace, state, adapter inputs)
|
|
64
|
+
* are the caller's.
|
|
65
|
+
*/
|
|
66
|
+
export const bwrapReadRoots = (env) => {
|
|
67
|
+
const declared = (key) => (Array.isArray(env?.sandbox?.filesystem?.[key])
|
|
68
|
+
? env.sandbox.filesystem[key].map(expandHome) : []);
|
|
69
|
+
return {
|
|
70
|
+
subpaths: [...new Set([...DEFAULT_RO_BINDS, ...declared('read-outside'), ...declared('write-outside')])],
|
|
71
|
+
literals: [],
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
|
|
58
75
|
// `bwrap --version` only proves the binary is there. On Ubuntu 24.04 and later
|
|
59
76
|
// AppArmor restricts unprivileged user namespaces, so bwrap is installed and
|
|
60
77
|
// every sandbox it starts fails (`setting up uid map: Permission denied`,
|
|
@@ -299,6 +299,59 @@ const OPEN_NETWORK = String.raw`
|
|
|
299
299
|
(global-name "com.apple.securityd.xpc"))
|
|
300
300
|
`;
|
|
301
301
|
|
|
302
|
+
// The static paths the baseline lets a jail READ (file-read* or file-read-data),
|
|
303
|
+
// as `subpaths` (whole trees) and `literals` (one file or directory entry).
|
|
304
|
+
// Read from PLATFORM_BASELINE itself, so a caller keeping a secret out of the
|
|
305
|
+
// jail (a public seat's provider key file) checks what the profile actually
|
|
306
|
+
// admits rather than a copy that drifts. Metadata-only grants are not reads;
|
|
307
|
+
// regex grants cover /dev only and are left out. Dynamic per-turn paths
|
|
308
|
+
// (workspace, state, executables, read-only directories) are the caller's.
|
|
309
|
+
const READ_OPERATIONS = ['file-read*', 'file-read-data'];
|
|
310
|
+
export const parseSeatbeltReadRoots = (profile) => {
|
|
311
|
+
const subpaths = new Set();
|
|
312
|
+
const literals = new Set();
|
|
313
|
+
const text = profile.replace(/;[^\n]*/g, '');
|
|
314
|
+
let at = text.indexOf('(allow ');
|
|
315
|
+
while (at !== -1) {
|
|
316
|
+
// The rule's balanced s-expression; parentheses inside a string don't count.
|
|
317
|
+
let depth = 0;
|
|
318
|
+
let end = at;
|
|
319
|
+
let inString = false;
|
|
320
|
+
for (; end < text.length; end += 1) {
|
|
321
|
+
const ch = text[end];
|
|
322
|
+
if (inString) { if (ch === '\\') end += 1; else if (ch === '"') inString = false; continue; }
|
|
323
|
+
if (ch === '"') inString = true;
|
|
324
|
+
else if (ch === '(') depth += 1;
|
|
325
|
+
else if (ch === ')') { depth -= 1; if (depth === 0) break; }
|
|
326
|
+
}
|
|
327
|
+
const rule = text.slice(at, end + 1);
|
|
328
|
+
const operations = rule.slice('(allow '.length).split('(')[0].trim().split(/\s+/);
|
|
329
|
+
if (operations.some((op) => READ_OPERATIONS.includes(op))) {
|
|
330
|
+
for (const match of rule.matchAll(/\((subpath|literal) "([^"]+)"\)/g)) {
|
|
331
|
+
(match[1] === 'subpath' ? subpaths : literals).add(match[2]);
|
|
332
|
+
}
|
|
333
|
+
// Fail closed: a read filter this cannot turn into a root would be a
|
|
334
|
+
// readable path the key-file check never sees. Regex grants are known
|
|
335
|
+
// to cover /dev only, and are held to that.
|
|
336
|
+
for (const filter of rule.matchAll(/\((path|prefix|regex|home-subpath|home-literal|home-prefix)\s+(#?"[^"]*")/g)) {
|
|
337
|
+
if (filter[1] !== 'regex' || !/^#?"\^\/dev\//.test(filter[2])) {
|
|
338
|
+
throw new Error(`seatbeltSystemReadRoots: a read rule uses a filter it cannot check: (${filter[1]} ${filter[2]})`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
at = text.indexOf('(allow ', end + 1);
|
|
343
|
+
}
|
|
344
|
+
return Object.freeze({
|
|
345
|
+
subpaths: Object.freeze([...subpaths]),
|
|
346
|
+
literals: Object.freeze([...literals]),
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
let systemReadRoots = null;
|
|
350
|
+
export const seatbeltSystemReadRoots = () => {
|
|
351
|
+
if (!systemReadRoots) systemReadRoots = parseSeatbeltReadRoots(PLATFORM_BASELINE);
|
|
352
|
+
return systemReadRoots;
|
|
353
|
+
};
|
|
354
|
+
|
|
302
355
|
// Both network modes. On macOS, c-ares (node's dns.Resolver and dns.resolve*)
|
|
303
356
|
// reads the resolver list from configd's DNSConfiguration service while the
|
|
304
357
|
// Resolver is constructed. Denied, that lookup does not always fail: at
|