Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing encoding of backslash in terminal shell integration environment variable values #240614

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

abrahan-munro
Copy link

@abrahan-munro abrahan-munro commented Feb 13, 2025

A colon is used as a separator for environment variables to be set in the shell via terminal shell integration. Colons in the values are encoded as a backslash escape sequence and decoded with echo -e.

However, as backslash is not also encoded, any existing backslash may be treated as an escape sequence when decoding. This is visible on Windows when using bash with the Python extension and a username that starts with a character that is recognised in a backslash escape sequence, as HOME will then be set incorrectly.

This change fixes the bug by encoding any backslash characters.

Resolves #240539. See for more details.

May also deal with microsoft/vscode-python#23604, #227223, #212508, #212487, #212958, microsoft/vscode-python#24717, etc.

@Tyriar
Copy link
Member

Tyriar commented Feb 13, 2025

Thanks for the investigation, do you know how to reproduce the problem though? This is what I see:

image

I have 3 extensions contributing to the environment:

image

# Terminal Environment Changes

## Extension: ms-python.python

- `PYTHONSTARTUP=c:\Users\Daniel\AppData\Roaming\Code - Insiders\User\workspaceStorage\54d3aa32f66add3fc5bf0ed69305c70a\ms-python.python\pythonrc.py`

## Extension: GitHub.copilot-chat

Enables use of the `copilot-debug` command in the terminal.

- `PATH=${env:PATH};c:\Users\Daniel\AppData\Roaming\Code - Insiders\User\globalStorage\github.copilot-chat\debugCommand`

## Extension: vscode.git

Enables the following features: git auth provider

- `GIT_ASKPASS=c:\Users\Daniel\AppData\Local\Programs\Microsoft VS Code Insiders\resources\app\extensions\git\dist\askpass.sh`
- `VSCODE_GIT_ASKPASS_NODE=C:\Users\Daniel\AppData\Local\Programs\Microsoft VS Code Insiders\Code - Insiders.exe`
- `VSCODE_GIT_ASKPASS_EXTRA_ARGS=`
- `VSCODE_GIT_ASKPASS_MAIN=c:\Users\Daniel\AppData\Local\Programs\Microsoft VS Code Insiders\resources\app\extensions\git\dist\askpass-main.js`
- `VSCODE_GIT_IPC_HANDLE=\\.\pipe\vscode-git-4523965613-sock`

It may be one of your extensions?

@Tyriar Tyriar added this to the February 2025 milestone Feb 13, 2025
@Tyriar
Copy link
Member

Tyriar commented Feb 13, 2025

and a username that starts with a character that is recognised in a backslash escape sequence

I might not be understanding this right?

@abrahan-munro
Copy link
Author

abrahan-munro commented Feb 13, 2025

This will only be seen when the value of the environment variable being encoded includes an escape sequence supported by echo -e. This is likely to happen with windows-style paths (i.e. \ path separator) and file or directory names starting with one of the characters used in an escape sequence (i.e. a, b, c, e, E, f, n, r, t, or v, also possibly 0, x, u, or U, depending on the next characters).

In my case, without the Python extension, terminal shell integration is inactive and HOME has the default value of /c/Users/abrahan. With the Python extension, terminal shell integration is active, and it is trying to set HOME to C:\Users\abrahan (🤷). But the \a is being interpreted as an escape sequence.

The issue and fix can be demonstrated as follows.

Without the fix:

$ encoded=$(node -e 'function _encodeColons(value) { return value.replaceAll(":", "\\x3a"); } console.log(_encodeColons(String.raw`C:\Users\abrahan`));'); echo "$encoded"; echo -e "$encoded"
C\x3a\Users\abrahan
C:\Usersbrahan

With the fix:

$ encoded=$(node -e 'function _encodeColons(value) { return value.replaceAll("\\", "\\\\").replaceAll(":", "\\x3a"); } console.log(_encodeColons(String.raw`C:\Users\abrahan`));'); echo "$encoded"; echo -e "$encoded"
C\x3a\\Users\\abrahan
C:\Users\abrahan

@Tyriar
Copy link
Member

Tyriar commented Feb 14, 2025

Oh I see, the trigger then is the Python's environment activation that replaces the PATH variable and it's not git bash aware. That feature is likely going to be removed soon (microsoft/vscode-python#24717 (comment)), but the encoding fix does look good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

\ path separators are interpreted as escape sequences when HOME is set in bash terminal
2 participants