@deepseek-ai/dsh-sandbox-local 0.0.1-rc.1
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 +28 -0
- package/README.i18n.yaml +6 -0
- package/README.md +38 -0
- package/README.zh.md +38 -0
- package/lib/index.js +554 -0
- package/lib/invariant.js +23 -0
- package/lib/types/index.d.ts +204 -0
- package/lib/types/invariant.d.ts +16 -0
- package/lib/types/profiles.d.ts +28 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, DeepSeek
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.i18n.yaml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
|
|
2
|
+
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
|
3
|
+
# after editing either side, bring the other along and re-record with:
|
|
4
|
+
# pnpm run verify-translation-pairing --write packages/sandbox/sandbox-local/README.md
|
|
5
|
+
README.md: 4d9e8275ba3fe0c1f49555b61e319f52194244bc
|
|
6
|
+
README.zh.md: 8a755e6c5b0c266538277bbbcd118fd24ab164f3
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @deepseek-ai/dsh-sandbox-local
|
|
2
|
+
|
|
3
|
+
English | [中文](README.zh.md)
|
|
4
|
+
|
|
5
|
+
Local implementation of the [`dsh-sandbox`](../sandbox/) seam. It selects and caches one platform runner: Linux prefers a working `bwrap` then Landlock; macOS uses Seatbelt. Multiple candidates are probed in order, while a sole candidate is selected directly.
|
|
6
|
+
|
|
7
|
+
The package root exports the default and named `LocalSandboxProvider` plugin and `Config`; platform profile builders stay internal.
|
|
8
|
+
|
|
9
|
+
Unsupported platforms and unusable runners fail closed with `SANDBOX_UNAVAILABLE`; execution never silently falls through unconfined. Each wrap carries structured runner-failure rules so consumers can distinguish a broken sandbox from a command failure. The [sandbox Agent Note](../../../.agents/notes/implemented/feature/2026-07-06-sandbox.md) owns selection rationale and profile differences.
|
|
10
|
+
|
|
11
|
+
Policy is per call; the provider stores only the mechanism and cached runner verdict. Each wrap reports enforcement completeness plus backend-specific denial signatures and runner-failure rules. Landlock requires exit 125 and a `landlock-run:` fatal line after excluding only the exact partial-enforcement notice; a notice with child exit 1, 2, or 125 remains a child outcome. Bubblewrap and Seatbelt remain signature-only because neither public contract reserves a launcher-failure status. Consumers spawn the returned argv directly, so a missing or unexecutable runner is an out-of-band spawn failure while a successfully launched child exit 126 or 127 remains ordinary. `runnerCommand` skips probes and requires one or more non-empty, single-line, case-insensitive `runnerFailureSignatures` entries for the custom runner's own fatal dialect. Because its mechanism is unknown, it carries both Linux denial dialects. `probeTimeoutMs` bounds functional probes. The [sandbox Agent Note](../../../.agents/notes/implemented/feature/2026-07-06-sandbox.md) owns selection and failure semantics.
|
|
12
|
+
|
|
13
|
+
The Seatbelt profile is allow-default with `(deny file-write*)` plus write allow-lists, so exactly the mode's promised file effects are governed: `read-only` grants the `/dev/null` literal alone; `workspace-write` adds the workspace root, `/tmp`, and the per-user darwin temp dir (`os.tmpdir()` — the platform's real temp area for mkstemp-family tools), every root canonicalized because Seatbelt matches resolved paths (`/tmp` IS `/private/tmp`). Apple marks the `sandbox-exec` CLI deprecated but ships it on every macOS; the functional probe is what fails closed if that ever changes.
|
|
14
|
+
|
|
15
|
+
[`@deepseek-ai/node-addon-landlock-run`](https://www.npmjs.com/package/@deepseek-ai/node-addon-landlock-run) supplies the platform launcher, functional probe, and CLI argument vocabulary. This provider owns only mode-to-grant mapping and runner selection. Keeping path resolution and probe parsing with the versioned binary prevents contract drift.
|
|
16
|
+
|
|
17
|
+
```yaml
|
|
18
|
+
- id: sandbox
|
|
19
|
+
name: '@deepseek-ai/dsh-sandbox-local'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Consumers: [`@deepseek-ai/dsh-bash-sandbox`](../../bash/bash-sandbox/); see [the acp-agent example](../../../examples/acp-agent/) for the runnable default composition.
|
|
23
|
+
|
|
24
|
+
## Model Experience
|
|
25
|
+
|
|
26
|
+
Indirectly, through [`dsh-bash-sandbox`](../../bash/bash-sandbox/README.md) and [`dsh-tool-bash`](../../bash/tool-bash/README.md), which render this provider's enforcement and denial facts while the [`dsh-sandbox`](../sandbox/README.md) seam owns the `SANDBOX_UNAVAILABLE` text and runner selection and profiles stay outside context.
|
|
27
|
+
|
|
28
|
+
#### KV Cache effect
|
|
29
|
+
|
|
30
|
+
No direct invalidation; the named consumer owns any request-prefix changes.
|
|
31
|
+
|
|
32
|
+
## Known Limitations and Deferred Work
|
|
33
|
+
|
|
34
|
+
- **Windows has no runner** — `win32` fails closed with `SANDBOX_UNAVAILABLE`; an AppContainer-family backend is deferred.
|
|
35
|
+
- **Landlock may be partial** — older supported kernel ABIs confine only the access classes they expose, reported as `enforcement: 'partial'` rather than overstated as full.
|
|
36
|
+
- **Seatbelt depends on deprecated `sandbox-exec`** — macOS still ships it, but this provider cannot replace or probe that private policy engine if Apple removes it.
|
|
37
|
+
- **Runner selection is cached for the provider lifetime** — installing, removing, or repairing a runner requires reloading the plugin before selection changes.
|
|
38
|
+
- **`runnerCommand` is an operator assertion** — a configured custom runner skips functional probes and is assumed to implement the bwrap-compatible profile honestly; if it is itself a Bash script, its interpreter startup runs before that script applies confinement.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @deepseek-ai/dsh-sandbox-local
|
|
2
|
+
|
|
3
|
+
[English](README.md) | 中文
|
|
4
|
+
|
|
5
|
+
[`dsh-sandbox`](../sandbox/) seam 的本地实现。它选择并缓存一个平台 runner:Linux 优先选择可工作的 `bwrap`,否则选择 Landlock;macOS 使用 Seatbelt。多个候选项会按顺序探测,只有一个候选项时则直接选择。
|
|
6
|
+
|
|
7
|
+
包根目录导出默认及命名的 `LocalSandboxProvider` 插件和 `Config`;平台 profile builder 仍为内部实现。
|
|
8
|
+
|
|
9
|
+
不受支持的平台和不可用 runner 会以 `SANDBOX_UNAVAILABLE` 拒绝执行;执行绝不会静默回退为不受限制。每次包装都携带结构化 runner 失败规则,使消费方能够区分损坏的沙箱与命令失败。[沙箱 Agent Note](../../../.agents/notes/implemented/feature/2026-07-06-sandbox.md)负责说明选择依据与 profile 差异。
|
|
10
|
+
|
|
11
|
+
策略逐调用传入;提供方只存储机制与缓存的 runner 结论。每次包装都会报告强制执行完整度,以及后端专用的拒绝签名和 runner 失败规则。Landlock 只有在退出码为 125,且仅排除完全匹配的部分强制执行通知后仍存在一行 `landlock-run:` 致命诊断时,才判定 runner 失败;携带该通知的子进程即使以 1、2 或 125 退出,也仍按子进程结果处理。Bubblewrap 和 Seatbelt 仍仅依据签名,因为两者的公开约定均未保留 launcher 失败状态。消费方会直接 spawn 返回的 argv,因此 runner 缺失或不可执行属于带外 spawn 失败,而成功启动的子进程以 126 或 127 退出时仍按普通结果处理。`runnerCommand` 会跳过探测,并要求为自定义 runner 自身的致命方言提供一个或多个非空、单行、不区分大小写的 `runnerFailureSignatures` 条目。由于其机制未知,它会同时携带两种 Linux 拒绝方言。`probeTimeoutMs` 限制功能探测。[沙箱 Agent Note](../../../.agents/notes/implemented/feature/2026-07-06-sandbox.md)负责说明选择与失败语义。
|
|
12
|
+
|
|
13
|
+
Seatbelt profile 默认允许,但带 `(deny file-write*)` 和写入 allow-list,因此恰好约束相应模式承诺的文件操作:`read-only` 只授予 `/dev/null` 字面路径;`workspace-write` 另加工作区根目录、`/tmp` 和逐用户 darwin 临时目录(`os.tmpdir()`,即平台供 mkstemp 家族工具使用的真实临时区域)。每个根目录都经过规范化,因为 Seatbelt 匹配解析后的路径(`/tmp` 就是 `/private/tmp`)。Apple 将 `sandbox-exec` CLI(命令行界面)标为 deprecated,但所有 macOS 系统仍会提供它;若情况发生变化,功能探测会使执行被拒绝。
|
|
14
|
+
|
|
15
|
+
[`@deepseek-ai/node-addon-landlock-run`](https://www.npmjs.com/package/@deepseek-ai/node-addon-landlock-run)提供平台 launcher、功能探测和 CLI 参数词汇。该提供方只负责模式到授权的映射与 runner 选择。把路径解析和探测解析保留在带版本的 binary 中,可防止约定漂移。
|
|
16
|
+
|
|
17
|
+
```yaml
|
|
18
|
+
- id: sandbox
|
|
19
|
+
name: '@deepseek-ai/dsh-sandbox-local'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
消费方:[`@deepseek-ai/dsh-bash-sandbox`](../../bash/bash-sandbox/);可运行的默认组合见 [acp-agent 示例](../../../examples/acp-agent/)。
|
|
23
|
+
|
|
24
|
+
## 模型体验
|
|
25
|
+
|
|
26
|
+
通过 [`dsh-bash-sandbox`](../../bash/bash-sandbox/README.md) 和 [`dsh-tool-bash`](../../bash/tool-bash/README.md) 间接影响;它们渲染该提供方的强制执行与拒绝事实,而 [`dsh-sandbox`](../sandbox/README.md) seam 负责定义 `SANDBOX_UNAVAILABLE` 文本,runner 选择与 profile 则不进入上下文。
|
|
27
|
+
|
|
28
|
+
#### KV Cache 影响
|
|
29
|
+
|
|
30
|
+
不会直接使 KV Cache 失效;请求前缀变更由上述消费方负责。
|
|
31
|
+
|
|
32
|
+
## 已知限制与暂缓事项
|
|
33
|
+
|
|
34
|
+
- **Windows 没有 runner**:`win32` 以 `SANDBOX_UNAVAILABLE` 拒绝执行;AppContainer 家族后端暂缓实现。
|
|
35
|
+
- **Landlock 可能只实现部分强制执行**:较旧且受支持的内核 ABI 只能限制自身公开的访问类别,因此报告 `enforcement: 'partial'`,不会夸大为完整强制执行。
|
|
36
|
+
- **Seatbelt 依赖已弃用的 `sandbox-exec`**:macOS 仍会提供它,但若 Apple 移除该私有策略引擎,该提供方无法替换或探测。
|
|
37
|
+
- **runner 选择在提供方生命周期内缓存**:安装、移除或修复 runner 后,必须重载插件才能改变选择。
|
|
38
|
+
- **`runnerCommand` 是操作方断言**:配置的自定义 runner 会跳过功能探测,并假定它诚实实现与 bwrap 兼容的 profile;如果它本身是 Bash 脚本,其解释器启动发生在该脚本施加约束之前。
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { LAUNCHER_BIN, LAUNCHER_FAILURE_EXIT, grantArgs, launcherPath, probe } from "@deepseek-ai/node-addon-landlock-run";
|
|
8
|
+
import z from "@deepseek-ai/schemastery";
|
|
9
|
+
import { assertNever } from "@deepseek-ai/dsh-llm";
|
|
10
|
+
import { SandboxProvider, SandboxUnavailableError, writableRoots } from "@deepseek-ai/dsh-sandbox";
|
|
11
|
+
import { AclWriteGrant, workspaceWriteSid } from "@deepseek-ai/dsh-sandbox-windows-acl";
|
|
12
|
+
//#region lib/types/profiles.js
|
|
13
|
+
/**
|
|
14
|
+
* Internal platform-profile builders for the local sandbox provider.
|
|
15
|
+
*
|
|
16
|
+
* @module @deepseek-ai/dsh-sandbox-local/profiles
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Build the bwrap profile arguments for one file-effect policy.
|
|
20
|
+
* @param policy - file-effect policy to express as bwrap mounts.
|
|
21
|
+
* @returns profile arguments before the trailing separator and command argv.
|
|
22
|
+
*/
|
|
23
|
+
function bwrapProfileArgs(policy) {
|
|
24
|
+
const args = [
|
|
25
|
+
"--ro-bind",
|
|
26
|
+
"/",
|
|
27
|
+
"/",
|
|
28
|
+
"--dev",
|
|
29
|
+
"/dev",
|
|
30
|
+
"--proc",
|
|
31
|
+
"/proc",
|
|
32
|
+
"--die-with-parent"
|
|
33
|
+
];
|
|
34
|
+
if (policy.mode === "workspace-write") {
|
|
35
|
+
args.push("--tmpfs", "/tmp");
|
|
36
|
+
args.push("--bind", policy.workspaceRoot, policy.workspaceRoot);
|
|
37
|
+
}
|
|
38
|
+
return args;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Build the Landlock launcher grants for one file-effect policy.
|
|
42
|
+
* @param policy - file-effect policy to express as Landlock allow-list grants.
|
|
43
|
+
* @returns launcher grant arguments before the trailing separator and command argv.
|
|
44
|
+
*/
|
|
45
|
+
function landlockProfileArgs(policy) {
|
|
46
|
+
const readWrite = ["/dev/null"];
|
|
47
|
+
if (policy.mode === "workspace-write") readWrite.push("/tmp", policy.workspaceRoot);
|
|
48
|
+
return grantArgs({
|
|
49
|
+
readOnly: ["/"],
|
|
50
|
+
readWrite
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Quote one path as an SBPL string literal. */
|
|
54
|
+
function sbplString(path) {
|
|
55
|
+
return `"${path.replaceAll("\\", String.raw`\\`).replaceAll("\"", String.raw`\"`)}"`;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Build the sandbox-exec arguments and SBPL profile for one policy. The
|
|
59
|
+
* writable roots come from the shared {@link writableRoots} helper (canonical,
|
|
60
|
+
* deduplicated) so the Seatbelt grant and the in-process fs fence
|
|
61
|
+
* (`@deepseek-ai/dsh-fs-sandbox`) can never drift apart.
|
|
62
|
+
* @param policy - file-effect policy to express as an SBPL profile.
|
|
63
|
+
* @returns sandbox-exec arguments before the trailing separator and command argv.
|
|
64
|
+
*/
|
|
65
|
+
function seatbeltProfileArgs(policy) {
|
|
66
|
+
const forms = [
|
|
67
|
+
"(version 1)",
|
|
68
|
+
"(allow default)",
|
|
69
|
+
"(deny file-write*)",
|
|
70
|
+
`(allow file-write* (literal ${sbplString("/dev/null")}))`
|
|
71
|
+
];
|
|
72
|
+
const roots = writableRoots(policy);
|
|
73
|
+
if (roots.length > 0) forms.push(`(allow file-write* ${roots.map((root) => `(subpath ${sbplString(root)})`).join(" ")})`);
|
|
74
|
+
return ["-p", forms.join(" ")];
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region lib/types/index.js
|
|
78
|
+
/**
|
|
79
|
+
* Local sandbox backend. It selects the platform runner chain (Linux bwrap then
|
|
80
|
+
* Landlock; macOS Seatbelt; Windows the ACL restricted-token runner), functionally probes
|
|
81
|
+
* competing candidates once, and reports each wrap's enforcement and stderr
|
|
82
|
+
* classification facts. Missing or unusable confinement fails closed rather
|
|
83
|
+
* than returning the original argv.
|
|
84
|
+
*
|
|
85
|
+
* The windows-acl rung additionally owns the write grants: the write SID is
|
|
86
|
+
* the per-WORKSPACE identity derived from the canonical workspace path
|
|
87
|
+
* (`workspaceWriteSid`), and the private temp subdirectory is DERIVED per
|
|
88
|
+
* session (session id + workspace — nothing stored). The
|
|
89
|
+
* workspace-root ACE materializes once per workspace per server lifetime
|
|
90
|
+
* and STANDS (the cross-session reuse cache — the exact-ACE skip makes
|
|
91
|
+
* every later provision O(1) instead of re-propagating the tree per
|
|
92
|
+
* session); the private-temp ACEs are revoked on dispose. The runner
|
|
93
|
+
* receives `--write-sid` (the derived identity; its presence marks the
|
|
94
|
+
* seam-managed contract) and stops managing DACLs itself.
|
|
95
|
+
* @module @deepseek-ai/dsh-sandbox-local
|
|
96
|
+
*/
|
|
97
|
+
/** Probe whether `bwrap` can create the profile; the provider caches the bounded result. */
|
|
98
|
+
function defaultProbeBwrap(timeoutMs) {
|
|
99
|
+
return spawnSync("bwrap", [
|
|
100
|
+
"--ro-bind",
|
|
101
|
+
"/",
|
|
102
|
+
"/",
|
|
103
|
+
"--dev",
|
|
104
|
+
"/dev",
|
|
105
|
+
"--proc",
|
|
106
|
+
"/proc",
|
|
107
|
+
"--die-with-parent",
|
|
108
|
+
"--",
|
|
109
|
+
"true"
|
|
110
|
+
], {
|
|
111
|
+
timeout: timeoutMs,
|
|
112
|
+
stdio: "ignore"
|
|
113
|
+
}).status === 0;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Functional Seatbelt probe: apply the real `read-only` profile through
|
|
117
|
+
* `sandbox-exec -p` and run `true` under it — exit 0 means the kernel
|
|
118
|
+
* accepted and enforced the profile (`sandbox-exec` exits non-zero when
|
|
119
|
+
* `sandbox_init` refuses it). A missing `sandbox-exec` (every non-macOS
|
|
120
|
+
* host) fails the spawn and probes `unusable`, exactly like the other
|
|
121
|
+
* rungs' absent binaries. Apple marks the CLI deprecated but ships it on
|
|
122
|
+
* every macOS; if it ever disappears, this probe is what fails closed.
|
|
123
|
+
*/
|
|
124
|
+
function defaultProbeSeatbelt(seatbeltExec, timeoutMs) {
|
|
125
|
+
return spawnSync(seatbeltExec, [
|
|
126
|
+
...seatbeltProfileArgs({
|
|
127
|
+
mode: "read-only",
|
|
128
|
+
workspaceRoot: "/"
|
|
129
|
+
}),
|
|
130
|
+
"--",
|
|
131
|
+
"true"
|
|
132
|
+
], {
|
|
133
|
+
timeout: timeoutMs,
|
|
134
|
+
stdio: "ignore"
|
|
135
|
+
}).status === 0;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Functional windows-acl probe: run the runner in read-only mode (zero grants,
|
|
139
|
+
* no ACL mutation) around `cmd /c exit 0` — exit 0 means the runner created
|
|
140
|
+
* the restricted token and spawned the child under it. The win32 chain is a
|
|
141
|
+
* sole candidate, so the product never probes; the probe exists for override
|
|
142
|
+
* chains and mirrors the other rungs' shape.
|
|
143
|
+
*/
|
|
144
|
+
function defaultProbeWindowsAcl(runnerInvocation, timeoutMs) {
|
|
145
|
+
const program = runnerInvocation[0];
|
|
146
|
+
if (program === void 0) return false;
|
|
147
|
+
return spawnSync(program, [
|
|
148
|
+
...runnerInvocation.slice(1),
|
|
149
|
+
"--workspace",
|
|
150
|
+
tmpdir(),
|
|
151
|
+
"--temp",
|
|
152
|
+
tmpdir(),
|
|
153
|
+
"--mode",
|
|
154
|
+
"read-only",
|
|
155
|
+
"--",
|
|
156
|
+
"cmd",
|
|
157
|
+
"/c",
|
|
158
|
+
"exit",
|
|
159
|
+
"0"
|
|
160
|
+
], {
|
|
161
|
+
timeout: timeoutMs,
|
|
162
|
+
stdio: "ignore"
|
|
163
|
+
}).status === 0;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The session's private temp subdirectory: `<tmpdir>\dsh-<16 hex>`, derived
|
|
167
|
+
* from the session id and its workspace instead of stored. The same session
|
|
168
|
+
* and workspace always name the same directory — a resumed session
|
|
169
|
+
* re-grants it (the exact-ACE skip keeps that O(1)) — while a fork's
|
|
170
|
+
* different session id names a fresh one. The name is predictable to anyone
|
|
171
|
+
* who knows the session id (the confined command sees it as
|
|
172
|
+
* `DSH_SESSION_ID`), so the provider creates the directory EXCLUSIVELY and
|
|
173
|
+
* rejects reparse points: a pre-placed entry fails the first confined run
|
|
174
|
+
* loudly, and cannot redirect the grant onto a foreign object.
|
|
175
|
+
* @param sessionId - the policy's calling-session identity.
|
|
176
|
+
* @param workspaceRoot - the resolved policy root.
|
|
177
|
+
* @returns the session's private temp subdirectory path.
|
|
178
|
+
*/
|
|
179
|
+
function sessionTempDir(sessionId, workspaceRoot) {
|
|
180
|
+
const digest = createHash("sha256").update(String(sessionId)).update("\0").update(workspaceRoot).digest("hex");
|
|
181
|
+
return join(tmpdir(), `dsh-${digest.slice(0, 16)}`);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The runner chain per platform — selection is BY PLATFORM first, probes
|
|
185
|
+
* second: a platform's chain is probed in preference order only when it has
|
|
186
|
+
* MORE than one candidate (probing arbitrates; it does not re-validate a
|
|
187
|
+
* choice that has no alternative). A platform with no chain fails closed at
|
|
188
|
+
* `confine()`. Linux prefers `bwrap` (its mount profile is closest to the
|
|
189
|
+
* mode vocabulary) over the Landlock launcher; darwin has exactly one
|
|
190
|
+
* candidate, selected without any probe.
|
|
191
|
+
*/
|
|
192
|
+
const PLATFORM_CHAINS = {
|
|
193
|
+
linux: ["bwrap", "landlock"],
|
|
194
|
+
darwin: ["seatbelt"],
|
|
195
|
+
win32: ["windows-acl"]
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Enforcement completeness a rung claims when selected WITHOUT a probe (a
|
|
199
|
+
* chain of one). `bwrap` and Seatbelt govern every promised file effect by
|
|
200
|
+
* construction, so the claim is a profile fact; `landlock` is listed for the
|
|
201
|
+
* table's totality but is unreachable unprobed today (the Linux chain has
|
|
202
|
+
* two rungs, so it is only ever selected through its probe, whose report is
|
|
203
|
+
* what distinguishes full from per-ABI-partial — and the launcher additionally
|
|
204
|
+
* self-reports partial enforcement on stderr at every confined run).
|
|
205
|
+
*/
|
|
206
|
+
const STATIC_ENFORCEMENT = {
|
|
207
|
+
bwrap: "full",
|
|
208
|
+
landlock: "full",
|
|
209
|
+
seatbelt: "full",
|
|
210
|
+
"windows-acl": "full"
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* A probe bound must be a positive finite number: Node treats
|
|
214
|
+
* `spawnSync({ timeout: 0 })` as NO timeout, so an unvalidated 0 would
|
|
215
|
+
* silently mean "unbounded" — the opposite of what the field promises.
|
|
216
|
+
*/
|
|
217
|
+
function assertPositiveFinite(name, value) {
|
|
218
|
+
if (!Number.isFinite(value) || value <= 0) throw new Error(`sandbox-local: ${name} must be a positive finite number`);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The denial dialect each runner's kernel speaks — the case-insensitive stderr substrings a
|
|
222
|
+
* denied file effect produces under it, carried on every wrap (the seam's
|
|
223
|
+
* `ConfinedArgv.denialSignatures`).
|
|
224
|
+
*/
|
|
225
|
+
const DENIAL_SIGNATURES = {
|
|
226
|
+
bwrap: ["read-only file system"],
|
|
227
|
+
landlock: ["permission denied"],
|
|
228
|
+
seatbelt: ["operation not permitted"],
|
|
229
|
+
"windows-acl": [
|
|
230
|
+
"access is denied",
|
|
231
|
+
"access to the path",
|
|
232
|
+
"permission denied"
|
|
233
|
+
],
|
|
234
|
+
runnerCommand: ["read-only file system", "permission denied"]
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Runner-owned fatal diagnostics. Landlock has a versioned exit-125 plus
|
|
238
|
+
* fatal-line launcher-failure contract. Bubblewrap's current fatal paths exit
|
|
239
|
+
* 1 but its public contract does not reserve that status, while sandbox-exec
|
|
240
|
+
* publishes no launcher-failure status; those backends remain signature-only.
|
|
241
|
+
* The windows-acl runner prints `windows-acl-run: <detail>` on every
|
|
242
|
+
* runner-side failure and exits 127 — the rule is exit-gated on that status
|
|
243
|
+
* so a confined command that merely PRINTS the signature (or a runner
|
|
244
|
+
* cleanup failure reported on a non-zero child exit) is never misclassified
|
|
245
|
+
* as "the command did not run". Keep the Landlock tuple aligned with the
|
|
246
|
+
* assembled snapshot fixture at
|
|
247
|
+
* `examples/acp-agent/tests/fixtures/partial-landlock-sandbox.ts`.
|
|
248
|
+
*/
|
|
249
|
+
const RUNNER_FAILURE_RULES = {
|
|
250
|
+
bwrap: [{ fatalSignatures: ["bwrap: "] }],
|
|
251
|
+
landlock: [{
|
|
252
|
+
allowedExitCodes: [LAUNCHER_FAILURE_EXIT],
|
|
253
|
+
fatalSignatures: [`${LAUNCHER_BIN}: `],
|
|
254
|
+
informationalLines: [`${LAUNCHER_BIN}: partial enforcement (older Landlock ABI)`]
|
|
255
|
+
}],
|
|
256
|
+
seatbelt: [{ fatalSignatures: ["sandbox-exec: "] }],
|
|
257
|
+
"windows-acl": [{
|
|
258
|
+
allowedExitCodes: [127],
|
|
259
|
+
fatalSignatures: ["windows-acl-run: "]
|
|
260
|
+
}]
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* Local process-sandbox provider. Registers as `ctx.sandbox`. Caches the
|
|
264
|
+
* chain verdict and, on the windows-acl rung, the write grants
|
|
265
|
+
* ({@link AclWriteGrant}: the standing workspace-root grant per workspace
|
|
266
|
+
* and the revocable private-temp grant per session, the latter revoked on
|
|
267
|
+
* provider dispose); the one-time probes spawn nothing else.
|
|
268
|
+
*/
|
|
269
|
+
var LocalSandboxProvider = class extends SandboxProvider {
|
|
270
|
+
static Config = z.object({
|
|
271
|
+
runnerCommand: z.array(z.string()).default([]),
|
|
272
|
+
runnerFailureSignatures: z.array(z.string()).default([]),
|
|
273
|
+
probeTimeoutMs: z.natural().default(5e3)
|
|
274
|
+
});
|
|
275
|
+
/** Test hook (mirrors the bash executors' `internals`). */
|
|
276
|
+
internals = {};
|
|
277
|
+
runnerCommand;
|
|
278
|
+
configuredRunnerFailureSignatures;
|
|
279
|
+
probeTimeoutMs;
|
|
280
|
+
/** Cached chain verdict; undefined until the first confined wrap needs it. */
|
|
281
|
+
selectedRunner;
|
|
282
|
+
/**
|
|
283
|
+
* Server-lifetime write grants (windows-acl rung): the STANDING
|
|
284
|
+
* workspace-root grant per workspace (its ACE is the cross-session reuse
|
|
285
|
+
* cache and outlives the provider — never revoked) and the REVOCABLE
|
|
286
|
+
* private-temp grant per session (revoked on provider dispose).
|
|
287
|
+
*/
|
|
288
|
+
workspaceGrants = /* @__PURE__ */ new Map();
|
|
289
|
+
tempGrants = /* @__PURE__ */ new Map();
|
|
290
|
+
/** Session id → the private temp directory this provider created (removed on dispose). */
|
|
291
|
+
tempDirs = /* @__PURE__ */ new Map();
|
|
292
|
+
constructor(ctx, config) {
|
|
293
|
+
super(ctx);
|
|
294
|
+
const runner = config.runnerCommand;
|
|
295
|
+
const runnerFailureSignatures = config.runnerFailureSignatures;
|
|
296
|
+
if (runner.length === 0 && runnerFailureSignatures.length > 0) throw new Error("sandbox-local: runnerFailureSignatures requires runnerCommand");
|
|
297
|
+
if (runner.length > 0 && runnerFailureSignatures.length === 0) throw new Error("sandbox-local: runnerCommand requires at least one runnerFailureSignatures entry");
|
|
298
|
+
if (runnerFailureSignatures.some((signature) => signature.trim().length === 0 || /[\r\n]/u.test(signature))) throw new Error("sandbox-local: runnerFailureSignatures entries must be non-empty single-line strings");
|
|
299
|
+
this.runnerCommand = runner.length > 0 ? runner : void 0;
|
|
300
|
+
this.configuredRunnerFailureSignatures = runnerFailureSignatures;
|
|
301
|
+
this.probeTimeoutMs = config.probeTimeoutMs;
|
|
302
|
+
assertPositiveFinite("probeTimeoutMs", this.probeTimeoutMs);
|
|
303
|
+
ctx.effect(() => () => {
|
|
304
|
+
this.revokeAclGrants();
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Wrap `argv` in the selected runner's invocation for `policy` — the configured
|
|
309
|
+
* `runnerCommand` when present (the operator's assertion, no probe), else the platform
|
|
310
|
+
* chain's runner speaking its own profile dialect.
|
|
311
|
+
*
|
|
312
|
+
* @param argv - the exact argv the caller is about to spawn.
|
|
313
|
+
* @param policy - the file-effect policy this execution runs under.
|
|
314
|
+
* @returns the wrapped argv plus the selected backend's enforcement completeness, denial
|
|
315
|
+
* signatures, and structured runner-failure rules; throws the fail-closed
|
|
316
|
+
* `SANDBOX_UNAVAILABLE` error when the platform has no usable runner.
|
|
317
|
+
*/
|
|
318
|
+
confine(argv, policy) {
|
|
319
|
+
if (this.runnerCommand !== void 0) return {
|
|
320
|
+
argv: [
|
|
321
|
+
...this.runnerCommand,
|
|
322
|
+
...bwrapProfileArgs(policy),
|
|
323
|
+
"--",
|
|
324
|
+
...argv
|
|
325
|
+
],
|
|
326
|
+
enforcement: "full",
|
|
327
|
+
denialSignatures: DENIAL_SIGNATURES.runnerCommand,
|
|
328
|
+
runnerFailureRules: [{ fatalSignatures: this.configuredRunnerFailureSignatures }]
|
|
329
|
+
};
|
|
330
|
+
const selected = this.selectRunner(policy.mode);
|
|
331
|
+
return {
|
|
332
|
+
argv: [
|
|
333
|
+
...this.runnerArgv(selected.runner, policy),
|
|
334
|
+
"--",
|
|
335
|
+
...argv
|
|
336
|
+
],
|
|
337
|
+
enforcement: selected.enforcement,
|
|
338
|
+
denialSignatures: DENIAL_SIGNATURES[selected.runner],
|
|
339
|
+
runnerFailureRules: RUNNER_FAILURE_RULES[selected.runner]
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
/** The selected rung's runner invocation (program + profile arguments) for one policy. */
|
|
343
|
+
runnerArgv(runner, policy) {
|
|
344
|
+
switch (runner) {
|
|
345
|
+
case "bwrap": return ["bwrap", ...bwrapProfileArgs(policy)];
|
|
346
|
+
case "landlock": return [this.landlockLauncher(), ...landlockProfileArgs(policy)];
|
|
347
|
+
case "seatbelt": return [this.seatbeltExec(), ...seatbeltProfileArgs(policy)];
|
|
348
|
+
case "windows-acl": return this.windowsAclRunnerArgv(policy);
|
|
349
|
+
default: return assertNever(runner);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* The windows-acl runner argv for one policy. With a calling session (the
|
|
354
|
+
* policy's `sessionId`), the write grants are materialized once per server
|
|
355
|
+
* lifetime — the standing workspace-root grant per workspace and the
|
|
356
|
+
* revocable private-temp grant per session — and the runner receives
|
|
357
|
+
* `--write-sid` (the workspace-derived identity; its presence marks the
|
|
358
|
+
* seam-managed DACL contract) plus, under workspace-write, the session's
|
|
359
|
+
* PRIVATE temp subdirectory (derived from session id + workspace) — it
|
|
360
|
+
* grants nothing and revokes nothing. Agentless calls pass the ambient
|
|
361
|
+
* temp root and no `--write-sid`: the runner self-manages its DACLs.
|
|
362
|
+
* @param policy - the resolved per-call policy.
|
|
363
|
+
* @returns the runner invocation.
|
|
364
|
+
*/
|
|
365
|
+
windowsAclRunnerArgv(policy) {
|
|
366
|
+
const sessionId = policy.sessionId;
|
|
367
|
+
if (sessionId === void 0) return [
|
|
368
|
+
...this.windowsAclRunnerInvocation(),
|
|
369
|
+
"--workspace",
|
|
370
|
+
policy.workspaceRoot,
|
|
371
|
+
"--temp",
|
|
372
|
+
tmpdir(),
|
|
373
|
+
"--mode",
|
|
374
|
+
policy.mode
|
|
375
|
+
];
|
|
376
|
+
this.materializeAclGrant(sessionId, policy.workspaceRoot, policy.mode);
|
|
377
|
+
return [
|
|
378
|
+
...this.windowsAclRunnerInvocation(),
|
|
379
|
+
"--workspace",
|
|
380
|
+
policy.workspaceRoot,
|
|
381
|
+
"--temp",
|
|
382
|
+
policy.mode === "workspace-write" ? sessionTempDir(sessionId, policy.workspaceRoot) : tmpdir(),
|
|
383
|
+
"--mode",
|
|
384
|
+
policy.mode,
|
|
385
|
+
"--write-sid",
|
|
386
|
+
workspaceWriteSid(policy.workspaceRoot)
|
|
387
|
+
];
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Materialize the session's ACEs once per server lifetime: lazily at its
|
|
391
|
+
* first confined execution, reused for every later call (the map hits are
|
|
392
|
+
* the whole call). The write SID is the per-workspace identity derived
|
|
393
|
+
* from the workspace. Workspace-write grants the workspace root STANDING
|
|
394
|
+
* (the ACE outlives every session — the reuse cache) and the session's
|
|
395
|
+
* private temp subdirectory REVOCABLY — the directory is derived from
|
|
396
|
+
* session id + workspace, created here EXCLUSIVELY (a pre-existing entry
|
|
397
|
+
* or a reparse point fails the first confined run loudly, so the grant
|
|
398
|
+
* never lands on a foreign object); read-only materializes NOTHING — its
|
|
399
|
+
* token alone restricts every write, and the standing grant from an
|
|
400
|
+
* earlier workspace-write period is KEPT through a downgrade (never
|
|
401
|
+
* revoked): the read-only restricted token carries no write SID (the
|
|
402
|
+
* read-only list), so the ACE is inert there, while the map hit keeps the
|
|
403
|
+
* re-upgrade free of re-propagation. Fail-closed: a half-materialized
|
|
404
|
+
* temp grant is revoked before the error propagates.
|
|
405
|
+
* @param sessionId - the policy's calling-session identity.
|
|
406
|
+
* @param workspaceRoot - the resolved policy root.
|
|
407
|
+
* @param mode - the policy mode (grants exist only under workspace-write).
|
|
408
|
+
*/
|
|
409
|
+
materializeAclGrant(sessionId, workspaceRoot, mode) {
|
|
410
|
+
if (mode === "read-only") return;
|
|
411
|
+
const writeSid = workspaceWriteSid(workspaceRoot);
|
|
412
|
+
const tempDir = sessionTempDir(sessionId, workspaceRoot);
|
|
413
|
+
if (!this.workspaceGrants.has(workspaceRoot)) {
|
|
414
|
+
const grant = AclWriteGrant.create(writeSid);
|
|
415
|
+
try {
|
|
416
|
+
grant.add(workspaceRoot, true);
|
|
417
|
+
} catch (error) {
|
|
418
|
+
try {
|
|
419
|
+
grant.dispose();
|
|
420
|
+
} catch (cleanupError) {
|
|
421
|
+
throw new AggregateError([error, cleanupError], "sandbox-local windows-acl workspace grant failed and its cleanup also failed");
|
|
422
|
+
}
|
|
423
|
+
throw error;
|
|
424
|
+
}
|
|
425
|
+
this.workspaceGrants.set(workspaceRoot, grant);
|
|
426
|
+
}
|
|
427
|
+
if (this.tempGrants.has(sessionId)) return;
|
|
428
|
+
const grant = AclWriteGrant.create(writeSid);
|
|
429
|
+
let created = false;
|
|
430
|
+
try {
|
|
431
|
+
mkdirSync(tempDir);
|
|
432
|
+
created = true;
|
|
433
|
+
grant.add(tempDir);
|
|
434
|
+
} catch (error) {
|
|
435
|
+
if (created) rmSync(tempDir, {
|
|
436
|
+
recursive: true,
|
|
437
|
+
force: true
|
|
438
|
+
});
|
|
439
|
+
try {
|
|
440
|
+
grant.dispose();
|
|
441
|
+
} catch (cleanupError) {
|
|
442
|
+
throw new AggregateError([error, cleanupError], "sandbox-local windows-acl temp grant materialization failed and its cleanup also failed");
|
|
443
|
+
}
|
|
444
|
+
throw error;
|
|
445
|
+
}
|
|
446
|
+
this.tempGrants.set(sessionId, grant);
|
|
447
|
+
this.tempDirs.set(sessionId, tempDir);
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Dispose every write grant (provider dispose): the revocable temp ACEs
|
|
451
|
+
* are revoked, the private temp directories this provider created are
|
|
452
|
+
* removed, and every SID allocation is freed; the standing workspace ACEs
|
|
453
|
+
* stay (the reuse cache). Cleanup failures are reported, not thrown:
|
|
454
|
+
* cordis teardown must not be aborted by grant cleanup. A crash skips all
|
|
455
|
+
* of it — the next resume then fails loudly at the exclusive creation and
|
|
456
|
+
* OS temp hygiene (or manual removal) recovers.
|
|
457
|
+
*/
|
|
458
|
+
revokeAclGrants() {
|
|
459
|
+
if (this.workspaceGrants.size === 0 && this.tempGrants.size === 0) return;
|
|
460
|
+
const failures = [];
|
|
461
|
+
for (const grant of [...this.workspaceGrants.values(), ...this.tempGrants.values()]) try {
|
|
462
|
+
grant.dispose();
|
|
463
|
+
} catch (error) {
|
|
464
|
+
failures.push(error);
|
|
465
|
+
}
|
|
466
|
+
const rmTempDir = this.internals.rmTempDir ?? ((dir) => {
|
|
467
|
+
rmSync(dir, {
|
|
468
|
+
recursive: true,
|
|
469
|
+
force: true
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
for (const dir of this.tempDirs.values()) try {
|
|
473
|
+
rmTempDir(dir);
|
|
474
|
+
} catch (error) {
|
|
475
|
+
failures.push(error);
|
|
476
|
+
}
|
|
477
|
+
this.workspaceGrants.clear();
|
|
478
|
+
this.tempGrants.clear();
|
|
479
|
+
this.tempDirs.clear();
|
|
480
|
+
if (failures.length > 0) {
|
|
481
|
+
this.ctx.logger.warn(`sandbox-local: windows-acl grant cleanup completed with ${failures.length} failure(s)`);
|
|
482
|
+
for (const error of failures) this.ctx.logger.warn(error);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Resolve which runner confines commands, once, for the provider's
|
|
487
|
+
* lifetime: this platform's chain ({@link PLATFORM_CHAINS}), its sole
|
|
488
|
+
* candidate selected directly, multiple candidates arbitrated by
|
|
489
|
+
* functional probes in chain order. Fail closed when the platform has no
|
|
490
|
+
* chain or no candidate passes — the command never runs.
|
|
491
|
+
*/
|
|
492
|
+
selectRunner(mode) {
|
|
493
|
+
this.selectedRunner ??= this.chainVerdict();
|
|
494
|
+
if (this.selectedRunner === "unavailable") throw new SandboxUnavailableError(mode);
|
|
495
|
+
return this.selectedRunner;
|
|
496
|
+
}
|
|
497
|
+
/** Walk this platform's chain: sole candidate unprobed, several probed in order, none usable → unavailable. */
|
|
498
|
+
chainVerdict() {
|
|
499
|
+
const chain = this.internals.chain ?? PLATFORM_CHAINS[this.internals.platform ?? process.platform] ?? [];
|
|
500
|
+
const [first, ...rest] = chain;
|
|
501
|
+
if (first === void 0) return "unavailable";
|
|
502
|
+
if (rest.length === 0) return {
|
|
503
|
+
runner: first,
|
|
504
|
+
enforcement: STATIC_ENFORCEMENT[first]
|
|
505
|
+
};
|
|
506
|
+
for (const runner of chain) {
|
|
507
|
+
const enforcement = this.probeRunner(runner);
|
|
508
|
+
if (enforcement !== "unusable") return {
|
|
509
|
+
runner,
|
|
510
|
+
enforcement
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
return "unavailable";
|
|
514
|
+
}
|
|
515
|
+
/** One rung's functional probe (each at most once, via the chain walk). */
|
|
516
|
+
probeRunner(runner) {
|
|
517
|
+
switch (runner) {
|
|
518
|
+
case "bwrap": return (this.internals.probeBwrap ?? (() => defaultProbeBwrap(this.probeTimeoutMs)))() ? "full" : "unusable";
|
|
519
|
+
case "landlock": return (this.internals.probeLandlock ?? ((launcher) => probe(launcher, { timeoutMs: this.probeTimeoutMs })))(this.landlockLauncher());
|
|
520
|
+
case "seatbelt": return (this.internals.probeSeatbelt ?? ((exec) => defaultProbeSeatbelt(exec, this.probeTimeoutMs)))(this.seatbeltExec()) ? "full" : "unusable";
|
|
521
|
+
case "windows-acl": return (this.internals.probeWindowsAcl ?? (() => defaultProbeWindowsAcl(this.windowsAclRunnerInvocation(), this.probeTimeoutMs)))() ? "full" : "unusable";
|
|
522
|
+
default: return assertNever(runner);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
/** The Landlock launcher to probe and exec (test hook over the resolved one). */
|
|
526
|
+
landlockLauncher() {
|
|
527
|
+
return this.internals.landlockLauncher ?? launcherPath();
|
|
528
|
+
}
|
|
529
|
+
/** The `sandbox-exec` executable to probe and exec (test hook over the system one). */
|
|
530
|
+
seatbeltExec() {
|
|
531
|
+
return this.internals.seatbeltExec ?? "sandbox-exec";
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* The windows-acl runner argv prefix: the built lib/runner.js entry when
|
|
535
|
+
* present (production), else the package source through tsx (development).
|
|
536
|
+
* The prefix stays `[node, runner, ...]` — a future native-exe runner keeps
|
|
537
|
+
* the same argv contract and only swaps these entries.
|
|
538
|
+
*/
|
|
539
|
+
windowsAclRunnerInvocation() {
|
|
540
|
+
const override = this.internals.windowsAclRunnerArgs;
|
|
541
|
+
if (override !== void 0) return override;
|
|
542
|
+
const builtEntry = this.internals.windowsAclRunnerEntry ?? fileURLToPath(import.meta.resolve("@deepseek-ai/dsh-sandbox-windows-acl/runner"));
|
|
543
|
+
if (existsSync(builtEntry)) return [process.execPath, builtEntry];
|
|
544
|
+
const sourceEntry = fileURLToPath(import.meta.resolve("@deepseek-ai/dsh-sandbox-windows-acl/src/runner.ts"));
|
|
545
|
+
return [
|
|
546
|
+
process.execPath,
|
|
547
|
+
"--import",
|
|
548
|
+
"tsx/esm",
|
|
549
|
+
sourceEntry
|
|
550
|
+
];
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
//#endregion
|
|
554
|
+
export { LocalSandboxProvider, LocalSandboxProvider as default, sessionTempDir };
|
package/lib/invariant.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region lib/types/invariant.js
|
|
2
|
+
/**
|
|
3
|
+
* Package-owned invariant companion for `@deepseek-ai/dsh-sandbox-local`.
|
|
4
|
+
* @module @deepseek-ai/dsh-sandbox-local/invariant
|
|
5
|
+
*/
|
|
6
|
+
const PACKAGE_NAME = "@deepseek-ai/dsh-sandbox-local";
|
|
7
|
+
/** Cordis companion plugin name. */
|
|
8
|
+
const name = "sandbox-local-invariant";
|
|
9
|
+
/** Service required before the companion can reserve package ownership. */
|
|
10
|
+
const inject = ["invariants"];
|
|
11
|
+
/**
|
|
12
|
+
* No runtime invariant: this package exposes no independent event sequence or mutable data relation
|
|
13
|
+
* beyond contracts enforced at its owning seam.
|
|
14
|
+
*/
|
|
15
|
+
const install = () => {};
|
|
16
|
+
/**
|
|
17
|
+
* Register this package's invariant companion.
|
|
18
|
+
* @param ctx - Cordis context carrying the invariant service.
|
|
19
|
+
* @returns the installed registration's disposer after setup succeeds.
|
|
20
|
+
*/
|
|
21
|
+
const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
22
|
+
//#endregion
|
|
23
|
+
export { apply, inject, name };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local sandbox backend. It selects the platform runner chain (Linux bwrap then
|
|
3
|
+
* Landlock; macOS Seatbelt; Windows the ACL restricted-token runner), functionally probes
|
|
4
|
+
* competing candidates once, and reports each wrap's enforcement and stderr
|
|
5
|
+
* classification facts. Missing or unusable confinement fails closed rather
|
|
6
|
+
* than returning the original argv.
|
|
7
|
+
*
|
|
8
|
+
* The windows-acl rung additionally owns the write grants: the write SID is
|
|
9
|
+
* the per-WORKSPACE identity derived from the canonical workspace path
|
|
10
|
+
* (`workspaceWriteSid`), and the private temp subdirectory is DERIVED per
|
|
11
|
+
* session (session id + workspace — nothing stored). The
|
|
12
|
+
* workspace-root ACE materializes once per workspace per server lifetime
|
|
13
|
+
* and STANDS (the cross-session reuse cache — the exact-ACE skip makes
|
|
14
|
+
* every later provision O(1) instead of re-propagating the tree per
|
|
15
|
+
* session); the private-temp ACEs are revoked on dispose. The runner
|
|
16
|
+
* receives `--write-sid` (the derived identity; its presence marks the
|
|
17
|
+
* seam-managed contract) and stops managing DACLs itself.
|
|
18
|
+
* @module @deepseek-ai/dsh-sandbox-local
|
|
19
|
+
*/
|
|
20
|
+
import { Context } from '@deepseek-ai/cordis';
|
|
21
|
+
import z from '@deepseek-ai/schemastery';
|
|
22
|
+
import { SandboxProvider } from '@deepseek-ai/dsh-sandbox';
|
|
23
|
+
import type { ConfinedArgv, SandboxEnforcement, SandboxPolicy } from '@deepseek-ai/dsh-sandbox';
|
|
24
|
+
import type { SessionId } from '@deepseek-ai/dsh-session';
|
|
25
|
+
/** Plugin config. All optional — `static Config` supplies the defaults. */
|
|
26
|
+
export interface Config {
|
|
27
|
+
/**
|
|
28
|
+
* Override the runner argv; bwrap-compatible profile arguments are appended. A
|
|
29
|
+
* non-empty override asserts full enforcement and skips built-in selection and
|
|
30
|
+
* probing. A runner that starts but refuses its profile must be identifiable by
|
|
31
|
+
* {@link runnerFailureSignatures}. Consumers classify a spawn rejection only after
|
|
32
|
+
* confirming the workdir is usable. `ENOENT` or `EACCES` identifies the runner when
|
|
33
|
+
* `error.path` equals argv[0] and `error.syscall` is `spawn` or `spawn <runner>`, or
|
|
34
|
+
* when `error.path` is absent and `error.syscall` is exactly `spawn <runner>`.
|
|
35
|
+
*/
|
|
36
|
+
runnerCommand?: string[];
|
|
37
|
+
/**
|
|
38
|
+
* Case-insensitive stderr substrings emitted when a configured
|
|
39
|
+
* {@link runnerCommand} refuses its profile before executing the wrapped
|
|
40
|
+
* command. Required and non-empty with `runnerCommand`; rejected without
|
|
41
|
+
* it. Each entry is a non-empty, single-line, case-insensitive substring
|
|
42
|
+
* covering the executable runner's own failure dialect.
|
|
43
|
+
*/
|
|
44
|
+
runnerFailureSignatures?: string[];
|
|
45
|
+
/** Positive timeout for each functional probe; zero would mean unbounded to Node. */
|
|
46
|
+
probeTimeoutMs?: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The session's private temp subdirectory: `<tmpdir>\dsh-<16 hex>`, derived
|
|
50
|
+
* from the session id and its workspace instead of stored. The same session
|
|
51
|
+
* and workspace always name the same directory — a resumed session
|
|
52
|
+
* re-grants it (the exact-ACE skip keeps that O(1)) — while a fork's
|
|
53
|
+
* different session id names a fresh one. The name is predictable to anyone
|
|
54
|
+
* who knows the session id (the confined command sees it as
|
|
55
|
+
* `DSH_SESSION_ID`), so the provider creates the directory EXCLUSIVELY and
|
|
56
|
+
* rejects reparse points: a pre-placed entry fails the first confined run
|
|
57
|
+
* loudly, and cannot redirect the grant onto a foreign object.
|
|
58
|
+
* @param sessionId - the policy's calling-session identity.
|
|
59
|
+
* @param workspaceRoot - the resolved policy root.
|
|
60
|
+
* @returns the session's private temp subdirectory path.
|
|
61
|
+
*/
|
|
62
|
+
export declare function sessionTempDir(sessionId: SessionId, workspaceRoot: string): string;
|
|
63
|
+
/** Test hook: inject probe verdicts / a fake launcher / a platform without real runners. */
|
|
64
|
+
export interface SandboxInternals {
|
|
65
|
+
/** Replaces `process.platform` for chain selection (exercise any platform's chain from any host). */
|
|
66
|
+
platform?: string;
|
|
67
|
+
/** Replaces the platform's chain wholesale (walk mechanics — e.g. probing a rung the product chains only reach unprobed). */
|
|
68
|
+
chain?: readonly SelectedRunner['runner'][];
|
|
69
|
+
/** Replaces the functional `bwrap` probe (the Linux chain's first rung). */
|
|
70
|
+
probeBwrap?: () => boolean;
|
|
71
|
+
/** Replaces the functional Landlock launcher probe (the Linux chain's second rung). */
|
|
72
|
+
probeLandlock?: (launcher: string) => SandboxEnforcement | 'unusable';
|
|
73
|
+
/** Replaces the functional Seatbelt probe (the darwin chain's sole rung — only consulted if that chain ever grows). */
|
|
74
|
+
probeSeatbelt?: (seatbeltExec: string) => boolean;
|
|
75
|
+
/** Replaces the resolved `landlock-run` launcher path (a fake launcher script). */
|
|
76
|
+
landlockLauncher?: string;
|
|
77
|
+
/** Replaces the `sandbox-exec` executable the probe and wraps invoke (a fake script). */
|
|
78
|
+
seatbeltExec?: string;
|
|
79
|
+
/** Replaces the resolved windows-acl runner argv prefix (a fake runner). */
|
|
80
|
+
windowsAclRunnerArgs?: string[];
|
|
81
|
+
/** Replaces the resolved windows-acl runner built entry path (a fake lib/runner.js location). */
|
|
82
|
+
windowsAclRunnerEntry?: string;
|
|
83
|
+
/** Replaces the functional windows-acl probe (the win32 chain's sole rung — only consulted if that chain ever grows). */
|
|
84
|
+
probeWindowsAcl?: () => boolean;
|
|
85
|
+
/** Replaces the private-temp-directory removal at provider dispose (a throwing fake exercises the cleanup-failure path). */
|
|
86
|
+
rmTempDir?: (path: string) => void;
|
|
87
|
+
}
|
|
88
|
+
/** The chain's verdict: which runner confines, and how completely it enforces. */
|
|
89
|
+
type SelectedRunner = {
|
|
90
|
+
runner: 'bwrap' | 'landlock' | 'seatbelt' | 'windows-acl';
|
|
91
|
+
enforcement: SandboxEnforcement;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Local process-sandbox provider. Registers as `ctx.sandbox`. Caches the
|
|
95
|
+
* chain verdict and, on the windows-acl rung, the write grants
|
|
96
|
+
* ({@link AclWriteGrant}: the standing workspace-root grant per workspace
|
|
97
|
+
* and the revocable private-temp grant per session, the latter revoked on
|
|
98
|
+
* provider dispose); the one-time probes spawn nothing else.
|
|
99
|
+
*/
|
|
100
|
+
export declare class LocalSandboxProvider extends SandboxProvider {
|
|
101
|
+
static Config: z<Config>;
|
|
102
|
+
/** Test hook (mirrors the bash executors' `internals`). */
|
|
103
|
+
internals: SandboxInternals;
|
|
104
|
+
private readonly runnerCommand;
|
|
105
|
+
private readonly configuredRunnerFailureSignatures;
|
|
106
|
+
private readonly probeTimeoutMs;
|
|
107
|
+
/** Cached chain verdict; undefined until the first confined wrap needs it. */
|
|
108
|
+
private selectedRunner;
|
|
109
|
+
/**
|
|
110
|
+
* Server-lifetime write grants (windows-acl rung): the STANDING
|
|
111
|
+
* workspace-root grant per workspace (its ACE is the cross-session reuse
|
|
112
|
+
* cache and outlives the provider — never revoked) and the REVOCABLE
|
|
113
|
+
* private-temp grant per session (revoked on provider dispose).
|
|
114
|
+
*/
|
|
115
|
+
private readonly workspaceGrants;
|
|
116
|
+
private readonly tempGrants;
|
|
117
|
+
/** Session id → the private temp directory this provider created (removed on dispose). */
|
|
118
|
+
private readonly tempDirs;
|
|
119
|
+
constructor(ctx: Context, config: Config);
|
|
120
|
+
/**
|
|
121
|
+
* Wrap `argv` in the selected runner's invocation for `policy` — the configured
|
|
122
|
+
* `runnerCommand` when present (the operator's assertion, no probe), else the platform
|
|
123
|
+
* chain's runner speaking its own profile dialect.
|
|
124
|
+
*
|
|
125
|
+
* @param argv - the exact argv the caller is about to spawn.
|
|
126
|
+
* @param policy - the file-effect policy this execution runs under.
|
|
127
|
+
* @returns the wrapped argv plus the selected backend's enforcement completeness, denial
|
|
128
|
+
* signatures, and structured runner-failure rules; throws the fail-closed
|
|
129
|
+
* `SANDBOX_UNAVAILABLE` error when the platform has no usable runner.
|
|
130
|
+
*/
|
|
131
|
+
confine(argv: readonly string[], policy: SandboxPolicy): ConfinedArgv;
|
|
132
|
+
/** The selected rung's runner invocation (program + profile arguments) for one policy. */
|
|
133
|
+
private runnerArgv;
|
|
134
|
+
/**
|
|
135
|
+
* The windows-acl runner argv for one policy. With a calling session (the
|
|
136
|
+
* policy's `sessionId`), the write grants are materialized once per server
|
|
137
|
+
* lifetime — the standing workspace-root grant per workspace and the
|
|
138
|
+
* revocable private-temp grant per session — and the runner receives
|
|
139
|
+
* `--write-sid` (the workspace-derived identity; its presence marks the
|
|
140
|
+
* seam-managed DACL contract) plus, under workspace-write, the session's
|
|
141
|
+
* PRIVATE temp subdirectory (derived from session id + workspace) — it
|
|
142
|
+
* grants nothing and revokes nothing. Agentless calls pass the ambient
|
|
143
|
+
* temp root and no `--write-sid`: the runner self-manages its DACLs.
|
|
144
|
+
* @param policy - the resolved per-call policy.
|
|
145
|
+
* @returns the runner invocation.
|
|
146
|
+
*/
|
|
147
|
+
private windowsAclRunnerArgv;
|
|
148
|
+
/**
|
|
149
|
+
* Materialize the session's ACEs once per server lifetime: lazily at its
|
|
150
|
+
* first confined execution, reused for every later call (the map hits are
|
|
151
|
+
* the whole call). The write SID is the per-workspace identity derived
|
|
152
|
+
* from the workspace. Workspace-write grants the workspace root STANDING
|
|
153
|
+
* (the ACE outlives every session — the reuse cache) and the session's
|
|
154
|
+
* private temp subdirectory REVOCABLY — the directory is derived from
|
|
155
|
+
* session id + workspace, created here EXCLUSIVELY (a pre-existing entry
|
|
156
|
+
* or a reparse point fails the first confined run loudly, so the grant
|
|
157
|
+
* never lands on a foreign object); read-only materializes NOTHING — its
|
|
158
|
+
* token alone restricts every write, and the standing grant from an
|
|
159
|
+
* earlier workspace-write period is KEPT through a downgrade (never
|
|
160
|
+
* revoked): the read-only restricted token carries no write SID (the
|
|
161
|
+
* read-only list), so the ACE is inert there, while the map hit keeps the
|
|
162
|
+
* re-upgrade free of re-propagation. Fail-closed: a half-materialized
|
|
163
|
+
* temp grant is revoked before the error propagates.
|
|
164
|
+
* @param sessionId - the policy's calling-session identity.
|
|
165
|
+
* @param workspaceRoot - the resolved policy root.
|
|
166
|
+
* @param mode - the policy mode (grants exist only under workspace-write).
|
|
167
|
+
*/
|
|
168
|
+
private materializeAclGrant;
|
|
169
|
+
/**
|
|
170
|
+
* Dispose every write grant (provider dispose): the revocable temp ACEs
|
|
171
|
+
* are revoked, the private temp directories this provider created are
|
|
172
|
+
* removed, and every SID allocation is freed; the standing workspace ACEs
|
|
173
|
+
* stay (the reuse cache). Cleanup failures are reported, not thrown:
|
|
174
|
+
* cordis teardown must not be aborted by grant cleanup. A crash skips all
|
|
175
|
+
* of it — the next resume then fails loudly at the exclusive creation and
|
|
176
|
+
* OS temp hygiene (or manual removal) recovers.
|
|
177
|
+
*/
|
|
178
|
+
private revokeAclGrants;
|
|
179
|
+
/**
|
|
180
|
+
* Resolve which runner confines commands, once, for the provider's
|
|
181
|
+
* lifetime: this platform's chain ({@link PLATFORM_CHAINS}), its sole
|
|
182
|
+
* candidate selected directly, multiple candidates arbitrated by
|
|
183
|
+
* functional probes in chain order. Fail closed when the platform has no
|
|
184
|
+
* chain or no candidate passes — the command never runs.
|
|
185
|
+
*/
|
|
186
|
+
private selectRunner;
|
|
187
|
+
/** Walk this platform's chain: sole candidate unprobed, several probed in order, none usable → unavailable. */
|
|
188
|
+
private chainVerdict;
|
|
189
|
+
/** One rung's functional probe (each at most once, via the chain walk). */
|
|
190
|
+
private probeRunner;
|
|
191
|
+
/** The Landlock launcher to probe and exec (test hook over the resolved one). */
|
|
192
|
+
private landlockLauncher;
|
|
193
|
+
/** The `sandbox-exec` executable to probe and exec (test hook over the system one). */
|
|
194
|
+
private seatbeltExec;
|
|
195
|
+
/**
|
|
196
|
+
* The windows-acl runner argv prefix: the built lib/runner.js entry when
|
|
197
|
+
* present (production), else the package source through tsx (development).
|
|
198
|
+
* The prefix stays `[node, runner, ...]` — a future native-exe runner keeps
|
|
199
|
+
* the same argv contract and only swaps these entries.
|
|
200
|
+
*/
|
|
201
|
+
private windowsAclRunnerInvocation;
|
|
202
|
+
}
|
|
203
|
+
export default LocalSandboxProvider;
|
|
204
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package-owned invariant companion for `@deepseek-ai/dsh-sandbox-local`.
|
|
3
|
+
* @module @deepseek-ai/dsh-sandbox-local/invariant
|
|
4
|
+
*/
|
|
5
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
6
|
+
/** Cordis companion plugin name. */
|
|
7
|
+
export declare const name = "sandbox-local-invariant";
|
|
8
|
+
/** Service required before the companion can reserve package ownership. */
|
|
9
|
+
export declare const inject: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Register this package's invariant companion.
|
|
12
|
+
* @param ctx - Cordis context carrying the invariant service.
|
|
13
|
+
* @returns the installed registration's disposer after setup succeeds.
|
|
14
|
+
*/
|
|
15
|
+
export declare const apply: (ctx: Context) => Promise<() => void>;
|
|
16
|
+
//# sourceMappingURL=invariant.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal platform-profile builders for the local sandbox provider.
|
|
3
|
+
*
|
|
4
|
+
* @module @deepseek-ai/dsh-sandbox-local/profiles
|
|
5
|
+
*/
|
|
6
|
+
import type { SandboxPolicy } from '@deepseek-ai/dsh-sandbox';
|
|
7
|
+
/**
|
|
8
|
+
* Build the bwrap profile arguments for one file-effect policy.
|
|
9
|
+
* @param policy - file-effect policy to express as bwrap mounts.
|
|
10
|
+
* @returns profile arguments before the trailing separator and command argv.
|
|
11
|
+
*/
|
|
12
|
+
export declare function bwrapProfileArgs(policy: SandboxPolicy): string[];
|
|
13
|
+
/**
|
|
14
|
+
* Build the Landlock launcher grants for one file-effect policy.
|
|
15
|
+
* @param policy - file-effect policy to express as Landlock allow-list grants.
|
|
16
|
+
* @returns launcher grant arguments before the trailing separator and command argv.
|
|
17
|
+
*/
|
|
18
|
+
export declare function landlockProfileArgs(policy: SandboxPolicy): string[];
|
|
19
|
+
/**
|
|
20
|
+
* Build the sandbox-exec arguments and SBPL profile for one policy. The
|
|
21
|
+
* writable roots come from the shared {@link writableRoots} helper (canonical,
|
|
22
|
+
* deduplicated) so the Seatbelt grant and the in-process fs fence
|
|
23
|
+
* (`@deepseek-ai/dsh-fs-sandbox`) can never drift apart.
|
|
24
|
+
* @param policy - file-effect policy to express as an SBPL profile.
|
|
25
|
+
* @returns sandbox-exec arguments before the trailing separator and command argv.
|
|
26
|
+
*/
|
|
27
|
+
export declare function seatbeltProfileArgs(policy: SandboxPolicy): string[];
|
|
28
|
+
//# sourceMappingURL=profiles.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deepseek-ai/dsh-sandbox-local",
|
|
3
|
+
"description": "Local process-sandbox backends for the DeepSeek Harness sandbox seam: bwrap, the npm-distributed landlock-run launcher, macOS Seatbelt, or the Windows ACL restricted-token runner — functionally probed, fail-closed",
|
|
4
|
+
"version": "0.0.1-rc.1",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "restricted"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/deepseek-ai/deepseek-harness.git",
|
|
11
|
+
"directory": "packages/sandbox/sandbox-local"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"types": "lib/types/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./lib/types/index.d.ts",
|
|
19
|
+
"default": "./lib/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./invariant": {
|
|
22
|
+
"types": "./lib/types/invariant.d.ts",
|
|
23
|
+
"default": "./lib/invariant.js"
|
|
24
|
+
},
|
|
25
|
+
"./src/*": "./src/*",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"lib/index.js",
|
|
30
|
+
"lib/invariant.js",
|
|
31
|
+
"lib/types/**/*.d.ts"
|
|
32
|
+
],
|
|
33
|
+
"license": "BSD-3-Clause",
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@deepseek-ai/dsh-invariants": "^0.0.1-rc.1",
|
|
36
|
+
"@deepseek-ai/dsh-llm": "^0.0.1-rc.1",
|
|
37
|
+
"@deepseek-ai/dsh-sandbox": "^0.0.1-rc.1",
|
|
38
|
+
"@deepseek-ai/dsh-session": "^0.0.1-rc.1",
|
|
39
|
+
"@deepseek-ai/cordis": "^4.0.1-rc.1"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@deepseek-ai/node-addon-landlock-run": "0.0.1",
|
|
43
|
+
"@deepseek-ai/schemastery": "^3.18.1-rc.1",
|
|
44
|
+
"@deepseek-ai/dsh-sandbox-windows-acl": "^0.0.1-rc.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@deepseek-ai/dsh-invariants": "^0.0.1-rc.1",
|
|
48
|
+
"@deepseek-ai/dsh-llm": "^0.0.1-rc.1",
|
|
49
|
+
"@deepseek-ai/dsh-session": "^0.0.1-rc.1",
|
|
50
|
+
"@deepseek-ai/cordis": "^4.0.1-rc.1",
|
|
51
|
+
"@deepseek-ai/dsh-sandbox": "^0.0.1-rc.1"
|
|
52
|
+
}
|
|
53
|
+
}
|