Skip to content

🐛 fix(xonsh): preserve shell state and path literals - #3411

Merged
gaborbernat merged 7 commits into
pypa:mainfrom
gaborbernat:fix/xonsh-activation-state
Oct 17, 2026
Merged

gaborbernat merged 7 commits into
pypa:mainfrom
gaborbernat:fix/xonsh-activation-state

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

The xonsh activation script exported its own "was unset" marker under the _OLD_VIRTUAL_<name> names the other shells use for real saved values. Each process started from an activated xonsh inherited _OLD_VIRTUAL_PYTHONHOME=__virtualenv_was_not_set__. A bash started from that shell, after source bin/activate and deactivate, ended with PYTHONHOME set to that string, and any Python it then ran failed during path configuration. A destination directory whose name contains a set variable, such as env$HOME, went onto $PATH as a different directory, so python resolved elsewhere while $VIRTUAL_ENV said the environment was active. A script read under $XONSH_ENCODING=gbk or big5 could lose the backslash in front of an escaped quote, so the rest of the prompt or path value ran as code. 🐛 After this PR the unset bookkeeping lives on the activator object and is never exported, the generator refuses a destination that xonsh would expand, and every generated literal is ASCII. The literal change is the one #3432 carries on its own, so the two merge in either order.

The unset marker was exported as a saved value

_save wrote either the variable's value or the marker string into $_OLD_VIRTUAL_<name>, a design from #3125. xonsh passes its environment variables to the processes it starts (environment variables), so the marker left the shell. Within the same shell deactivate recognised the marker and removed the variable. The bash script does not know the marker: its deactivate restores PYTHONHOME from any non-empty _OLD_VIRTUAL_PYTHONHOME, and source bin/activate runs deactivate nondestructive first. A child bash therefore set PYTHONHOME to the marker before it saved and unset it, and put the marker back on deactivate. With PYTHONHOME=__virtualenv_was_not_set__ CPython stops at startup with a path configuration error. The same happened to TCL_LIBRARY and TK_LIBRARY when the environment set them. A user whose PYTHONHOME was the marker string got it unset instead of restored.

_save now exports a backup only when the variable had a value. Otherwise it adds the name to unset_vars, a set on the activator object, and removes any stale backup. deactivate restores from the backup when one is exported and otherwise removes the variables recorded as unset. Any saved value, the former marker included, is restored as a value. The record now lives on the object, so a second script sourced into the same shell must not discard the first object's record before it runs: the module-level code calls deactivate(["nondestructive"]) on the previous instance before it replaces it. Switching from an environment that set TCL_LIBRARY to one that does not now leaves the variable unset, which the switch test checks. The marker test checks that no backup is exported for an unset PYTHONHOME and that a real value equal to the marker comes back. Both tests run xonsh, so they skip where it is not installed, which includes CI.

sequenceDiagram
    participant X as xonsh parent
    participant E as exported environment
    participant B as child bash
    rect rgb(255, 214, 214)
    X->>E: _OLD_VIRTUAL_PYTHONHOME = __virtualenv_was_not_set__
    E-->>B: inherited at start
    B->>B: source bin/activate, deactivate
    Note over B: PYTHONHOME is the marker, python fails to start
    end
    rect rgb(216, 243, 220)
    X->>X: unset_vars = {"PYTHONHOME"} on the activator object
    E-->>B: no backup variable
    Note over B: PYTHONHOME stays unset
    end
Loading

PATH entries went through variable and tilde expansion

The template puts the literal destination joined with the bin directory at the front of $PATH. xonsh keeps $PATH as an EnvPath (environment types), and each read of an entry goes through _expandpath. With $EXPAND_ENV_VARS on, its default, expandvars replaces $NAME and ${'NAME'} for each variable that is set, and expand_path applies ~ expansion to the text after the first =, around each os.pathsep, the rule bash uses for assignments. Command lookup and child process environments see the expanded entries. An environment at /work/env$HOME put /work/env/home/user/bin on $PATH, a directory that does not exist, so python resolved to the next interpreter on the path. Quoting cannot prevent this: the expansion runs on the value after the literal is parsed. The os.pathsep case cannot arise, since #3399 rejects such destinations; a ~ right after = can.

generate now matches the destination against \$(?:\w+|{)|=~. On a match it logs a warning that names the destination, removes an activate.xsh left by an earlier run, and returns no files; the other activators still run. The pattern is wider than xonsh's: ${HOME} is refused although xonsh leaves it alone, and a $ not followed by a word character or { passes. The skip test covers the three forms, the warning text and the removed file.

flowchart LR
    L["literal '/work/env$HOME/bin'"] --> P["Python string parsed"]
    P --> S["stored as a PATH entry"]
    S --> R["read through _expandpath"]
    R --> V["expandvars: $HOME replaced"]
    V --> T["expand_path: ~ after = replaced"]
    T --> X["/work/env/home/user/bin on PATH"]
    G["generate: destination matches"] --> W["warning, no activate.xsh"]
    classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000;
    classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332;
    class X bad;
    class W ok;
Loading

Source decoding merged a backslash into the character before it

quote returned repr of the value, which keeps printable non-ASCII characters as they are, and the file is written as UTF-8. xonsh's source reads the file with $XONSH_ENCODING (source_alias_fn). In GBK and Big5 a lead byte between 0x81 and 0xFE takes the next byte in the range 0x40 to 0xFE as its trail. The last UTF-8 byte of 中 is 0xAD, so 中\ decodes as 涓璡 and the backslash is gone. A value with both kinds of quote is written as '中\'…', which under GBK becomes '涓璡'…': the literal ends after two characters and the parser reads the rest of the value as code. quote now returns ascii of the value (quote), so every byte of the file is below 0x80 and no double-byte codec pairs them; the parser turns 中 back into 中. The quote test round-trips the literal through GBK and ast.literal_eval, and the xonsh test sources a script with a non-ASCII path and prompt under GBK and Big5. This also addresses GHSA-394h-qwmc-48pf; #3432 has the full account.

sequenceDiagram
    participant G as generator
    participant D as xonsh source decoder
    participant P as literal parser
    rect rgb(255, 214, 214)
    G->>D: repr, bytes e4 b8 ad 5c 27 for 中\'
    D->>P: GBK pairs ad 5c, the backslash is gone
    Note over P: literal closes early, the rest runs as code
    end
    rect rgb(216, 243, 220)
    G->>D: ascii, every byte below 0x80
    D->>P: text unchanged under any ASCII-compatible codec
    Note over P: escapes decode back to the original value
    end
Loading

Behavior changes

Activation no longer exports _OLD_VIRTUAL_<name> for a variable that was unset; child processes see no backup variable. A saved value equal to __virtualenv_was_not_set__ is restored as a value. A child shell started from a parent activated with an older script still inherits the marker as a backup, and the new script restores it as a value, so deactivate in that child leaves PYTHONHOME set to the marker until the parent environment is regenerated and re-activated. Sourcing a second environment's script deactivates the first environment before it activates the second. A destination containing $ followed by a word character or {, or containing =~, gets no activate.xsh: virtualenv logs a warning, removes an existing activate.xsh, and generates the other activators as before. Non-ASCII characters in paths and prompts are written as escapes. Scripts generated by earlier versions keep their old literals and backup handling until the environment is regenerated.

Reported by Anthropic's OSS Scanner: ANT-2026-PPZYMXEQ, ANT-2026-ZBWJ4FLK.

@gaborbernat
gaborbernat force-pushed the fix/xonsh-activation-state branch from 57a5cd6 to 4f8cdbc Compare October 16, 2026 14:23
Reported by Anthropic's OSS Scanner: ANT-2026-PPZYMXEQ, ANT-2026-ZBWJ4FLK.
@gaborbernat
gaborbernat force-pushed the fix/xonsh-activation-state branch from 4f8cdbc to f4425b5 Compare October 16, 2026 14:25
@gaborbernat
gaborbernat marked this pull request as draft October 16, 2026 23:55
@gaborbernat gaborbernat added the security Fixes a vulnerability or hardens the supply chain, CI or release label Oct 16, 2026
Replace the quote test that still described repr() with the round trip
through GBK from pypa#3432, and give quote() the same docstring, so the two
PRs merge without conflict in either order. Assert the warning the
generator logs when it skips an expanding destination, and run xonsh
through one fixture instead of three copies of the subprocess call.
@gaborbernat
gaborbernat marked this pull request as ready for review October 17, 2026 09:24
@gaborbernat
gaborbernat merged commit 172dcbb into pypa:main Oct 17, 2026
73 of 74 checks passed
@gaborbernat
gaborbernat deleted the fix/xonsh-activation-state branch October 17, 2026 10:41
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