GHSA-8rjx-v5ww-45pp
CVE Information
Impact
ViaTemplateActivator.instantiate_templatereplaces the placeholders in an activation template one key at a time, each across the whole text. When a value that it already substituted and quoted contains the name of a later placeholder, the later pass rewrites that name inside the quoted literal. The inserted value brings its own quotes, which close the outer literal, and the rest of the path runs as code when a user activates the environment.
__VIRTUAL_NAME__holds the environment's directory name, so a destination such asA__VIRTUAL_NAME__B/x'$(touch PWNED)'yis enough. In the defaultactivatefor bash, theVIRTUAL_ENVline renders as:VIRTUAL_ENV='/tmp/t/A'x'"'"'$(touch PWNED)'"'"'y'B/x'"'"'$(touch PWNED)'"'"'y'Sourcing that script runs
touch PWNED. The same payload in a path without the placeholder name runs nothing.activate.fishruns the command the same way, and the reporter also showed Python code execution throughactivate.xshandbin/activate_this.py. Inactivate.csh, a directory named after a prompt display placeholder, such as__VIRTUAL_PROMPT_DISPLAY_TCSH__, breaks the script withUnmatched '''.No change to
quote()can stop this, because the rewrite happens afterquote()ran; it defeatsshlex.quoteandrepr()alike.An attacker needs to influence the destination path or
--prompt: a CI job or wrapper tool that builds the path from a branch or project name, or a directory in a cloned repository or unpacked archive.The bug affects versions 20.26.6 through 21.14.1. Version 20.26.6 first quoted template values (#2768) and introduced the per-key loop.
Patches
Fixed in 21.14.2. Activation scripts now keep a placeholder name in the environment's path or prompt as plain text, and activating the example above runs nothing. Fix: https://github.com/pypa/virtualenv/commit/602b26623a724827ff171bf11fef4ad186d01f74
Workarounds
Do not create an environment whose path or
--promptcontains a placeholder name such as__VIRTUAL_NAME__,__VIRTUAL_ENV__or__VIRTUAL_PROMPT__, and inspect the generated activation script before you source one you did not create.Credits
Kevin Stubbings (@Kwstubbs) of GitHub Security Lab reported this issue.