Skip to content

🐛 fix(activation): stop bash re-expanding the prompt in PS1 - #3326

Merged
gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:fix/bash-prompt-expansion
Sep 26, 2026
Merged

gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:fix/bash-prompt-expansion

Conversation

@gaborbernat

Copy link
Copy Markdown
Contributor

Bash re-expands PS1 every time it draws the prompt, so a venv whose name or --prompt holds $(...) or backticks ran that command on each prompt after activation; this reproduces with bash 5.3. 🐛

activate.sh now escapes \, $ and backticks before putting the prompt into PS1. The escaping runs under bash only: dash rejects the syntax and would abort activation, and zsh does not expand PS1 by default. A new test sources the script under dash.

@gaborbernat gaborbernat added bug security Fixes a vulnerability or hardens the supply chain, CI or release labels Sep 26, 2026
@gaborbernat
gaborbernat force-pushed the fix/bash-prompt-expansion branch from e83f9af to 4f9c756 Compare September 26, 2026 16:12
Bash re-runs parameter, command and arithmetic expansion on PS1 each time
it draws the prompt, so an environment name or --prompt holding $(...),
backticks or backslashes ran as a command on every prompt. Escape those
characters when building PS1. The escaping uses bash-only substitution,
so it runs only under bash: dash rejects the syntax and would abort the
whole activation, and zsh does not expand PS1 without PROMPT_SUBST.
@gaborbernat
gaborbernat force-pushed the fix/bash-prompt-expansion branch from 4f9c756 to 12d6f80 Compare September 26, 2026 16:23
@gaborbernat
gaborbernat merged commit a70c1da into pypa:main Sep 26, 2026
66 checks passed
@gaborbernat
gaborbernat deleted the fix/bash-prompt-expansion branch October 3, 2026 15:12
gaborbernat added a commit that referenced this pull request Oct 12, 2026
bash handles `PS1` in two passes each time it draws the prompt: it first
decodes backslash prompt escapes, turning `\\` into `\` and `\$` into
`$` (or `#` for root), and then expands the result. The bash `activate`
script escaped the environment name for the expansion pass only (#3326),
so a backslash in the name undoes that escaping. 🔐 A name such as
`a\$(touch PWNED)`, from the directory, `--prompt` or
`VIRTUALENV_PROMPT`, runs the command each time bash draws the prompt.
For root, `\$` decodes to `#`, so the same name renders wrong but runs
nothing.

Under bash, `activate` now escapes backslashes, dollar signs and
backticks a second time, so both passes leave the name literal. zsh does
no decode pass, so it keeps the single escaping added in 5b91536. The
prompt tests gain cases for a backslash before `$(...)` and before
backticks, and for names with a backslash before `$`, before a backtick,
doubled, and trailing; they render through bash 5.2 and zsh 5.9 and pass
as root and as an unprivileged user.

Every release up to 21.14.6 is affected, and earlier releases before
21.13.0 do not escape the name at all.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug security Fixes a vulnerability or hardens the supply chain, CI or release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant