Summary
virtualenv activation scripts can mishandle already-escaped values by placing them inside an additional quoted context. If a user sources a crafted activation script, shell metacharacters may be interpreted as commands and execute with that user's privileges.
Affected context
The vulnerability occurs in generated Bash, Zsh, and Fish activation scripts when virtual environment paths or Tcl/Tk library paths contain crafted characters. It is most relevant when a relocated environment lacks its recorded directory, or when library path values are incorporated into Fish activation logic, and a user sources the generated script.
Exploit preconditions
- Attack surface: Generated shell activation scripts - Required access: Ability to influence the virtual environment path or
relevant Tcl/Tk library path, and an opportunity for a user to source the
resulting script
- Trigger point: Execution of the activation script through a shell sourcing
operation
- Why exploitation is possible: Values escaped for shell use are inserted
into another quoted context, allowing crafted delimiters and metacharacters
to escape data-only handling and be parsed as shell commands
Why this happens
The underlying bug is an unsafe composition of shell-escaped data and an additional quoting layer. The scripts treat a value returned by an escaping routine as though it can safely be embedded inside a separate quoted expression, but the two contexts do not compose safely for all inputs. This permits data intended to initialize environment variables to alter shell syntax, mapping to CWE-78 because externally influenced data can reach an operating-system command interpreter.
Potential impact
- An attacker may execute arbitrary commands with the privileges of the user who sources the script.
- The attacker could access or modify files available to that user.
- Credentials, environment variables, or project data could be exposed.
- A compromised developer or automation account could be used for further compromise.
General mitigation guidance
Mitigation generally focuses on eliminating ambiguous shell quoting and treating generated activation scripts as security-sensitive code. Secure patterns include using shell-native assignments that preserve data boundaries, applying context-appropriate escaping exactly once, avoiding unnecessary command interpretation, and using secure-by-default activation templates. Input trust can be reduced by constraining environment and library paths to validated representations and by avoiding execution of activation scripts obtained from untrusted locations. Least-privilege user and process permissions can limit the consequences if script execution occurs.
How teams detect issues like this
Detection can include static analysis for nested quoting, string concatenation around shell-escaped values, command substitutions, and generated scripts that interpolate filesystem paths into executable shell syntax. Code review should examine every shell and Fish assignment for context-appropriate escaping and test values containing separators, substitutions, quotes, and metacharacters. Policy enforcement can require generated scripts to pass shell-aware linting, isolated parsing tests, and automated security review before release.

