Skip to content

Commit b8bb851

Browse files
committed
feat: move ssh sandboxing into core
1 parent 33edb57 commit b8bb851

28 files changed

Lines changed: 1724 additions & 684 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Docs: https://docs.openclaw.ai
2323
- Telegram/actions: add `topic-edit` for forum-topic renames and icon updates while sharing the same Telegram topic-edit transport used by the plugin runtime. (#47798) Thanks @obviyus.
2424
- secrets: harden read-only SecretRef command paths and diagnostics. (#47794) Thanks @joshavant.
2525
- Sandbox/runtime: add pluggable sandbox backends, ship an OpenShell backend with `mirror` and `remote` workspace modes, and make sandbox list/recreate/prune backend-aware instead of Docker-only.
26+
- Sandbox/SSH: add a core SSH sandbox backend with secret-backed key, certificate, and known_hosts inputs, move shared remote exec/filesystem tooling into core, and keep OpenShell focused on sandbox lifecycle plus optional `mirror` mode.
2627

2728
### Fixes
2829

‎docs/cli/sandbox.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ OpenClaw can run agents in isolated sandbox runtimes for security. The `sandbox`
1616
Today that usually means:
1717

1818
- Docker sandbox containers
19+
- SSH sandbox runtimes when `agents.defaults.sandbox.backend = "ssh"`
1920
- OpenShell sandbox runtimes when `agents.defaults.sandbox.backend = "openshell"`
2021

2122
## Commands
@@ -97,6 +98,22 @@ openclaw sandbox recreate --all
9798
openclaw sandbox recreate --all
9899
```
99100

101+
### After changing SSH target or SSH auth material
102+
103+
```bash
104+
# Edit config:
105+
# - agents.defaults.sandbox.backend
106+
# - agents.defaults.sandbox.ssh.target
107+
# - agents.defaults.sandbox.ssh.workspaceRoot
108+
# - agents.defaults.sandbox.ssh.identityFile / certificateFile / knownHostsFile
109+
# - agents.defaults.sandbox.ssh.identityData / certificateData / knownHostsData
110+
111+
openclaw sandbox recreate --all
112+
```
113+
114+
For the core `ssh` backend, recreate deletes the per-scope remote workspace root
115+
on the SSH target. The next run seeds it again from the local workspace.
116+
100117
### After changing OpenShell source, policy, or mode
101118

102119
```bash
@@ -150,7 +167,7 @@ Sandbox settings live in `~/.openclaw/openclaw.json` under `agents.defaults.sand
150167
"defaults": {
151168
"sandbox": {
152169
"mode": "all", // off, non-main, all
153-
"backend": "docker", // docker, openshell
170+
"backend": "docker", // docker, ssh, openshell
154171
"scope": "agent", // session, agent, shared
155172
"docker": {
156173
"image": "openclaw-sandbox:bookworm-slim",

‎docs/gateway/configuration-reference.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ Optional sandboxing for the embedded agent. See [Sandboxing](/gateway/sandboxing
11251125
defaults: {
11261126
sandbox: {
11271127
mode: "non-main", // off | non-main | all
1128-
backend: "docker", // docker | openshell
1128+
backend: "docker", // docker | ssh | openshell
11291129
scope: "agent", // session | agent | shared
11301130
workspaceAccess: "none", // none | ro | rw
11311131
workspaceRoot: "~/.openclaw/sandboxes",
@@ -1154,6 +1154,20 @@ Optional sandboxing for the embedded agent. See [Sandboxing](/gateway/sandboxing
11541154
extraHosts: ["internal.service:10.0.0.5"],
11551155
binds: ["/home/user/source:/source:rw"],
11561156
},
1157+
ssh: {
1158+
target: "user@gateway-host:22",
1159+
command: "ssh",
1160+
workspaceRoot: "/tmp/openclaw-sandboxes",
1161+
strictHostKeyChecking: true,
1162+
updateHostKeys: true,
1163+
identityFile: "~/.ssh/id_ed25519",
1164+
certificateFile: "~/.ssh/id_ed25519-cert.pub",
1165+
knownHostsFile: "~/.ssh/known_hosts",
1166+
// SecretRefs / inline contents also supported:
1167+
// identityData: { source: "env", provider: "default", id: "SSH_IDENTITY" },
1168+
// certificateData: { source: "env", provider: "default", id: "SSH_CERTIFICATE" },
1169+
// knownHostsData: { source: "env", provider: "default", id: "SSH_KNOWN_HOSTS" },
1170+
},
11571171
browser: {
11581172
enabled: false,
11591173
image: "openclaw-sandbox-browser:bookworm-slim",
@@ -1203,11 +1217,29 @@ Optional sandboxing for the embedded agent. See [Sandboxing](/gateway/sandboxing
12031217
**Backend:**
12041218

12051219
- `docker`: local Docker runtime (default)
1220+
- `ssh`: generic SSH-backed remote runtime
12061221
- `openshell`: OpenShell runtime
12071222

12081223
When `backend: "openshell"` is selected, runtime-specific settings move to
12091224
`plugins.entries.openshell.config`.
12101225

1226+
**SSH backend config:**
1227+
1228+
- `target`: SSH target in `user@host[:port]` form
1229+
- `command`: SSH client command (default: `ssh`)
1230+
- `workspaceRoot`: absolute remote root used for per-scope workspaces
1231+
- `identityFile` / `certificateFile` / `knownHostsFile`: existing local files passed to OpenSSH
1232+
- `identityData` / `certificateData` / `knownHostsData`: inline contents or SecretRefs that OpenClaw materializes into temp files at runtime
1233+
- `strictHostKeyChecking` / `updateHostKeys`: OpenSSH host-key policy knobs
1234+
1235+
**SSH backend behavior:**
1236+
1237+
- seeds the remote workspace once after create or recreate
1238+
- then keeps the remote SSH workspace canonical
1239+
- routes `exec`, file tools, and media paths over SSH
1240+
- does not sync remote changes back to the host automatically
1241+
- does not support sandbox browser containers
1242+
12111243
**Workspace access:**
12121244

12131245
- `none`: per-scope sandbox workspace under `~/.openclaw/sandboxes`
@@ -1252,6 +1284,7 @@ When `backend: "openshell"` is selected, runtime-specific settings move to
12521284
- `remote`: seed remote once when the sandbox is created, then keep the remote workspace canonical
12531285

12541286
In `remote` mode, host-local edits made outside OpenClaw are not synced into the sandbox automatically after the seed step.
1287+
Transport is SSH into the OpenShell sandbox, but the plugin owns sandbox lifecycle and optional mirror sync.
12551288

12561289
**`setupCommand`** runs once after container creation (via `sh -lc`). Needs network egress, writable root, root user.
12571290

‎docs/gateway/sandboxing.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,61 @@ Not sandboxed:
5959
`agents.defaults.sandbox.backend` controls **which runtime** provides the sandbox:
6060

6161
- `"docker"` (default): local Docker-backed sandbox runtime.
62+
- `"ssh"`: generic SSH-backed remote sandbox runtime.
6263
- `"openshell"`: OpenShell-backed sandbox runtime.
6364

65+
SSH-specific config lives under `agents.defaults.sandbox.ssh`.
6466
OpenShell-specific config lives under `plugins.entries.openshell.config`.
6567

68+
### SSH backend
69+
70+
Use `backend: "ssh"` when you want OpenClaw to sandbox `exec`, file tools, and media reads on
71+
an arbitrary SSH-accessible machine.
72+
73+
```json5
74+
{
75+
agents: {
76+
defaults: {
77+
sandbox: {
78+
mode: "all",
79+
backend: "ssh",
80+
scope: "session",
81+
workspaceAccess: "rw",
82+
ssh: {
83+
target: "user@gateway-host:22",
84+
workspaceRoot: "/tmp/openclaw-sandboxes",
85+
strictHostKeyChecking: true,
86+
updateHostKeys: true,
87+
identityFile: "~/.ssh/id_ed25519",
88+
certificateFile: "~/.ssh/id_ed25519-cert.pub",
89+
knownHostsFile: "~/.ssh/known_hosts",
90+
// Or use SecretRefs / inline contents instead of local files:
91+
// identityData: { source: "env", provider: "default", id: "SSH_IDENTITY" },
92+
// certificateData: { source: "env", provider: "default", id: "SSH_CERTIFICATE" },
93+
// knownHostsData: { source: "env", provider: "default", id: "SSH_KNOWN_HOSTS" },
94+
},
95+
},
96+
},
97+
},
98+
}
99+
```
100+
101+
How it works:
102+
103+
- OpenClaw creates a per-scope remote root under `sandbox.ssh.workspaceRoot`.
104+
- On first use after create or recreate, OpenClaw seeds that remote workspace from the local workspace once.
105+
- After that, `exec`, `read`, `write`, `edit`, `apply_patch`, prompt media reads, and inbound media staging run directly against the remote workspace over SSH.
106+
- OpenClaw does not sync remote changes back to the local workspace automatically.
107+
108+
This is a **remote-canonical** model. The remote SSH workspace becomes the real sandbox state after the initial seed.
109+
110+
Important consequences:
111+
112+
- Host-local edits made outside OpenClaw after the seed step are not visible remotely until you recreate the sandbox.
113+
- `openclaw sandbox recreate` deletes the per-scope remote root and seeds again from local on next use.
114+
- Browser sandboxing is not supported on the SSH backend.
115+
- `sandbox.docker.*` settings do not apply to the SSH backend.
116+
66117
```json5
67118
{
68119
agents: {
@@ -96,6 +147,9 @@ OpenShell modes:
96147
- `mirror` (default): local workspace stays canonical. OpenClaw syncs local files into OpenShell before exec and syncs the remote workspace back after exec.
97148
- `remote`: OpenShell workspace is canonical after the sandbox is created. OpenClaw seeds the remote workspace once from the local workspace, then file tools and exec run directly against the remote sandbox without syncing changes back.
98149

150+
OpenShell reuses the same core SSH transport and remote filesystem bridge as the generic SSH backend.
151+
The plugin adds OpenShell-specific lifecycle (`sandbox create/get/delete`, `sandbox ssh-config`) and the optional `mirror` mode.
152+
99153
Current OpenShell limitations:
100154

101155
- sandbox browser is not supported yet
@@ -136,6 +190,7 @@ Behavior:
136190
- After that, `exec`, `read`, `write`, `edit`, and `apply_patch` operate directly against the remote OpenShell workspace.
137191
- OpenClaw does **not** sync remote changes back into the local workspace after exec.
138192
- Prompt-time media reads still work because file and media tools read through the sandbox bridge instead of assuming a local host path.
193+
- Transport is SSH into the OpenShell sandbox returned by `openshell sandbox ssh-config`.
139194

140195
Important consequences:
141196

‎docs/gateway/secrets.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Examples of inactive surfaces:
4141
- Web search provider-specific keys that are not selected by `tools.web.search.provider`.
4242
In auto mode (provider unset), keys are consulted by precedence for provider auto-detection until one resolves.
4343
After selection, non-selected provider keys are treated as inactive until selected.
44+
- Sandbox SSH auth material (`agents.defaults.sandbox.ssh.identityData`,
45+
`certificateData`, `knownHostsData`, plus per-agent overrides) is active only
46+
when the effective sandbox backend is `ssh` for the default agent or an enabled agent.
4447
- `gateway.remote.token` / `gateway.remote.password` SecretRefs are active if one of these is true:
4548
- `gateway.mode=remote`
4649
- `gateway.remote.url` is configured

‎extensions/openshell/src/backend.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe("openshell backend manager", () => {
101101
image: "openclaw",
102102
configLabelKind: "Source",
103103
},
104+
config: {},
104105
});
105106

106107
expect(cliMocks.runOpenShellCli).toHaveBeenCalledWith({

‎extensions/openshell/src/backend.ts‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,44 @@ import path from "node:path";
44
import type {
55
CreateSandboxBackendParams,
66
OpenClawConfig,
7+
RemoteShellSandboxHandle,
78
SandboxBackendCommandParams,
89
SandboxBackendCommandResult,
910
SandboxBackendFactory,
1011
SandboxBackendHandle,
1112
SandboxBackendManager,
13+
SshSandboxSession,
14+
} from "openclaw/plugin-sdk/core";
15+
import {
16+
createRemoteShellSandboxFsBridge,
17+
disposeSshSandboxSession,
18+
resolvePreferredOpenClawTmpDir,
19+
runSshSandboxCommand,
1220
} from "openclaw/plugin-sdk/core";
13-
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/core";
1421
import {
1522
buildExecRemoteCommand,
1623
buildRemoteCommand,
1724
createOpenShellSshSession,
18-
disposeOpenShellSshSession,
1925
runOpenShellCli,
20-
runOpenShellSshCommand,
2126
type OpenShellExecContext,
22-
type OpenShellSshSession,
2327
} from "./cli.js";
2428
import { resolveOpenShellPluginConfig, type ResolvedOpenShellPluginConfig } from "./config.js";
2529
import { createOpenShellFsBridge } from "./fs-bridge.js";
2630
import { replaceDirectoryContents } from "./mirror.js";
27-
import { createOpenShellRemoteFsBridge } from "./remote-fs-bridge.js";
2831

2932
type CreateOpenShellSandboxBackendFactoryParams = {
3033
pluginConfig: ResolvedOpenShellPluginConfig;
3134
};
3235

3336
type PendingExec = {
34-
sshSession: OpenShellSshSession;
37+
sshSession: SshSandboxSession;
3538
};
3639

37-
export type OpenShellSandboxBackend = SandboxBackendHandle & {
38-
mode: "mirror" | "remote";
39-
remoteWorkspaceDir: string;
40-
remoteAgentWorkspaceDir: string;
41-
runRemoteShellScript(params: SandboxBackendCommandParams): Promise<SandboxBackendCommandResult>;
42-
syncLocalPathToRemote(localPath: string, remotePath: string): Promise<void>;
43-
};
40+
export type OpenShellSandboxBackend = SandboxBackendHandle &
41+
RemoteShellSandboxHandle & {
42+
mode: "mirror" | "remote";
43+
syncLocalPathToRemote(localPath: string, remotePath: string): Promise<void>;
44+
};
4445

4546
export function createOpenShellSandboxBackendFactory(
4647
params: CreateOpenShellSandboxBackendFactoryParams,
@@ -129,9 +130,9 @@ async function createOpenShellSandboxBackend(params: {
129130
runShellCommand: async (command) => await impl.runRemoteShellScript(command),
130131
createFsBridge: ({ sandbox }) =>
131132
params.pluginConfig.mode === "remote"
132-
? createOpenShellRemoteFsBridge({
133+
? createRemoteShellSandboxFsBridge({
133134
sandbox,
134-
backend: impl.asHandle(),
135+
runtime: impl.asHandle(),
135136
})
136137
: createOpenShellFsBridge({
137138
sandbox,
@@ -186,9 +187,9 @@ class OpenShellSandboxBackendImpl {
186187
runShellCommand: async (command) => await self.runRemoteShellScript(command),
187188
createFsBridge: ({ sandbox }) =>
188189
this.params.execContext.config.mode === "remote"
189-
? createOpenShellRemoteFsBridge({
190+
? createRemoteShellSandboxFsBridge({
190191
sandbox,
191-
backend: self.asHandle(),
192+
runtime: self.asHandle(),
192193
})
193194
: createOpenShellFsBridge({
194195
sandbox,
@@ -242,7 +243,7 @@ class OpenShellSandboxBackendImpl {
242243
}
243244
} finally {
244245
if (token?.sshSession) {
245-
await disposeOpenShellSshSession(token.sshSession);
246+
await disposeSshSandboxSession(token.sshSession);
246247
}
247248
}
248249
}
@@ -262,7 +263,7 @@ class OpenShellSandboxBackendImpl {
262263
context: this.params.execContext,
263264
});
264265
try {
265-
return await runOpenShellSshCommand({
266+
return await runSshSandboxCommand({
266267
session,
267268
remoteCommand: buildRemoteCommand([
268269
"/bin/sh",
@@ -276,7 +277,7 @@ class OpenShellSandboxBackendImpl {
276277
signal: params.signal,
277278
});
278279
} finally {
279-
await disposeOpenShellSshSession(session);
280+
await disposeSshSandboxSession(session);
280281
}
281282
}
282283

0 commit comments

Comments
 (0)