@backendkit-labs/agent-sandbox 0.0.0-stage → 0.1.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.
- package/README.md +116 -3
- package/dist/backends/docker.d.ts +56 -0
- package/dist/backends/docker.d.ts.map +1 -0
- package/dist/backends/docker.js +140 -0
- package/dist/backends/docker.js.map +1 -0
- package/dist/backends/none.d.ts +13 -0
- package/dist/backends/none.d.ts.map +1 -0
- package/dist/backends/none.js +28 -0
- package/dist/backends/none.js.map +1 -0
- package/dist/backends/types.d.ts +32 -0
- package/dist/backends/types.d.ts.map +1 -0
- package/dist/backends/types.js +3 -0
- package/dist/backends/types.js.map +1 -0
- package/dist/env.d.ts +14 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +32 -0
- package/dist/env.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/policy.d.ts +65 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +116 -0
- package/dist/policy.js.map +1 -0
- package/dist/presets.d.ts +27 -0
- package/dist/presets.d.ts.map +1 -0
- package/dist/presets.js +37 -0
- package/dist/presets.js.map +1 -0
- package/dist/process.d.ts +24 -0
- package/dist/process.d.ts.map +1 -0
- package/dist/process.js +77 -0
- package/dist/process.js.map +1 -0
- package/dist/service.d.ts +25 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +112 -0
- package/dist/service.js.map +1 -0
- package/package.json +46 -6
package/README.md
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @backendkit-labs/agent-sandbox
|
|
2
|
+
|
|
3
|
+
Confinement and permissions for what an agent runs. One policy decides, per call, how much of the host a command may touch; swappable backends enforce it; if the required confinement cannot be enforced on this host, the call **fails** instead of running unconfined.
|
|
4
|
+
|
|
5
|
+
> **Wired into the coding engine.** `createCodingEngine({ sandbox: { preset: 'ask' } })` (agent-coding) or `createBaseEngine({ sandbox })` (agent-core) put every `run_command` behind this service. Without `sandbox`, `run_command` keeps its old behaviour: the host, or the deprecated `BK_RUN_COMMAND_SANDBOX=docker`, now translated to the equivalent policy of this package.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @backendkit-labs/agent-sandbox
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Modes
|
|
12
|
+
|
|
13
|
+
| Mode | Meaning |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `read-only` | nothing can be written, anywhere |
|
|
16
|
+
| `workspace-write` | writes only inside the workspace |
|
|
17
|
+
| `danger-full-access` | no confinement |
|
|
18
|
+
|
|
19
|
+
Each call also has a network setting: `none` (default for the first two modes) or `allow`.
|
|
20
|
+
|
|
21
|
+
## Fail closed
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createSandboxPolicy, createSandboxService, DockerBackend, NoneBackend } from '@backendkit-labs/agent-sandbox';
|
|
25
|
+
|
|
26
|
+
const sandbox = createSandboxService({
|
|
27
|
+
policy: createSandboxPolicy({ preset: 'ask' }), // workspace-write, network none
|
|
28
|
+
backends: [new DockerBackend({ image: 'node:22' }), new NoneBackend()],
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const out = await sandbox.run({ agentId: 'coder', sessionId: 's1' }, { tool: 'run_command', command: 'npm test', cwd: dir });
|
|
32
|
+
if (!out.ok) {
|
|
33
|
+
out.code; // 'SANDBOX_DENIED' | 'SANDBOX_UNAVAILABLE'
|
|
34
|
+
out.reason; // why, and which backend was skipped for what reason
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The service picks the first backend that can really **enforce** the mode:
|
|
39
|
+
|
|
40
|
+
- `docker` enforces `read-only` and `workspace-write` (never `danger-full-access`: a container is not full access). If the daemon does not answer you get `SANDBOX_UNAVAILABLE`, not a run on the host.
|
|
41
|
+
- `none` (the host) claims only `danger-full-access` with the network open. Checking that a directory is inside the workspace is not confinement, so it does not claim anything stricter.
|
|
42
|
+
|
|
43
|
+
## Presets
|
|
44
|
+
|
|
45
|
+
| Preset | Mode | Needs a human for |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| `read-only` | `read-only` | nothing (nothing writes) |
|
|
48
|
+
| `ask` (default) | `workspace-write` | file writes and commands |
|
|
49
|
+
| `auto-edit` | `workspace-write` | commands |
|
|
50
|
+
| `full` | `danger-full-access` | nothing |
|
|
51
|
+
|
|
52
|
+
`auto-edit` differs from the SPEC-0006 table: the command allowlist of `run_command` is still a hard block, so "auto-edit" means edits run and every command asks. `needsApproval(level, kind)` and `matchPreset(mode, approval)` are exported; a combination that matches no preset is reported as `custom`.
|
|
53
|
+
|
|
54
|
+
## The policy is data
|
|
55
|
+
|
|
56
|
+
`SandboxPolicyConfig` is plain JSON, so it can come from a file or a control request:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const policy = createSandboxPolicy({
|
|
60
|
+
preset: 'ask',
|
|
61
|
+
roots: ['/data/shared'], // extra working directories besides the workspace
|
|
62
|
+
rules: [ // the first match wins
|
|
63
|
+
{ agentId: 'reviewer', mode: 'read-only' },
|
|
64
|
+
{ tool: 'run_command', network: 'allow' },
|
|
65
|
+
],
|
|
66
|
+
env: { allow: ['GH_TOKEN'] },
|
|
67
|
+
writeTools: ['write_file', 'edit_file'], // refused outright while read-only
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
policy.snapshot(); // a copy of the configuration
|
|
71
|
+
policy.set(newConfig); // validated; takes effect on the NEXT call, never mid-call; throws and keeps the old one if invalid
|
|
72
|
+
policy.version; // increments on every successful set
|
|
73
|
+
parsePolicyConfig(untrusted); // validates a YAML/JSON object and names the path of the problem
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## In an engine
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const engine = createCodingEngine({
|
|
80
|
+
providers, defaultProvider: 'deepseek', transport,
|
|
81
|
+
sandbox: { preset: 'ask' }, // or { preset: 'read-only' | 'auto-edit' | 'full', policy: {...}, backends: ['docker', 'none'], docker: { image: 'node:22' }, auditLog: 'logs/sandbox.ndjson' }
|
|
82
|
+
onToolApproval, // the preset says WHICH tools ask; this is who answers
|
|
83
|
+
});
|
|
84
|
+
(engine.sandbox as SandboxService).policy.set({ preset: 'read-only' }); // takes effect on the next call
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
- `run_command` goes through the service; an unmet confinement comes back as `Error: SANDBOX_UNAVAILABLE: …` and nothing runs.
|
|
88
|
+
- In `read-only` the engine refuses the write tools by name (`write_file`, `edit_file`), whatever the approval gate says. The memory tools keep working: they write the agent's own store, outside the workspace.
|
|
89
|
+
- The preset replaces the engine's built-in list of tools that ask. **If a tool needs approval and no `onToolApproval` is set, it is refused** (`SANDBOX_DENIED`): an unattended flow, trigger or fleet agent can never run what nobody approved.
|
|
90
|
+
- Every decision is a `sandbox` event on the transport and a line in the engine's audit log.
|
|
91
|
+
- File tools (`write_file`, `edit_file`) still enforce the workspace with their own path check; moving them behind the service is the next delivery.
|
|
92
|
+
- Containers run as your uid:gid on Linux and macOS (not root), so the files they create are yours.
|
|
93
|
+
|
|
94
|
+
## Observability
|
|
95
|
+
|
|
96
|
+
Every decision (allowed, denied, unavailable) is structured:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
sandbox.onDecision(d => console.log(d.agentId, d.tool, d.mode, d.backend, d.outcome, d.reason));
|
|
100
|
+
createSandboxService({ policy, backends, auditLog: 'logs/sandbox.ndjson' }); // one JSON line per decision, in order
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Environment hygiene
|
|
104
|
+
|
|
105
|
+
Host processes start from a scrubbed environment: names matching `KEY`, `SECRET`, `TOKEN`, `PASSWORD`, `PASSWD` or `CREDENTIAL` (case-insensitive) are dropped. It is a denylist on purpose (Windows needs `SystemRoot`, `ComSpec`, `PATHEXT`). Let specific ones through with `env.allow` (`GH_TOKEN`, or `DEEPSEEK_*`), drop more with `env.deny`. Containers never inherit the host environment.
|
|
106
|
+
|
|
107
|
+
## What it does not confine
|
|
108
|
+
|
|
109
|
+
- **Tools that start processes on their own.** Only calls that go through `ctx.sandbox.run(...)` are confined (`run_command` does). A custom or MCP tool that spawns a process itself is outside the sandbox: write it against `ctx.sandbox`.
|
|
110
|
+
- **`read-only` refuses tools by name.** It refuses exactly the tools listed in `writeTools` (the coding set lists `write_file` and `edit_file`). MCP tools and your own tools that write are not blocked unless you list them.
|
|
111
|
+
- **File tools** still enforce the workspace with their own path check; they move behind the service in the next delivery.
|
|
112
|
+
- **Several engines may share one service**: each engine reports only the decisions of its own calls on its transport and audit log. `sandbox.onDecision(...)` on the service sees all of them.
|
|
113
|
+
|
|
114
|
+
## Not here yet
|
|
115
|
+
|
|
116
|
+
`request_escalation` (S2), file tools and terminal through the service (S3), `bwrap`/`landlock`, Windows and `ssh` backends (S4), private temp directories and waiting for child exit after a kill.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { SandboxMode, SandboxNetwork } from '@backendkit-labs/agent-core';
|
|
2
|
+
import type { BackendExecRequest, SandboxBackend } from './types';
|
|
3
|
+
export interface DockerBackendOptions {
|
|
4
|
+
/** Docker CLI to call. Default `docker`. */
|
|
5
|
+
bin?: string;
|
|
6
|
+
/** Image the command runs in; it must ship the tools the command needs. Default `node:22`. */
|
|
7
|
+
image?: string;
|
|
8
|
+
/** Memory limit. Default `2g`. */
|
|
9
|
+
memory?: string;
|
|
10
|
+
/** Process limit. Default 512. */
|
|
11
|
+
pidsLimit?: number;
|
|
12
|
+
/**
|
|
13
|
+
* User the command runs as inside the container (`uid:gid`). Default on POSIX: the uid and gid of this process,
|
|
14
|
+
* so the command is not root and the files it creates belong to you. With all capabilities dropped, a root
|
|
15
|
+
* container cannot touch host files owned by another user, which is the usual case on Linux. Pass `false`
|
|
16
|
+
* to leave the image's default user. On Windows there is no uid and nothing is passed.
|
|
17
|
+
*/
|
|
18
|
+
user?: string | false;
|
|
19
|
+
/** Replace the availability probe (tests, or an already known state). */
|
|
20
|
+
probe?: () => Promise<boolean>;
|
|
21
|
+
/** How long a negative probe result is trusted before asking the daemon again. Default 5 s. */
|
|
22
|
+
negativeTtlMs?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Arguments of `docker run` for one confined command: all capabilities dropped, no new privileges,
|
|
26
|
+
* memory and process limits, only the workspace mounted (read-only in `read-only` mode).
|
|
27
|
+
*/
|
|
28
|
+
export declare function buildDockerArgs(name: string, req: Pick<BackendExecRequest, 'command' | 'cwd' | 'workspace' | 'mode' | 'network'>, opts: {
|
|
29
|
+
image: string;
|
|
30
|
+
memory: string;
|
|
31
|
+
pidsLimit: number;
|
|
32
|
+
user?: string;
|
|
33
|
+
}): string[];
|
|
34
|
+
/**
|
|
35
|
+
* Container confinement. `read-only` mounts the workspace read-only (and the container root); `workspace-write`
|
|
36
|
+
* writes only inside the workspace; the network is `none` unless the policy allows it. It never claims
|
|
37
|
+
* `danger-full-access` (a container is not full access), and when the daemon does not answer the service
|
|
38
|
+
* reports SANDBOX_UNAVAILABLE instead of running on the host.
|
|
39
|
+
*/
|
|
40
|
+
export declare class DockerBackend implements SandboxBackend {
|
|
41
|
+
readonly name = "docker";
|
|
42
|
+
private readonly bin;
|
|
43
|
+
private readonly image;
|
|
44
|
+
private readonly memory;
|
|
45
|
+
private readonly pidsLimit;
|
|
46
|
+
private readonly user;
|
|
47
|
+
private readonly probe;
|
|
48
|
+
private readonly negativeTtlMs;
|
|
49
|
+
private cached?;
|
|
50
|
+
constructor(opts?: DockerBackendOptions);
|
|
51
|
+
supports(mode: SandboxMode, _network: SandboxNetwork): boolean;
|
|
52
|
+
available(): Promise<boolean>;
|
|
53
|
+
unavailableReason(): string;
|
|
54
|
+
exec(req: BackendExecRequest): Promise<import("@backendkit-labs/agent-core").SandboxExecResult>;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL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IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAqB;IAC1C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAyB;IAC/C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,MAAM,CAAC,CAA8B;gBAEjC,IAAI,GAAE,oBAAyB;IAW3C,QAAQ,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,cAAc,GAAG,OAAO;IAIxD,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAQnC,iBAAiB,IAAI,MAAM;IAI3B,IAAI,CAAC,GAAG,EAAE,kBAAkB;CAW/B"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.DockerBackend = void 0;
|
|
37
|
+
exports.buildDockerArgs = buildDockerArgs;
|
|
38
|
+
const child_process_1 = require("child_process");
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const process_1 = require("../process");
|
|
41
|
+
function defaultProbe(bin) {
|
|
42
|
+
return new Promise(resolve => {
|
|
43
|
+
let done = false;
|
|
44
|
+
const finish = (ok) => { if (!done) {
|
|
45
|
+
done = true;
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
resolve(ok);
|
|
48
|
+
} };
|
|
49
|
+
let proc;
|
|
50
|
+
try {
|
|
51
|
+
proc = (0, child_process_1.spawn)(bin, ['version', '--format', '{{.Server.Version}}'], { stdio: 'ignore' });
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
resolve(false);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const timer = setTimeout(() => { try {
|
|
58
|
+
proc.kill();
|
|
59
|
+
}
|
|
60
|
+
catch { /* gone */ } finish(false); }, 4000);
|
|
61
|
+
proc.on('error', () => finish(false));
|
|
62
|
+
proc.on('close', code => finish(code === 0));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Arguments of `docker run` for one confined command: all capabilities dropped, no new privileges,
|
|
67
|
+
* memory and process limits, only the workspace mounted (read-only in `read-only` mode).
|
|
68
|
+
*/
|
|
69
|
+
function buildDockerArgs(name, req, opts) {
|
|
70
|
+
const rel = path.relative(req.workspace, req.cwd).split(path.sep).join('/');
|
|
71
|
+
const readOnly = req.mode === 'read-only';
|
|
72
|
+
return [
|
|
73
|
+
'run', '--rm', '--name', name,
|
|
74
|
+
'--network', req.network === 'none' ? 'none' : 'bridge',
|
|
75
|
+
'--cap-drop', 'ALL',
|
|
76
|
+
'--security-opt', 'no-new-privileges',
|
|
77
|
+
'--memory', opts.memory,
|
|
78
|
+
'--pids-limit', String(opts.pidsLimit),
|
|
79
|
+
...(opts.user ? ['--user', opts.user] : []),
|
|
80
|
+
...(readOnly ? ['--read-only', '--tmpfs', '/tmp'] : []),
|
|
81
|
+
'-v', `${req.workspace}:/workspace${readOnly ? ':ro' : ''}`,
|
|
82
|
+
'-w', rel ? `/workspace/${rel}` : '/workspace',
|
|
83
|
+
opts.image,
|
|
84
|
+
'sh', '-c', req.command,
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Container confinement. `read-only` mounts the workspace read-only (and the container root); `workspace-write`
|
|
89
|
+
* writes only inside the workspace; the network is `none` unless the policy allows it. It never claims
|
|
90
|
+
* `danger-full-access` (a container is not full access), and when the daemon does not answer the service
|
|
91
|
+
* reports SANDBOX_UNAVAILABLE instead of running on the host.
|
|
92
|
+
*/
|
|
93
|
+
class DockerBackend {
|
|
94
|
+
name = 'docker';
|
|
95
|
+
bin;
|
|
96
|
+
image;
|
|
97
|
+
memory;
|
|
98
|
+
pidsLimit;
|
|
99
|
+
user;
|
|
100
|
+
probe;
|
|
101
|
+
negativeTtlMs;
|
|
102
|
+
cached;
|
|
103
|
+
constructor(opts = {}) {
|
|
104
|
+
this.bin = opts.bin ?? 'docker';
|
|
105
|
+
this.image = opts.image ?? 'node:22';
|
|
106
|
+
this.memory = opts.memory ?? '2g';
|
|
107
|
+
this.pidsLimit = opts.pidsLimit ?? 512;
|
|
108
|
+
this.user = opts.user === false ? undefined
|
|
109
|
+
: opts.user ?? (typeof process.getuid === 'function' && typeof process.getgid === 'function' ? `${process.getuid()}:${process.getgid()}` : undefined);
|
|
110
|
+
this.probe = opts.probe ?? (() => defaultProbe(this.bin));
|
|
111
|
+
this.negativeTtlMs = opts.negativeTtlMs ?? 5000;
|
|
112
|
+
}
|
|
113
|
+
supports(mode, _network) {
|
|
114
|
+
return mode === 'read-only' || mode === 'workspace-write';
|
|
115
|
+
}
|
|
116
|
+
async available() {
|
|
117
|
+
const now = Date.now();
|
|
118
|
+
if (this.cached && (this.cached.ok || now - this.cached.at < this.negativeTtlMs))
|
|
119
|
+
return this.cached.ok;
|
|
120
|
+
const ok = await this.probe().catch(() => false);
|
|
121
|
+
this.cached = { ok, at: now };
|
|
122
|
+
return ok;
|
|
123
|
+
}
|
|
124
|
+
unavailableReason() {
|
|
125
|
+
return `the docker daemon is not reachable ("${this.bin} version" failed)`;
|
|
126
|
+
}
|
|
127
|
+
exec(req) {
|
|
128
|
+
const name = `bk-run-${process.pid}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
129
|
+
const proc = (0, child_process_1.spawn)(this.bin, buildDockerArgs(name, req, { image: this.image, memory: this.memory, pidsLimit: this.pidsLimit, user: this.user }));
|
|
130
|
+
return (0, process_1.runProcess)(proc, {
|
|
131
|
+
timeoutMs: req.timeoutMs, signal: req.signal, onOutput: req.onOutput,
|
|
132
|
+
kill: (p) => {
|
|
133
|
+
(0, child_process_1.spawn)(this.bin, ['kill', name], { stdio: 'ignore' }).on('error', () => undefined);
|
|
134
|
+
(0, process_1.killProcessTree)(p);
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.DockerBackend = DockerBackend;
|
|
140
|
+
//# sourceMappingURL=docker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docker.js","sourceRoot":"","sources":["../../src/backends/docker.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;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AAS,CAAC,CAAC;gBAClF,IAAA,yBAAe,EAAC,CAAC,CAAC,CAAC;YACvB,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;CACJ;AAjDD,sCAiDC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SandboxMode, SandboxNetwork } from '@backendkit-labs/agent-core';
|
|
2
|
+
import type { BackendExecRequest, SandboxBackend } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Runs on the host with no confinement. It therefore claims exactly one thing: `danger-full-access`
|
|
5
|
+
* with the network open. Anything stricter makes the service look for another backend, or fail.
|
|
6
|
+
*/
|
|
7
|
+
export declare class NoneBackend implements SandboxBackend {
|
|
8
|
+
readonly name = "none";
|
|
9
|
+
supports(mode: SandboxMode, network: SandboxNetwork): boolean;
|
|
10
|
+
available(): Promise<boolean>;
|
|
11
|
+
exec(req: BackendExecRequest): Promise<import("@backendkit-labs/agent-core").SandboxExecResult>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=none.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"none.d.ts","sourceRoot":"","sources":["../../src/backends/none.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE/E,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAElE;;;GAGG;AACH,qBAAa,WAAY,YAAW,cAAc;IAC9C,QAAQ,CAAC,IAAI,UAAU;IAEvB,QAAQ,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO;IAIvD,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAEnC,IAAI,CAAC,GAAG,EAAE,kBAAkB;CAU/B"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NoneBackend = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const process_1 = require("../process");
|
|
6
|
+
/**
|
|
7
|
+
* Runs on the host with no confinement. It therefore claims exactly one thing: `danger-full-access`
|
|
8
|
+
* with the network open. Anything stricter makes the service look for another backend, or fail.
|
|
9
|
+
*/
|
|
10
|
+
class NoneBackend {
|
|
11
|
+
name = 'none';
|
|
12
|
+
supports(mode, network) {
|
|
13
|
+
return mode === 'danger-full-access' && network === 'allow';
|
|
14
|
+
}
|
|
15
|
+
async available() { return true; }
|
|
16
|
+
exec(req) {
|
|
17
|
+
const proc = (0, child_process_1.spawn)(req.hostCommand ?? req.command, {
|
|
18
|
+
shell: true,
|
|
19
|
+
cwd: req.cwd,
|
|
20
|
+
env: req.env,
|
|
21
|
+
// Own process group on POSIX, so the whole tree can be signalled.
|
|
22
|
+
detached: process.platform !== 'win32',
|
|
23
|
+
});
|
|
24
|
+
return (0, process_1.runProcess)(proc, { timeoutMs: req.timeoutMs, signal: req.signal, onOutput: req.onOutput });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.NoneBackend = NoneBackend;
|
|
28
|
+
//# sourceMappingURL=none.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"none.js","sourceRoot":"","sources":["../../src/backends/none.ts"],"names":[],"mappings":";;;AAAA,iDAAsC;AAEtC,wCAAwC;AAGxC;;;GAGG;AACH,MAAa,WAAW;IACX,IAAI,GAAG,MAAM,CAAC;IAEvB,QAAQ,CAAC,IAAiB,EAAE,OAAuB;QAC/C,OAAO,IAAI,KAAK,oBAAoB,IAAI,OAAO,KAAK,OAAO,CAAC;IAChE,CAAC;IAED,KAAK,CAAC,SAAS,KAAuB,OAAO,IAAI,CAAC,CAAC,CAAC;IAEpD,IAAI,CAAC,GAAuB;QACxB,MAAM,IAAI,GAAG,IAAA,qBAAK,EAAC,GAAG,CAAC,WAAW,IAAI,GAAG,CAAC,OAAO,EAAE;YAC/C,KAAK,EAAE,IAAI;YACX,GAAG,EAAE,GAAG,CAAC,GAAG;YACZ,GAAG,EAAE,GAAG,CAAC,GAAG;YACZ,kEAAkE;YAClE,QAAQ,EAAE,OAAO,CAAC,QAAQ,KAAK,OAAO;SACzC,CAAC,CAAC;QACH,OAAO,IAAA,oBAAU,EAAC,IAAI,EAAE,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IACtG,CAAC;CACJ;AAnBD,kCAmBC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { SandboxExecResult, SandboxMode, SandboxNetwork } from '@backendkit-labs/agent-core';
|
|
2
|
+
export interface BackendExecRequest {
|
|
3
|
+
/** Portable command line. */
|
|
4
|
+
command: string;
|
|
5
|
+
/** Command line adjusted for the host shell, for backends that run on the host. */
|
|
6
|
+
hostCommand?: string;
|
|
7
|
+
cwd: string;
|
|
8
|
+
/** Directory exposed to the command (mounted by container backends). */
|
|
9
|
+
workspace: string;
|
|
10
|
+
mode: SandboxMode;
|
|
11
|
+
network: SandboxNetwork;
|
|
12
|
+
/** Environment for backends that run on the host (already scrubbed). */
|
|
13
|
+
env: Record<string, string>;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
onOutput?: (chunk: string) => void;
|
|
17
|
+
}
|
|
18
|
+
/** Something that can run a command with a given confinement. */
|
|
19
|
+
export interface SandboxBackend {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
/**
|
|
22
|
+
* Can this backend ENFORCE `mode` with `network`? Honest answers only: checking that a path is inside
|
|
23
|
+
* the workspace is not confinement, so a host backend must not claim `workspace-write`.
|
|
24
|
+
*/
|
|
25
|
+
supports(mode: SandboxMode, network: SandboxNetwork): boolean;
|
|
26
|
+
/** Is the backend usable on this host right now (e.g. the docker daemon answers)? */
|
|
27
|
+
available(): Promise<boolean>;
|
|
28
|
+
/** Why `available()` is false, for the error shown to the operator. */
|
|
29
|
+
unavailableReason?(): string;
|
|
30
|
+
exec(req: BackendExecRequest): Promise<SandboxExecResult>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/backends/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAElG,MAAM,WAAW,kBAAkB;IAC/B,6BAA6B;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,mFAAmF;IACnF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,wEAAwE;IACxE,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,cAAc,CAAC;IACxB,wEAAwE;IACxE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACtC;AAED,iEAAiE;AACjE,MAAM,WAAW,cAAc;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC;IAC9D,qFAAqF;IACrF,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,uEAAuE;IACvE,iBAAiB,CAAC,IAAI,MAAM,CAAC;IAC7B,IAAI,CAAC,GAAG,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAC7D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/backends/types.ts"],"names":[],"mappings":""}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment hygiene for confined processes: variables that look like credentials are not handed down.
|
|
3
|
+
* A case-insensitive denylist by name pattern, never an allowlist-only filter (cmd.exe needs SystemRoot,
|
|
4
|
+
* ComSpec and PATHEXT, and most tools need PATH/HOME-like variables).
|
|
5
|
+
*/
|
|
6
|
+
export interface EnvPolicy {
|
|
7
|
+
/** Names (exact, or with `*` as wildcard) that are passed even if they look like credentials, e.g. `GH_TOKEN`. */
|
|
8
|
+
allow?: string[];
|
|
9
|
+
/** Extra names (exact, or with `*`) to drop on top of the default patterns. */
|
|
10
|
+
deny?: string[];
|
|
11
|
+
}
|
|
12
|
+
/** The variables of `env` a confined process may see. Undefined values are dropped. */
|
|
13
|
+
export declare function scrubEnv(env: Record<string, string | undefined>, policy?: EnvPolicy): Record<string, string>;
|
|
14
|
+
//# sourceMappingURL=env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,MAAM,WAAW,SAAS;IACtB,kHAAkH;IAClH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,+EAA+E;IAC/E,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB;AAQD,uFAAuF;AACvF,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,EAAE,MAAM,GAAE,SAAc,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAWhH"}
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Environment hygiene for confined processes: variables that look like credentials are not handed down.
|
|
4
|
+
* A case-insensitive denylist by name pattern, never an allowlist-only filter (cmd.exe needs SystemRoot,
|
|
5
|
+
* ComSpec and PATHEXT, and most tools need PATH/HOME-like variables).
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.scrubEnv = scrubEnv;
|
|
9
|
+
const DEFAULT_DENY = /KEY|SECRET|TOKEN|PASSWORD|PASSWD|CREDENTIAL/i;
|
|
10
|
+
function matcher(patterns) {
|
|
11
|
+
const regexes = (patterns ?? []).map(p => new RegExp('^' + p.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*') + '$', 'i'));
|
|
12
|
+
return name => regexes.some(r => r.test(name));
|
|
13
|
+
}
|
|
14
|
+
/** The variables of `env` a confined process may see. Undefined values are dropped. */
|
|
15
|
+
function scrubEnv(env, policy = {}) {
|
|
16
|
+
const allowed = matcher(policy.allow);
|
|
17
|
+
const denied = matcher(policy.deny);
|
|
18
|
+
const out = {};
|
|
19
|
+
for (const [name, value] of Object.entries(env)) {
|
|
20
|
+
if (value === undefined)
|
|
21
|
+
continue;
|
|
22
|
+
if (allowed(name)) {
|
|
23
|
+
out[name] = value;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (denied(name) || DEFAULT_DENY.test(name))
|
|
27
|
+
continue;
|
|
28
|
+
out[name] = value;
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=env.js.map
|
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AAkBH,4BAWC;AA3BD,MAAM,YAAY,GAAG,8CAA8C,CAAC;AASpE,SAAS,OAAO,CAAC,QAA8B;IAC3C,MAAM,OAAO,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CACrC,IAAI,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAC/F,OAAO,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;AACnD,CAAC;AAED,uFAAuF;AACvF,SAAgB,QAAQ,CAAC,GAAuC,EAAE,SAAoB,EAAE;IACpF,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACtC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACpC,MAAM,GAAG,GAA2B,EAAE,CAAC;IACvC,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QAC9C,IAAI,KAAK,KAAK,SAAS;YAAE,SAAS;QAClC,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YAAC,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;YAAC,SAAS;QAAC,CAAC;QACnD,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;YAAE,SAAS;QACtD,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;IACtB,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { createSandboxService } from './service';
|
|
2
|
+
export type { SandboxService, SandboxServiceOptions, DecisionListener } from './service';
|
|
3
|
+
export { createSandboxPolicy, parsePolicyConfig, SandboxPolicyError } from './policy';
|
|
4
|
+
export type { SandboxPolicy, SandboxPolicyConfig, SandboxRule, ResolvedPolicy } from './policy';
|
|
5
|
+
export { PERMISSION_PRESETS, isPermissionPreset, matchPreset, needsApproval } from './presets';
|
|
6
|
+
export type { PermissionPreset, PresetDefinition, ApprovalLevel, ActionKind } from './presets';
|
|
7
|
+
export { scrubEnv } from './env';
|
|
8
|
+
export type { EnvPolicy } from './env';
|
|
9
|
+
export { NoneBackend } from './backends/none';
|
|
10
|
+
export { DockerBackend, buildDockerArgs } from './backends/docker';
|
|
11
|
+
export type { DockerBackendOptions } from './backends/docker';
|
|
12
|
+
export type { SandboxBackend, BackendExecRequest } from './backends/types';
|
|
13
|
+
export { runProcess, killProcessTree, DEFAULT_MAX_OUTPUT_CHARS } from './process';
|
|
14
|
+
export type { RunProcessOptions } from './process';
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AACjD,YAAY,EAAE,cAAc,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AACzF,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AACtF,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAChG,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC/F,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/F,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjC,YAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACnE,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,YAAY,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3E,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAClF,YAAY,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_MAX_OUTPUT_CHARS = exports.killProcessTree = exports.runProcess = exports.buildDockerArgs = exports.DockerBackend = exports.NoneBackend = exports.scrubEnv = exports.needsApproval = exports.matchPreset = exports.isPermissionPreset = exports.PERMISSION_PRESETS = exports.SandboxPolicyError = exports.parsePolicyConfig = exports.createSandboxPolicy = exports.createSandboxService = void 0;
|
|
4
|
+
var service_1 = require("./service");
|
|
5
|
+
Object.defineProperty(exports, "createSandboxService", { enumerable: true, get: function () { return service_1.createSandboxService; } });
|
|
6
|
+
var policy_1 = require("./policy");
|
|
7
|
+
Object.defineProperty(exports, "createSandboxPolicy", { enumerable: true, get: function () { return policy_1.createSandboxPolicy; } });
|
|
8
|
+
Object.defineProperty(exports, "parsePolicyConfig", { enumerable: true, get: function () { return policy_1.parsePolicyConfig; } });
|
|
9
|
+
Object.defineProperty(exports, "SandboxPolicyError", { enumerable: true, get: function () { return policy_1.SandboxPolicyError; } });
|
|
10
|
+
var presets_1 = require("./presets");
|
|
11
|
+
Object.defineProperty(exports, "PERMISSION_PRESETS", { enumerable: true, get: function () { return presets_1.PERMISSION_PRESETS; } });
|
|
12
|
+
Object.defineProperty(exports, "isPermissionPreset", { enumerable: true, get: function () { return presets_1.isPermissionPreset; } });
|
|
13
|
+
Object.defineProperty(exports, "matchPreset", { enumerable: true, get: function () { return presets_1.matchPreset; } });
|
|
14
|
+
Object.defineProperty(exports, "needsApproval", { enumerable: true, get: function () { return presets_1.needsApproval; } });
|
|
15
|
+
var env_1 = require("./env");
|
|
16
|
+
Object.defineProperty(exports, "scrubEnv", { enumerable: true, get: function () { return env_1.scrubEnv; } });
|
|
17
|
+
var none_1 = require("./backends/none");
|
|
18
|
+
Object.defineProperty(exports, "NoneBackend", { enumerable: true, get: function () { return none_1.NoneBackend; } });
|
|
19
|
+
var docker_1 = require("./backends/docker");
|
|
20
|
+
Object.defineProperty(exports, "DockerBackend", { enumerable: true, get: function () { return docker_1.DockerBackend; } });
|
|
21
|
+
Object.defineProperty(exports, "buildDockerArgs", { enumerable: true, get: function () { return docker_1.buildDockerArgs; } });
|
|
22
|
+
var process_1 = require("./process");
|
|
23
|
+
Object.defineProperty(exports, "runProcess", { enumerable: true, get: function () { return process_1.runProcess; } });
|
|
24
|
+
Object.defineProperty(exports, "killProcessTree", { enumerable: true, get: function () { return process_1.killProcessTree; } });
|
|
25
|
+
Object.defineProperty(exports, "DEFAULT_MAX_OUTPUT_CHARS", { enumerable: true, get: function () { return process_1.DEFAULT_MAX_OUTPUT_CHARS; } });
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAiD;AAAxC,+GAAA,oBAAoB,OAAA;AAE7B,mCAAsF;AAA7E,6GAAA,mBAAmB,OAAA;AAAE,2GAAA,iBAAiB,OAAA;AAAE,4GAAA,kBAAkB,OAAA;AAEnE,qCAA+F;AAAtF,6GAAA,kBAAkB,OAAA;AAAE,6GAAA,kBAAkB,OAAA;AAAE,sGAAA,WAAW,OAAA;AAAE,wGAAA,aAAa,OAAA;AAE3E,6BAAiC;AAAxB,+FAAA,QAAQ,OAAA;AAEjB,wCAA8C;AAArC,mGAAA,WAAW,OAAA;AACpB,4CAAmE;AAA1D,uGAAA,aAAa,OAAA;AAAE,yGAAA,eAAe,OAAA;AAGvC,qCAAkF;AAAzE,qGAAA,UAAU,OAAA;AAAE,0GAAA,eAAe,OAAA;AAAE,mHAAA,wBAAwB,OAAA"}
|
package/dist/policy.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { SandboxCaller, SandboxMode, SandboxNetwork } from '@backendkit-labs/agent-core';
|
|
2
|
+
import type { EnvPolicy } from './env';
|
|
3
|
+
import { type ApprovalLevel, type PermissionPreset } from './presets';
|
|
4
|
+
/** A more specific setting for one agent and/or one tool. The first rule that matches wins. */
|
|
5
|
+
export interface SandboxRule {
|
|
6
|
+
agentId?: string;
|
|
7
|
+
tool?: string;
|
|
8
|
+
mode?: SandboxMode;
|
|
9
|
+
network?: SandboxNetwork;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The whole policy as plain JSON, so it can live in a file (`fleet.yaml`), travel in a control request
|
|
13
|
+
* and be shown back (`snapshot()`).
|
|
14
|
+
*/
|
|
15
|
+
export interface SandboxPolicyConfig {
|
|
16
|
+
/** Starting point for `mode` and `approval`. Default `ask`. */
|
|
17
|
+
preset?: PermissionPreset;
|
|
18
|
+
/** Overrides the preset's mode. */
|
|
19
|
+
mode?: SandboxMode;
|
|
20
|
+
/** Overrides the preset's approval level. */
|
|
21
|
+
approval?: ApprovalLevel;
|
|
22
|
+
/** Default: `none` for `read-only` and `workspace-write`, `allow` for `danger-full-access`. */
|
|
23
|
+
network?: SandboxNetwork;
|
|
24
|
+
/** Extra directories (besides the call's workspace) a confined command may use as its working directory. */
|
|
25
|
+
roots?: string[];
|
|
26
|
+
rules?: SandboxRule[];
|
|
27
|
+
env?: EnvPolicy;
|
|
28
|
+
/** Tools refused outright while the mode is `read-only` (they write). The integrator lists them. */
|
|
29
|
+
writeTools?: string[];
|
|
30
|
+
/** Tools that run commands (what the approval level calls "commands"). Default `['run_command']`. */
|
|
31
|
+
commandTools?: string[];
|
|
32
|
+
}
|
|
33
|
+
export interface ResolvedPolicy {
|
|
34
|
+
mode: SandboxMode;
|
|
35
|
+
network: SandboxNetwork;
|
|
36
|
+
approval: ApprovalLevel;
|
|
37
|
+
roots: string[];
|
|
38
|
+
env: EnvPolicy;
|
|
39
|
+
/** Preset that matches the effective mode and approval, or `custom`. */
|
|
40
|
+
preset: PermissionPreset | 'custom';
|
|
41
|
+
}
|
|
42
|
+
export declare class SandboxPolicyError extends Error {
|
|
43
|
+
constructor(path: string, message: string);
|
|
44
|
+
}
|
|
45
|
+
/** Checks an untrusted object (a YAML file, a control request) and returns it as a typed config. */
|
|
46
|
+
export declare function parsePolicyConfig(input: unknown): SandboxPolicyConfig;
|
|
47
|
+
/**
|
|
48
|
+
* The single place that decides how a call is confined. It is plain data behind two methods: `set` swaps the
|
|
49
|
+
* whole configuration (taking effect on the NEXT call, never in the middle of one) and `snapshot` shows it.
|
|
50
|
+
*/
|
|
51
|
+
export interface SandboxPolicy {
|
|
52
|
+
resolve(caller: SandboxCaller, tool: string): ResolvedPolicy;
|
|
53
|
+
/** Replaces the configuration after validating it. A throwing call leaves the current policy untouched. */
|
|
54
|
+
set(config: SandboxPolicyConfig): void;
|
|
55
|
+
/** A copy of the current configuration. */
|
|
56
|
+
snapshot(): SandboxPolicyConfig;
|
|
57
|
+
/** Tools refused while `read-only`, for the caller to enforce. */
|
|
58
|
+
writeTools(): readonly string[];
|
|
59
|
+
/** Tools that count as running a command for the approval level. */
|
|
60
|
+
commandTools(): readonly string[];
|
|
61
|
+
/** Increments on every successful `set`, so a control plane can tell whether its copy is current. */
|
|
62
|
+
readonly version: number;
|
|
63
|
+
}
|
|
64
|
+
export declare function createSandboxPolicy(initial?: SandboxPolicyConfig): SandboxPolicy;
|
|
65
|
+
//# sourceMappingURL=policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.d.ts","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA,OAAO,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C;AAgBD,oGAAoG;AACpG,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,mBAAmB,CAsCrE;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC1B,OAAO,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,CAAC;IAC7D,2GAA2G;IAC3G,GAAG,CAAC,MAAM,EAAE,mBAAmB,GAAG,IAAI,CAAC;IACvC,2CAA2C;IAC3C,QAAQ,IAAI,mBAAmB,CAAC;IAChC,kEAAkE;IAClE,UAAU,IAAI,SAAS,MAAM,EAAE,CAAC;IAChC,oEAAoE;IACpE,YAAY,IAAI,SAAS,MAAM,EAAE,CAAC;IAClC,qGAAqG;IACrG,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC5B;AAED,wBAAgB,mBAAmB,CAAC,OAAO,GAAE,mBAAwB,GAAG,aAAa,CAkCpF"}
|
package/dist/policy.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxPolicyError = void 0;
|
|
4
|
+
exports.parsePolicyConfig = parsePolicyConfig;
|
|
5
|
+
exports.createSandboxPolicy = createSandboxPolicy;
|
|
6
|
+
const presets_1 = require("./presets");
|
|
7
|
+
const MODES = ['read-only', 'workspace-write', 'danger-full-access'];
|
|
8
|
+
const NETWORKS = ['none', 'allow'];
|
|
9
|
+
const APPROVALS = ['none', 'commands', 'writes-and-commands'];
|
|
10
|
+
class SandboxPolicyError extends Error {
|
|
11
|
+
constructor(path, message) {
|
|
12
|
+
super(`Invalid sandbox policy at ${path}: ${message}`);
|
|
13
|
+
this.name = 'SandboxPolicyError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.SandboxPolicyError = SandboxPolicyError;
|
|
17
|
+
const defaultNetwork = (mode) => (mode === 'danger-full-access' ? 'allow' : 'none');
|
|
18
|
+
function oneOf(path, value, allowed) {
|
|
19
|
+
if (typeof value !== 'string' || !allowed.includes(value)) {
|
|
20
|
+
throw new SandboxPolicyError(path, `expected one of ${allowed.join(' | ')}, got ${JSON.stringify(value)}`);
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function stringList(path, value) {
|
|
25
|
+
if (!Array.isArray(value) || value.some(v => typeof v !== 'string'))
|
|
26
|
+
throw new SandboxPolicyError(path, 'expected an array of strings');
|
|
27
|
+
return [...value];
|
|
28
|
+
}
|
|
29
|
+
/** Checks an untrusted object (a YAML file, a control request) and returns it as a typed config. */
|
|
30
|
+
function parsePolicyConfig(input) {
|
|
31
|
+
if (typeof input !== 'object' || input === null || Array.isArray(input))
|
|
32
|
+
throw new SandboxPolicyError('$', 'expected an object');
|
|
33
|
+
const c = input;
|
|
34
|
+
const out = {};
|
|
35
|
+
if (c.preset !== undefined) {
|
|
36
|
+
if (!(0, presets_1.isPermissionPreset)(c.preset))
|
|
37
|
+
throw new SandboxPolicyError('preset', `unknown preset ${JSON.stringify(c.preset)}`);
|
|
38
|
+
out.preset = c.preset;
|
|
39
|
+
}
|
|
40
|
+
if (c.mode !== undefined)
|
|
41
|
+
out.mode = oneOf('mode', c.mode, MODES);
|
|
42
|
+
if (c.approval !== undefined)
|
|
43
|
+
out.approval = oneOf('approval', c.approval, APPROVALS);
|
|
44
|
+
if (c.network !== undefined)
|
|
45
|
+
out.network = oneOf('network', c.network, NETWORKS);
|
|
46
|
+
if (c.roots !== undefined)
|
|
47
|
+
out.roots = stringList('roots', c.roots);
|
|
48
|
+
if (c.writeTools !== undefined)
|
|
49
|
+
out.writeTools = stringList('writeTools', c.writeTools);
|
|
50
|
+
if (c.commandTools !== undefined)
|
|
51
|
+
out.commandTools = stringList('commandTools', c.commandTools);
|
|
52
|
+
if (c.env !== undefined) {
|
|
53
|
+
const e = c.env;
|
|
54
|
+
if (typeof e !== 'object' || e === null)
|
|
55
|
+
throw new SandboxPolicyError('env', 'expected an object');
|
|
56
|
+
out.env = {
|
|
57
|
+
...(e.allow !== undefined ? { allow: stringList('env.allow', e.allow) } : {}),
|
|
58
|
+
...(e.deny !== undefined ? { deny: stringList('env.deny', e.deny) } : {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (c.rules !== undefined) {
|
|
62
|
+
if (!Array.isArray(c.rules))
|
|
63
|
+
throw new SandboxPolicyError('rules', 'expected an array');
|
|
64
|
+
out.rules = c.rules.map((r, i) => {
|
|
65
|
+
const rule = r;
|
|
66
|
+
if (typeof rule !== 'object' || rule === null)
|
|
67
|
+
throw new SandboxPolicyError(`rules[${i}]`, 'expected an object');
|
|
68
|
+
if (rule.agentId !== undefined && typeof rule.agentId !== 'string')
|
|
69
|
+
throw new SandboxPolicyError(`rules[${i}].agentId`, 'expected a string');
|
|
70
|
+
if (rule.tool !== undefined && typeof rule.tool !== 'string')
|
|
71
|
+
throw new SandboxPolicyError(`rules[${i}].tool`, 'expected a string');
|
|
72
|
+
return {
|
|
73
|
+
...(rule.agentId !== undefined ? { agentId: rule.agentId } : {}),
|
|
74
|
+
...(rule.tool !== undefined ? { tool: rule.tool } : {}),
|
|
75
|
+
...(rule.mode !== undefined ? { mode: oneOf(`rules[${i}].mode`, rule.mode, MODES) } : {}),
|
|
76
|
+
...(rule.network !== undefined ? { network: oneOf(`rules[${i}].network`, rule.network, NETWORKS) } : {}),
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
function createSandboxPolicy(initial = {}) {
|
|
83
|
+
let config = parsePolicyConfig(initial);
|
|
84
|
+
let version = 1;
|
|
85
|
+
return {
|
|
86
|
+
resolve(caller, tool) {
|
|
87
|
+
const preset = presets_1.PERMISSION_PRESETS[config.preset ?? 'ask'];
|
|
88
|
+
let mode = config.mode ?? preset.mode;
|
|
89
|
+
let ruleNetwork;
|
|
90
|
+
const rule = (config.rules ?? []).find(r => (r.agentId === undefined || r.agentId === caller.agentId) && (r.tool === undefined || r.tool === tool));
|
|
91
|
+
if (rule) {
|
|
92
|
+
if (rule.mode)
|
|
93
|
+
mode = rule.mode;
|
|
94
|
+
ruleNetwork = rule.network;
|
|
95
|
+
}
|
|
96
|
+
const approval = config.approval ?? preset.approval;
|
|
97
|
+
return {
|
|
98
|
+
mode,
|
|
99
|
+
network: ruleNetwork ?? config.network ?? defaultNetwork(mode),
|
|
100
|
+
approval,
|
|
101
|
+
roots: [...(config.roots ?? [])],
|
|
102
|
+
env: { allow: [...(config.env?.allow ?? [])], deny: [...(config.env?.deny ?? [])] },
|
|
103
|
+
preset: (0, presets_1.matchPreset)(mode, approval),
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
set(next) {
|
|
107
|
+
config = parsePolicyConfig(next);
|
|
108
|
+
version++;
|
|
109
|
+
},
|
|
110
|
+
snapshot: () => JSON.parse(JSON.stringify(config)),
|
|
111
|
+
writeTools: () => config.writeTools ?? [],
|
|
112
|
+
commandTools: () => config.commandTools ?? ['run_command'],
|
|
113
|
+
get version() { return version; },
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":";;;AAuEA,8CAsCC;AAoBD,kDAkCC;AAjKD,uCAA2H;AAE3H,MAAM,KAAK,GAAkB,CAAC,WAAW,EAAE,iBAAiB,EAAE,oBAAoB,CAAC,CAAC;AACpF,MAAM,QAAQ,GAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACrD,MAAM,SAAS,GAAoB,CAAC,MAAM,EAAE,UAAU,EAAE,qBAAqB,CAAC,CAAC;AA2C/E,MAAa,kBAAmB,SAAQ,KAAK;IACzC,YAAY,IAAY,EAAE,OAAe;QACrC,KAAK,CAAC,6BAA6B,IAAI,KAAK,OAAO,EAAE,CAAC,CAAC;QACvD,I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,CAAC,aAAa,CAAC;QAC1D,IAAI,OAAO,KAAK,OAAO,OAAO,CAAC,CAAC,CAAC;KACpC,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { SandboxMode } from '@backendkit-labs/agent-core';
|
|
2
|
+
/**
|
|
3
|
+
* What needs a human before it runs:
|
|
4
|
+
* - `none`: nothing.
|
|
5
|
+
* - `commands`: running commands.
|
|
6
|
+
* - `writes-and-commands`: file writes and running commands.
|
|
7
|
+
*/
|
|
8
|
+
export type ApprovalLevel = 'none' | 'commands' | 'writes-and-commands';
|
|
9
|
+
export type PermissionPreset = 'read-only' | 'ask' | 'auto-edit' | 'full';
|
|
10
|
+
export interface PresetDefinition {
|
|
11
|
+
mode: SandboxMode;
|
|
12
|
+
approval: ApprovalLevel;
|
|
13
|
+
description: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Sandbox mode + approval, by name. `auto-edit` deviates from the SPEC-0006 table on purpose: the command
|
|
17
|
+
* allowlist of `run_command` stays a hard block (it is not a soft list yet), so here "auto-edit" means file
|
|
18
|
+
* edits run without asking and every command asks.
|
|
19
|
+
*/
|
|
20
|
+
export declare const PERMISSION_PRESETS: Readonly<Record<PermissionPreset, PresetDefinition>>;
|
|
21
|
+
export declare function isPermissionPreset(x: unknown): x is PermissionPreset;
|
|
22
|
+
/** The preset a (mode, approval) pair corresponds to, or `custom` when it matches none. */
|
|
23
|
+
export declare function matchPreset(mode: SandboxMode, approval: ApprovalLevel): PermissionPreset | 'custom';
|
|
24
|
+
export type ActionKind = 'read' | 'write' | 'command';
|
|
25
|
+
/** Does an action of this kind need a human under this approval level? */
|
|
26
|
+
export declare function needsApproval(level: ApprovalLevel, kind: ActionKind): boolean;
|
|
27
|
+
//# sourceMappingURL=presets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presets.d.ts","sourceRoot":"","sources":["../src/presets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE/D;;;;;GAKG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,UAAU,GAAG,qBAAqB,CAAC;AAExE,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,KAAK,GAAG,WAAW,GAAG,MAAM,CAAC;AAE1E,MAAM,WAAW,gBAAgB;IAC7B,IAAI,EAAE,WAAW,CAAC;IAClB,QAAQ,EAAE,aAAa,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,EAAE,QAAQ,CAAC,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAKnF,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,gBAAgB,CAEpE;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,aAAa,GAAG,gBAAgB,GAAG,QAAQ,CAKnG;AAED,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;AAEtD,0EAA0E;AAC1E,wBAAgB,aAAa,CAAC,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAI7E"}
|
package/dist/presets.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PERMISSION_PRESETS = void 0;
|
|
4
|
+
exports.isPermissionPreset = isPermissionPreset;
|
|
5
|
+
exports.matchPreset = matchPreset;
|
|
6
|
+
exports.needsApproval = needsApproval;
|
|
7
|
+
/**
|
|
8
|
+
* Sandbox mode + approval, by name. `auto-edit` deviates from the SPEC-0006 table on purpose: the command
|
|
9
|
+
* allowlist of `run_command` stays a hard block (it is not a soft list yet), so here "auto-edit" means file
|
|
10
|
+
* edits run without asking and every command asks.
|
|
11
|
+
*/
|
|
12
|
+
exports.PERMISSION_PRESETS = {
|
|
13
|
+
'read-only': { mode: 'read-only', approval: 'none', description: 'nothing can write; no approval needed' },
|
|
14
|
+
'ask': { mode: 'workspace-write', approval: 'writes-and-commands', description: 'writes only in the workspace; ask before writes and commands' },
|
|
15
|
+
'auto-edit': { mode: 'workspace-write', approval: 'commands', description: 'writes only in the workspace; file edits run, commands ask' },
|
|
16
|
+
'full': { mode: 'danger-full-access', approval: 'none', description: 'no confinement, no approval' },
|
|
17
|
+
};
|
|
18
|
+
function isPermissionPreset(x) {
|
|
19
|
+
return typeof x === 'string' && Object.prototype.hasOwnProperty.call(exports.PERMISSION_PRESETS, x);
|
|
20
|
+
}
|
|
21
|
+
/** The preset a (mode, approval) pair corresponds to, or `custom` when it matches none. */
|
|
22
|
+
function matchPreset(mode, approval) {
|
|
23
|
+
for (const [name, def] of Object.entries(exports.PERMISSION_PRESETS)) {
|
|
24
|
+
if (def.mode === mode && def.approval === approval)
|
|
25
|
+
return name;
|
|
26
|
+
}
|
|
27
|
+
return 'custom';
|
|
28
|
+
}
|
|
29
|
+
/** Does an action of this kind need a human under this approval level? */
|
|
30
|
+
function needsApproval(level, kind) {
|
|
31
|
+
if (level === 'none')
|
|
32
|
+
return false;
|
|
33
|
+
if (kind === 'command')
|
|
34
|
+
return true;
|
|
35
|
+
return level === 'writes-and-commands' && kind === 'write';
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=presets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"presets.js","sourceRoot":"","sources":["../src/presets.ts"],"names":[],"mappings":";;;AA8BA,gDAEC;AAGD,kCAKC;AAKD,sCAIC;AA/BD;;;;GAIG;AACU,QAAA,kBAAkB,GAAyD;IACpF,WAAW,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,uCAAuC,EAAE;IAC1G,KAAK,EAAE,EAAE,IAAI,EAAE,iBAAiB,EAAE,QAAQ,EAAE,qBAAqB,EAAE,WAAW,EAAE,8DAA8D,EAAE;IAChJ,WAAW,EAAE,EAAE,IAAI,EAAE,iBAAiB,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,4DAA4D,EAAE;IACzI,MAAM,EAAE,EAAE,IAAI,EAAE,oBAAoB,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,6BAA6B,EAAE;CACvG,CAAC;AAEF,SAAgB,kBAAkB,CAAC,CAAU;IACzC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,0BAAkB,EAAE,CAAC,CAAC,CAAC;AAChG,CAAC;AAED,2FAA2F;AAC3F,SAAgB,WAAW,CAAC,IAAiB,EAAE,QAAuB;IAClE,KAAK,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,0BAAkB,CAAgD,EAAE,CAAC;QAC1G,IAAI,GAAG,CAAC,IAAI,KAAK,IAAI,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;IACpE,CAAC;IACD,OAAO,QAAQ,CAAC;AACpB,CAAC;AAID,0EAA0E;AAC1E,SAAgB,aAAa,CAAC,KAAoB,EAAE,IAAgB;IAChE,IAAI,KAAK,KAAK,MAAM;QAAE,OAAO,KAAK,CAAC;IACnC,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACpC,OAAO,KAAK,KAAK,qBAAqB,IAAI,IAAI,KAAK,OAAO,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ChildProcess } from 'child_process';
|
|
2
|
+
import type { SandboxExecResult } from '@backendkit-labs/agent-core';
|
|
3
|
+
/** Stdout+stderr kept in `SandboxExecResult.output` (the callback still gets everything). */
|
|
4
|
+
export declare const DEFAULT_MAX_OUTPUT_CHARS = 40000;
|
|
5
|
+
/**
|
|
6
|
+
* Stops a process and everything it started. A plain `proc.kill()` leaves the children of a shell
|
|
7
|
+
* running (and on Windows does not reach them at all).
|
|
8
|
+
*/
|
|
9
|
+
export declare function killProcessTree(proc: ChildProcess): void;
|
|
10
|
+
export interface RunProcessOptions {
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
onOutput?: (chunk: string) => void;
|
|
14
|
+
maxOutputChars?: number;
|
|
15
|
+
/** Stops the process (default: the whole process tree). */
|
|
16
|
+
kill?: (proc: ChildProcess) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Drives an already spawned process to the end: collects stdout and stderr interleaved in arrival order,
|
|
20
|
+
* enforces the timeout, honours the abort signal and kills the whole tree when either fires.
|
|
21
|
+
* Resolves as soon as the process closes, or immediately after it was killed for timeout / abort.
|
|
22
|
+
*/
|
|
23
|
+
export declare function runProcess(proc: ChildProcess, o?: RunProcessOptions): Promise<SandboxExecResult>;
|
|
24
|
+
//# sourceMappingURL=process.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.d.ts","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAErE,6FAA6F;AAC7F,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAE/C;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,YAAY,GAAG,IAAI,CAaxD;AAED,MAAM,WAAW,iBAAiB;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,2DAA2D;IAC3D,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,YAAY,KAAK,IAAI,CAAC;CACvC;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC,GAAE,iBAAsB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAwCpG"}
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_MAX_OUTPUT_CHARS = void 0;
|
|
4
|
+
exports.killProcessTree = killProcessTree;
|
|
5
|
+
exports.runProcess = runProcess;
|
|
6
|
+
const child_process_1 = require("child_process");
|
|
7
|
+
/** Stdout+stderr kept in `SandboxExecResult.output` (the callback still gets everything). */
|
|
8
|
+
exports.DEFAULT_MAX_OUTPUT_CHARS = 40_000;
|
|
9
|
+
/**
|
|
10
|
+
* Stops a process and everything it started. A plain `proc.kill()` leaves the children of a shell
|
|
11
|
+
* running (and on Windows does not reach them at all).
|
|
12
|
+
*/
|
|
13
|
+
function killProcessTree(proc) {
|
|
14
|
+
if (process.platform === 'win32' && proc.pid) {
|
|
15
|
+
// taskkill must see the tree while the shell is alive (once cmd.exe dies Windows forgets its
|
|
16
|
+
// children), so the shell is not killed here: /F ends it together with the rest.
|
|
17
|
+
const tk = (0, child_process_1.spawn)('taskkill', ['/pid', String(proc.pid), '/T', '/F'], { stdio: 'ignore' });
|
|
18
|
+
tk.on('error', () => proc.kill());
|
|
19
|
+
tk.on('close', (code) => { if (code !== 0)
|
|
20
|
+
proc.kill(); });
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (proc.pid) {
|
|
24
|
+
try {
|
|
25
|
+
process.kill(-proc.pid, 'SIGKILL');
|
|
26
|
+
}
|
|
27
|
+
catch { /* not a process group leader */ }
|
|
28
|
+
}
|
|
29
|
+
proc.kill();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Drives an already spawned process to the end: collects stdout and stderr interleaved in arrival order,
|
|
33
|
+
* enforces the timeout, honours the abort signal and kills the whole tree when either fires.
|
|
34
|
+
* Resolves as soon as the process closes, or immediately after it was killed for timeout / abort.
|
|
35
|
+
*/
|
|
36
|
+
function runProcess(proc, o = {}) {
|
|
37
|
+
const max = o.maxOutputChars ?? exports.DEFAULT_MAX_OUTPUT_CHARS;
|
|
38
|
+
const kill = o.kill ?? killProcessTree;
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
const chunks = [];
|
|
41
|
+
let total = 0;
|
|
42
|
+
let settled = false;
|
|
43
|
+
const finish = (r) => {
|
|
44
|
+
if (settled)
|
|
45
|
+
return;
|
|
46
|
+
settled = true;
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
o.signal?.removeEventListener('abort', onAbort);
|
|
49
|
+
resolve({ ...r, output: chunks.join('') });
|
|
50
|
+
};
|
|
51
|
+
const collect = (data) => {
|
|
52
|
+
const s = data.toString('utf-8');
|
|
53
|
+
o.onOutput?.(s);
|
|
54
|
+
const room = max - total;
|
|
55
|
+
if (room <= 0)
|
|
56
|
+
return;
|
|
57
|
+
chunks.push(s.length > room ? s.slice(0, room) : s);
|
|
58
|
+
total += Math.min(s.length, room);
|
|
59
|
+
};
|
|
60
|
+
proc.stdout?.on('data', collect);
|
|
61
|
+
proc.stderr?.on('data', collect);
|
|
62
|
+
const onAbort = () => {
|
|
63
|
+
kill(proc);
|
|
64
|
+
finish({ exitCode: null, timedOut: false, aborted: true });
|
|
65
|
+
};
|
|
66
|
+
const timer = o.timeoutMs
|
|
67
|
+
? setTimeout(() => { kill(proc); finish({ exitCode: null, timedOut: true, aborted: false }); }, o.timeoutMs)
|
|
68
|
+
: undefined;
|
|
69
|
+
if (o.signal?.aborted)
|
|
70
|
+
onAbort();
|
|
71
|
+
else
|
|
72
|
+
o.signal?.addEventListener('abort', onAbort, { once: true });
|
|
73
|
+
proc.on('error', (err) => finish({ exitCode: null, timedOut: false, aborted: false, error: err.message }));
|
|
74
|
+
proc.on('close', (code) => finish({ exitCode: code, timedOut: false, aborted: false }));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":";;;AAUA,0CAaC;AAgBD,gCAwCC;AA/ED,iDAAyD;AAGzD,6FAA6F;AAChF,QAAA,wBAAwB,GAAG,MAAM,CAAC;AAE/C;;;GAGG;AACH,SAAgB,eAAe,CAAC,IAAkB;IAC9C,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;QAC3C,6FAA6F;QAC7F,iFAAiF;QACjF,MAAM,EAAE,GAAG,IAAA,qBAAK,EAAC,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC1F,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAClC,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,GAAG,IAAI,IAAI,KAAK,CAAC;YAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3D,OAAO;IACX,CAAC;IACD,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;QACX,IAAI,CAAC;YAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAC,gCAAgC,CAAC,CAAC;IAC1F,CAAC;IACD,IAAI,CAAC,IAAI,EAAE,CAAC;AAChB,CAAC;AAWD;;;;GAIG;AACH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AAC;AACP,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SandboxDecision, SandboxRunner } from '@backendkit-labs/agent-core';
|
|
2
|
+
import type { SandboxBackend } from './backends/types';
|
|
3
|
+
import type { SandboxPolicy } from './policy';
|
|
4
|
+
export type DecisionListener = (decision: SandboxDecision) => void;
|
|
5
|
+
export interface SandboxServiceOptions {
|
|
6
|
+
policy: SandboxPolicy;
|
|
7
|
+
/** Tried in order; the first that can ENFORCE the required mode (and is available) runs the call. */
|
|
8
|
+
backends: SandboxBackend[];
|
|
9
|
+
/** Environment the confined process starts from, before scrubbing. Default `process.env`. */
|
|
10
|
+
baseEnv?: Record<string, string | undefined>;
|
|
11
|
+
/** Appends every decision as one JSON line to this file. */
|
|
12
|
+
auditLog?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Hand the command a scrubbed environment (credential-looking variables removed). Default true.
|
|
15
|
+
* Only the legacy `BK_RUN_COMMAND_SANDBOX` path turns it off, to keep its old behaviour.
|
|
16
|
+
*/
|
|
17
|
+
scrubEnvironment?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface SandboxService extends SandboxRunner {
|
|
20
|
+
readonly policy: SandboxPolicy;
|
|
21
|
+
/** Called for every decision (allowed, denied, unavailable). A throwing listener never affects the call. */
|
|
22
|
+
onDecision(listener: DecisionListener): () => void;
|
|
23
|
+
}
|
|
24
|
+
export declare function createSandboxService(options: SandboxServiceOptions): SandboxService;
|
|
25
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACO,eAAe,EAAuD,aAAa,EACrG,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,MAAM,MAAM,gBAAgB,GAAG,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,CAAC;AAEnE,MAAM,WAAW,qBAAqB;IAClC,MAAM,EAAE,aAAa,CAAC;IACtB,qGAAqG;IACrG,QAAQ,EAAE,cAAc,EAAE,CAAC;IAC3B,6FAA6F;IAC7F,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IAC7C,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,cAAe,SAAQ,aAAa;IACjD,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,4GAA4G;IAC5G,UAAU,CAAC,QAAQ,EAAE,gBAAgB,GAAG,MAAM,IAAI,CAAC;CACtD;AAaD,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CA4FnF"}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSandboxService = createSandboxService;
|
|
4
|
+
const promises_1 = require("fs/promises");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const env_1 = require("./env");
|
|
7
|
+
const presets_1 = require("./presets");
|
|
8
|
+
/** Normalised containment test; case-insensitive on Windows, separator-agnostic. */
|
|
9
|
+
function isInside(root, target) {
|
|
10
|
+
const norm = (p) => {
|
|
11
|
+
const s = (0, path_1.resolve)(p).replace(/\\/g, '/');
|
|
12
|
+
return process.platform === 'win32' ? s.toLowerCase() : s;
|
|
13
|
+
};
|
|
14
|
+
const r = norm(root);
|
|
15
|
+
const t = norm(target);
|
|
16
|
+
return t === r || t.startsWith(r.endsWith('/') ? r : r + '/');
|
|
17
|
+
}
|
|
18
|
+
function createSandboxService(options) {
|
|
19
|
+
const { policy, backends } = options;
|
|
20
|
+
const listeners = new Set();
|
|
21
|
+
// One chain, so lines reach the file in the order the decisions were taken.
|
|
22
|
+
let auditChain;
|
|
23
|
+
const emit = (d) => {
|
|
24
|
+
for (const l of listeners) {
|
|
25
|
+
try {
|
|
26
|
+
l(d);
|
|
27
|
+
}
|
|
28
|
+
catch { /* an observer must not affect the call */ }
|
|
29
|
+
}
|
|
30
|
+
if (options.auditLog) {
|
|
31
|
+
const file = options.auditLog;
|
|
32
|
+
const line = JSON.stringify({ ts: new Date(d.at).toISOString(), ...d }) + '\n';
|
|
33
|
+
auditChain = (auditChain ?? (0, promises_1.mkdir)((0, path_1.dirname)(file), { recursive: true }).catch(() => undefined))
|
|
34
|
+
.then(() => (0, promises_1.appendFile)(file, line))
|
|
35
|
+
.catch(() => undefined);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const base = (caller, tool) => ({ at: Date.now(), agentId: caller.agentId, sessionId: caller.sessionId, tool });
|
|
39
|
+
return {
|
|
40
|
+
policy,
|
|
41
|
+
onDecision(listener) {
|
|
42
|
+
listeners.add(listener);
|
|
43
|
+
return () => { listeners.delete(listener); };
|
|
44
|
+
},
|
|
45
|
+
authorizeTool(caller, tool) {
|
|
46
|
+
const r = policy.resolve(caller, tool);
|
|
47
|
+
if (r.mode === 'read-only' && policy.writeTools().includes(tool)) {
|
|
48
|
+
const reason = `tool "${tool}" writes, and the sandbox policy for agent "${caller.agentId}" is read-only`;
|
|
49
|
+
emit({ ...base(caller, tool), mode: r.mode, network: r.network, outcome: 'SANDBOX_DENIED', reason });
|
|
50
|
+
return { ok: false, code: 'SANDBOX_DENIED', reason };
|
|
51
|
+
}
|
|
52
|
+
return { ok: true };
|
|
53
|
+
},
|
|
54
|
+
approvalFor(caller, tool) {
|
|
55
|
+
const r = policy.resolve(caller, tool);
|
|
56
|
+
const kind = policy.commandTools().includes(tool) ? 'command' : policy.writeTools().includes(tool) ? 'write' : 'read';
|
|
57
|
+
return (0, presets_1.needsApproval)(r.approval, kind) ? 'ask' : 'none';
|
|
58
|
+
},
|
|
59
|
+
async run(caller, req) {
|
|
60
|
+
const r = policy.resolve(caller, req.tool);
|
|
61
|
+
const refuse = (code, reason, backend) => {
|
|
62
|
+
emit({ ...base(caller, req.tool), mode: r.mode, network: r.network, backend, outcome: code, reason });
|
|
63
|
+
return { ok: false, code, reason, mode: r.mode, network: r.network, ...(backend ? { backend } : {}) };
|
|
64
|
+
};
|
|
65
|
+
const workspace = req.workspace ?? req.cwd;
|
|
66
|
+
if (r.mode !== 'danger-full-access' && ![workspace, ...r.roots].some(root => isInside(root, req.cwd))) {
|
|
67
|
+
return refuse('SANDBOX_DENIED', `working directory "${req.cwd}" is outside the allowed roots`);
|
|
68
|
+
}
|
|
69
|
+
// First backend that can really enforce the mode. Never "the host, because nothing else works".
|
|
70
|
+
const skipped = [];
|
|
71
|
+
let chosen;
|
|
72
|
+
for (const b of backends) {
|
|
73
|
+
if (!b.supports(r.mode, r.network)) {
|
|
74
|
+
skipped.push(`${b.name}: cannot enforce ${r.mode} with network ${r.network}`);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (!(await b.available())) {
|
|
78
|
+
skipped.push(`${b.name}: ${b.unavailableReason?.() ?? 'not available'}`);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
chosen = b;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
if (!chosen) {
|
|
85
|
+
return refuse('SANDBOX_UNAVAILABLE', `cannot enforce ${r.mode} (network ${r.network}) on this host; nothing was run. ${skipped.join('; ') || 'no backends configured'}`);
|
|
86
|
+
}
|
|
87
|
+
let result;
|
|
88
|
+
try {
|
|
89
|
+
result = await chosen.exec({
|
|
90
|
+
command: req.command,
|
|
91
|
+
...(req.hostCommand !== undefined ? { hostCommand: req.hostCommand } : {}),
|
|
92
|
+
cwd: req.cwd,
|
|
93
|
+
workspace,
|
|
94
|
+
mode: r.mode,
|
|
95
|
+
network: r.network,
|
|
96
|
+
env: options.scrubEnvironment === false
|
|
97
|
+
? Object.fromEntries(Object.entries(options.baseEnv ?? process.env).filter((e) => e[1] !== undefined))
|
|
98
|
+
: (0, env_1.scrubEnv)(options.baseEnv ?? process.env, r.env),
|
|
99
|
+
timeoutMs: req.timeoutMs,
|
|
100
|
+
signal: req.signal,
|
|
101
|
+
onOutput: req.onOutput,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
result = { exitCode: null, output: '', timedOut: false, aborted: false, error: err instanceof Error ? err.message : String(err) };
|
|
106
|
+
}
|
|
107
|
+
emit({ ...base(caller, req.tool), mode: r.mode, network: r.network, backend: chosen.name, outcome: 'allowed' });
|
|
108
|
+
return { ok: true, result, mode: r.mode, network: r.network, backend: chosen.name };
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":";;AA4CA,oDA4FC;AAxID,0CAAgD;AAChD,+BAAuD;AAKvD,+BAAiC;AAEjC,uCAA0C;AAyB1C,oFAAoF;AACpF,SAAS,QAAQ,CAAC,IAAY,EAAE,MAAc;IAC1C,MAAM,IAAI,GAAG,CAAC,CAAS,EAAE,EAAE;QACvB,MAAM,CAAC,GAAG,IAAA,cAAW,EAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAC7C,OAAO,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9D,CAAC,CAAC;IACF,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;IACrB,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IACvB,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,UA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,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;QACxF,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@backendkit-labs/agent-sandbox",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@backendkit-labs/agent-sandbox",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Confinement and permissions for what agents run: sandbox modes, presets, a single policy point, swappable backends (host, docker) and environment hygiene, for @backendkit-labs/agent-core",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"dev": "tsc --watch",
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"prepublishOnly": "npm run build",
|
|
22
|
+
"test": "jest"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@backendkit-labs/agent-core": ">=0.30.0 <0.31.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/jest": "^29.0.0",
|
|
29
|
+
"@types/node": "^20.0.0",
|
|
30
|
+
"jest": "^29.0.0",
|
|
31
|
+
"ts-jest": "^29.0.0",
|
|
32
|
+
"typescript": "^5.4.0"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=20.0.0"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/BackendKit-labs/backendkit-agent-framework.git",
|
|
40
|
+
"directory": "packages/sandbox"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"license": "MIT"
|
|
46
|
+
}
|