@morlay/configurable-sandbox 0.0.0-stage → 0.2.0-alpha.0

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.
@@ -0,0 +1,114 @@
1
+ // 进程沙箱:底座是官方 `LocalSandboxProvider`,本类在它的 `confine` 结果上追加本包的规则。
2
+ //
3
+ // 形态与 `@morlay/dsh-sandbox` 的 `SandboxDefault` 一致:`fromCordis(契约, 上游类)` 包装。上游类是
4
+ // cordis `Service` —— `mount` 用根 ctx 构造它(`super(ctx, "sandbox")` 在那里注册),所以装配面
5
+ // `without(SandboxDefault)` 之后本行是 `sandbox` 的唯一提供者。规则与配置经 `mount` 的 deps 交给上游实例:
6
+ // 它按 `ctx.get("sandboxRules")` 读到的是这份契约面,不是全局单例。
7
+ import type { ConfinedArgv } from "@morlay/dsh-sandbox/contract";
8
+
9
+ /** 基类 `confine` 的第二个参数类型:按覆盖签名推导,避免与契约别名在可选字段上的差异。 */
10
+ type SandboxConfinePolicy = Parameters<LocalSandboxProvider["confine"]>[1];
11
+ import type { LocalSandboxConfig as UpstreamSandboxConfig } from "@morlay/dsh-sandbox";
12
+ import { LocalSandboxProvider } from "@morlay/dsh-sandbox";
13
+ import { Sandbox } from "@morlay/dsh-sandbox";
14
+ import { inject, Injectable, Logger, type Context } from "@morlay/zephyr";
15
+ import { fromCordis } from "@morlay/zephyr/compat";
16
+ import { SandboxRules, type SandboxSettings } from "./contract/sandbox-rules.contract.ts";
17
+ import { extendConfinedArgv } from "./dialects.ts";
18
+ import {
19
+ compileRules,
20
+ isEmptyRules,
21
+ withoutAllowRoots,
22
+ type CompiledRules,
23
+ type RuleSource,
24
+ } from "./rules.ts";
25
+
26
+ // 上游构造面的 ctx 类型:直接推导,不把 `@deepseek-ai/cordis` 引成依赖。
27
+ type UpstreamSandboxContext = ConstructorParameters<typeof LocalSandboxProvider>[0];
28
+
29
+ // deps 上的名字:上游实例按 `ctx.get("sandboxRules")` 读它(`fromCordis` 的 deps 替换读面)。
30
+ const RULES_DEP = "sandboxRules";
31
+
32
+ // 交给官方基类的配置:它认的三个字段(其余字段是本包自己的)。
33
+ export function upstreamConfigOf(settings: SandboxSettings) {
34
+ return {
35
+ runnerCommand: [...settings.runnerCommand],
36
+ runnerFailureSignatures: [...settings.runnerFailureSignatures],
37
+ probeTimeoutMs: settings.probeTimeoutMs,
38
+ };
39
+ }
40
+
41
+ // 官方提供者 + 规则追加。构造面与官方一致(`(ctx, config)`),规则从 deps 注入的读面取。
42
+ export class ConfigurableSandboxProvider extends LocalSandboxProvider {
43
+ // `#` 私有:与包在外面的 Provider 字段不会撞名(TS 的 `private` 会)。
44
+ private readonly rules: SandboxRules;
45
+
46
+ private readonly compiled = new Map<string, CompiledRules>();
47
+
48
+ constructor(ctx: UpstreamSandboxContext, config: UpstreamSandboxConfig) {
49
+ super(ctx, config);
50
+ this.rules = (ctx as unknown as { get(name: string, strict?: boolean): unknown }).get(
51
+ RULES_DEP,
52
+ ) as SandboxRules;
53
+ }
54
+
55
+ override async confine(
56
+ argv: readonly string[],
57
+ policy: SandboxConfinePolicy,
58
+ signal?: AbortSignal,
59
+ ): Promise<ConfinedArgv> {
60
+ const confined = await super.confine(argv, policy, signal);
61
+ const rules = this.rulesFor(policy.workspaceRoot);
62
+ // `rw` 只在 `workspace-write` 下生效:显式选定的只读边界不因额外可写根放松。
63
+ const effective = policy.mode === "workspace-write" ? rules : withoutAllowRoots(rules);
64
+ if (isEmptyRules(effective)) return confined;
65
+ return { ...confined, argv: extendConfinedArgv(confined.argv, effective) };
66
+ }
67
+
68
+ // 规则源是启动时读一次的那份:按工作区缓存编译结果。
69
+ private rulesFor(workspaceRoot: string): CompiledRules {
70
+ const cached = this.compiled.get(workspaceRoot);
71
+ if (cached !== undefined) return cached;
72
+ const compiled = compileRules(this.rules.source(), workspaceRoot);
73
+ this.compiled.set(workspaceRoot, compiled);
74
+ return compiled;
75
+ }
76
+ }
77
+
78
+ @Injectable()
79
+ export class ConfigurableSandbox extends fromCordis(Sandbox, ConfigurableSandboxProvider) {
80
+ // 注入字段必须是**普通属性**:装配期回填注入走 Object.keys(instance),# 私有字段不可见。
81
+ private readonly rulesSource = inject(SandboxRules);
82
+
83
+ private readonly loggerFace = inject(Logger);
84
+
85
+ override async onInit(ctx: Context): Promise<void> {
86
+ await this.mount(ctx, upstreamConfigOf(this.rulesSource.settings()), {
87
+ [RULES_DEP]: this.rulesSource,
88
+ });
89
+ this.warnAboutDegradedRules(this.rulesSource.source());
90
+ }
91
+
92
+ // 装配期告警:平台表达不了的条目点名说明(不静默失效)。
93
+ private warnAboutDegradedRules(rules: RuleSource): void {
94
+ const grants = rules.allowWrite.length > 0;
95
+ const readOnly = rules.readOnly.length > 0;
96
+ const denials = rules.deny.length > 0;
97
+ if (!grants && !readOnly && !denials) return;
98
+ if (process.platform === "darwin") return;
99
+ if (readOnly || denials) {
100
+ this.loggerFace.warn(
101
+ `configurable-sandbox: "r-" / "--" entries cannot be fully enforced for confined subprocesses on ${process.platform} ` +
102
+ '(Seatbelt enforces both; bwrap binds the path read-only, so "--" degrades to write-only; ' +
103
+ "Landlock and the Windows ACL runner cannot express a subpath rule at all) " +
104
+ "— tools that read through ctx.fs stay covered",
105
+ );
106
+ }
107
+ if (grants && process.platform === "win32") {
108
+ this.loggerFace.warn(
109
+ 'configurable-sandbox: "rw" entries cannot be granted to confined subprocesses on win32; ' +
110
+ "ctx.fs covers the extra roots, the Windows ACL runner does not",
111
+ );
112
+ }
113
+ }
114
+ }