@buildanagent/harness 0.0.0-stage → 0.1.0-alpha.29
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 +54 -0
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +822 -2
- package/dist/ablation.d.ts +2 -0
- package/dist/ablation.js +3 -0
- package/dist/ablation.js.map +1 -0
- package/dist/completion/check.d.ts +64 -0
- package/dist/completion/check.js +152 -0
- package/dist/completion/check.js.map +1 -0
- package/dist/completion/checks.d.ts +45 -0
- package/dist/completion/checks.js +173 -0
- package/dist/completion/checks.js.map +1 -0
- package/dist/completion/flags.d.ts +25 -0
- package/dist/completion/flags.js +87 -0
- package/dist/completion/flags.js.map +1 -0
- package/dist/completion/index.d.ts +8 -0
- package/dist/completion/index.js +9 -0
- package/dist/completion/index.js.map +1 -0
- package/dist/completion/snapshot.d.ts +33 -0
- package/dist/completion/snapshot.js +83 -0
- package/dist/completion/snapshot.js.map +1 -0
- package/dist/context/cap.d.ts +32 -0
- package/dist/context/cap.js +50 -0
- package/dist/context/cap.js.map +1 -0
- package/dist/context/estimate.d.ts +18 -0
- package/dist/context/estimate.js +20 -0
- package/dist/context/estimate.js.map +1 -0
- package/dist/context/file-spill.d.ts +15 -0
- package/dist/context/file-spill.js +40 -0
- package/dist/context/file-spill.js.map +1 -0
- package/dist/context/index.d.ts +10 -0
- package/dist/context/index.js +10 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/mask.d.ts +27 -0
- package/dist/context/mask.js +71 -0
- package/dist/context/mask.js.map +1 -0
- package/dist/context/mid-turn.d.ts +40 -0
- package/dist/context/mid-turn.js +82 -0
- package/dist/context/mid-turn.js.map +1 -0
- package/dist/context/policy.d.ts +9 -0
- package/dist/context/policy.js +140 -0
- package/dist/context/policy.js.map +1 -0
- package/dist/context/spill-ledger.d.ts +25 -0
- package/dist/context/spill-ledger.js +54 -0
- package/dist/context/spill-ledger.js.map +1 -0
- package/dist/context/spill.d.ts +10 -0
- package/dist/context/spill.js +18 -0
- package/dist/context/spill.js.map +1 -0
- package/dist/context/summarize.d.ts +53 -0
- package/dist/context/summarize.js +204 -0
- package/dist/context/summarize.js.map +1 -0
- package/dist/context/summary-template.d.ts +29 -0
- package/dist/context/summary-template.js +69 -0
- package/dist/context/summary-template.js.map +1 -0
- package/dist/context/types.d.ts +150 -0
- package/dist/context/types.js +2 -0
- package/dist/context/types.js.map +1 -0
- package/dist/context/write-cap.d.ts +19 -0
- package/dist/context/write-cap.js +41 -0
- package/dist/context/write-cap.js.map +1 -0
- package/dist/create-harness.d.ts +7 -0
- package/dist/create-harness.js +21 -0
- package/dist/create-harness.js.map +1 -0
- package/dist/credentials/addresses.d.ts +14 -0
- package/dist/credentials/addresses.js +144 -0
- package/dist/credentials/addresses.js.map +1 -0
- package/dist/credentials/allowlist.d.ts +54 -0
- package/dist/credentials/allowlist.js +134 -0
- package/dist/credentials/allowlist.js.map +1 -0
- package/dist/credentials/egress-policy.d.ts +69 -0
- package/dist/credentials/egress-policy.js +170 -0
- package/dist/credentials/egress-policy.js.map +1 -0
- package/dist/credentials/egress-proxy.d.ts +82 -0
- package/dist/credentials/egress-proxy.js +650 -0
- package/dist/credentials/egress-proxy.js.map +1 -0
- package/dist/credentials/git-route.d.ts +68 -0
- package/dist/credentials/git-route.js +288 -0
- package/dist/credentials/git-route.js.map +1 -0
- package/dist/credentials/index.d.ts +13 -0
- package/dist/credentials/index.js +14 -0
- package/dist/credentials/index.js.map +1 -0
- package/dist/credentials/scrub.d.ts +23 -0
- package/dist/credentials/scrub.js +115 -0
- package/dist/credentials/scrub.js.map +1 -0
- package/dist/errors.d.ts +60 -0
- package/dist/errors.js +67 -0
- package/dist/errors.js.map +1 -0
- package/dist/extensions/compose.d.ts +27 -0
- package/dist/extensions/compose.js +83 -0
- package/dist/extensions/compose.js.map +1 -0
- package/dist/extensions/context-files-node.d.ts +28 -0
- package/dist/extensions/context-files-node.js +74 -0
- package/dist/extensions/context-files-node.js.map +1 -0
- package/dist/extensions/context-files.d.ts +68 -0
- package/dist/extensions/context-files.js +214 -0
- package/dist/extensions/context-files.js.map +1 -0
- package/dist/extensions/digest.d.ts +6 -0
- package/dist/extensions/digest.js +68 -0
- package/dist/extensions/digest.js.map +1 -0
- package/dist/extensions/index.d.ts +10 -0
- package/dist/extensions/index.js +10 -0
- package/dist/extensions/index.js.map +1 -0
- package/dist/extensions/load.d.ts +42 -0
- package/dist/extensions/load.js +211 -0
- package/dist/extensions/load.js.map +1 -0
- package/dist/extensions/lock.d.ts +8 -0
- package/dist/extensions/lock.js +95 -0
- package/dist/extensions/lock.js.map +1 -0
- package/dist/extensions/node.d.ts +10 -0
- package/dist/extensions/node.js +11 -0
- package/dist/extensions/node.js.map +1 -0
- package/dist/extensions/project-dirs.d.ts +24 -0
- package/dist/extensions/project-dirs.js +97 -0
- package/dist/extensions/project-dirs.js.map +1 -0
- package/dist/extensions/skills-node.d.ts +34 -0
- package/dist/extensions/skills-node.js +123 -0
- package/dist/extensions/skills-node.js.map +1 -0
- package/dist/extensions/skills.d.ts +51 -0
- package/dist/extensions/skills.js +175 -0
- package/dist/extensions/skills.js.map +1 -0
- package/dist/extensions/trust-file.d.ts +3 -0
- package/dist/extensions/trust-file.js +62 -0
- package/dist/extensions/trust-file.js.map +1 -0
- package/dist/extensions/trust-store-node.d.ts +10 -0
- package/dist/extensions/trust-store-node.js +33 -0
- package/dist/extensions/trust-store-node.js.map +1 -0
- package/dist/extensions/types.d.ts +42 -0
- package/dist/extensions/types.js +2 -0
- package/dist/extensions/types.js.map +1 -0
- package/dist/harness.d.ts +156 -0
- package/dist/harness.js +2 -0
- package/dist/harness.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/connection.d.ts +22 -0
- package/dist/mcp/connection.js +267 -0
- package/dist/mcp/connection.js.map +1 -0
- package/dist/mcp/content.d.ts +27 -0
- package/dist/mcp/content.js +177 -0
- package/dist/mcp/content.js.map +1 -0
- package/dist/mcp/errors.d.ts +19 -0
- package/dist/mcp/errors.js +21 -0
- package/dist/mcp/errors.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.js +10 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/manager.d.ts +7 -0
- package/dist/mcp/manager.js +173 -0
- package/dist/mcp/manager.js.map +1 -0
- package/dist/mcp/record.d.ts +7 -0
- package/dist/mcp/record.js +20 -0
- package/dist/mcp/record.js.map +1 -0
- package/dist/mcp/stdio.d.ts +39 -0
- package/dist/mcp/stdio.js +382 -0
- package/dist/mcp/stdio.js.map +1 -0
- package/dist/mcp/types.d.ts +172 -0
- package/dist/mcp/types.js +2 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.js +3 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/permissions/default-rules.d.ts +7 -0
- package/dist/permissions/default-rules.js +42 -0
- package/dist/permissions/default-rules.js.map +1 -0
- package/dist/permissions/errors.d.ts +11 -0
- package/dist/permissions/errors.js +14 -0
- package/dist/permissions/errors.js.map +1 -0
- package/dist/permissions/index.d.ts +9 -0
- package/dist/permissions/index.js +9 -0
- package/dist/permissions/index.js.map +1 -0
- package/dist/permissions/match.d.ts +33 -0
- package/dist/permissions/match.js +287 -0
- package/dist/permissions/match.js.map +1 -0
- package/dist/permissions/pipeline.d.ts +9 -0
- package/dist/permissions/pipeline.js +235 -0
- package/dist/permissions/pipeline.js.map +1 -0
- package/dist/permissions/rules.d.ts +22 -0
- package/dist/permissions/rules.js +74 -0
- package/dist/permissions/rules.js.map +1 -0
- package/dist/permissions/shadow.d.ts +5 -0
- package/dist/permissions/shadow.js +22 -0
- package/dist/permissions/shadow.js.map +1 -0
- package/dist/permissions/types.d.ts +104 -0
- package/dist/permissions/types.js +2 -0
- package/dist/permissions/types.js.map +1 -0
- package/dist/sandbox/base.d.ts +45 -0
- package/dist/sandbox/base.js +74 -0
- package/dist/sandbox/base.js.map +1 -0
- package/dist/sandbox/docker/index.d.ts +41 -0
- package/dist/sandbox/docker/index.js +497 -0
- package/dist/sandbox/docker/index.js.map +1 -0
- package/dist/sandbox/errors.d.ts +44 -0
- package/dist/sandbox/errors.js +57 -0
- package/dist/sandbox/errors.js.map +1 -0
- package/dist/sandbox/host-files.d.ts +65 -0
- package/dist/sandbox/host-files.js +258 -0
- package/dist/sandbox/host-files.js.map +1 -0
- package/dist/sandbox/index.d.ts +10 -0
- package/dist/sandbox/index.js +10 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sandbox/none.d.ts +17 -0
- package/dist/sandbox/none.js +108 -0
- package/dist/sandbox/none.js.map +1 -0
- package/dist/sandbox/policy.d.ts +49 -0
- package/dist/sandbox/policy.js +149 -0
- package/dist/sandbox/policy.js.map +1 -0
- package/dist/sandbox/process.d.ts +61 -0
- package/dist/sandbox/process.js +253 -0
- package/dist/sandbox/process.js.map +1 -0
- package/dist/sandbox/spawn-env.d.ts +20 -0
- package/dist/sandbox/spawn-env.js +128 -0
- package/dist/sandbox/spawn-env.js.map +1 -0
- package/dist/sandbox/srt/index.d.ts +38 -0
- package/dist/sandbox/srt/index.js +283 -0
- package/dist/sandbox/srt/index.js.map +1 -0
- package/dist/sandbox/srt/settings.d.ts +60 -0
- package/dist/sandbox/srt/settings.js +84 -0
- package/dist/sandbox/srt/settings.js.map +1 -0
- package/dist/sandbox/types.d.ts +125 -0
- package/dist/sandbox/types.js +6 -0
- package/dist/sandbox/types.js.map +1 -0
- package/dist/server/answers.d.ts +16 -0
- package/dist/server/answers.js +43 -0
- package/dist/server/answers.js.map +1 -0
- package/dist/server/connection.d.ts +99 -0
- package/dist/server/connection.js +309 -0
- package/dist/server/connection.js.map +1 -0
- package/dist/server/dispatcher.d.ts +86 -0
- package/dist/server/dispatcher.js +800 -0
- package/dist/server/dispatcher.js.map +1 -0
- package/dist/server/index.d.ts +13 -0
- package/dist/server/index.js +12 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/local-transport.d.ts +44 -0
- package/dist/server/local-transport.js +103 -0
- package/dist/server/local-transport.js.map +1 -0
- package/dist/server/outbox.d.ts +31 -0
- package/dist/server/outbox.js +130 -0
- package/dist/server/outbox.js.map +1 -0
- package/dist/server/params.d.ts +25 -0
- package/dist/server/params.js +112 -0
- package/dist/server/params.js.map +1 -0
- package/dist/server/protocol.schema.json +2448 -0
- package/dist/server/rpc-session.d.ts +43 -0
- package/dist/server/rpc-session.js +167 -0
- package/dist/server/rpc-session.js.map +1 -0
- package/dist/server/stdio-streams.d.ts +67 -0
- package/dist/server/stdio-streams.js +218 -0
- package/dist/server/stdio-streams.js.map +1 -0
- package/dist/server/stdio.d.ts +59 -0
- package/dist/server/stdio.js +189 -0
- package/dist/server/stdio.js.map +1 -0
- package/dist/server/websocket.d.ts +106 -0
- package/dist/server/websocket.js +327 -0
- package/dist/server/websocket.js.map +1 -0
- package/dist/session/approvals.d.ts +81 -0
- package/dist/session/approvals.js +217 -0
- package/dist/session/approvals.js.map +1 -0
- package/dist/session/code-call-log.d.ts +42 -0
- package/dist/session/code-call-log.js +63 -0
- package/dist/session/code-call-log.js.map +1 -0
- package/dist/session/code-call-result.d.ts +19 -0
- package/dist/session/code-call-result.js +95 -0
- package/dist/session/code-call-result.js.map +1 -0
- package/dist/session/code-call.d.ts +49 -0
- package/dist/session/code-call.js +345 -0
- package/dist/session/code-call.js.map +1 -0
- package/dist/session/effect-scope.d.ts +11 -0
- package/dist/session/effect-scope.js +10 -0
- package/dist/session/effect-scope.js.map +1 -0
- package/dist/session/errors.d.ts +19 -0
- package/dist/session/errors.js +27 -0
- package/dist/session/errors.js.map +1 -0
- package/dist/session/file-index.d.ts +8 -0
- package/dist/session/file-index.js +70 -0
- package/dist/session/file-index.js.map +1 -0
- package/dist/session/file-key-store.d.ts +16 -0
- package/dist/session/file-key-store.js +169 -0
- package/dist/session/file-key-store.js.map +1 -0
- package/dist/session/file-lock.d.ts +52 -0
- package/dist/session/file-lock.js +324 -0
- package/dist/session/file-lock.js.map +1 -0
- package/dist/session/index.d.ts +22 -0
- package/dist/session/index.js +20 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/jsonl-store.d.ts +37 -0
- package/dist/session/jsonl-store.js +386 -0
- package/dist/session/jsonl-store.js.map +1 -0
- package/dist/session/jsonl.d.ts +9 -0
- package/dist/session/jsonl.js +10 -0
- package/dist/session/jsonl.js.map +1 -0
- package/dist/session/lock.d.ts +19 -0
- package/dist/session/lock.js +2 -0
- package/dist/session/lock.js.map +1 -0
- package/dist/session/memory-store.d.ts +12 -0
- package/dist/session/memory-store.js +60 -0
- package/dist/session/memory-store.js.map +1 -0
- package/dist/session/migrate.d.ts +41 -0
- package/dist/session/migrate.js +86 -0
- package/dist/session/migrate.js.map +1 -0
- package/dist/session/pointer.d.ts +15 -0
- package/dist/session/pointer.js +22 -0
- package/dist/session/pointer.js.map +1 -0
- package/dist/session/rebuild.d.ts +53 -0
- package/dist/session/rebuild.js +230 -0
- package/dist/session/rebuild.js.map +1 -0
- package/dist/session/resume.d.ts +26 -0
- package/dist/session/resume.js +128 -0
- package/dist/session/resume.js.map +1 -0
- package/dist/session/retention.d.ts +97 -0
- package/dist/session/retention.js +702 -0
- package/dist/session/retention.js.map +1 -0
- package/dist/session/runner.d.ts +156 -0
- package/dist/session/runner.js +656 -0
- package/dist/session/runner.js.map +1 -0
- package/dist/session/scheduler.d.ts +47 -0
- package/dist/session/scheduler.js +85 -0
- package/dist/session/scheduler.js.map +1 -0
- package/dist/session/secret-key-store.d.ts +47 -0
- package/dist/session/secret-key-store.js +79 -0
- package/dist/session/secret-key-store.js.map +1 -0
- package/dist/session/session.d.ts +106 -0
- package/dist/session/session.js +353 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/sticky.d.ts +48 -0
- package/dist/session/sticky.js +232 -0
- package/dist/session/sticky.js.map +1 -0
- package/dist/session/store.d.ts +108 -0
- package/dist/session/store.js +163 -0
- package/dist/session/store.js.map +1 -0
- package/dist/telemetry/index.d.ts +2 -0
- package/dist/telemetry/index.js +3 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/testing/fake-sandbox.d.ts +52 -0
- package/dist/testing/fake-sandbox.js +370 -0
- package/dist/testing/fake-sandbox.js.map +1 -0
- package/dist/testing/index.d.ts +10 -0
- package/dist/testing/index.js +11 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/key-store-conformance.d.ts +22 -0
- package/dist/testing/key-store-conformance.js +97 -0
- package/dist/testing/key-store-conformance.js.map +1 -0
- package/dist/testing/run-cases.d.ts +21 -0
- package/dist/testing/run-cases.js +20 -0
- package/dist/testing/run-cases.js.map +1 -0
- package/dist/testing/sandbox-conformance.d.ts +64 -0
- package/dist/testing/sandbox-conformance.js +468 -0
- package/dist/testing/sandbox-conformance.js.map +1 -0
- package/dist/testing/scheduler-conformance.d.ts +32 -0
- package/dist/testing/scheduler-conformance.js +72 -0
- package/dist/testing/scheduler-conformance.js.map +1 -0
- package/dist/testing/store-conformance.d.ts +21 -0
- package/dist/testing/store-conformance.js +320 -0
- package/dist/testing/store-conformance.js.map +1 -0
- package/dist/tools/builtins/bash.d.ts +9 -0
- package/dist/tools/builtins/bash.js +75 -0
- package/dist/tools/builtins/bash.js.map +1 -0
- package/dist/tools/builtins/builtins.d.ts +38 -0
- package/dist/tools/builtins/builtins.js +48 -0
- package/dist/tools/builtins/builtins.js.map +1 -0
- package/dist/tools/builtins/descriptions.d.ts +69 -0
- package/dist/tools/builtins/descriptions.js +94 -0
- package/dist/tools/builtins/descriptions.js.map +1 -0
- package/dist/tools/builtins/fetch.d.ts +12 -0
- package/dist/tools/builtins/fetch.js +157 -0
- package/dist/tools/builtins/fetch.js.map +1 -0
- package/dist/tools/builtins/files.d.ts +27 -0
- package/dist/tools/builtins/files.js +274 -0
- package/dist/tools/builtins/files.js.map +1 -0
- package/dist/tools/builtins/paths.d.ts +30 -0
- package/dist/tools/builtins/paths.js +62 -0
- package/dist/tools/builtins/paths.js.map +1 -0
- package/dist/tools/builtins/results.d.ts +15 -0
- package/dist/tools/builtins/results.js +31 -0
- package/dist/tools/builtins/results.js.map +1 -0
- package/dist/tools/builtins/search.d.ts +18 -0
- package/dist/tools/builtins/search.js +217 -0
- package/dist/tools/builtins/search.js.map +1 -0
- package/dist/tools/errors.d.ts +13 -0
- package/dist/tools/errors.js +17 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/index.d.ts +12 -0
- package/dist/tools/index.js +11 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +3 -0
- package/dist/tools/registry.js +234 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/tool-search.d.ts +30 -0
- package/dist/tools/tool-search.js +118 -0
- package/dist/tools/tool-search.js.map +1 -0
- package/dist/tools/types.d.ts +106 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/types.js.map +1 -0
- package/llms.txt +469 -0
- package/package.json +118 -3
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@buildanagent/harness/sandbox/srt`: the OS-tier backend. It runs each command under Anthropic's `srt` sandbox
|
|
3
|
+
* runtime, which uses bubblewrap on Linux and starts a proxy that allows only the policy's hosts. It claims to stop
|
|
4
|
+
* `mistakes` and `exfiltration`, never a `hostile_process`: the kernel is shared with the host, and `srt` is a
|
|
5
|
+
* pre-1.0 beta (as of November 2026). Only Linux is supported, because it is the only platform this backend is tested
|
|
6
|
+
* on. A backend that can't start throws `SandboxUnavailableError` and runs nothing. It is Node-only.
|
|
7
|
+
*
|
|
8
|
+
* The file calls run in the host, not under `srt`: two `srt` processes in one workspace can disturb each other, because
|
|
9
|
+
* each makes and removes empty files there. A path outside the roots is refused by its real path, so a symlink that
|
|
10
|
+
* leads out is refused too, and the opened file is checked again through `/proc`, so a path swapped for a link in
|
|
11
|
+
* between is caught. A hostile process could still win that race, which is why no `hostile_process` stop is claimed.
|
|
12
|
+
*/
|
|
13
|
+
import { HarnessError } from "../../errors.js";
|
|
14
|
+
import { BackendSandbox } from "../base.js";
|
|
15
|
+
import { SandboxUnavailableError } from "../errors.js";
|
|
16
|
+
import { absolutePath, checkPaths, existing, findOnPath, HostFiles, isProtectedPath, realOrSelf, spellings, workspaceRoot, } from "../host-files.js";
|
|
17
|
+
import { nodeModules, ProcessRunner } from "../process.js";
|
|
18
|
+
import { srtPolicyFromSettings, srtSettings } from "./settings.js";
|
|
19
|
+
export { srtPolicyFromSettings, srtSettings } from "./settings.js";
|
|
20
|
+
/** The system folders a program needs to start. Each one that exists is readable, and nothing else outside the roots. */
|
|
21
|
+
export const SRT_SYSTEM_READ_ROOTS = [
|
|
22
|
+
"/usr",
|
|
23
|
+
"/bin",
|
|
24
|
+
"/sbin",
|
|
25
|
+
"/lib",
|
|
26
|
+
"/lib32",
|
|
27
|
+
"/lib64",
|
|
28
|
+
"/libx32",
|
|
29
|
+
"/etc",
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* The variables `srt` 0.0.83 adds to a command's environment, which are its proxy settings and the few that go with
|
|
33
|
+
* them. A command sees these besides the variables it was given, so a test that compares environments lists them.
|
|
34
|
+
*/
|
|
35
|
+
export const SRT_ADDED_ENVIRONMENT = [
|
|
36
|
+
"ALL_PROXY",
|
|
37
|
+
"CLAUDE_CODE_HOST_HTTP_PROXY_PORT",
|
|
38
|
+
"CLAUDE_CODE_HOST_SOCKS_PROXY_PORT",
|
|
39
|
+
"CLOUDSDK_PROXY_ADDRESS",
|
|
40
|
+
"CLOUDSDK_PROXY_PASSWORD",
|
|
41
|
+
"CLOUDSDK_PROXY_PORT",
|
|
42
|
+
"CLOUDSDK_PROXY_TYPE",
|
|
43
|
+
"CLOUDSDK_PROXY_USERNAME",
|
|
44
|
+
"DOCKER_HTTPS_PROXY",
|
|
45
|
+
"DOCKER_HTTP_PROXY",
|
|
46
|
+
"FTP_PROXY",
|
|
47
|
+
"GIT_CONFIG_PARAMETERS",
|
|
48
|
+
"GIT_SSH_COMMAND",
|
|
49
|
+
"GRPC_PROXY",
|
|
50
|
+
"HTTPS_PROXY",
|
|
51
|
+
"HTTP_PROXY",
|
|
52
|
+
"JAVA_TOOL_OPTIONS",
|
|
53
|
+
"NO_PROXY",
|
|
54
|
+
"RSYNC_PROXY",
|
|
55
|
+
"SANDBOX_RUNTIME",
|
|
56
|
+
"all_proxy",
|
|
57
|
+
"ftp_proxy",
|
|
58
|
+
"grpc_proxy",
|
|
59
|
+
"http_proxy",
|
|
60
|
+
"https_proxy",
|
|
61
|
+
"no_proxy",
|
|
62
|
+
];
|
|
63
|
+
/** The programs `srt` runs on Linux, which it finds on the `PATH`. */
|
|
64
|
+
const REQUIRED_PROGRAMS = ["bwrap", "socat"];
|
|
65
|
+
const PROBE_TIMEOUT_MS = 20_000;
|
|
66
|
+
// How long `srt` has to clean up after a `SIGTERM` before the command's whole group is killed.
|
|
67
|
+
const GRACE_MS = 2000;
|
|
68
|
+
/** How to start `srt`, or a `SandboxUnavailableError` when there is nothing to start. */
|
|
69
|
+
async function srtCommand(modules, binary) {
|
|
70
|
+
if (binary !== undefined) {
|
|
71
|
+
// Resolved once against this process's folder, because it is started from the workspace.
|
|
72
|
+
const program = modules.path.resolve(binary);
|
|
73
|
+
try {
|
|
74
|
+
await modules.fs.access(program, modules.fs.constants.X_OK);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw new SandboxUnavailableError("srt", `${binary} is not an executable file`, { cause: error });
|
|
78
|
+
}
|
|
79
|
+
return { program, args: [], readRoots: [] };
|
|
80
|
+
}
|
|
81
|
+
let cli;
|
|
82
|
+
try {
|
|
83
|
+
const url = import.meta.resolve("@anthropic-ai/sandbox-runtime/dist/cli.js");
|
|
84
|
+
cli = await modules.fs.realpath(modules.url.fileURLToPath(url));
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new SandboxUnavailableError("srt", "the @anthropic-ai/sandbox-runtime package is not installed", {
|
|
88
|
+
cause: error,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
// `srt` runs its seccomp helper from the package's own folder, inside the sandbox.
|
|
92
|
+
return { program: process.execPath, args: [cli], readRoots: [modules.path.resolve(cli, "..", "..", "vendor")] };
|
|
93
|
+
}
|
|
94
|
+
/** A `SandboxUnavailableError` unless this platform and host can run `srt`, looking for its programs on `hostPath`. */
|
|
95
|
+
async function checkHost(modules, hostPath) {
|
|
96
|
+
if (process.platform !== "linux") {
|
|
97
|
+
throw new SandboxUnavailableError("srt", `only Linux is supported, because it is the only platform this backend is tested on, and this is ${process.platform}`);
|
|
98
|
+
}
|
|
99
|
+
for (const program of REQUIRED_PROGRAMS) {
|
|
100
|
+
if ((await findOnPath(modules, hostPath, program)) === undefined) {
|
|
101
|
+
throw new SandboxUnavailableError("srt", `${program} is not on the PATH, and srt needs it on Linux`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// A variable name that `env` can set, which is every name a process can be given.
|
|
106
|
+
const VARIABLE_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
107
|
+
class SrtSandbox extends BackendSandbox {
|
|
108
|
+
runner;
|
|
109
|
+
state;
|
|
110
|
+
constructor(state) {
|
|
111
|
+
super(state);
|
|
112
|
+
this.state = state;
|
|
113
|
+
this.runner = new ProcessRunner(state.modules);
|
|
114
|
+
}
|
|
115
|
+
/** The program that starts `srt`. */
|
|
116
|
+
get program() {
|
|
117
|
+
return this.state.command.program;
|
|
118
|
+
}
|
|
119
|
+
/** The arguments that start `srt` with this sandbox's settings, then run `inner`. */
|
|
120
|
+
srtArguments(inner) {
|
|
121
|
+
return [...this.state.command.args, "--settings", this.state.settingsFile, "--", ...inner];
|
|
122
|
+
}
|
|
123
|
+
/** The environment `srt` itself runs with: where to find its programs, and where to make its sockets. Nothing else. */
|
|
124
|
+
launchEnvironment() {
|
|
125
|
+
return { PATH: this.state.hostPath, TMPDIR: this.state.runFolder };
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The command that runs inside `srt`. `env` sets the command's whole environment from `srt`'s own, so a variable that
|
|
129
|
+
* makes a program load code, such as `NODE_OPTIONS`, never reaches `srt` or the programs it starts outside the
|
|
130
|
+
* sandbox. The `--` ends `env`'s options, so a first word that starts with `-` is a program name.
|
|
131
|
+
*/
|
|
132
|
+
innerCommand(command, env) {
|
|
133
|
+
const unset = [];
|
|
134
|
+
for (const name of ["PATH", "TMPDIR"]) {
|
|
135
|
+
if (!(name in env))
|
|
136
|
+
unset.push("-u", name);
|
|
137
|
+
}
|
|
138
|
+
const assignments = [];
|
|
139
|
+
for (const [name, value] of Object.entries(env)) {
|
|
140
|
+
if (!VARIABLE_NAME.test(name)) {
|
|
141
|
+
throw new HarnessError("invalid_sandbox_options", `${JSON.stringify(name)} is not a variable name`);
|
|
142
|
+
}
|
|
143
|
+
assignments.push(`${name}=${value}`);
|
|
144
|
+
}
|
|
145
|
+
return ["/usr/bin/env", ...unset, "--", ...assignments, ...command];
|
|
146
|
+
}
|
|
147
|
+
/** Runs `command` under `srt` in `cwd`, with `env` as its whole environment. */
|
|
148
|
+
runUnderSrt(command, cwd, env, options) {
|
|
149
|
+
return this.runner.run({
|
|
150
|
+
program: this.state.command.program,
|
|
151
|
+
args: this.srtArguments(this.innerCommand(command, env)),
|
|
152
|
+
cwd,
|
|
153
|
+
env: this.launchEnvironment(),
|
|
154
|
+
missingProgram: "reject",
|
|
155
|
+
graceMs: GRACE_MS,
|
|
156
|
+
}, options);
|
|
157
|
+
}
|
|
158
|
+
async exec(command, options) {
|
|
159
|
+
const prepared = await this.prepare(command, options);
|
|
160
|
+
if (!("cwd" in prepared))
|
|
161
|
+
return prepared;
|
|
162
|
+
const { stdoutBytes: _bytes, ...result } = await this.runUnderSrt(command, prepared.cwd, options.env, options);
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
/** Reads the settings file `srt` is started with, as it is on disk now, and maps it back to a policy. */
|
|
166
|
+
async readBack() {
|
|
167
|
+
this.live();
|
|
168
|
+
const text = await this.state.modules.fs.readFile(this.state.settingsFile, "utf8");
|
|
169
|
+
return srtPolicyFromSettings(JSON.parse(text), this.state.names);
|
|
170
|
+
}
|
|
171
|
+
async shutDown() {
|
|
172
|
+
await this.runner.stopAll();
|
|
173
|
+
await this.state.modules.fs.rm(this.state.folder, { recursive: true, force: true });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** The first line of what `srt` printed, for the message of a failed start. */
|
|
177
|
+
function firstLine(text) {
|
|
178
|
+
return text.trim().split("\n")[0] ?? "no output";
|
|
179
|
+
}
|
|
180
|
+
/** The settings and the real roots for one sandbox, with every path's symlinks resolved. */
|
|
181
|
+
async function planSandbox(modules, policy, options, command, runFolder) {
|
|
182
|
+
const real = (paths) => Promise.all(paths.map((path) => realOrSelf(modules, path)));
|
|
183
|
+
const [readRoots, writeRoots, extra, denyWrite, helpers] = await Promise.all([
|
|
184
|
+
real(policy.readRoots),
|
|
185
|
+
real(policy.writeRoots),
|
|
186
|
+
real(options.extraReadRoots ?? []),
|
|
187
|
+
real(options.denyWrite ?? []),
|
|
188
|
+
real(command.readRoots),
|
|
189
|
+
]);
|
|
190
|
+
const systemReadRoots = await existing(modules, await real(SRT_SYSTEM_READ_ROOTS));
|
|
191
|
+
// Only the run folder: the settings file beside it holds a proxy address that a command must not read.
|
|
192
|
+
const helperReadRoots = [runFolder, ...helpers, ...extra];
|
|
193
|
+
const input = {
|
|
194
|
+
policy: { ...policy, readRoots, writeRoots },
|
|
195
|
+
systemReadRoots,
|
|
196
|
+
helperReadRoots,
|
|
197
|
+
denyWrite,
|
|
198
|
+
...(options.parentProxy === undefined ? {} : { parentProxy: options.parentProxy }),
|
|
199
|
+
};
|
|
200
|
+
const names = {
|
|
201
|
+
read: spellings(readRoots, policy.readRoots),
|
|
202
|
+
write: spellings(writeRoots, policy.writeRoots),
|
|
203
|
+
implicitReads: [...systemReadRoots, ...helperReadRoots],
|
|
204
|
+
};
|
|
205
|
+
return { settings: srtSettings(input), roots: { read: readRoots, write: writeRoots, denyWrite }, names };
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Builds the `srt` backend. `provision()` checks the platform, the programs `srt` needs and `srt` itself, by running a
|
|
209
|
+
* command that does nothing, so a host that can't confine a process throws `SandboxUnavailableError` and runs nothing.
|
|
210
|
+
* The policy's roots, `extraReadRoots` and `denyWrite` must be absolute folders other than `/`. Each command runs under
|
|
211
|
+
* its own `srt`, so a command sees the proxy variables `srt` adds to its environment, besides those it was given.
|
|
212
|
+
* Before it returns, `provision()` reads the settings file back from disk and throws `SandboxPolicyMismatchError` when
|
|
213
|
+
* the policy it applies differs from the one requested. The backend lists no capability: it can't resume, snapshot or
|
|
214
|
+
* pause.
|
|
215
|
+
*/
|
|
216
|
+
export function srtSandbox(options = {}) {
|
|
217
|
+
const client = {
|
|
218
|
+
tier: "os",
|
|
219
|
+
stops: ["mistakes", "exfiltration"],
|
|
220
|
+
async provision(manifest, policy) {
|
|
221
|
+
const modules = nodeModules("srt");
|
|
222
|
+
const hostPath = absolutePath(modules, process.env.PATH);
|
|
223
|
+
await checkHost(modules, hostPath);
|
|
224
|
+
const root = await workspaceRoot(modules, manifest.root);
|
|
225
|
+
checkPaths(modules, [...policy.readRoots, ...policy.writeRoots], "a sandbox root");
|
|
226
|
+
checkPaths(modules, options.extraReadRoots ?? [], "an extra read root");
|
|
227
|
+
checkPaths(modules, options.denyWrite ?? [], "a denyWrite folder");
|
|
228
|
+
const command = await srtCommand(modules, options.binary);
|
|
229
|
+
const folder = await modules.fs.mkdtemp(modules.path.join(modules.os.tmpdir(), "harness-srt-"));
|
|
230
|
+
try {
|
|
231
|
+
await modules.fs.chmod(folder, 0o700);
|
|
232
|
+
const runFolder = modules.path.join(folder, "run");
|
|
233
|
+
await modules.fs.mkdir(runFolder, { mode: 0o700 });
|
|
234
|
+
const plan = await planSandbox(modules, policy, options, command, runFolder);
|
|
235
|
+
const settingsFile = modules.path.join(folder, "settings.json");
|
|
236
|
+
await modules.fs.writeFile(settingsFile, JSON.stringify(plan.settings), { mode: 0o600 });
|
|
237
|
+
const sandbox = new SrtSandbox({
|
|
238
|
+
modules,
|
|
239
|
+
command,
|
|
240
|
+
root,
|
|
241
|
+
files: new HostFiles(modules, root, plan.roots, isProtectedPath),
|
|
242
|
+
folder,
|
|
243
|
+
runFolder,
|
|
244
|
+
settingsFile,
|
|
245
|
+
hostPath,
|
|
246
|
+
names: plan.names,
|
|
247
|
+
});
|
|
248
|
+
await probe(modules, sandbox, root);
|
|
249
|
+
await sandbox.confirm(client, policy);
|
|
250
|
+
const encoder = new TextEncoder();
|
|
251
|
+
for (const [path, data] of Object.entries(manifest.files ?? {})) {
|
|
252
|
+
await sandbox.writeFile(path, typeof data === "string" ? encoder.encode(data) : data);
|
|
253
|
+
}
|
|
254
|
+
return sandbox;
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
await modules.fs.rm(folder, { recursive: true, force: true });
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
return client;
|
|
263
|
+
}
|
|
264
|
+
/** Starts `srt` once with a command that does nothing, and throws `SandboxUnavailableError` when it can't. */
|
|
265
|
+
async function probe(modules, sandbox, root) {
|
|
266
|
+
const runner = new ProcessRunner(modules);
|
|
267
|
+
const result = await runner
|
|
268
|
+
.run({
|
|
269
|
+
program: sandbox.program,
|
|
270
|
+
args: sandbox.srtArguments(["/usr/bin/true"]),
|
|
271
|
+
cwd: root,
|
|
272
|
+
env: sandbox.launchEnvironment(),
|
|
273
|
+
missingProgram: "reject",
|
|
274
|
+
graceMs: GRACE_MS,
|
|
275
|
+
}, { env: {}, signal: new AbortController().signal, timeoutMs: PROBE_TIMEOUT_MS })
|
|
276
|
+
.catch((error) => {
|
|
277
|
+
throw new SandboxUnavailableError("srt", "it could not be started", { cause: error });
|
|
278
|
+
});
|
|
279
|
+
if (result.exitCode !== 0) {
|
|
280
|
+
throw new SandboxUnavailableError("srt", `it did not start a command: ${firstLine(result.stderr)}`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/sandbox/srt/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EACL,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,UAAU,EACV,SAAS,EACT,eAAe,EACf,UAAU,EACV,SAAS,EACT,aAAa,GACd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAoB,WAAW,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE7E,OAAO,EAA8C,qBAAqB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG/G,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAkBnE,yHAAyH;AACzH,MAAM,CAAC,MAAM,qBAAqB,GAAsB;IACtD,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;IACN,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,MAAM;CACP,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAsB;IACtD,WAAW;IACX,kCAAkC;IAClC,mCAAmC;IACnC,wBAAwB;IACxB,yBAAyB;IACzB,qBAAqB;IACrB,qBAAqB;IACrB,yBAAyB;IACzB,oBAAoB;IACpB,mBAAmB;IACnB,WAAW;IACX,uBAAuB;IACvB,iBAAiB;IACjB,YAAY;IACZ,aAAa;IACb,YAAY;IACZ,mBAAmB;IACnB,UAAU;IACV,aAAa;IACb,iBAAiB;IACjB,WAAW;IACX,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ACxB,OAAO,EAAE,QAAQ;KAClB,EACD,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,eAAe,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAC/E;SACA,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;QACxB,MAAM,IAAI,uBAAuB,CAAC,KAAK,EAAE,yBAAyB,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACxF,CAAC,CAAC,CAAC;IACL,IAAI,MAAM,CAAC,QAAQ,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,uBAAuB,CAAC,KAAK,EAAE,+BAA+B,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACtG,CAAC;AACH,CAAC","sourcesContent":["/**\n * `@buildanagent/harness/sandbox/srt`: the OS-tier backend. It runs each command under Anthropic's `srt` sandbox\n * runtime, which uses bubblewrap on Linux and starts a proxy that allows only the policy's hosts. It claims to stop\n * `mistakes` and `exfiltration`, never a `hostile_process`: the kernel is shared with the host, and `srt` is a\n * pre-1.0 beta (as of November 2026). Only Linux is supported, because it is the only platform this backend is tested\n * on. A backend that can't start throws `SandboxUnavailableError` and runs nothing. It is Node-only.\n *\n * The file calls run in the host, not under `srt`: two `srt` processes in one workspace can disturb each other, because\n * each makes and removes empty files there. A path outside the roots is refused by its real path, so a symlink that\n * leads out is refused too, and the opened file is checked again through `/proc`, so a path swapped for a link in\n * between is caught. A hostile process could still win that race, which is why no `hostile_process` stop is claimed.\n */\nimport { HarnessError } from \"../../errors.js\";\nimport { BackendSandbox } from \"../base.js\";\nimport { SandboxUnavailableError } from \"../errors.js\";\nimport {\n absolutePath,\n checkPaths,\n existing,\n findOnPath,\n HostFiles,\n isProtectedPath,\n realOrSelf,\n spellings,\n workspaceRoot,\n} from \"../host-files.js\";\nimport { type NodeModules, nodeModules, ProcessRunner } from \"../process.js\";\nimport type { ExecOptions, ExecResult, Sandbox, SandboxClient, SandboxPolicy, WorkspaceManifest } from \"../types.js\";\nimport { type SrtPolicyNames, type SrtSettingsInput, srtPolicyFromSettings, srtSettings } from \"./settings.js\";\n\nexport type { SrtPathEntry, SrtPolicyNames, SrtSettings, SrtSettingsInput } from \"./settings.js\";\nexport { srtPolicyFromSettings, srtSettings } from \"./settings.js\";\n\n/** The options of `srtSandbox()`. */\nexport interface SrtSandboxOptions {\n /**\n * The `srt` executable. Without it the backend runs the `srt` that the installed `@anthropic-ai/sandbox-runtime`\n * package ships, with this process's Node. A binary from elsewhere must have its own install folder in\n * `extraReadRoots`, because `srt` reads its helper program from there inside the sandbox.\n */\n readonly binary?: string;\n /** Folders to read besides the policy's roots and the system's, such as the folder a toolchain is installed in. */\n readonly extraReadRoots?: readonly string[];\n /** Folders that stay read-only even inside a write root, such as the session's own log folder. */\n readonly denyWrite?: readonly string[];\n /** An upstream HTTP proxy that the allowed traffic goes through, for a host that sits behind one. */\n readonly parentProxy?: { readonly http?: string; readonly https?: string };\n}\n\n/** The system folders a program needs to start. Each one that exists is readable, and nothing else outside the roots. */\nexport const SRT_SYSTEM_READ_ROOTS: readonly string[] = [\n \"/usr\",\n \"/bin\",\n \"/sbin\",\n \"/lib\",\n \"/lib32\",\n \"/lib64\",\n \"/libx32\",\n \"/etc\",\n];\n\n/**\n * The variables `srt` 0.0.83 adds to a command's environment, which are its proxy settings and the few that go with\n * them. A command sees these besides the variables it was given, so a test that compares environments lists them.\n */\nexport const SRT_ADDED_ENVIRONMENT: readonly string[] = [\n \"ALL_PROXY\",\n \"CLAUDE_CODE_HOST_HTTP_PROXY_PORT\",\n \"CLAUDE_CODE_HOST_SOCKS_PROXY_PORT\",\n \"CLOUDSDK_PROXY_ADDRESS\",\n \"CLOUDSDK_PROXY_PASSWORD\",\n \"CLOUDSDK_PROXY_PORT\",\n \"CLOUDSDK_PROXY_TYPE\",\n \"CLOUDSDK_PROXY_USERNAME\",\n \"DOCKER_HTTPS_PROXY\",\n \"DOCKER_HTTP_PROXY\",\n \"FTP_PROXY\",\n \"GIT_CONFIG_PARAMETERS\",\n \"GIT_SSH_COMMAND\",\n \"GRPC_PROXY\",\n \"HTTPS_PROXY\",\n \"HTTP_PROXY\",\n \"JAVA_TOOL_OPTIONS\",\n \"NO_PROXY\",\n \"RSYNC_PROXY\",\n \"SANDBOX_RUNTIME\",\n \"all_proxy\",\n \"ftp_proxy\",\n \"grpc_proxy\",\n \"http_proxy\",\n \"https_proxy\",\n \"no_proxy\",\n];\n\n/** The programs `srt` runs on Linux, which it finds on the `PATH`. */\nconst REQUIRED_PROGRAMS = [\"bwrap\", \"socat\"] as const;\nconst PROBE_TIMEOUT_MS = 20_000;\n// How long `srt` has to clean up after a `SIGTERM` before the command's whole group is killed.\nconst GRACE_MS = 2000;\n\n/** The program and leading arguments that start `srt`. */\ninterface SrtCommand {\n readonly program: string;\n readonly args: readonly string[];\n /** The folders `srt` reads inside the sandbox, as absolute paths. */\n readonly readRoots: readonly string[];\n}\n\n/** How to start `srt`, or a `SandboxUnavailableError` when there is nothing to start. */\nasync function srtCommand(modules: NodeModules, binary: string | undefined): Promise<SrtCommand> {\n if (binary !== undefined) {\n // Resolved once against this process's folder, because it is started from the workspace.\n const program = modules.path.resolve(binary);\n try {\n await modules.fs.access(program, modules.fs.constants.X_OK);\n } catch (error) {\n throw new SandboxUnavailableError(\"srt\", `${binary} is not an executable file`, { cause: error });\n }\n return { program, args: [], readRoots: [] };\n }\n let cli: string;\n try {\n const url = import.meta.resolve(\"@anthropic-ai/sandbox-runtime/dist/cli.js\");\n cli = await modules.fs.realpath(modules.url.fileURLToPath(url));\n } catch (error) {\n throw new SandboxUnavailableError(\"srt\", \"the @anthropic-ai/sandbox-runtime package is not installed\", {\n cause: error,\n });\n }\n // `srt` runs its seccomp helper from the package's own folder, inside the sandbox.\n return { program: process.execPath, args: [cli], readRoots: [modules.path.resolve(cli, \"..\", \"..\", \"vendor\")] };\n}\n\n/** A `SandboxUnavailableError` unless this platform and host can run `srt`, looking for its programs on `hostPath`. */\nasync function checkHost(modules: NodeModules, hostPath: string): Promise<void> {\n if (process.platform !== \"linux\") {\n throw new SandboxUnavailableError(\n \"srt\",\n `only Linux is supported, because it is the only platform this backend is tested on, and this is ${process.platform}`,\n );\n }\n for (const program of REQUIRED_PROGRAMS) {\n if ((await findOnPath(modules, hostPath, program)) === undefined) {\n throw new SandboxUnavailableError(\"srt\", `${program} is not on the PATH, and srt needs it on Linux`);\n }\n }\n}\n\n// A variable name that `env` can set, which is every name a process can be given.\nconst VARIABLE_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n/** Everything a provisioned sandbox holds. */\ninterface SrtSandboxState {\n readonly modules: NodeModules;\n readonly command: SrtCommand;\n readonly root: string;\n readonly files: HostFiles;\n /** A private folder that holds the settings file and the run folder, and is removed on dispose. */\n readonly folder: string;\n /** The folder `srt` makes its proxy sockets in, and the only part of `folder` a command may read. */\n readonly runFolder: string;\n readonly settingsFile: string;\n /** The `PATH` that `srt` itself runs with, which finds `bwrap` and `socat`. */\n readonly hostPath: string;\n /** How the settings file's real paths map back to the request's spelling, for the read-back. */\n readonly names: SrtPolicyNames;\n}\n\nclass SrtSandbox extends BackendSandbox {\n private readonly runner: ProcessRunner;\n private readonly state: SrtSandboxState;\n\n constructor(state: SrtSandboxState) {\n super(state);\n this.state = state;\n this.runner = new ProcessRunner(state.modules);\n }\n\n /** The program that starts `srt`. */\n get program(): string {\n return this.state.command.program;\n }\n\n /** The arguments that start `srt` with this sandbox's settings, then run `inner`. */\n srtArguments(inner: readonly string[]): string[] {\n return [...this.state.command.args, \"--settings\", this.state.settingsFile, \"--\", ...inner];\n }\n\n /** The environment `srt` itself runs with: where to find its programs, and where to make its sockets. Nothing else. */\n launchEnvironment(): Record<string, string> {\n return { PATH: this.state.hostPath, TMPDIR: this.state.runFolder };\n }\n\n /**\n * The command that runs inside `srt`. `env` sets the command's whole environment from `srt`'s own, so a variable that\n * makes a program load code, such as `NODE_OPTIONS`, never reaches `srt` or the programs it starts outside the\n * sandbox. The `--` ends `env`'s options, so a first word that starts with `-` is a program name.\n */\n private innerCommand(command: readonly string[], env: Readonly<Record<string, string>>): string[] {\n const unset: string[] = [];\n for (const name of [\"PATH\", \"TMPDIR\"]) {\n if (!(name in env)) unset.push(\"-u\", name);\n }\n const assignments: string[] = [];\n for (const [name, value] of Object.entries(env)) {\n if (!VARIABLE_NAME.test(name)) {\n throw new HarnessError(\"invalid_sandbox_options\", `${JSON.stringify(name)} is not a variable name`);\n }\n assignments.push(`${name}=${value}`);\n }\n return [\"/usr/bin/env\", ...unset, \"--\", ...assignments, ...command];\n }\n\n /** Runs `command` under `srt` in `cwd`, with `env` as its whole environment. */\n private runUnderSrt(\n command: readonly string[],\n cwd: string,\n env: Readonly<Record<string, string>>,\n options: ExecOptions,\n ) {\n return this.runner.run(\n {\n program: this.state.command.program,\n args: this.srtArguments(this.innerCommand(command, env)),\n cwd,\n env: this.launchEnvironment(),\n missingProgram: \"reject\",\n graceMs: GRACE_MS,\n },\n options,\n );\n }\n\n async exec(command: readonly string[], options: ExecOptions): Promise<ExecResult> {\n const prepared = await this.prepare(command, options);\n if (!(\"cwd\" in prepared)) return prepared;\n const { stdoutBytes: _bytes, ...result } = await this.runUnderSrt(command, prepared.cwd, options.env, options);\n return result;\n }\n\n /** Reads the settings file `srt` is started with, as it is on disk now, and maps it back to a policy. */\n async readBack(): Promise<SandboxPolicy> {\n this.live();\n const text = await this.state.modules.fs.readFile(this.state.settingsFile, \"utf8\");\n return srtPolicyFromSettings(JSON.parse(text) as unknown, this.state.names);\n }\n\n protected async shutDown(): Promise<void> {\n await this.runner.stopAll();\n await this.state.modules.fs.rm(this.state.folder, { recursive: true, force: true });\n }\n}\n\n/** The first line of what `srt` printed, for the message of a failed start. */\nfunction firstLine(text: string): string {\n return text.trim().split(\"\\n\")[0] ?? \"no output\";\n}\n\n/** The settings and the real roots for one sandbox, with every path's symlinks resolved. */\nasync function planSandbox(\n modules: NodeModules,\n policy: SandboxPolicy,\n options: SrtSandboxOptions,\n command: SrtCommand,\n runFolder: string,\n) {\n const real = (paths: readonly string[]) => Promise.all(paths.map((path) => realOrSelf(modules, path)));\n const [readRoots, writeRoots, extra, denyWrite, helpers] = await Promise.all([\n real(policy.readRoots),\n real(policy.writeRoots),\n real(options.extraReadRoots ?? []),\n real(options.denyWrite ?? []),\n real(command.readRoots),\n ]);\n const systemReadRoots = await existing(modules, await real(SRT_SYSTEM_READ_ROOTS));\n // Only the run folder: the settings file beside it holds a proxy address that a command must not read.\n const helperReadRoots = [runFolder, ...helpers, ...extra];\n const input: SrtSettingsInput = {\n policy: { ...policy, readRoots, writeRoots },\n systemReadRoots,\n helperReadRoots,\n denyWrite,\n ...(options.parentProxy === undefined ? {} : { parentProxy: options.parentProxy }),\n };\n const names: SrtPolicyNames = {\n read: spellings(readRoots, policy.readRoots),\n write: spellings(writeRoots, policy.writeRoots),\n implicitReads: [...systemReadRoots, ...helperReadRoots],\n };\n return { settings: srtSettings(input), roots: { read: readRoots, write: writeRoots, denyWrite }, names };\n}\n\n/**\n * Builds the `srt` backend. `provision()` checks the platform, the programs `srt` needs and `srt` itself, by running a\n * command that does nothing, so a host that can't confine a process throws `SandboxUnavailableError` and runs nothing.\n * The policy's roots, `extraReadRoots` and `denyWrite` must be absolute folders other than `/`. Each command runs under\n * its own `srt`, so a command sees the proxy variables `srt` adds to its environment, besides those it was given.\n * Before it returns, `provision()` reads the settings file back from disk and throws `SandboxPolicyMismatchError` when\n * the policy it applies differs from the one requested. The backend lists no capability: it can't resume, snapshot or\n * pause.\n */\nexport function srtSandbox(options: SrtSandboxOptions = {}): SandboxClient {\n const client: SandboxClient = {\n tier: \"os\",\n stops: [\"mistakes\", \"exfiltration\"],\n async provision(manifest: WorkspaceManifest, policy: SandboxPolicy): Promise<Sandbox> {\n const modules = nodeModules(\"srt\");\n const hostPath = absolutePath(modules, process.env.PATH);\n await checkHost(modules, hostPath);\n const root = await workspaceRoot(modules, manifest.root);\n checkPaths(modules, [...policy.readRoots, ...policy.writeRoots], \"a sandbox root\");\n checkPaths(modules, options.extraReadRoots ?? [], \"an extra read root\");\n checkPaths(modules, options.denyWrite ?? [], \"a denyWrite folder\");\n const command = await srtCommand(modules, options.binary);\n const folder = await modules.fs.mkdtemp(modules.path.join(modules.os.tmpdir(), \"harness-srt-\"));\n try {\n await modules.fs.chmod(folder, 0o700);\n const runFolder = modules.path.join(folder, \"run\");\n await modules.fs.mkdir(runFolder, { mode: 0o700 });\n const plan = await planSandbox(modules, policy, options, command, runFolder);\n const settingsFile = modules.path.join(folder, \"settings.json\");\n await modules.fs.writeFile(settingsFile, JSON.stringify(plan.settings), { mode: 0o600 });\n const sandbox = new SrtSandbox({\n modules,\n command,\n root,\n files: new HostFiles(modules, root, plan.roots, isProtectedPath),\n folder,\n runFolder,\n settingsFile,\n hostPath,\n names: plan.names,\n });\n await probe(modules, sandbox, root);\n await sandbox.confirm(client, policy);\n const encoder = new TextEncoder();\n for (const [path, data] of Object.entries(manifest.files ?? {})) {\n await sandbox.writeFile(path, typeof data === \"string\" ? encoder.encode(data) : data);\n }\n return sandbox;\n } catch (error) {\n await modules.fs.rm(folder, { recursive: true, force: true });\n throw error;\n }\n },\n };\n return client;\n}\n\n/** Starts `srt` once with a command that does nothing, and throws `SandboxUnavailableError` when it can't. */\nasync function probe(modules: NodeModules, sandbox: SrtSandbox, root: string): Promise<void> {\n const runner = new ProcessRunner(modules);\n const result = await runner\n .run(\n {\n program: sandbox.program,\n args: sandbox.srtArguments([\"/usr/bin/true\"]),\n cwd: root,\n env: sandbox.launchEnvironment(),\n missingProgram: \"reject\",\n graceMs: GRACE_MS,\n },\n { env: {}, signal: new AbortController().signal, timeoutMs: PROBE_TIMEOUT_MS },\n )\n .catch((error: unknown) => {\n throw new SandboxUnavailableError(\"srt\", \"it could not be started\", { cause: error });\n });\n if (result.exitCode !== 0) {\n throw new SandboxUnavailableError(\"srt\", `it did not start a command: ${firstLine(result.stderr)}`);\n }\n}\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { SandboxPolicy } from "../types.js";
|
|
2
|
+
/** A path that `srt` must read as written. Without `literal`, `srt` reads a spelling such as `*.ts` as a pattern. */
|
|
3
|
+
export interface SrtPathEntry {
|
|
4
|
+
readonly path: string;
|
|
5
|
+
readonly literal: true;
|
|
6
|
+
}
|
|
7
|
+
/** The settings `srt` reads from its `--settings` file. */
|
|
8
|
+
export interface SrtSettings {
|
|
9
|
+
readonly network: {
|
|
10
|
+
readonly allowedDomains: readonly string[];
|
|
11
|
+
readonly deniedDomains: readonly string[];
|
|
12
|
+
readonly parentProxy?: {
|
|
13
|
+
readonly http?: string;
|
|
14
|
+
readonly https?: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
readonly filesystem: {
|
|
18
|
+
readonly denyRead: readonly SrtPathEntry[];
|
|
19
|
+
readonly allowRead: readonly SrtPathEntry[];
|
|
20
|
+
readonly allowWrite: readonly SrtPathEntry[];
|
|
21
|
+
readonly denyWrite: readonly SrtPathEntry[];
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** What the settings are built from, beyond the policy. Every path is absolute with its symlinks resolved. */
|
|
25
|
+
export interface SrtSettingsInput {
|
|
26
|
+
readonly policy: SandboxPolicy;
|
|
27
|
+
/** The system folders every program needs to start, which the policy does not list. */
|
|
28
|
+
readonly systemReadRoots: readonly string[];
|
|
29
|
+
/** The folders `srt` itself needs to read, such as its own helper and its private socket folder. */
|
|
30
|
+
readonly helperReadRoots: readonly string[];
|
|
31
|
+
/** Folders to keep read-only inside a write root, such as the session's own log folder. */
|
|
32
|
+
readonly denyWrite: readonly string[];
|
|
33
|
+
readonly parentProxy?: {
|
|
34
|
+
readonly http?: string;
|
|
35
|
+
readonly https?: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Denies every read and then allows the policy's read and write roots, the system folders and the helper folders back.
|
|
40
|
+
* Writes are allowed only under the write roots, and `denyWrite` takes precedence over them. The network allows only the
|
|
41
|
+
* policy's hosts. Each path is `literal`, so a folder named `*` is never read as a pattern.
|
|
42
|
+
*/
|
|
43
|
+
export declare function srtSettings(input: SrtSettingsInput): SrtSettings;
|
|
44
|
+
/** How a settings file's real paths map back to the spelling of the request, for `srtPolicyFromSettings()`. */
|
|
45
|
+
export interface SrtPolicyNames {
|
|
46
|
+
/** For each real read root, the request's spellings that resolved to it. */
|
|
47
|
+
readonly read: ReadonlyMap<string, readonly string[]>;
|
|
48
|
+
/** For each real write root, the request's spellings that resolved to it. */
|
|
49
|
+
readonly write: ReadonlyMap<string, readonly string[]>;
|
|
50
|
+
/** The readable folders the backend adds itself: the system's, `srt`'s own and the caller's extra read roots. */
|
|
51
|
+
readonly implicitReads: readonly string[];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The policy a settings file applies, in the spelling of the request. A write root maps back through `names.write`,
|
|
55
|
+
* and a readable folder through `names.read`. A folder the backend adds itself, and a write root, which is readable
|
|
56
|
+
* anyway, are left out, so any other folder in the file shows up as a read root the request didn't ask for. A file
|
|
57
|
+
* that doesn't deny every read first reads back as the read root `/`. A file that isn't in the shape `srtSettings()`
|
|
58
|
+
* writes throws `SandboxPolicyMismatchError`.
|
|
59
|
+
*/
|
|
60
|
+
export declare function srtPolicyFromSettings(settings: unknown, names: SrtPolicyNames): SandboxPolicy;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `srt` settings file for a sandbox policy. It is a pure function of the policy, so a test can read the file a
|
|
3
|
+
* backend would write without starting `srt`. `srt` is a pre-1.0 beta whose settings format may change (as of
|
|
4
|
+
* November 2026), so this is the one place that knows the format.
|
|
5
|
+
*/
|
|
6
|
+
import { SandboxPolicyMismatchError } from "../errors.js";
|
|
7
|
+
const literal = (path) => ({ path, literal: true });
|
|
8
|
+
/** Removes repeats, keeping the first of each. */
|
|
9
|
+
const unique = (paths) => [...new Set(paths)];
|
|
10
|
+
/**
|
|
11
|
+
* Denies every read and then allows the policy's read and write roots, the system folders and the helper folders back.
|
|
12
|
+
* Writes are allowed only under the write roots, and `denyWrite` takes precedence over them. The network allows only the
|
|
13
|
+
* policy's hosts. Each path is `literal`, so a folder named `*` is never read as a pattern.
|
|
14
|
+
*/
|
|
15
|
+
export function srtSettings(input) {
|
|
16
|
+
const { policy } = input;
|
|
17
|
+
const readable = unique([
|
|
18
|
+
...policy.readRoots,
|
|
19
|
+
...policy.writeRoots,
|
|
20
|
+
...input.systemReadRoots,
|
|
21
|
+
...input.helperReadRoots,
|
|
22
|
+
]);
|
|
23
|
+
return {
|
|
24
|
+
network: {
|
|
25
|
+
allowedDomains: [...policy.network.allow],
|
|
26
|
+
deniedDomains: [...policy.network.deny],
|
|
27
|
+
...(input.parentProxy === undefined ? {} : { parentProxy: input.parentProxy }),
|
|
28
|
+
},
|
|
29
|
+
filesystem: {
|
|
30
|
+
denyRead: [literal("/")],
|
|
31
|
+
allowRead: readable.map(literal),
|
|
32
|
+
allowWrite: unique(policy.writeRoots).map(literal),
|
|
33
|
+
denyWrite: unique(input.denyWrite).map(literal),
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
38
|
+
/** The strings of a list, or a mismatch naming `field` when it is anything else. */
|
|
39
|
+
function strings(value, field) {
|
|
40
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) {
|
|
41
|
+
throw new SandboxPolicyMismatchError(field);
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
/** The paths of a list of literal entries. An entry `srt` would read as a pattern is a mismatch naming `field`. */
|
|
46
|
+
function literalPaths(value, field) {
|
|
47
|
+
if (!Array.isArray(value))
|
|
48
|
+
throw new SandboxPolicyMismatchError(field);
|
|
49
|
+
return value.map((entry) => {
|
|
50
|
+
if (!isRecord(entry) || typeof entry.path !== "string" || entry.literal !== true) {
|
|
51
|
+
throw new SandboxPolicyMismatchError(field);
|
|
52
|
+
}
|
|
53
|
+
return entry.path;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The policy a settings file applies, in the spelling of the request. A write root maps back through `names.write`,
|
|
58
|
+
* and a readable folder through `names.read`. A folder the backend adds itself, and a write root, which is readable
|
|
59
|
+
* anyway, are left out, so any other folder in the file shows up as a read root the request didn't ask for. A file
|
|
60
|
+
* that doesn't deny every read first reads back as the read root `/`. A file that isn't in the shape `srtSettings()`
|
|
61
|
+
* writes throws `SandboxPolicyMismatchError`.
|
|
62
|
+
*/
|
|
63
|
+
export function srtPolicyFromSettings(settings, names) {
|
|
64
|
+
const network = isRecord(settings) && isRecord(settings.network) ? settings.network : undefined;
|
|
65
|
+
const filesystem = isRecord(settings) && isRecord(settings.filesystem) ? settings.filesystem : undefined;
|
|
66
|
+
if (network === undefined || filesystem === undefined)
|
|
67
|
+
throw new SandboxPolicyMismatchError("settings");
|
|
68
|
+
const writeReal = literalPaths(filesystem.allowWrite, "writeRoots");
|
|
69
|
+
const denyRead = literalPaths(filesystem.denyRead, "readRoots");
|
|
70
|
+
const allowRead = literalPaths(filesystem.allowRead, "readRoots");
|
|
71
|
+
const implied = new Set([...writeReal, ...names.implicitReads]);
|
|
72
|
+
const readRoots = denyRead.length === 1 && denyRead[0] === "/"
|
|
73
|
+
? allowRead.flatMap((path) => names.read.get(path) ?? (implied.has(path) ? [] : [path]))
|
|
74
|
+
: ["/"];
|
|
75
|
+
return {
|
|
76
|
+
readRoots,
|
|
77
|
+
writeRoots: writeReal.flatMap((path) => names.write.get(path) ?? [path]),
|
|
78
|
+
network: {
|
|
79
|
+
allow: strings(network.allowedDomains, "network.allow"),
|
|
80
|
+
deny: strings(network.deniedDomains, "network.deny"),
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=settings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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O,CAAC,OAAO,CAAC,aAAa,EAAE,cAAc,CAAC;SACrD;KACF,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The `srt` settings file for a sandbox policy. It is a pure function of the policy, so a test can read the file a\n * backend would write without starting `srt`. `srt` is a pre-1.0 beta whose settings format may change (as of\n * November 2026), so this is the one place that knows the format.\n */\nimport { SandboxPolicyMismatchError } from \"../errors.js\";\nimport type { SandboxPolicy } from \"../types.js\";\n\n/** A path that `srt` must read as written. Without `literal`, `srt` reads a spelling such as `*.ts` as a pattern. */\nexport interface SrtPathEntry {\n readonly path: string;\n readonly literal: true;\n}\n\n/** The settings `srt` reads from its `--settings` file. */\nexport interface SrtSettings {\n readonly network: {\n readonly allowedDomains: readonly string[];\n readonly deniedDomains: readonly string[];\n readonly parentProxy?: { readonly http?: string; readonly https?: string };\n };\n readonly filesystem: {\n readonly denyRead: readonly SrtPathEntry[];\n readonly allowRead: readonly SrtPathEntry[];\n readonly allowWrite: readonly SrtPathEntry[];\n readonly denyWrite: readonly SrtPathEntry[];\n };\n}\n\n/** What the settings are built from, beyond the policy. Every path is absolute with its symlinks resolved. */\nexport interface SrtSettingsInput {\n readonly policy: SandboxPolicy;\n /** The system folders every program needs to start, which the policy does not list. */\n readonly systemReadRoots: readonly string[];\n /** The folders `srt` itself needs to read, such as its own helper and its private socket folder. */\n readonly helperReadRoots: readonly string[];\n /** Folders to keep read-only inside a write root, such as the session's own log folder. */\n readonly denyWrite: readonly string[];\n readonly parentProxy?: { readonly http?: string; readonly https?: string };\n}\n\nconst literal = (path: string): SrtPathEntry => ({ path, literal: true });\n\n/** Removes repeats, keeping the first of each. */\nconst unique = (paths: readonly string[]): string[] => [...new Set(paths)];\n\n/**\n * Denies every read and then allows the policy's read and write roots, the system folders and the helper folders back.\n * Writes are allowed only under the write roots, and `denyWrite` takes precedence over them. The network allows only the\n * policy's hosts. Each path is `literal`, so a folder named `*` is never read as a pattern.\n */\nexport function srtSettings(input: SrtSettingsInput): SrtSettings {\n const { policy } = input;\n const readable = unique([\n ...policy.readRoots,\n ...policy.writeRoots,\n ...input.systemReadRoots,\n ...input.helperReadRoots,\n ]);\n return {\n network: {\n allowedDomains: [...policy.network.allow],\n deniedDomains: [...policy.network.deny],\n ...(input.parentProxy === undefined ? {} : { parentProxy: input.parentProxy }),\n },\n filesystem: {\n denyRead: [literal(\"/\")],\n allowRead: readable.map(literal),\n allowWrite: unique(policy.writeRoots).map(literal),\n denyWrite: unique(input.denyWrite).map(literal),\n },\n };\n}\n\n/** How a settings file's real paths map back to the spelling of the request, for `srtPolicyFromSettings()`. */\nexport interface SrtPolicyNames {\n /** For each real read root, the request's spellings that resolved to it. */\n readonly read: ReadonlyMap<string, readonly string[]>;\n /** For each real write root, the request's spellings that resolved to it. */\n readonly write: ReadonlyMap<string, readonly string[]>;\n /** The readable folders the backend adds itself: the system's, `srt`'s own and the caller's extra read roots. */\n readonly implicitReads: readonly string[];\n}\n\nconst isRecord = (value: unknown): value is Record<string, unknown> => typeof value === \"object\" && value !== null;\n\n/** The strings of a list, or a mismatch naming `field` when it is anything else. */\nfunction strings(value: unknown, field: string): string[] {\n if (!Array.isArray(value) || !value.every((item) => typeof item === \"string\")) {\n throw new SandboxPolicyMismatchError(field);\n }\n return value;\n}\n\n/** The paths of a list of literal entries. An entry `srt` would read as a pattern is a mismatch naming `field`. */\nfunction literalPaths(value: unknown, field: string): string[] {\n if (!Array.isArray(value)) throw new SandboxPolicyMismatchError(field);\n return value.map((entry: unknown) => {\n if (!isRecord(entry) || typeof entry.path !== \"string\" || entry.literal !== true) {\n throw new SandboxPolicyMismatchError(field);\n }\n return entry.path;\n });\n}\n\n/**\n * The policy a settings file applies, in the spelling of the request. A write root maps back through `names.write`,\n * and a readable folder through `names.read`. A folder the backend adds itself, and a write root, which is readable\n * anyway, are left out, so any other folder in the file shows up as a read root the request didn't ask for. A file\n * that doesn't deny every read first reads back as the read root `/`. A file that isn't in the shape `srtSettings()`\n * writes throws `SandboxPolicyMismatchError`.\n */\nexport function srtPolicyFromSettings(settings: unknown, names: SrtPolicyNames): SandboxPolicy {\n const network = isRecord(settings) && isRecord(settings.network) ? settings.network : undefined;\n const filesystem = isRecord(settings) && isRecord(settings.filesystem) ? settings.filesystem : undefined;\n if (network === undefined || filesystem === undefined) throw new SandboxPolicyMismatchError(\"settings\");\n const writeReal = literalPaths(filesystem.allowWrite, \"writeRoots\");\n const denyRead = literalPaths(filesystem.denyRead, \"readRoots\");\n const allowRead = literalPaths(filesystem.allowRead, \"readRoots\");\n const implied = new Set([...writeReal, ...names.implicitReads]);\n const readRoots =\n denyRead.length === 1 && denyRead[0] === \"/\"\n ? allowRead.flatMap((path) => names.read.get(path) ?? (implied.has(path) ? [] : [path]))\n : [\"/\"];\n return {\n readRoots,\n writeRoots: writeReal.flatMap((path) => names.write.get(path) ?? [path]),\n network: {\n allow: strings(network.allowedDomains, \"network.allow\"),\n deny: strings(network.deniedDomains, \"network.deny\"),\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sandbox interface: a pluggable client with honest tiers. The host owns the policy and the approval, never the
|
|
3
|
+
* model, and a backend that can't start throws instead of running unconfined (harness guide section 6, ADR 0020).
|
|
4
|
+
*/
|
|
5
|
+
/** What a sandbox may touch. Roots are absolute directories; a backend that enforces them denies the rest. */
|
|
6
|
+
export interface SandboxPolicy {
|
|
7
|
+
/** Directories the sandbox may read. The write roots are readable too. */
|
|
8
|
+
readonly readRoots: readonly string[];
|
|
9
|
+
/** Directories the sandbox may write. */
|
|
10
|
+
readonly writeRoots: readonly string[];
|
|
11
|
+
/** The hosts the sandbox may reach: `allow` names them, and `deny` names the ones that stay closed inside `allow`. */
|
|
12
|
+
readonly network: {
|
|
13
|
+
readonly allow: readonly string[];
|
|
14
|
+
readonly deny: readonly string[];
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** When a denied action may be asked for again, which is the host's decision and never the sandbox's. */
|
|
18
|
+
export type ApprovalPolicy = "on-request" | "granular" | "never";
|
|
19
|
+
/**
|
|
20
|
+
* How strongly a backend confines a process. `none`: not at all. `os`: an operating-system sandbox that shares the
|
|
21
|
+
* host's kernel. `container`: a container. `gvisor`: a user-space kernel. `microvm`: a virtual machine.
|
|
22
|
+
*/
|
|
23
|
+
export type SandboxTier = "none" | "os" | "container" | "gvisor" | "microvm";
|
|
24
|
+
/**
|
|
25
|
+
* What a backend claims to stop. `mistakes`: the agent's own errors, such as a write outside the workspace.
|
|
26
|
+
* `exfiltration`: sending data to a host that is not allowed. `hostile_process`: a process that is trying to escape.
|
|
27
|
+
* A backend lists a stop only when its tier and its applied policy deliver it, which `stopsFor()` computes: only a
|
|
28
|
+
* `microvm` stops a `hostile_process`, and none of the backends in this package is one.
|
|
29
|
+
*/
|
|
30
|
+
export type SandboxStop = "mistakes" | "exfiltration" | "hostile_process";
|
|
31
|
+
/** The workspace a sandbox is provisioned for: its root directory and the files to put there first. */
|
|
32
|
+
export interface WorkspaceManifest {
|
|
33
|
+
/** The workspace's absolute root. Relative paths and working directories resolve against it. */
|
|
34
|
+
readonly root: string;
|
|
35
|
+
/** Files to write under the root before the first command, by path relative to it. */
|
|
36
|
+
readonly files?: Readonly<Record<string, string | Uint8Array>>;
|
|
37
|
+
}
|
|
38
|
+
/** What the policy refused. `target` names the path or host, and `reason` says which rule. */
|
|
39
|
+
export interface SandboxDenial {
|
|
40
|
+
readonly kind: "filesystem_read" | "filesystem_write" | "network";
|
|
41
|
+
readonly target: string;
|
|
42
|
+
readonly reason: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* What a command did: its exit status and output, or the denial that stopped it. A failing command is a result.
|
|
46
|
+
* `exitCode` is 124 for a timeout, 127 for a program that can't be found, and 128 plus the signal's number for a
|
|
47
|
+
* command that a signal killed, with `signal` naming it. `timedOut` is `true` only when the sandbox stopped the command
|
|
48
|
+
* at its timeout, so a command that exits with 124 itself can be told apart. `truncated` is `true` when the output
|
|
49
|
+
* passed the cap and the rest was dropped.
|
|
50
|
+
*/
|
|
51
|
+
export type ExecResult = {
|
|
52
|
+
exitCode: number;
|
|
53
|
+
stdout: string;
|
|
54
|
+
stderr: string;
|
|
55
|
+
signal?: string;
|
|
56
|
+
timedOut?: boolean;
|
|
57
|
+
truncated?: boolean;
|
|
58
|
+
} | {
|
|
59
|
+
denied: SandboxDenial;
|
|
60
|
+
};
|
|
61
|
+
/** The options of `Sandbox.exec()`. */
|
|
62
|
+
export interface ExecOptions {
|
|
63
|
+
/** The working directory, relative to the workspace root. Defaults to the root. */
|
|
64
|
+
readonly cwd?: string;
|
|
65
|
+
/** The command's whole environment. Build it with `spawnEnv()`: nothing else is added. */
|
|
66
|
+
readonly env: Readonly<Record<string, string>>;
|
|
67
|
+
/** Aborts the command and every process it started. An already aborted signal rejects before the command runs. */
|
|
68
|
+
readonly signal: AbortSignal;
|
|
69
|
+
/** Stops a command, and every process it started, that runs longer than this, with exit code 124. */
|
|
70
|
+
readonly timeoutMs?: number;
|
|
71
|
+
/** The most bytes of each of standard output and standard error to keep. Defaults to 8 MiB; the rest is dropped. */
|
|
72
|
+
readonly maxOutputBytes?: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* What a backend can do besides provisioning. `resume`: reattach to a sandbox by its id, such as after `pause` or a lost
|
|
76
|
+
* connection, or start one from a snapshot's id. `snapshot`: save a sandbox's state under an id that `resume` takes.
|
|
77
|
+
* `pause`: suspend a sandbox, keeping its state, until `resume`. A local backend has none of them.
|
|
78
|
+
*/
|
|
79
|
+
export type SandboxCapability = "resume" | "snapshot" | "pause";
|
|
80
|
+
/** A provisioned sandbox. */
|
|
81
|
+
export interface Sandbox {
|
|
82
|
+
/** Runs `command`, an executable and its arguments with no shell, and resolves with what it did. */
|
|
83
|
+
exec(command: readonly string[], options: ExecOptions): Promise<ExecResult>;
|
|
84
|
+
/** Reads a file by path relative to the workspace root, or absolute. A missing file rejects with `sandbox_not_found`. */
|
|
85
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
86
|
+
/** Writes a file by path relative to the workspace root, or absolute, making its folders. */
|
|
87
|
+
writeFile(path: string, data: Uint8Array): Promise<void>;
|
|
88
|
+
/** Stops what is still running, and releases the sandbox. Calling it again does nothing, and any other call afterwards rejects. */
|
|
89
|
+
dispose(): Promise<void>;
|
|
90
|
+
/** The id `SandboxClient.resume()` takes. Only a sandbox from a backend with the `resume` capability has one. */
|
|
91
|
+
readonly id?: string;
|
|
92
|
+
/**
|
|
93
|
+
* The stops the applied policy delivers, as `stopsFor()` computes them from the read-back. A backend that reads its
|
|
94
|
+
* policy back sets it when it provisions, and one that enforces nothing leaves it out.
|
|
95
|
+
*/
|
|
96
|
+
readonly stops?: readonly SandboxStop[];
|
|
97
|
+
/**
|
|
98
|
+
* Reads back the policy the backend applied, from the backend's own record of it, such as the settings file it wrote
|
|
99
|
+
* or the policy a hosted provider reports. A backend that claims any stop has it, and `provision()` compares its
|
|
100
|
+
* answer with the request and throws `SandboxPolicyMismatchError` when they differ.
|
|
101
|
+
*/
|
|
102
|
+
readBack?(): Promise<SandboxPolicy>;
|
|
103
|
+
/** Saves the sandbox's state and resolves with an id that `resume()` starts a sandbox from. Needs `snapshot`. */
|
|
104
|
+
snapshot?(): Promise<string>;
|
|
105
|
+
/** Suspends the sandbox until `resume()` with its `id`. Needs `pause`. */
|
|
106
|
+
pause?(): Promise<void>;
|
|
107
|
+
}
|
|
108
|
+
/** A sandbox backend. */
|
|
109
|
+
export interface SandboxClient {
|
|
110
|
+
readonly tier: SandboxTier;
|
|
111
|
+
/** The stops the backend claims for every policy it accepts. `provision()` checks them against the applied policy. */
|
|
112
|
+
readonly stops: readonly SandboxStop[];
|
|
113
|
+
/** What the backend can do besides provisioning. Leaving it out means none; call each one through its helper. */
|
|
114
|
+
readonly capabilities?: readonly SandboxCapability[];
|
|
115
|
+
/**
|
|
116
|
+
* Provisions a sandbox, or throws `SandboxUnavailableError` when the backend can't start. It never falls back. A
|
|
117
|
+
* backend that claims a stop reads its policy back first, and throws `SandboxPolicyMismatchError` on a difference.
|
|
118
|
+
*/
|
|
119
|
+
provision(manifest: WorkspaceManifest, policy: SandboxPolicy): Promise<Sandbox>;
|
|
120
|
+
/**
|
|
121
|
+
* Reattaches to the sandbox with this id, or starts one from the snapshot with this id. `policy` is the policy it must
|
|
122
|
+
* still apply. Needs `resume`; call it through `resumeSandbox()`, which checks the read-back.
|
|
123
|
+
*/
|
|
124
|
+
resume?(id: string, policy: SandboxPolicy): Promise<Sandbox>;
|
|
125
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sandbox interface: a pluggable client with honest tiers. The host owns the policy and the approval, never the
|
|
3
|
+
* model, and a backend that can't start throws instead of running unconfined (harness guide section 6, ADR 0020).
|
|
4
|
+
*/
|
|
5
|
+
export {};
|
|
6
|
+
//# sourceMappingURL=types.js.map
|