Skip to content

Commit ce3294d

Browse files
authored
Validate MXC SDK 0.7.0 on Windows build 26200.8728 (#786)
1 parent 638386e commit ce3294d

28 files changed

Lines changed: 2963 additions & 381 deletions

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

Lines changed: 104 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,15 @@ private NodeInvokeResponse HandleRunPrepare(NodeInvokeRequest request)
275275

276276
var command = argv[0];
277277
var rawCommand = GetStringArg(request.Args, "rawCommand");
278+
var requestedShell = GetStringArg(request.Args, "shell");
279+
var effectiveShell = _commandRunner?.ResolveEffectiveShell(requestedShell)
280+
?? ResolveDefaultEffectiveShell(requestedShell);
278281
var cwd = GetStringArg(request.Args, "cwd");
279282
var agentId = GetStringArg(request.Args, "agentId");
280283
var sessionKey = request.SessionKey ?? GetStringArg(request.Args, "sessionKey");
281284

282-
Logger.Info($"system.run.prepare: {rawCommand} (cwd={cwd ?? "default"})");
285+
Logger.Info(
286+
$"system.run.prepare: {rawCommand} (shell={effectiveShell}, requestedShell={requestedShell ?? "auto"}, cwd={cwd ?? "default"})");
283287

284288
return Success(new
285289
{
@@ -289,11 +293,27 @@ private NodeInvokeResponse HandleRunPrepare(NodeInvokeRequest request)
289293
argv,
290294
cwd,
291295
rawCommand,
296+
requestedShell = string.IsNullOrWhiteSpace(requestedShell) ? null : requestedShell.Trim(),
297+
effectiveShell,
292298
agentId,
293299
sessionKey
294300
}
295301
});
296302
}
303+
304+
private static string ResolveDefaultEffectiveShell(string? requestedShell)
305+
{
306+
if (string.IsNullOrWhiteSpace(requestedShell))
307+
return "powershell";
308+
309+
return requestedShell.Trim().ToLowerInvariant() switch
310+
{
311+
"cmd" => "cmd",
312+
"pwsh" => "pwsh",
313+
"powershell" => "powershell",
314+
_ => "powershell",
315+
};
316+
}
297317

298318
private async Task<NodeInvokeResponse> HandleRunAsync(NodeInvokeRequest request)
299319
{
@@ -400,50 +420,35 @@ private async Task<NodeInvokeResponse> HandleRunAsync(NodeInvokeRequest request)
400420
var fullCommand = args != null
401421
? FormatExecCommand([command!, ..args])
402422
: command;
423+
var requestedShell = string.IsNullOrWhiteSpace(shell) ? null : shell.Trim();
424+
var effectiveShell = _commandRunner.ResolveEffectiveShell(requestedShell);
425+
var approvedHostFallbackShell = _commandRunner is IHostFallbackAwareCommandRunner fallbackAwareRunner
426+
? fallbackAwareRunner.ResolveHostFallbackShellForApproval(requestedShell, effectiveShell)
427+
: null;
403428

404-
Logger.Info($"system.run: {fullCommand} (shell={shell ?? "auto"}, timeout={timeoutMs}ms)");
429+
Logger.Info($"system.run: {fullCommand} (shell={effectiveShell}, requestedShell={shell ?? "auto"}, timeout={timeoutMs}ms)");
405430

406431
// Check exec approval policy
407432
if (_approvalPolicy != null)
408433
{
409-
var approval = _approvalPolicy.Evaluate(fullCommand, shell);
410-
var approvalCheck = await EnsureApprovedAsync(fullCommand, shell, approval, sessionKey, correlationId);
411-
if (!approvalCheck.Allowed)
412-
{
413-
Logger.Warn($"system.run DENIED: {fullCommand} ({approval.Reason})");
414-
return Error($"Command denied by exec policy: {approval.Reason}");
415-
}
416-
417-
var outerApprovalCoversNestedTargets =
418-
approvalCheck.PromptDecisionKind != null ||
419-
IsExactAllowRuleForCommand(approval, fullCommand);
420-
421-
var parseResult = ExecShellWrapperParser.Expand(fullCommand, shell);
422-
if (!string.IsNullOrWhiteSpace(parseResult.Error))
423-
{
424-
Logger.Warn($"system.run DENIED: {fullCommand} ({parseResult.Error})");
425-
return Error($"Command denied by exec policy: {parseResult.Error}");
426-
}
434+
var approvalError = await EnsureCommandAndNestedTargetsApprovedAsync(
435+
fullCommand,
436+
effectiveShell,
437+
sessionKey,
438+
correlationId);
439+
if (approvalError != null)
440+
return approvalError;
427441

428-
foreach (var target in parseResult.Targets)
442+
if (!string.IsNullOrWhiteSpace(approvedHostFallbackShell)
443+
&& !string.Equals(approvedHostFallbackShell, effectiveShell, StringComparison.OrdinalIgnoreCase))
429444
{
430-
var innerApproval = _approvalPolicy.Evaluate(target.Command, target.Shell);
431-
if (outerApprovalCoversNestedTargets && !IsExplicitDeny(innerApproval))
432-
{
433-
if (!innerApproval.Allowed)
434-
{
435-
Logger.Info(
436-
$"system.run nested approval covered by approved wrapper: {target.Command} ({innerApproval.Reason})");
437-
}
438-
continue;
439-
}
440-
441-
var innerApprovalCheck = await EnsureApprovedAsync(target.Command, target.Shell, innerApproval, sessionKey, correlationId);
442-
if (!innerApprovalCheck.Allowed)
443-
{
444-
Logger.Warn($"system.run DENIED: {target.Command} ({innerApproval.Reason})");
445-
return Error($"Command denied by exec policy: {innerApproval.Reason}");
446-
}
445+
approvalError = await EnsureCommandAndNestedTargetsApprovedAsync(
446+
fullCommand,
447+
approvedHostFallbackShell,
448+
sessionKey,
449+
correlationId);
450+
if (approvalError != null)
451+
return approvalError;
447452
}
448453
}
449454

@@ -453,10 +458,12 @@ private async Task<NodeInvokeResponse> HandleRunAsync(NodeInvokeRequest request)
453458
{
454459
Command = command,
455460
Args = args,
456-
Shell = shell,
461+
Shell = requestedShell,
457462
Cwd = cwd,
458463
TimeoutMs = timeoutMs,
459-
Env = env
464+
Env = env,
465+
ApprovedEffectiveShell = effectiveShell,
466+
ApprovedHostFallbackShell = approvedHostFallbackShell
460467
});
461468

462469
return Success(new
@@ -531,6 +538,63 @@ private async Task<ExecApprovalCheckResult> EnsureApprovedAsync(
531538
return new ExecApprovalCheckResult(true, decision.Kind);
532539
}
533540

541+
private async Task<NodeInvokeResponse?> EnsureCommandAndNestedTargetsApprovedAsync(
542+
string fullCommand,
543+
string? shell,
544+
string? sessionKey,
545+
string correlationId)
546+
{
547+
if (_approvalPolicy == null)
548+
return null;
549+
550+
var approval = _approvalPolicy.Evaluate(fullCommand, shell);
551+
var approvalCheck = await EnsureApprovedAsync(fullCommand, shell, approval, sessionKey, correlationId);
552+
if (!approvalCheck.Allowed)
553+
{
554+
Logger.Warn($"system.run DENIED: {fullCommand} ({approval.Reason})");
555+
return Error($"Command denied by exec policy: {approval.Reason}");
556+
}
557+
558+
var outerApprovalCoversNestedTargets =
559+
approvalCheck.PromptDecisionKind != null ||
560+
IsExactAllowRuleForCommand(approval, fullCommand);
561+
562+
var parseResult = ExecShellWrapperParser.Expand(fullCommand, shell);
563+
if (!string.IsNullOrWhiteSpace(parseResult.Error))
564+
{
565+
Logger.Warn($"system.run DENIED: {fullCommand} ({parseResult.Error})");
566+
return Error($"Command denied by exec policy: {parseResult.Error}");
567+
}
568+
569+
foreach (var target in parseResult.Targets)
570+
{
571+
var innerApproval = _approvalPolicy.Evaluate(target.Command, target.Shell);
572+
if (outerApprovalCoversNestedTargets && !IsExplicitDeny(innerApproval))
573+
{
574+
if (!innerApproval.Allowed)
575+
{
576+
Logger.Info(
577+
$"system.run nested approval covered by approved wrapper: {target.Command} ({innerApproval.Reason})");
578+
}
579+
continue;
580+
}
581+
582+
var innerApprovalCheck = await EnsureApprovedAsync(
583+
target.Command,
584+
target.Shell,
585+
innerApproval,
586+
sessionKey,
587+
correlationId);
588+
if (!innerApprovalCheck.Allowed)
589+
{
590+
Logger.Warn($"system.run DENIED: {target.Command} ({innerApproval.Reason})");
591+
return Error($"Command denied by exec policy: {innerApproval.Reason}");
592+
}
593+
}
594+
595+
return null;
596+
}
597+
534598
private static bool CanPersistExactAllowRule(string command) =>
535599
!string.IsNullOrWhiteSpace(command) &&
536600
command.IndexOfAny(['*', '?']) < 0;

‎src/OpenClaw.Shared/ICommandRunner.cs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ public class CommandRequest
3535

3636
/// <summary>Additional environment variables</summary>
3737
public Dictionary<string, string>? Env { get; set; }
38+
39+
/// <summary>
40+
/// Optional effective shell that already passed shell-scoped approval.
41+
/// Dynamic runners must execute this shell, or a separately approved host
42+
/// fallback shell, so live settings cannot change the approved boundary.
43+
/// </summary>
44+
public string? ApprovedEffectiveShell { get; set; }
45+
46+
/// <summary>
47+
/// Optional host fallback shell that has already passed shell-scoped approval.
48+
/// Sandboxed runners use this only when a compatibility fallback would execute
49+
/// a different host shell than the sandbox effective shell.
50+
/// </summary>
51+
public string? ApprovedHostFallbackShell { get; set; }
3852
}
3953

4054
/// <summary>
@@ -57,7 +71,39 @@ public interface ICommandRunner
5771
{
5872
/// <summary>Human-readable name of this runner (e.g., "local", "docker", "wsl")</summary>
5973
string Name { get; }
74+
75+
/// <summary>
76+
/// Resolve the shell that will actually execute the request. Approval checks
77+
/// must use this value so shell-scoped rules cannot approve one shell while
78+
/// the runner executes another.
79+
/// </summary>
80+
string ResolveEffectiveShell(string? requestedShell)
81+
{
82+
if (string.IsNullOrWhiteSpace(requestedShell))
83+
return "powershell";
84+
85+
return requestedShell.Trim().ToLowerInvariant() switch
86+
{
87+
"cmd" => "cmd",
88+
"pwsh" => "pwsh",
89+
"powershell" => "powershell",
90+
_ => "powershell",
91+
};
92+
}
6093

6194
/// <summary>Execute a command and return the result.</summary>
6295
Task<CommandResult> RunAsync(CommandRequest request, CancellationToken ct = default);
6396
}
97+
98+
/// <summary>
99+
/// Optional contract for runners that may preserve compatibility through an
100+
/// uncontained host fallback with a shell different from their sandbox shell.
101+
/// </summary>
102+
public interface IHostFallbackAwareCommandRunner : ICommandRunner
103+
{
104+
/// <summary>
105+
/// Returns the host fallback shell that needs separate approval, or null when
106+
/// fallback cannot change the already-approved effective shell.
107+
/// </summary>
108+
string? ResolveHostFallbackShellForApproval(string? requestedShell, string effectiveShell);
109+
}

‎src/OpenClaw.Shared/LocalCommandRunner.cs‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace OpenClaw.Shared;
88

99
/// <summary>
10-
/// Executes commands locally via Process.Start (pwsh.exe / cmd.exe).
10+
/// Executes commands locally via Process.Start (pwsh.exe / powershell.exe / cmd.exe).
1111
/// This is the default runner. Swap with DockerCommandRunner, WslCommandRunner, etc.
1212
/// </summary>
1313
public class LocalCommandRunner : ICommandRunner
@@ -22,6 +22,8 @@ public LocalCommandRunner(IOpenClawLogger? logger = null)
2222
{
2323
_logger = logger ?? NullLogger.Instance;
2424
}
25+
26+
public string ResolveEffectiveShell(string? requestedShell) => ResolveEffectiveShellName(requestedShell);
2527

2628
public async Task<CommandResult> RunAsync(CommandRequest request, CancellationToken ct = default)
2729
{
@@ -232,9 +234,10 @@ private static void ValidateDirectExecutable(string executable)
232234
$"Direct-argv mode cannot guarantee argv fidelity for batch scripts: {executable}", nameof(executable));
233235
}
234236

235-
private static (string fileName, string arguments) BuildProcessArgs(CommandRequest request)
237+
internal static (string fileName, string arguments) BuildProcessArgs(CommandRequest request, string? pathEnvVar = null)
236238
{
237-
var shell = request.Shell ?? "powershell";
239+
var defaultShell = string.IsNullOrWhiteSpace(request.Shell);
240+
var shell = ResolveEffectiveShellName(request.Shell, pathEnvVar);
238241
var command = request.Command;
239242
var isCmd = shell.Equals("cmd", StringComparison.OrdinalIgnoreCase);
240243

@@ -249,8 +252,66 @@ private static (string fileName, string arguments) BuildProcessArgs(CommandReque
249252
if (isCmd)
250253
return ("cmd.exe", $"/C {command}");
251254
if (shell.Equals("pwsh", StringComparison.OrdinalIgnoreCase))
252-
return ("pwsh.exe", $"-NoProfile -NonInteractive -Command {command}");
253-
return ("powershell.exe", $"-NoProfile -NonInteractive -Command {command}");
255+
{
256+
var pwshPath = ResolveOnPath("pwsh.exe", pathEnvVar);
257+
if (pwshPath is not null || !defaultShell)
258+
return (pwshPath ?? "pwsh.exe", $"-NoProfile -NonInteractive -Command {command}");
259+
}
260+
261+
return (ResolveWindowsPowerShellExe(), $"-NoProfile -NonInteractive -Command {command}");
262+
}
263+
264+
internal static string ResolveEffectiveShellName(string? requestedShell)
265+
=> ResolveEffectiveShellName(requestedShell, pathEnvVar: null);
266+
267+
private static string ResolveEffectiveShellName(string? requestedShell, string? pathEnvVar)
268+
{
269+
if (!string.IsNullOrWhiteSpace(requestedShell))
270+
{
271+
return requestedShell.Trim().ToLowerInvariant() switch
272+
{
273+
"cmd" => "cmd",
274+
"pwsh" => "pwsh",
275+
"powershell" => "powershell",
276+
_ => "powershell",
277+
};
278+
}
279+
280+
return "powershell";
281+
}
282+
283+
private static string? ResolveOnPath(string executableName, string? pathEnvVar = null)
284+
{
285+
var path = pathEnvVar
286+
?? Environment.GetEnvironmentVariable("PATH")
287+
?? Environment.GetEnvironmentVariable("Path");
288+
if (string.IsNullOrWhiteSpace(path))
289+
return null;
290+
291+
foreach (var dir in path.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
292+
{
293+
try
294+
{
295+
var candidate = Path.Combine(dir, executableName);
296+
if (File.Exists(candidate))
297+
return candidate;
298+
}
299+
catch
300+
{
301+
// Ignore malformed PATH entries.
302+
}
303+
}
304+
305+
return null;
306+
}
307+
308+
private static string ResolveWindowsPowerShellExe()
309+
{
310+
var systemRoot = Environment.GetEnvironmentVariable("SystemRoot")
311+
?? Environment.GetEnvironmentVariable("windir");
312+
return string.IsNullOrWhiteSpace(systemRoot)
313+
? "powershell.exe"
314+
: Path.Combine(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
254315
}
255316

256317
/// <summary>

0 commit comments

Comments
 (0)