@zenera/sandbox 1.3.45 → 1.3.47

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/dist/bwrap.js CHANGED
@@ -9,7 +9,7 @@ import { sandboxLimits } from "./limits.js";
9
9
  import { WORKSPACE_MOUNT } from "./mounts.js";
10
10
  import { MAX_TIMEOUT_MS, runProcess } from "./process.js";
11
11
  import { RootedFs } from "./rooted-fs.js";
12
- import { DEFAULT_SANDBOX_IMAGE, SandboxError, } from "./contract.js";
12
+ import { DEFAULT_SANDBOX_IMAGE, SandboxError, checkVarNames, } from "./contract.js";
13
13
  // ---------------------------------------------------------------------------
14
14
  // The bwrap sandbox
15
15
  //
@@ -187,7 +187,7 @@ export class BwrapSandbox {
187
187
  input: command,
188
188
  timeoutMs: this.#timeout(opts.timeout),
189
189
  signal,
190
- env: this.#env(),
190
+ env: this.#env(opts.env),
191
191
  maxBytes: opts.maxBytes,
192
192
  });
193
193
  refuseSetupFailure(res);
@@ -221,7 +221,7 @@ export class BwrapSandbox {
221
221
  ...this.#args(cwd, ['/bin/sh', JOB_SCRIPT], ['--ro-bind', script, JOB_SCRIPT]),
222
222
  ],
223
223
  // Its own process group, so stopping it signals bwrap and nothing of ours.
224
- { stdio: ['ignore', fd, fd], env: this.#env(), detached: true });
224
+ { stdio: ['ignore', fd, fd], env: this.#env(opts.env), detached: true });
225
225
  }
226
226
  finally {
227
227
  closeSync(fd);
@@ -360,9 +360,12 @@ export class BwrapSandbox {
360
360
  /**
361
361
  * Exactly what a command sees, and also what bwrap is started with: bwrap's
362
362
  * own environment stays readable to the command (its `/proc/1/environ`), so
363
- * nothing of the server's may reach bwrap in the first place.
363
+ * nothing of the server's may reach bwrap in the first place. `call`: one command's own
364
+ * (`ExecOptions.env`), over the sandbox's.
364
365
  */
365
- #env() {
366
+ #env(call) {
367
+ if (call)
368
+ checkVarNames(Object.keys(call));
366
369
  const host = this.#host.env ?? process.env;
367
370
  const image = this.#host.imageEnv ?? imageEnv();
368
371
  const env = {};
@@ -385,7 +388,7 @@ export class BwrapSandbox {
385
388
  env[name] = value;
386
389
  }
387
390
  }
388
- Object.assign(env, this.spec.execEnv);
391
+ Object.assign(env, this.spec.execEnv, call);
389
392
  for (const [name, dirs] of Object.entries(this.spec.execPaths)) {
390
393
  if (dirs.length) {
391
394
  env[name] = [...dirs, ...(env[name] ? [env[name]] : [])].join(':');
@@ -685,11 +688,7 @@ function resolveSpec(opts) {
685
688
  if (!posix.isAbsolute(workdir)) {
686
689
  throw new SandboxError(`sandbox workdir must be absolute: ${workdir}`);
687
690
  }
688
- for (const name of [...Object.keys(opts.execEnv ?? {}), ...Object.keys(opts.execPaths ?? {})]) {
689
- if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
690
- throw new SandboxError(`not a variable name: ${JSON.stringify(name)}`);
691
- }
692
- }
691
+ checkVarNames([...Object.keys(opts.execEnv ?? {}), ...Object.keys(opts.execPaths ?? {})]);
693
692
  const { hardening } = sandboxLimits(opts);
694
693
  return {
695
694
  root: opts.root,
@@ -182,7 +182,15 @@ export interface ExecOptions {
182
182
  signal?: AbortSignal;
183
183
  /** `exec` only: the bytes kept of stdout and of stderr each, 64 KB otherwise */
184
184
  maxBytes?: number;
185
+ /**
186
+ * Variables for this command only, over `execEnv`: values of one call rather than of the
187
+ * sandbox - the user a page's call acts for, when one kept sandbox serves several. Plain
188
+ * values, as `execEnv`: nothing secret.
189
+ */
190
+ env?: Record<string, string>;
185
191
  }
192
+ /** Throws unless every name is a plain variable name: they reach a command line or an env. */
193
+ export declare function checkVarNames(names: Iterable<string>): void;
186
194
  export interface ExecResult {
187
195
  exit_code: number;
188
196
  stdout: string;
package/dist/contract.js CHANGED
@@ -24,4 +24,12 @@ export class SandboxError extends Error {
24
24
  this.hint = hint;
25
25
  }
26
26
  }
27
+ /** Throws unless every name is a plain variable name: they reach a command line or an env. */
28
+ export function checkVarNames(names) {
29
+ for (const name of names) {
30
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
31
+ throw new SandboxError(`not a variable name: ${JSON.stringify(name)}`);
32
+ }
33
+ }
34
+ }
27
35
  //# sourceMappingURL=contract.js.map
package/dist/local.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isAbsolute, resolve } from 'node:path';
2
- import { SandboxError, } from "./contract.js";
2
+ import { SandboxError, checkVarNames, } from "./contract.js";
3
3
  import { localFs } from "./fs.js";
4
4
  import { MAX_TIMEOUT_MS, runProcess } from "./process.js";
5
5
  const DEFAULT_TIMEOUT_SECONDS = 120;
@@ -29,7 +29,7 @@ export class ProcessSandbox {
29
29
  input: `cd ${quoted} || exit 1\n${command}`,
30
30
  timeoutMs: Math.min((opts.timeout ?? this.#opts.timeout ?? DEFAULT_TIMEOUT_SECONDS) * 1000, MAX_TIMEOUT_MS),
31
31
  signal,
32
- env: this.#env(),
32
+ env: this.#env(opts.env),
33
33
  group: true,
34
34
  ...(opts.maxBytes !== undefined ? { maxBytes: opts.maxBytes } : {}),
35
35
  });
@@ -42,8 +42,10 @@ export class ProcessSandbox {
42
42
  duration_ms: Date.now() - startedAt,
43
43
  };
44
44
  }
45
- #env() {
46
- const env = { ...(this.#opts.env ?? process.env) };
45
+ #env(call) {
46
+ if (call)
47
+ checkVarNames(Object.keys(call));
48
+ const env = { ...(this.#opts.env ?? process.env), ...call };
47
49
  for (const [name, dirs] of Object.entries(this.#opts.execPaths ?? {})) {
48
50
  if (dirs.length)
49
51
  env[name] = [...dirs, env[name]].filter(Boolean).join(':');
package/dist/podman.js CHANGED
@@ -5,7 +5,7 @@ import { sandboxLimits } from "./limits.js";
5
5
  import { WORKSPACE_MOUNT } from "./mounts.js";
6
6
  import { RootedFs } from "./rooted-fs.js";
7
7
  import { MAX_TIMEOUT_MS, resilient, runProcess } from "./process.js";
8
- import { DEFAULT_SANDBOX_IMAGE, SandboxError, } from "./contract.js";
8
+ import { DEFAULT_SANDBOX_IMAGE, SandboxError, checkVarNames, } from "./contract.js";
9
9
  // ---------------------------------------------------------------------------
10
10
  // The podman sandbox
11
11
  //
@@ -197,10 +197,16 @@ export class PodmanSandbox {
197
197
  * environment. The script still arrives on stdin; when a path has to be
198
198
  * extended, the shell that reads it is started by a fixed `-c` line that
199
199
  * exports the variable and `exec`s it — built from host-chosen names and
200
- * paths only, so no text of the agent's is ever part of a parse here.
200
+ * paths only, so no text of the agent's is ever part of a parse here. `call`: one
201
+ * command's own variables (`ExecOptions.env`), over the sandbox's.
201
202
  */
202
- #exec(options = []) {
203
- const env = Object.entries(this.spec.execEnv).flatMap(([k, v]) => ['--env', `${k}=${v}`]);
203
+ #exec(options = [], call) {
204
+ if (call)
205
+ checkVarNames(Object.keys(call));
206
+ const env = Object.entries({ ...this.spec.execEnv, ...call }).flatMap(([k, v]) => [
207
+ '--env',
208
+ `${k}=${v}`,
209
+ ]);
204
210
  const paths = Object.entries(this.spec.execPaths).filter(([, dirs]) => dirs.length);
205
211
  const shell = paths.length
206
212
  ? [
@@ -223,7 +229,7 @@ export class PodmanSandbox {
223
229
  const cwd = this.inside(opts.cwd);
224
230
  const timeoutMs = this.#timeout(opts.timeout);
225
231
  const startedAt = Date.now();
226
- const once = () => this.#run(this.engine, this.#exec(['--workdir', cwd]), {
232
+ const once = () => this.#run(this.engine, this.#exec(['--workdir', cwd], opts.env), {
227
233
  input: command,
228
234
  timeoutMs,
229
235
  signal: opts.signal,
@@ -291,7 +297,7 @@ export class PodmanSandbox {
291
297
  // the job missing.
292
298
  `i=0; while [ ! -s ${JOBS}/${id}.pid ] && [ $i -lt 50 ]; do i=$((i+1)); sleep 0.1; done`,
293
299
  ].join('\n');
294
- const started = await this.#run(this.engine, this.#exec(), {
300
+ const started = await this.#run(this.engine, this.#exec([], opts.env), {
295
301
  input: launch,
296
302
  timeoutMs: 30_000,
297
303
  });
@@ -504,11 +510,7 @@ function resolveSpec(opts) {
504
510
  const limits = sandboxLimits(opts);
505
511
  const { hardening } = limits;
506
512
  const strict = hardening === 'strict';
507
- for (const name of [...Object.keys(opts.execEnv ?? {}), ...Object.keys(opts.execPaths ?? {})]) {
508
- if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
509
- throw new SandboxError(`not a variable name: ${JSON.stringify(name)}`);
510
- }
511
- }
513
+ checkVarNames([...Object.keys(opts.execEnv ?? {}), ...Object.keys(opts.execPaths ?? {})]);
512
514
  return {
513
515
  root: opts.root,
514
516
  key: opts.key,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenera/sandbox",
3
- "version": "1.3.45",
3
+ "version": "1.3.47",
4
4
  "description": "Where an agent's commands run and the files they see: the Sandbox contract, podman and bubblewrap implementations, filesystem drivers and a contract test suite for other implementations.",
5
5
  "keywords": [
6
6
  "sandbox",