Skip to content

Commit e772b5e

Browse files
shanselmanCopilot
andcommitted
security: harden system.run approval checks
Closes #184 by blocking dangerous environment overrides and by re-evaluating nested shell-wrapper payloads and chained commands against the exec approval policy. This extends the partial env-only approach discussed in PR #186 so the Windows node closes both vectors called out in the issue. Co-authored-by: Copilot <[email protected]>
1 parent e46dfe7 commit e772b5e

6 files changed

Lines changed: 561 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ When Node Mode is enabled in Settings, your Windows PC becomes a **node** that t
250250

251251
> 🔒 **Exec Policy**: `system.run` is gated by an approval policy on the Windows node at `%LOCALAPPDATA%\OpenClawTray\exec-policy.json` (schema: `{ "defaultAction": "...", "rules": [...] }`). This is separate from gateway-side `~/.openclaw/exec-approvals.json`.
252252
>
253-
> Rules are matched against the `command` token (`argv[0]`). If your call runs `powershell.exe -File script.ps1`, allow `powershell.exe`/`pwsh.exe` (not just the script path), or you'll get `No matching rule; default policy applied`.
253+
> Rules are matched against the full command line. Known wrapper payloads such as `cmd /c ...`, `powershell -Command ...`, `pwsh -EncodedCommand ...`, and `bash -c ...` are also evaluated before execution. Dangerous environment overrides like `PATH`, `PATHEXT`, `NODE_OPTIONS`, `GIT_SSH_COMMAND`, `LD_*`, and `DYLD_*` are rejected.
254254
>
255255
> ```bash
256256
> openclaw nodes invoke --node <id> --command system.execApprovals.set --params '{"rules":[{"pattern":"powershell.exe","action":"allow"},{"pattern":"pwsh.exe","action":"allow"},{"pattern":"echo *","action":"allow"},{"pattern":"*","action":"deny"}],"defaultAction":"deny"}'

‎src/OpenClaw.Shared/Capabilities/SystemCapability.cs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,22 @@ private async Task<NodeInvokeResponse> HandleRunAsync(NodeInvokeRequest request)
255255
request.Args.TryGetProperty("env", out var envEl) &&
256256
envEl.ValueKind == System.Text.Json.JsonValueKind.Object)
257257
{
258-
env = new Dictionary<string, string>();
258+
env = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
259259
foreach (var prop in envEl.EnumerateObject())
260260
{
261261
if (prop.Value.ValueKind == System.Text.Json.JsonValueKind.String)
262262
env[prop.Name] = prop.Value.GetString() ?? "";
263263
}
264264
}
265+
266+
var envResult = ExecEnvSanitizer.Sanitize(env);
267+
if (envResult.Blocked.Length > 0)
268+
{
269+
var blockedList = string.Join(", ", envResult.Blocked.OrderBy(n => n, StringComparer.OrdinalIgnoreCase));
270+
Logger.Warn($"system.run DENIED: blocked environment overrides [{blockedList}]");
271+
return Error($"Unsafe environment variable override blocked: {blockedList}");
272+
}
273+
env = envResult.Allowed;
265274

266275
// Build the full command string for policy evaluation and logging.
267276
// When command arrives as an argv array, we must evaluate the entire
@@ -282,6 +291,23 @@ private async Task<NodeInvokeResponse> HandleRunAsync(NodeInvokeRequest request)
282291
Logger.Warn($"system.run DENIED: {fullCommand} ({approval.Reason})");
283292
return Error($"Command denied by exec policy: {approval.Reason}");
284293
}
294+
295+
var parseResult = ExecShellWrapperParser.Expand(fullCommand, shell);
296+
if (!string.IsNullOrWhiteSpace(parseResult.Error))
297+
{
298+
Logger.Warn($"system.run DENIED: {fullCommand} ({parseResult.Error})");
299+
return Error($"Command denied by exec policy: {parseResult.Error}");
300+
}
301+
302+
foreach (var target in parseResult.Targets)
303+
{
304+
var innerApproval = _approvalPolicy.Evaluate(target.Command, target.Shell);
305+
if (!innerApproval.Allowed)
306+
{
307+
Logger.Warn($"system.run DENIED: {target.Command} ({innerApproval.Reason})");
308+
return Error($"Command denied by exec policy: {innerApproval.Reason}");
309+
}
310+
}
285311
}
286312

287313
try
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace OpenClaw.Shared;
5+
6+
internal sealed class ExecEnvSanitizeResult
7+
{
8+
public Dictionary<string, string>? Allowed { get; init; }
9+
public string[] Blocked { get; init; } = Array.Empty<string>();
10+
}
11+
12+
internal static class ExecEnvSanitizer
13+
{
14+
private static readonly HashSet<string> _blockedNames = new(StringComparer.OrdinalIgnoreCase)
15+
{
16+
"PATH",
17+
"PATHEXT",
18+
"ComSpec",
19+
"PSModulePath",
20+
"NODE_OPTIONS",
21+
"NODE_PATH",
22+
"PYTHONPATH",
23+
"PYTHONSTARTUP",
24+
"PYTHONUSERBASE",
25+
"RUBYOPT",
26+
"RUBYLIB",
27+
"PERL5OPT",
28+
"PERL5LIB",
29+
"PERLIO",
30+
"GIT_SSH",
31+
"GIT_SSH_COMMAND",
32+
"GIT_EXEC_PATH",
33+
"GIT_PROXY_COMMAND",
34+
"GIT_ASKPASS",
35+
"BASH_ENV",
36+
"ENV",
37+
"CDPATH",
38+
"PROMPT_COMMAND",
39+
"ZDOTDIR",
40+
"LD_PRELOAD",
41+
"LD_LIBRARY_PATH",
42+
"LD_AUDIT",
43+
"DYLD_INSERT_LIBRARIES",
44+
"DYLD_LIBRARY_PATH"
45+
};
46+
47+
internal static ExecEnvSanitizeResult Sanitize(Dictionary<string, string>? env)
48+
{
49+
if (env is not { Count: > 0 })
50+
{
51+
return new ExecEnvSanitizeResult { Allowed = env };
52+
}
53+
54+
var allowed = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
55+
var blocked = new List<string>();
56+
57+
foreach (var (name, value) in env)
58+
{
59+
if (IsBlocked(name))
60+
{
61+
blocked.Add(name);
62+
continue;
63+
}
64+
65+
allowed[name] = value;
66+
}
67+
68+
return new ExecEnvSanitizeResult
69+
{
70+
Allowed = allowed.Count > 0 ? allowed : null,
71+
Blocked = blocked.ToArray()
72+
};
73+
}
74+
75+
internal static bool IsBlocked(string? name)
76+
{
77+
if (string.IsNullOrWhiteSpace(name))
78+
return true;
79+
80+
if (name.IndexOfAny(['=', '\0', '\r', '\n']) >= 0)
81+
return true;
82+
83+
foreach (var c in name)
84+
{
85+
if (char.IsControl(c) || char.IsWhiteSpace(c))
86+
return true;
87+
}
88+
89+
return _blockedNames.Contains(name)
90+
|| name.StartsWith("LD_", StringComparison.OrdinalIgnoreCase)
91+
|| name.StartsWith("DYLD_", StringComparison.OrdinalIgnoreCase);
92+
}
93+
}

0 commit comments

Comments
 (0)