Skip to content

[Security] system.run: env variable injection and shell wrapper approval bypass #184

Closed

Description

@AlexAlves87

There are two gaps in the current system.run implementation that can allow a connected agent to bypass the intended exec approval policy or hijack execution of an otherwise approved command.

1. Environment variable injection

The request env dictionary is parsed in SystemCapability.cs:266 and passed to the runner at SystemCapability.cs:303. LocalCommandRunner then copies it directly into ProcessStartInfo.Environment at LocalCommandRunner.cs:52.

There is currently no denylist, allowlist, normalization, or blocking for sensitive variables such as PATH, PATHEXT, NODE_OPTIONS, GIT_SSH_COMMAND, LD_*, DYLD_*, BASH_ENV, PSModulePath, ComSpec, etc.

Example:

{
  "command": ["git", "status"],
  "env": {
    "GIT_SSH_COMMAND": "evil.sh"
  }
}

The command can pass approval as git status, but execution behavior is silently altered by the injected environment. Similar risks apply to variables such as NODE_OPTIONS=--require=/tmp/payload.js, LD_PRELOAD=/evil.so, and BASH_ENV=/evil_script.

The current tests also assert that arbitrary env values are accepted as-is in SystemRunTests.cs:110.

2. Shell wrapper approval bypass

The policy evaluates fullCommand once in SystemCapability.cs:293, before execution. It does not parse or evaluate payloads inside shell wrappers such as bash -c, sh -c, cmd /c, powershell -Command, or pwsh -Command.

LocalCommandRunner then executes through cmd.exe, pwsh.exe, or powershell.exe depending on request.Shell (LocalCommandRunner.cs:177).

That means an inner wrapper payload can avoid rules intended to block the effective command. For example:

{
  "command": ["powershell", "-Command", "Remove-Item -Recurse -Force C:\important"]
}

If policy allows powershell *, or if defaultAction=allow, the outer command can be approved while the inner destructive payload is never evaluated against rules like Remove-Item *.

Existing tests cover direct argv evaluation, e.g. ["rm", "-rf", "/"] in ExecApprovalPolicyTests.cs:471, but there is no coverage for wrapper forms like:

["powershell", "-Command", "Remove-Item -Recurse -Force C:\important"]

or:

["cmd", "/c", "del /s /q C:\important\*"]

Proposed fix

I suggest two scoped PRs, both in OpenClaw.Shared:

  1. ExecEnvSanitizer

    • Block known execution-hook / loader / shell-influence variables such as NODE_OPTIONS, LD_*, DYLD_*, GIT_SSH_COMMAND, BASH_ENV, ENV, PATH, PATHEXT, PSModulePath, ComSpec, etc.
    • Reject empty names and names containing invalid or suspicious characters.
    • Compare names case-insensitively on Windows.
    • Further restrict environment overrides when the command is detected as a shell wrapper, ideally allowing only low-risk variables such as locale/display settings.
  2. ExecShellWrapperParser

    • Detect wrapper forms such as bash -c, sh -c, cmd /c, cmd.exe /c, powershell -Command, powershell.exe -Command, powershell.exe -EncodedCommand, and pwsh -Command.
    • Unwrap bounded chains, e.g. up to depth 4.
    • Evaluate the inner payload against the exec policy in addition to the outer command.
    • For -EncodedCommand, decode UTF-16LE and evaluate the decoded content, or deny if decoding fails.

Happy to open separate PRs for each if the approach sounds right.

Activity

  1. added
    bug
    on Apr 20, 2026
  2. added 2 commits that reference this issue on Apr 20, 2026
    89fc8bb
    e772b5e
  3. locked as resolved and limited conversation to collaborators on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsecurity

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions