Summary
virtualenv can allow command injection in generated Windows activation scripts when prompt text is inserted into a batch assignment without safely handling double quotes. If an attacker can influence the prompt value, commands may run with the privileges of a user who activates the environment.
Affected context
The issue affects virtualenv's Windows batch activation path. It arises when prompt text supplied through --prompt, VIRTUALENV_PROMPT, or the corresponding configuration value is included in an activate.bat assignment without being safely handled. Risk depends on whether an attacker can influence that value and a user activates the generated environment.
Exploit preconditions
- Attack surface: Windows batch activation scripts generated by virtualenv
- Required access: Ability to influence the prompt through a command-line option, environment variable, or configuration
- Trigger point: A user activates the generated environment
- Why exploitation is possible: A double quote in the prompt can close the batch assignment, allowing following command-interpreter operators to be treated as executable syntax
Why this happens
The prompt value is returned unchanged by BatchActivator.quote() and then inserted into a cmd.exe set "VAR=value" statement. Because the value is not safely encoded for the batch command context, quote characters can alter the statement's boundaries and expose subsequent text to command interpretation. This is OS command injection, mapped to CWE-78.
Potential impact
- Commands may execute with the privileges of the user activating the environment
- The user's files or accessible resources could be modified or disclosed
- Additional software or scripts available to that user could be affected
General mitigation guidance
Defensive approaches include safely encoding values for the specific Windows batch command context or avoiding direct interpolation into shell syntax. Treat prompt values and configuration as untrusted, prefer mechanisms that do not invoke a command interpreter when passing data, and limit the privileges of processes that activate environments. Keep configuration sources controlled and avoid relying on validation alone where context-aware escaping is needed.
How teams detect issues like this
Review batch-script generation for externally influenced values inserted into command statements, especially assignments and quoting boundaries. Static analysis and code review can look for unescaped interpolation into shell or batch syntax. Tests and policy checks can verify that generated scripts handle quotes and command metacharacters as data rather than executable syntax.

