Summary
A command injection vulnerability in virtualenv's Bash activation script can cause shell commands embedded in an environment name to execute when Bash redraws the prompt. The issue becomes risky when an attacker can influence the virtual environment name and a user activates that environment in Bash.
A command injection vulnerability in virtualenv's Bash activation script can cause shell commands embedded in an environment name to execute when Bash redraws the prompt. The issue becomes risky when an attacker can influence the virtual environment name and a user activates that environment in Bash.
Affected context
The vulnerability occurs in the Bash activation script generated for affected virtualenv environments. It is relevant when an environment name contains shell-sensitive characters, including backslashes and command-substitution syntax, and the activation script is sourced by Bash. Exploitation depends on a user activating the affected environment and subsequently having the shell render its prompt.
The vulnerability occurs in the Bash activation script generated for affected virtualenv environments. It is relevant when an environment name contains shell-sensitive characters, including backslashes and command-substitution syntax, and the activation script is sourced by Bash. Exploitation depends on a user activating the affected environment and subsequently having the shell render its prompt.
Exploit preconditions
- Attack surface: The Bash activation script and the shell prompt rendering path
- Required access: The attacker must be able to influence or provide the virtual environment name or its generated activation files
- Trigger point: A user sources the activation script in Bash and the shell draws the prompt
- Why exploitation is possible: Incomplete escaping across Bash's prompt-processing passes can leave command-substitution syntax executable
- Attack surface: The Bash activation script and the shell prompt rendering path
- Required access: The attacker must be able to influence or provide the virtual environment name or its generated activation files
- Trigger point: A user sources the activation script in Bash and the shell draws the prompt
- Why exploitation is possible: Incomplete escaping across Bash's prompt-processing passes can leave command-substitution syntax executable
Why this happens
The underlying bug is incomplete output escaping in a shell-generated prompt value. The activation script escapes the environment name for only one of Bash's processing passes, allowing a backslash in the name to undo that protection during a later pass and expose command-substitution syntax. This maps primarily to CWE-78: Improper Neutralization of Special Elements used in an OS Command, with CWE-116: Improper Encoding or Escaping of Output describing the flawed escaping behavior.
The underlying bug is incomplete output escaping in a shell-generated prompt value. The activation script escapes the environment name for only one of Bash's processing passes, allowing a backslash in the name to undo that protection during a later pass and expose command-substitution syntax. This maps primarily to CWE-78: Improper Neutralization of Special Elements used in an OS Command, with CWE-116: Improper Encoding or Escaping of Output describing the flawed escaping behavior.
Potential impact
- An attacker's commands may execute with the privileges of the affected user
- User files, credentials, source code, or environment variables could be exposed
- The attacker could modify local projects, virtual environments, or shell configuration
- Development workflows or build processes could be disrupted or used for further compromise
- An attacker's commands may execute with the privileges of the affected user
- User files, credentials, source code, or environment variables could be exposed
- The attacker could modify local projects, virtual environments, or shell configuration
- Development workflows or build processes could be disrupted or used for further compromise
General mitigation guidance
Mitigation focuses on treating environment names and other generated shell values as untrusted data.
Common defensive patterns include:
- Avoiding interpolation of untrusted values into executable shell or prompt expressions
- Applying escaping that is correct for every parsing and evaluation pass
- Preferably using prompt mechanisms that keep displayed data separate from executable shell syntax
- Restricting who can create or rename virtual environments in shared workspaces
- Running development and automation tasks with least privilege and minimizing sensitive data in shell environments
- Reviewing generated activation scripts before sourcing them when their names or contents originate outside a trusted workflow
Mitigation focuses on treating environment names and other generated shell values as untrusted data.
Common defensive patterns include:
- Avoiding interpolation of untrusted values into executable shell or prompt expressions
- Applying escaping that is correct for every parsing and evaluation pass
- Preferably using prompt mechanisms that keep displayed data separate from executable shell syntax
- Restricting who can create or rename virtual environments in shared workspaces
- Running development and automation tasks with least privilege and minimizing sensitive data in shell environments
- Reviewing generated activation scripts before sourcing them when their names or contents originate outside a trusted workflow
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules identifying untrusted values embedded in shell commands, PS1 assignments, or command-substitution contexts
- Code review checks for multiple shell parsing passes and incomplete escaping of backslashes, quotes, or substitution markers
- Tests that exercise generated scripts with shell metacharacters and prompt redraw behavior
- Repository and CI policies that flag dynamic shell evaluation and unsafe prompt construction
- Runtime monitoring for unexpected child processes launched when activation scripts are sourced or prompts are rendered
Organizations can look for this class of issue through:
- Static analysis rules identifying untrusted values embedded in shell commands, PS1 assignments, or command-substitution contexts
- Code review checks for multiple shell parsing passes and incomplete escaping of backslashes, quotes, or substitution markers
- Tests that exercise generated scripts with shell metacharacters and prompt redraw behavior
- Repository and CI policies that flag dynamic shell evaluation and unsafe prompt construction
- Runtime monitoring for unexpected child processes launched when activation scripts are sourced or prompts are rendered

