fix(security): sanitize workdir parameter in terminal tool backends#5629
Merged
Conversation
Shell injection via unquoted workdir interpolation in docker, singularity, and SSH backends. When workdir contained shell metacharacters (e.g. ~/;id), arbitrary commands could execute. Changes: - Add shlex.quote() at each interpolation point in docker.py, singularity.py, and ssh.py with tilde-aware quoting (keep ~ unquoted for shell expansion, quote only the subpath) - Add _validate_workdir() allowlist in terminal_tool.py as defense-in-depth before workdir reaches any backend Original work by Mariano A. Nicolini (PR #5620). Salvaged with fixes for tilde expansion (shlex.quote breaks cd ~/path) and replaced incomplete deny-list with strict character allowlist. Co-authored-by: Mariano A. Nicolini <entropidelic@users.noreply.github.com>
This was referenced Apr 6, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes shell injection vulnerability where the
workdirparameter is interpolated unquoted intocd {work_dir} && ...shell commands in docker, singularity, and SSH backends. A malicious AGENTS.md could poison workdir values to execute arbitrary commands (e.g.workdir: "~/;id").Salvaged from PR #5620 by @entropidelic with the following improvements:
Fixes applied on top of #5620
shlex.quote(work_dir)which wraps the entire path in single quotes, breaking tilde expansion (cd '~/project'→ tilde does NOT expand). Fixed to keep~unquoted and quote only the subpath:cd ~/{shlex.quote(subpath)}._WORKDIR_BANNED_CHARSmissed&,>,<, quotes, and other metacharacters. Replaced with a strict regex allowlist ([A-Za-z0-9/_\-.~ +@=,]) so novel metacharacters can't slip through.Changes
tools/environments/docker.py— tilde-awareshlex.quotefor~/workdir pathstools/environments/singularity.py— sametools/environments/ssh.py— tilde-aware quoting for all workdir paths in_execute_oneshottools/terminal_tool.py—_validate_workdir()allowlist as defense-in-depthBackends NOT affected
cwd=parameter tosubprocess.run(), not shell-interpolatedshlex.quoteinpersistent_shell.pyTest plan
&, quotes, backticks, etc.)Closes #5620