Repository navigation
馃悰 fix(bash): preserve literal prompts in supported shells - #3418
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/shell-prompt-selection
branch
from
October 16, 2026 14:23
944b8ff to
37ba203
Compare
gaborbernat
marked this pull request as draft
October 16, 2026 19:04
Reported by Anthropic's OSS Scanner: ANT-2026-1MOI2X3T.
Bash expands PS1 in posix mode even with promptvars disabled, so the literal branch let a $(...) in the prompt run at every draw when sourced from sh or under set -o posix. Use the variable reference whenever bash expands the prompt, fold the prompt block into one branch per expansion mode, and have the dash test check PS1 stays untouched.
gaborbernat
force-pushed
the
fix/shell-prompt-selection
branch
from
October 17, 2026 09:30
e439b3b to
c571d5b
Compare
gaborbernat
marked this pull request as ready for review
October 17, 2026 09:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The POSIX
activatescript puts the environment name in parentheses in front ofPS1. bash, and zsh underPROMPT_SUBST, expandPS1each time they draw the prompt, so the script escaped backslashes, dollar signs and backticks for those two shells and assigned the plain name in every other shell. ksh93 expandsPS1the same way and got the plain name, so a name holding$(...)ran as a command at every draw there; the test that sources the script in bash withBASH_VERSIONunset shows that path. Under bash the escaping depended on the shell's own settings: it was built for the two passes bash runs withpromptvarson, and the stray backslashes showed when the option was off, while underBASH_COMPAT4.2 or lower, the level bash 3.2 on macOS runs at, the escaping produced one backslash where two were meant and a name ending in\\lost a character. 馃悰 The script now changesPS1only under bash and zsh. Under bash it stores a reference toVIRTUAL_ENV_PROMPTwhenever bash expands the prompt, and otherwise doubles backslashes and nothing else. This builds on #3326, on the fix for GHSA-6w3r-xr62-4xcp, and on #3390 and #3392, which escaped the name for both of bash's passes.PS1 was assigned in every shell
The prompt block checked only
VIRTUAL_ENV_DISABLE_PROMPT. Its bash branch (#3390) and its zsh branch (the advisory fix) escaped the name, and the assignment that followed ran in every shell, as it has since #1454. dash does not expandPS1, so it showed the plain name; ksh93 does, so it ran whatever the name held. The guard now also requiresBASH_VERSIONorZSH_VERSION. Other shells keep theirPS1; they still getVIRTUAL_ENV_PROMPTexported for their own prompt code, and the dash test checks thatPS1comes back untouched.flowchart LR A[activate sourced] --> S{shell} S -- bash --> B[escaped for bash] S -- zsh --> Z[escaped for zsh] S -- other --> O[plain name into PS1] O --> K[ksh93 expands it at every draw] S -- other, now --> U[PS1 untouched, VIRTUAL_ENV_PROMPT exported] classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000; classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332; class O,K bad; class U ok;The bash escaping assumed one expansion mode
bash draws
PS1in two passes, described under Controlling the Prompt. It first decodes backslash escapes, turning\\into\and\$into$or#. Then, when thepromptvarsoption is set, it runs parameter expansion, command substitution, arithmetic expansion and quote removal on the result. In POSIX mode, entered through--posix,set -o posix,POSIXLY_CORRECTor invocation assh, the second pass runs regardless ofpromptvars. The old bash branch from #3390 escaped the name once for each pass, in every mode. Withpromptvarsoff and no POSIX mode only the decode ran, so the prompt showed\$,\`and doubled backslashes. The escapes came from${name//pattern/replacement}substitutions whose replacement strings undergo quote removal only after bash 4.2; atBASH_COMPAT4.2 or lower the same substitutions yielded half the backslashes, so a name holding\\rendered as\.The bash branch now tests
shopt -q promptvars || shopt -qo posix, the two conditions under which the second pass runs, and stores'(${VIRTUAL_ENV_PROMPT-}) 'in front of the old prompt. The decode pass finds no backslash in that prefix. The expansion pass substitutes the variable's value, and bash does not scan the text a parameter expansion produced for further expansions, so the name is drawn as it is at any compatibility level, and no substitution on the name takes place at activation time. The-default keeps aset -usession drawing()afterunset VIRTUAL_ENV_PROMPTinstead of failing withunbound variable. When neither condition holds, the other branch doubles backslashes for the decode pass and leaves$and backticks alone, since no expansion follows; zsh keeps its$, backtick and%escapes in the same branch. One test sources a--prompt 'x$(touch PWNED)y'environment withpromptvarsoff, withpromptvarsoff underset -o posix, and underset -uwith the variable unset, and another renders backslash names atBASH_COMPAT3.1, 4.2 and 5.1.sequenceDiagram participant A as activate participant P as PS1 participant B as bash draw rect rgb(255, 214, 214) A->>P: before: name escaped for both passes B->>B: decode, backslash escapes removed B->>B: promptvars off: stop, stray backslashes drawn end rect rgb(216, 243, 220) A->>P: now, promptvars or posix: '(${VIRTUAL_ENV_PROMPT-}) ' B->>B: decode, no backslash to act on B->>B: expand, variable value inserted and not rescanned end rect rgb(207, 228, 255) A->>P: now, neither: name with backslashes doubled B->>B: decode only, each pair drawn as one backslash endBehavior changes
Under bash,
$PS1after activation reads(${VIRTUAL_ENV_PROMPT-})followed by the previous prompt, rather than the escaped name; the drawn prompt is the same. ChangingVIRTUAL_ENV_PROMPTafter activation changes the drawn prefix, and unsetting it draws(), also underset -u. Turningpromptvarsoff after activation outside POSIX mode draws the literal(${VIRTUAL_ENV_PROMPT-}). A session activated withpromptvarsoff and outside POSIX mode holds the name inPS1with only its backslashes doubled; turningpromptvarson afterwards expands that text, so a name holding$(...)runs there, where the old form stayed literal. In that same branch atBASH_COMPAT4.2 or lower, a backslash followed by a prompt escape letter, such as\n, is decoded, because the doubling substitution keeps one backslash there. dash, ksh and every other shell that is not bash or zsh keep theirPS1;VIRTUAL_ENV_PROMPTis still exported for them. zsh keeps its escaping, and withoutPROMPT_SUBSTit still shows\$and\\literally, as it did before this PR. Existing activation scripts need regeneration.Reported by Anthropic's OSS Scanner: ANT-2026-1MOI2X3T.