Skip to content

Commit 2539e92

Browse files
AlexAlves87claude
andcommitted
fix: detect env modifiers across the full unwrap chain
The modifier guard only checked the outer env wrapper, so a nested form such as `env env FOO=bar node` passed: the outer level has no modifier, and the resolver then unwraps every level down to `node`, dropping the inner FOO=bar. The payload would run without the approved environment. Add AnyWrapperHasModifiers, which walks the same unwrap chain the resolver uses and fails closed if any level carries assignments or flags. Transparent nested wrappers (`env env git status`) still emit a payload. Validation: dotnet build openclaw-windows-node.slnx — succeeded, 0 errors dotnet test OpenClaw.Shared.Tests — 2333 passing, 0 failed dotnet test OpenClaw.Tray.Tests — 1088 passing, 0 failed Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent e208cf6 commit 2539e92

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

‎src/OpenClaw.Shared/ExecApprovals/ExecApprovalsCoordinator.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,14 @@ public async Task<ExecApprovalV2Result> HandleAsync(NodeInvokeRequest request, s
253253
|| resolvedPath.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase))
254254
return null;
255255

256-
// If the command is an env invocation with modifiers (VAR=val assignments or
256+
// If any env wrapper in the chain carries modifiers (VAR=val assignments or
257257
// flags), the direct-argv payload cannot faithfully carry those semantics: the
258258
// modifier would be silently dropped, and the process would run in a different
259-
// environment than the one that was approved. Fail closed rather than execute
260-
// a command that differs from what was evaluated.
261-
if (identity.Command.Count > 0
262-
&& ExecCommandToken.IsEnv(identity.Command[0].Trim())
263-
&& ExecEnvInvocationUnwrapper.HasModifiers(identity.Command))
259+
// environment than the one that was approved. This walks the full unwrap chain
260+
// so a nested form such as `env env FOO=bar node` is caught, not just the outer
261+
// wrapper. Fail closed rather than execute a command that differs from what was
262+
// evaluated.
263+
if (ExecEnvInvocationUnwrapper.AnyWrapperHasModifiers(identity.Command))
264264
return null;
265265

266266
// Transparent env wrappers (no modifiers) are safe to unwrap: the inner

‎src/OpenClaw.Shared/ExecApprovals/ExecEnvInvocationUnwrapper.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ internal static bool HasModifiers(IReadOnlyList<string> command)
8181
return false;
8282
}
8383

84+
// Returns true when any env wrapper in the full unwrap chain carries modifiers
85+
// (VAR=val assignments or flags), including nested forms such as `env env FOO=bar node`.
86+
// UnwrapForResolution strips every level for resolution, so checking only the outer
87+
// wrapper would let an inner modifier slip through and be dropped from execution.
88+
internal static bool AnyWrapperHasModifiers(IReadOnlyList<string> command)
89+
{
90+
var current = command;
91+
for (var depth = 0; depth < MaxWrapperDepth; depth++)
92+
{
93+
if (current.Count == 0) break;
94+
var token = current[0].Trim();
95+
if (token.Length == 0) break;
96+
if (!ExecCommandToken.IsEnv(token)) break;
97+
if (HasModifiers(current)) return true;
98+
var unwrapped = Unwrap(current);
99+
if (unwrapped is null || unwrapped.Count == 0) break;
100+
current = unwrapped;
101+
}
102+
return false;
103+
}
104+
84105
// Iteratively strips env wrappers for executable resolution only.
85106
internal static IReadOnlyList<string> UnwrapForResolution(IReadOnlyList<string> command)
86107
{

‎tests/OpenClaw.Shared.Tests/ExecApprovalsCoordinatorTests.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,30 @@ public void BuildApprovedExecution_UsesEffectiveCommandWhenEnvWrapperWasUnwrappe
525525
Assert.NotEqual(new[] { @"C:\Program Files\Git\bin\git.exe", "git", "status" }, exec.Argv);
526526
}
527527

528+
[Fact]
529+
public void BuildApprovedExecution_NestedTransparentEnvWrapper_EmitsUnwrappedPayload()
530+
{
531+
// A nested env wrapper with no modifiers (`env env git status`) is transparent:
532+
// the inner command is the real executable and the args are preserved verbatim.
533+
var resolution = new ExecCommandResolution(
534+
RawExecutable: "git",
535+
ResolvedPath: @"C:\Program Files\Git\bin\git.exe",
536+
ExecutableName: "git.exe",
537+
Cwd: null);
538+
var identity = MakeIdentity(new[] { "env", "env", "git", "status" }, resolution);
539+
540+
var exec = ExecApprovalsCoordinator.BuildApprovedExecution(identity, sanitizedEnv: null);
541+
542+
Assert.NotNull(exec);
543+
Assert.Equal(new[] { @"C:\Program Files\Git\bin\git.exe", "status" }, exec!.Argv);
544+
}
545+
528546
[Theory]
529547
[InlineData("env", "FOO=bar", "node", "script.js")]
530548
[InlineData("env", "-i", "node", "script.js")]
531549
[InlineData("env", "--unset=FOO", "node", "script.js")]
550+
[InlineData("env", "env", "FOO=bar", "node", "script.js")] // nested modifier on the inner wrapper
551+
[InlineData("env", "env", "-i", "node", "script.js")]
532552
public void BuildApprovedExecution_ReturnsNull_WhenEnvHasModifiers(params string[] command)
533553
{
534554
// A modified env wrapper (assignments or flags) cannot be faithfully represented

0 commit comments

Comments
 (0)