Skip to content

🐛 fix(xonsh): preserve literals across source encodings - #3432

Merged
gaborbernat merged 4 commits into
pypa:mainfrom
gaborbernat:security/xonsh-ascii-literals
Oct 17, 2026
Merged

gaborbernat merged 4 commits into
pypa:mainfrom
gaborbernat:security/xonsh-ascii-literals

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

The xonsh activation script holds the environment path and the prompt as Python string literals, written by repr into a UTF-8 file. xonsh reads the file with the codec named by $XONSH_ENCODING. Under GBK or Big5, the last byte of a non-ASCII character pairs with the ASCII byte after it, so a backslash in front of an escaped quote disappears, the quote ends the literal early, and the rest of the value runs as code when the script is sourced. The prompt is a command-line argument, so whoever supplies it supplies the code. 🐛 After this PR every literal in the script is ASCII and decodes to the same text under any ASCII-compatible codec. This resolves GHSA-394h-qwmc-48pf. #3411 carries the same quote change next to its activation-state fixes, so the two merge in either order.

Source decoding merged a backslash into the character before it

quote, from #3125, returned repr of the value. repr keeps printable non-ASCII characters as they are, and when the value holds both kinds of quote it delimits with ' and writes the inner ' as the escape sequence \'. The generator writes the file as UTF-8. xonsh's source opens it with $XONSH_ENCODING and $XONSH_ENCODING_ERRORS (source_alias_fn, open), whose defaults are sys.getdefaultencoding() and surrogateescape, and hands the text to the parser. GBK and Big5 are double-byte codecs (standard encodings): a lead byte between 0x81 and 0xFE takes the next byte in the range 0x40 to 0xFE as its trail. The UTF-8 form of 中 is e4 b8 ad, and \ is 5c, so 中\ decodes as the two characters 涓璡 and the backslash is gone. A prompt written as '中\'…' therefore reads as '涓璡'…': the literal closes after two characters, and the tokenizer continues with the rest of the value as statements. With ' and " in a prompt, a ; and a # make that remainder a valid statement followed by a comment; sourced under XONSH_ENCODING=gbk or big5 the old script runs it. A value whose last byte has no valid trail, such as 中 followed by ', is read through surrogateescape as a lone surrogate instead: the literal keeps its shape and the value is wrong.

quote now returns ascii of the value (quote). ascii writes every non-ASCII character as a \x, \u or \U escape, so every byte of the file is below 0x80, no double-byte codec pairs two of them, and the literal parser turns 中 back into 中. The Python activator took the same step in #3402 (quote). The quote test encodes the literal as ASCII, decodes it as GBK and checks that ast.literal_eval returns the original value, for a value that holds 中, a quote and a backslash.

sequenceDiagram
    participant G as generator
    participant F as activate.xsh, UTF-8 bytes
    participant D as xonsh source decoder
    participant P as literal parser
    rect rgb(255, 214, 214)
    G->>F: repr: 27 e4 b8 ad 5c 27 for '中\'
    F->>D: $XONSH_ENCODING=gbk
    D->>P: '涓璡' and the backslash is gone
    Note over P: literal closes early, the rest of the value runs as code
    end
    rect rgb(216, 243, 220)
    G->>F: ascii: 27 5c 75 34 65 32 64 5c 27 for '\u4e2d\'
    F->>D: any ASCII-compatible codec
    D->>P: text unchanged
    Note over P: escapes decode back to 中 and '
    end
Loading

Behavior changes

Generated activate.xsh files write non-ASCII characters in the environment path and prompt as escapes instead of literally. Scripts generated by earlier versions are not rewritten; running virtualenv on the environment again regenerates them. Nothing else in the script changes.

@gaborbernat gaborbernat added bug security Fixes a vulnerability or hardens the supply chain, CI or release labels Oct 16, 2026
gaborbernat added a commit to gaborbernat/virtualenv that referenced this pull request Oct 17, 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:16
@gaborbernat
gaborbernat merged commit e5fc2e6 into pypa:main Oct 17, 2026
73 of 74 checks passed
@gaborbernat
gaborbernat deleted the security/xonsh-ascii-literals branch October 17, 2026 10:42
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