Skip to content

terminal: preserve special characters in file names dropped into terminal#309187

Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/terminal-special-chars-drag-drop
Open

terminal: preserve special characters in file names dropped into terminal#309187
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/terminal-special-chars-drag-drop

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

Characters such as ~, `, !, #, $, ^, &, *, |, ;, <, > were silently stripped from file paths when dragging files from the Explorer into the terminal. This produced invalid paths pointing to non-existent files.

Root cause

escapeNonWindowsPath in src/vs/platform/terminal/common/terminalEnvironment.ts applied a bannedChars regex replacement that removed those characters before quoting:

const bannedChars = /[\`\$\|\&\>\~\#\!\^\*\;\<]/g;
newPath = newPath.replace(bannedChars, '');

Why the removal is both wrong and unnecessary

The function always wraps the resulting path in shell-appropriate quotes (single quotes for bash/sh/zsh, $'...' ANSI C quoting when both quote types are present). Inside single quotes all of the listed characters are treated as literals — they cannot cause shell injection. Removing them was incorrect behavior that corrupted valid file names.

Fix

Remove the bannedChars stripping. The quoting already provides full safety. Update the related test to assert the correct preserving behavior and add coverage for the affected characters.

Test

The existing 'should remove dangerous characters' test has been renamed 'should preserve special characters that are safe inside single quotes' with corrected expectations.

Fixes #276999

…inal

Characters such as ~, !, #, $, ^, &, *, |, ;, <, > and backtick were
silently stripped from file paths when dragging files from the Explorer
into the terminal. This produced invalid paths pointing to non-existent
files.

The stripping was unnecessary: escapeNonWindowsPath always wraps paths
in shell-appropriate quotes (e.g. single quotes for bash/sh/zsh), inside
which all of those characters are treated as literals and cannot cause
shell injection.

Remove the bannedChars replacement and update the related test to assert
the correct (preserving) behavior.

Fixes microsoft#276999
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.

Dragging-n-dropping a file with tilde in the name into terminal

2 participants