Skip to content

Commit b0aa479

Browse files
AlexAlves87claude
andcommitted
feat: add structural input validation for system.run exec approvals
Adds ExecApprovalV2InputValidator (phase 1 of the V2 exec approval pipeline) and its typed output ValidatedRunRequest. Validates argv, cwd, env, and timeout with fail-closed typed denials; no resolution, policy evaluation, or production wiring. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent cbd3cf7 commit b0aa479

3 files changed

Lines changed: 804 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json;
3+
4+
namespace OpenClaw.Shared.ExecApprovals;
5+
6+
/// <summary>
7+
/// Phase 1 of the V2 exec approval pipeline: structural input validation (rail 18, step 1).
8+
/// Parses a raw NodeInvokeRequest into a ValidatedRunRequest or returns validation-failed.
9+
/// Does not resolve executables, detect shell wrappers, or evaluate policy.
10+
/// </summary>
11+
public static class ExecApprovalV2InputValidator
12+
{
13+
private const int DefaultTimeoutMs = 30_000;
14+
15+
public static ExecApprovalV2ValidationOutcome Validate(NodeInvokeRequest request)
16+
{
17+
var argv = TryParseArgv(request.Args, out bool malformedCommand);
18+
if (malformedCommand)
19+
return Deny("malformed-command");
20+
if (argv == null || argv.Length == 0)
21+
return Deny("missing-command");
22+
if (string.IsNullOrWhiteSpace(argv[0]))
23+
return Deny("empty-command");
24+
25+
// cwd — optional, but empty/whitespace is a caller error; wrong type is a protocol violation
26+
string? cwd = null;
27+
if (request.Args.ValueKind == JsonValueKind.Object &&
28+
request.Args.TryGetProperty("cwd", out var cwdEl))
29+
{
30+
if (cwdEl.ValueKind != JsonValueKind.String)
31+
return Deny("malformed-cwd");
32+
var rawCwd = cwdEl.GetString();
33+
if (string.IsNullOrWhiteSpace(rawCwd))
34+
return Deny("empty-cwd");
35+
cwd = rawCwd;
36+
}
37+
38+
// env — must be a JSON object if present; non-string values are a protocol violation
39+
IReadOnlyDictionary<string, string>? env = null;
40+
if (request.Args.ValueKind == JsonValueKind.Object &&
41+
request.Args.TryGetProperty("env", out var envEl))
42+
{
43+
if (envEl.ValueKind != JsonValueKind.Object)
44+
return Deny("malformed-env");
45+
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
46+
foreach (var prop in envEl.EnumerateObject())
47+
{
48+
if (prop.Value.ValueKind != JsonValueKind.String)
49+
return Deny("malformed-env");
50+
dict[prop.Name] = prop.Value.GetString() ?? "";
51+
}
52+
env = dict;
53+
}
54+
55+
// timeoutMs / timeout — positive integer; defaults to 30 000
56+
var timeoutMs = DefaultTimeoutMs;
57+
if (request.Args.ValueKind == JsonValueKind.Object)
58+
{
59+
if (request.Args.TryGetProperty("timeoutMs", out var tmsEl))
60+
{
61+
if (tmsEl.ValueKind != JsonValueKind.Number || !tmsEl.TryGetInt32(out var v) || v <= 0)
62+
return Deny("invalid-timeout");
63+
timeoutMs = v;
64+
}
65+
else if (request.Args.TryGetProperty("timeout", out var tEl))
66+
{
67+
if (tEl.ValueKind != JsonValueKind.Number || !tEl.TryGetInt32(out var v) || v <= 0)
68+
return Deny("invalid-timeout");
69+
timeoutMs = v;
70+
}
71+
}
72+
73+
return ExecApprovalV2ValidationOutcome.Ok(new ValidatedRunRequest(
74+
argv,
75+
TryGetString(request.Args, "shell"),
76+
cwd,
77+
timeoutMs,
78+
env,
79+
TryGetString(request.Args, "agentId"),
80+
TryGetString(request.Args, "sessionKey")));
81+
}
82+
83+
private static ExecApprovalV2ValidationOutcome Deny(string reason)
84+
=> ExecApprovalV2ValidationOutcome.Fail(ExecApprovalV2Result.ValidationFailed(reason));
85+
86+
private static string[]? TryParseArgv(JsonElement args, out bool malformed)
87+
{
88+
malformed = false;
89+
if (args.ValueKind != JsonValueKind.Object ||
90+
!args.TryGetProperty("command", out var cmdEl))
91+
return null;
92+
93+
if (cmdEl.ValueKind == JsonValueKind.Array)
94+
{
95+
var list = new List<string>();
96+
foreach (var item in cmdEl.EnumerateArray())
97+
{
98+
if (item.ValueKind != JsonValueKind.String) { malformed = true; return null; }
99+
list.Add(item.GetString()?.Trim() ?? "");
100+
}
101+
return list.Count > 0 ? [.. list] : null;
102+
}
103+
104+
if (cmdEl.ValueKind == JsonValueKind.String)
105+
{
106+
var cmd = cmdEl.GetString()?.Trim();
107+
if (string.IsNullOrEmpty(cmd)) return null;
108+
109+
// Also merge a separate "args" array when command is a bare string
110+
if (args.TryGetProperty("args", out var argsEl) &&
111+
argsEl.ValueKind == JsonValueKind.Array)
112+
{
113+
var list = new List<string> { cmd };
114+
foreach (var item in argsEl.EnumerateArray())
115+
{
116+
if (item.ValueKind != JsonValueKind.String) { malformed = true; return null; }
117+
list.Add(item.GetString()?.Trim() ?? "");
118+
}
119+
return [.. list];
120+
}
121+
return [cmd];
122+
}
123+
124+
return null;
125+
}
126+
127+
private static string? TryGetString(JsonElement args, string key)
128+
{
129+
if (args.ValueKind != JsonValueKind.Object ||
130+
!args.TryGetProperty(key, out var el) ||
131+
el.ValueKind != JsonValueKind.String)
132+
return null;
133+
return el.GetString();
134+
}
135+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections.Generic;
2+
3+
namespace OpenClaw.Shared.ExecApprovals;
4+
5+
/// <summary>
6+
/// Structurally-valid system.run input produced by ExecApprovalV2InputValidator.
7+
/// Argv is guaranteed non-empty with a non-blank first element.
8+
/// </summary>
9+
public sealed class ValidatedRunRequest
10+
{
11+
public string[] Argv { get; }
12+
public string? Shell { get; }
13+
public string? Cwd { get; }
14+
public int TimeoutMs { get; }
15+
public IReadOnlyDictionary<string, string>? Env { get; }
16+
public string? AgentId { get; }
17+
public string? SessionKey { get; }
18+
19+
internal ValidatedRunRequest(
20+
string[] argv,
21+
string? shell,
22+
string? cwd,
23+
int timeoutMs,
24+
IReadOnlyDictionary<string, string>? env,
25+
string? agentId,
26+
string? sessionKey)
27+
{
28+
Argv = argv;
29+
Shell = shell;
30+
Cwd = cwd;
31+
TimeoutMs = timeoutMs;
32+
Env = env;
33+
AgentId = agentId;
34+
SessionKey = sessionKey;
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Either a ValidatedRunRequest (IsValid=true) or a typed denial (IsValid=false).
40+
/// Produced by ExecApprovalV2InputValidator; consumed by the coordinator pipeline.
41+
/// </summary>
42+
public sealed class ExecApprovalV2ValidationOutcome
43+
{
44+
public bool IsValid { get; }
45+
public ValidatedRunRequest? Request { get; }
46+
public ExecApprovalV2Result? Error { get; }
47+
48+
private ExecApprovalV2ValidationOutcome(ValidatedRunRequest request)
49+
{
50+
IsValid = true;
51+
Request = request;
52+
}
53+
54+
private ExecApprovalV2ValidationOutcome(ExecApprovalV2Result error)
55+
{
56+
IsValid = false;
57+
Error = error;
58+
}
59+
60+
public static ExecApprovalV2ValidationOutcome Ok(ValidatedRunRequest r) => new(r);
61+
public static ExecApprovalV2ValidationOutcome Fail(ExecApprovalV2Result e) => new(e);
62+
}

0 commit comments

Comments
 (0)