Skip to content

Commit 4206611

Browse files
authored
fix(mxc): PowerShell fails in the Windows node sandbox (#1147)
Closes #1143. Adds a confirmed, default-off Windows UI API opt-in for MXC-contained commands and preserves all other sandbox and approval controls.
1 parent 582a447 commit 4206611

24 files changed

Lines changed: 456 additions & 22 deletions

‎docs/OPENCLAW_GATEWAY_NODE_EXEC_FAQ.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ The Windows app knows:
601601
602602
- whether MXC is available on this host;
603603
- whether Windows sandboxing is enabled;
604-
- local filesystem, network, clipboard, timeout, and output policies;
604+
- local filesystem, network, clipboard, Windows UI API, timeout, and output policies;
605605
- whether uncontained host fallback is allowed when MXC is unavailable.
606606
607607
The gateway knows that it routed `system.run` to a Windows node and receives the
@@ -648,6 +648,12 @@ fallback if MXC is unavailable. Enabling **block host fallback when MXC is
648648
unavailable** changes that case to a deny. The actual result reports whether
649649
execution used sandbox, host fallback, or host mode.
650650
651+
MXC also blocks Win32k system calls by default. PowerShell, `whoami`, `tasklist`,
652+
and many other console programs initialize Windows UI APIs even when they do not
653+
show a window. Enable **Allow Windows UI APIs** on the Node Sandbox page when
654+
those programs are required. This keeps filesystem, network, clipboard, timeout,
655+
and command approval controls in force, but removes the Win32k syscall boundary.
656+
651657
**Evidence:** local runner wiring is in
652658
[`NodeService.cs`](https://github.com/openclaw/openclaw-windows-node/blob/d7d153ca5d409487e06ef584b1de1184520e90e6/src/OpenClaw.Tray.WinUI/Services/NodeService.cs#L589-L679).
653659
Fallback behavior is in

‎docs/WINDOWS_NODE_TESTING.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Local MCP clients also see MCP-only `app.*` commands such as `app.navigate`, `ap
179179
```
180180

181181
### Full Gateway `system.run` MXC runtime proof
182-
- The focused E2E below provisions a fresh WSL Gateway, starts an isolated tray instance, sets local exec approval policy, invokes `system.run` through the real Gateway `node.invoke` path, and verifies tray MXC diagnostics show contained `mxc-direct-appc` execution for a bound `hostname.exe` allowlist rule, full-policy shell execution, and denied writes to the tray data directory.
182+
- The focused E2E below provisions a fresh WSL Gateway, starts an isolated tray instance, enables the explicit Windows UI API sandbox opt-in, sets local exec approval policy, invokes `system.run` through the real Gateway `node.invoke` path, and verifies tray MXC diagnostics show contained `mxc-direct-appc` execution for a bound `hostname.exe` allowlist rule, PowerShell and full-policy shell execution, and denied writes to the tray data directory.
183183
- Run it when validating the Gateway/Windows node runtime path, not just direct MCP or shared library behavior.
184184
- GitHub-hosted Actions runners do not provide a working MXC/AppContainer runtime. The regular cloud E2E matrix should report these MXC proofs as skipped while still running the rest of setup-connect. Run the proof on a local MXC-enabled Windows machine. Only set `OPENCLAW_RUN_MXC_E2E=1` in GitHub Actions when using an MXC-enabled self-hosted runner.
185185
- Use `.\scripts\validate-mxc-e2e.ps1` for normal local validation. It sets `OPENCLAW_RUN_E2E` and `OPENCLAW_RUN_MXC_E2E`, runs the real Gateway MXC proofs, and fails if the MXC proof skips. `-AllowSkip` is only for documenting a non-MXC host, not for merge validation of MXC-related work.
@@ -205,9 +205,9 @@ Local MCP clients also see MCP-only `app.*` commands such as `app.navigate`, `ap
205205

206206
- Expected proof markers:
207207
- The bound-hostname proof succeeds with a local `**/hostname.exe` rule, logs `promptAttempted=false`, and reaches MXC as `shell=<direct-argv>`.
208-
- Gateway response contains `OPENCLAW_GATEWAY_SYSTEM_RUN_MXC_OK` with `exitCode=0`.
208+
- Gateway response contains PowerShell output `OPENCLAW_GATEWAY_SYSTEM_RUN_MXC_OK` with `exitCode=0`.
209209
- The denied-write proof targets a fresh file under the isolated tray data directory, returns non-zero, and leaves that file absent.
210-
- `openclaw-tray.log` contains `[mxc] system.run sandbox request` with `executor=mxc-direct-appc` and `contained=True`.
210+
- `openclaw-tray.log` contains `[mxc] system.run sandbox request` with `executor=mxc-direct-appc`, `contained=True`, `shell=<direct-argv>`, and `uiAllowWindows=True` for the PowerShell proof.
211211
- `openclaw-tray.log` contains `[mxc] system.run sandbox result` with `containment=mxc` for both the successful execution and the denied write.
212212
- E2E artifacts are written under `TestResults\E2E\<run-id>` and skip known secret-bearing files such as gateway records and settings.
213213

‎src/OpenClaw.Shared/Mxc/MxcCommandRunner.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ public async Task<CommandResult> RunAsync(CommandRequest request, CancellationTo
217217
{
218218
return DenySandboxUnavailable(
219219
"Sandboxed system.run cannot execute PowerShell-family shells with the current MXC UI-deny policy. " +
220-
"Retry with shell='cmd' or explicitly disable sandboxing if uncontained host execution is acceptable.",
220+
"Enable 'Allow Windows UI APIs' in Node Sandbox settings, retry with shell='cmd', " +
221+
"or disable sandboxing if uncontained host execution is acceptable.",
221222
$"[mxc] system.run denied: PowerShell-family shell unsupported by MXC UI-deny policy: {ex.Message}");
222223
}
223224

@@ -453,7 +454,7 @@ private void LogSandboxRequest(
453454
"[mxc] system.run sandbox request " +
454455
$"executor={_executor.Name}; contained={_executor.IsContained}; " +
455456
$"sandboxSettings={{enabled={settings.SystemRunSandboxEnabled},blockHostFallbackWhenMxcUnavailable={settings.SystemRunBlockHostFallbackWhenMxcUnavailable}," +
456-
$"allowOutbound={settings.SystemRunAllowOutbound},clipboard={settings.SandboxClipboard},documents={settings.SandboxDocumentsAccess?.ToString() ?? "<null>"}," +
457+
$"allowOutbound={settings.SystemRunAllowOutbound},allowWindowsUi={settings.SystemRunAllowWindowsUi},clipboard={settings.SandboxClipboard},documents={settings.SandboxDocumentsAccess?.ToString() ?? "<null>"}," +
457458
$"downloads={settings.SandboxDownloadsAccess?.ToString() ?? "<null>"},desktop={settings.SandboxDesktopAccess?.ToString() ?? "<null>"}," +
458459
$"customFolderCount={settings.SandboxCustomFolders?.Count ?? 0},timeoutMs={settings.SandboxTimeoutMs},maxOutputBytes={settings.SandboxMaxOutputBytes}," +
459460
$"settingsDirectoryPath={(string.IsNullOrWhiteSpace(settingsDirectoryPath) ? "<null>" : "<set>")}}}; " +
@@ -484,6 +485,7 @@ private static object ToSandboxSettingsDiagnostic(SettingsData settings, string
484485
systemRunSandboxEnabled = settings.SystemRunSandboxEnabled,
485486
systemRunBlockHostFallbackWhenMxcUnavailable = settings.SystemRunBlockHostFallbackWhenMxcUnavailable,
486487
systemRunAllowOutbound = settings.SystemRunAllowOutbound,
488+
systemRunAllowWindowsUi = settings.SystemRunAllowWindowsUi,
487489
sandboxClipboard = settings.SandboxClipboard,
488490
sandboxDocumentsAccess = settings.SandboxDocumentsAccess,
489491
sandboxDownloadsAccess = settings.SandboxDownloadsAccess,

‎src/OpenClaw.Shared/Mxc/MxcConfigBuilder.cs‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ internal static MxcConfig Build(
7777
if (args.DirectArgv is null)
7878
{
7979
shell = NormalizeSupportedShell(args.Shell);
80-
if (IsPowerShellFamilyShell(shell) && policy?.Ui?.AllowWindows != true)
81-
{
82-
throw new NotSupportedException(
83-
"PowerShell-family shells require UI access with the Windows MXC 0.7 processcontainer backend.");
84-
}
80+
}
81+
82+
var requiresWindowsUi = args.DirectArgv is { Count: > 0 }
83+
? IsPowerShellFamilyExecutable(args.DirectArgv[0])
84+
: IsPowerShellFamilyShell(shell!);
85+
if (requiresWindowsUi && policy?.Ui?.AllowWindows != true)
86+
{
87+
throw new NotSupportedException(
88+
"PowerShell-family shells require UI access with the Windows MXC 0.7 processcontainer backend.");
8589
}
8690

8791
if (request.Env is { Count: > 0 })
@@ -555,6 +559,25 @@ private static bool IsPowerShellFamilyShell(string shell)
555559
return normalized is "powershell" or "pwsh";
556560
}
557561

562+
private static bool IsPowerShellFamilyExecutable(string executable)
563+
{
564+
if (string.IsNullOrWhiteSpace(executable))
565+
return false;
566+
567+
var fileName = GetWindowsExecutableFileName(executable);
568+
var stem = fileName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)
569+
? fileName[..^4]
570+
: fileName;
571+
return IsPowerShellFamilyShell(stem);
572+
}
573+
574+
private static string GetWindowsExecutableFileName(string executable)
575+
{
576+
var normalized = executable.Trim().Replace('\\', '/');
577+
var separator = normalized.LastIndexOf('/');
578+
return separator >= 0 ? normalized[(separator + 1)..] : normalized;
579+
}
580+
558581
private static string NormalizeSupportedShell(string shell)
559582
{
560583
var normalized = string.IsNullOrWhiteSpace(shell)

‎src/OpenClaw.Shared/Mxc/MxcPolicyBuilder.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ namespace OpenClaw.Shared.Mxc;
2121
/// credentials, ElevenLabs key), <c>~/.ssh</c>, and the common browser profile
2222
/// roots (Chrome / Edge / Firefox / Brave). Always blocked regardless of grants.</item>
2323
/// <item><c>network.allowOutbound</c> — bound by <see cref="SettingsData.SystemRunAllowOutbound"/>.</item>
24-
/// <item><c>ui</c> — default-deny in base policy. PowerShell-family shells
25-
/// need an explicit <c>allowWindows</c> policy on MXC 0.7 and fail closed under
26-
/// the default UI-deny policy.</item>
24+
/// <item><c>ui</c> — Win32k access is bound by
25+
/// <see cref="SettingsData.SystemRunAllowWindowsUi"/> and remains denied by
26+
/// default. PowerShell-family shells and many console utilities require this
27+
/// opt-in on MXC 0.7.</item>
2728
/// </list>
2829
/// </remarks>
2930
public static class MxcPolicyBuilder
@@ -114,7 +115,7 @@ public static SandboxPolicy ForSystemRun(SettingsData settings, string settingsD
114115
// exposed: MXC team confirmed only internetClient is validated today.
115116
AllowLocalNetwork: false),
116117
Ui: new UiPolicy(
117-
AllowWindows: false,
118+
AllowWindows: settings.SystemRunAllowWindowsUi,
118119
Clipboard: MapClipboard(settings.SandboxClipboard),
119120
AllowInputInjection: false),
120121
TimeoutMs: settings.SandboxTimeoutMs > 0 ? settings.SandboxTimeoutMs : null);

‎src/OpenClaw.Shared/SettingsData.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ public record class SettingsData
181181
/// </summary>
182182
public bool SystemRunAllowOutbound { get; set; } = false;
183183

184+
/// <summary>
185+
/// When sandboxed, allow system.run commands to use Windows UI system calls.
186+
/// This is required by PowerShell and many console utilities on current MXC
187+
/// processcontainer backends. Default false preserves Win32k syscall blocking.
188+
/// </summary>
189+
public bool SystemRunAllowWindowsUi { get; set; } = false;
190+
184191
/// <summary>
185192
/// Clipboard access policy inside the sandbox. Default <c>None</c> — the
186193
/// sandboxed payload cannot see or change the user's clipboard.

‎src/OpenClaw.Tray.WinUI/Pages/SandboxPage.xaml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,36 @@
386386
</StackPanel>
387387
</Expander>
388388

389+
<!-- Windows UI system calls -->
390+
<Expander IsExpanded="True" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
391+
<Expander.Header>
392+
<TextBlock x:Uid="SandboxPage_CommandCompatibility"
393+
Text="🪟 Command compatibility"
394+
Style="{StaticResource BodyStrongTextBlockStyle}" />
395+
</Expander.Header>
396+
<StackPanel Spacing="12" Padding="4">
397+
<ToggleSwitch x:Uid="SandboxPage_AllowWindowsUi"
398+
x:Name="WindowsUiToggle"
399+
AutomationProperties.AutomationId="SandboxWindowsUiToggle"
400+
AutomationProperties.Name="Allow Windows UI APIs for sandboxed commands"
401+
Header="Allow Windows UI APIs"
402+
OffContent="Off: blocks Win32k system calls"
403+
OnContent="On: PowerShell and similar tools can start"
404+
Toggled="OnWindowsUiToggled" />
405+
<TextBlock x:Uid="SandboxPage_WindowsUiCompatibilityDescription"
406+
Text="PowerShell, whoami, tasklist, and many other console tools use Windows UI APIs during startup."
407+
TextWrapping="Wrap"
408+
Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
409+
<InfoBar x:Uid="SandboxPage_WindowsUiWarning"
410+
x:Name="WindowsUiWarningBar"
411+
IsOpen="False"
412+
IsClosable="False"
413+
Severity="Warning"
414+
Title="Win32k boundary disabled"
415+
Message="File, network, clipboard, timeout, and command approval controls still apply, but Win32k system calls are allowed." />
416+
</StackPanel>
417+
</Expander>
418+
389419
<!-- Limits -->
390420
<Expander IsExpanded="False" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
391421
<Expander.Header>

‎src/OpenClaw.Tray.WinUI/Pages/SandboxPage.xaml.cs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ private sealed record SandboxPreset(
8282
string Tag,
8383
bool SandboxEnabled,
8484
bool AllowOutbound,
85+
bool AllowWindowsUi,
8586
SandboxFolderAccess? DocumentsAccess,
8687
SandboxFolderAccess? DownloadsAccess,
8788
SandboxFolderAccess? DesktopAccess,
@@ -93,6 +94,7 @@ private sealed record SandboxPreset(
9394
Tag: "LockedDown",
9495
SandboxEnabled: true,
9596
AllowOutbound: false,
97+
AllowWindowsUi: false,
9698
DocumentsAccess: null,
9799
DownloadsAccess: null,
98100
DesktopAccess: null,
@@ -104,6 +106,7 @@ private sealed record SandboxPreset(
104106
Tag: "Balanced",
105107
SandboxEnabled: true,
106108
AllowOutbound: true,
109+
AllowWindowsUi: false,
107110
DocumentsAccess: SandboxFolderAccess.ReadOnly,
108111
DownloadsAccess: SandboxFolderAccess.ReadOnly,
109112
DesktopAccess: SandboxFolderAccess.ReadOnly,
@@ -115,6 +118,7 @@ private sealed record SandboxPreset(
115118
Tag: "Permissive",
116119
SandboxEnabled: true,
117120
AllowOutbound: true,
121+
AllowWindowsUi: false,
118122
DocumentsAccess: SandboxFolderAccess.ReadWrite,
119123
DownloadsAccess: SandboxFolderAccess.ReadWrite,
120124
DesktopAccess: SandboxFolderAccess.ReadWrite,
@@ -156,6 +160,7 @@ private void LoadState()
156160
SandboxEnabledToggle.IsOn = settings.SystemRunSandboxEnabled;
157161

158162
NetInternetToggle.IsOn = settings.SystemRunAllowOutbound;
163+
WindowsUiToggle.IsOn = settings.SystemRunAllowWindowsUi;
159164

160165
SelectAccessTag(DocsAccessCombo, settings.SandboxDocumentsAccess);
161166
SelectAccessTag(DownloadsAccessCombo, settings.SandboxDownloadsAccess);
@@ -186,6 +191,7 @@ private void LoadState()
186191
}
187192

188193
NormalizeSandboxToggleForAvailability();
194+
UpdateWindowsUiWarning();
189195
UpdatePresetHighlight();
190196
UpdateSandboxStatusCard();
191197
UpdateControlsEnabledState();
@@ -459,6 +465,7 @@ private void ApplyPreset(SandboxPreset preset)
459465
{
460466
s.SystemRunSandboxEnabled = preset.SandboxEnabled;
461467
s.SystemRunAllowOutbound = preset.AllowOutbound;
468+
s.SystemRunAllowWindowsUi = preset.AllowWindowsUi;
462469
s.SandboxDocumentsAccess = preset.DocumentsAccess;
463470
s.SandboxDownloadsAccess = preset.DownloadsAccess;
464471
s.SandboxDesktopAccess = preset.DesktopAccess;
@@ -560,6 +567,7 @@ private static bool Matches(SettingsManager s, SandboxPreset p)
560567
{
561568
return s.SystemRunSandboxEnabled == p.SandboxEnabled
562569
&& s.SystemRunAllowOutbound == p.AllowOutbound
570+
&& s.SystemRunAllowWindowsUi == p.AllowWindowsUi
563571
&& s.SandboxDocumentsAccess == p.DocumentsAccess
564572
&& s.SandboxDownloadsAccess == p.DownloadsAccess
565573
&& s.SandboxDesktopAccess == p.DesktopAccess
@@ -745,6 +753,77 @@ private void OnNetInternetToggled(object sender, RoutedEventArgs e)
745753
Save();
746754
}
747755

756+
private void OnWindowsUiToggled(object sender, RoutedEventArgs e) =>
757+
AsyncEventHandlerGuard.Run(
758+
OnWindowsUiToggledAsync,
759+
new OpenClawTray.AppLogger(),
760+
nameof(OnWindowsUiToggled));
761+
762+
private async Task OnWindowsUiToggledAsync()
763+
{
764+
if (_suppress) return;
765+
if (CurrentApp.Settings is not { } s) return;
766+
767+
var newValue = WindowsUiToggle.IsOn;
768+
var oldValue = s.SystemRunAllowWindowsUi;
769+
770+
if (newValue && !oldValue)
771+
{
772+
if (_dialogOpen)
773+
{
774+
RestoreWindowsUiToggle(oldValue);
775+
return;
776+
}
777+
778+
var dialog = new ContentDialog
779+
{
780+
Title = L("SandboxPage_AllowWindowsUiDialogTitle"),
781+
Content = L("SandboxPage_AllowWindowsUiDialogContent"),
782+
PrimaryButtonText = L("SandboxPage_AllowWindowsUiDialogPrimary"),
783+
CloseButtonText = L("SandboxPage_AllowWindowsUiDialogCancel"),
784+
DefaultButton = ContentDialogButton.Close,
785+
XamlRoot = XamlRoot,
786+
};
787+
788+
ContentDialogResult result;
789+
_dialogOpen = true;
790+
try
791+
{
792+
result = await dialog.ShowAsync();
793+
}
794+
catch (System.Runtime.InteropServices.COMException)
795+
{
796+
RestoreWindowsUiToggle(oldValue);
797+
return;
798+
}
799+
finally
800+
{
801+
_dialogOpen = false;
802+
}
803+
804+
if (result != ContentDialogResult.Primary)
805+
{
806+
RestoreWindowsUiToggle(oldValue);
807+
return;
808+
}
809+
}
810+
811+
s.SystemRunAllowWindowsUi = newValue;
812+
UpdateWindowsUiWarning();
813+
Save();
814+
}
815+
816+
private void RestoreWindowsUiToggle(bool value)
817+
{
818+
_suppress = true;
819+
try { WindowsUiToggle.IsOn = value; }
820+
finally { _suppress = false; }
821+
UpdateWindowsUiWarning();
822+
}
823+
824+
private void UpdateWindowsUiWarning() =>
825+
WindowsUiWarningBar.IsOpen = WindowsUiToggle.IsOn;
826+
748827
private void OnDocsAccessChanged(object sender, SelectionChangedEventArgs e)
749828
{
750829
if (_suppress) return;

‎src/OpenClaw.Tray.WinUI/Services/NodeService.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,13 +705,15 @@ private SettingsData SnapshotSettings()
705705
SystemRunSandboxEnabled = true,
706706
SystemRunBlockHostFallbackWhenMxcUnavailable = false,
707707
SystemRunAllowOutbound = false,
708+
SystemRunAllowWindowsUi = false,
708709
};
709710

710711
return new SettingsData
711712
{
712713
SystemRunSandboxEnabled = _settings.SystemRunSandboxEnabled,
713714
SystemRunBlockHostFallbackWhenMxcUnavailable = _settings.SystemRunBlockHostFallbackWhenMxcUnavailable,
714715
SystemRunAllowOutbound = _settings.SystemRunAllowOutbound,
716+
SystemRunAllowWindowsUi = _settings.SystemRunAllowWindowsUi,
715717
// Sandbox page fields — read by MxcPolicyBuilder.ForSystemRun.
716718
SandboxClipboard = _settings.SandboxClipboard,
717719
SandboxDocumentsAccess = _settings.SandboxDocumentsAccess,

0 commit comments

Comments
 (0)