Skip to content

Commit 530c1b4

Browse files
committed
shellIntegration.ps1: escape all non-alnum values in serialization
Instead of only escaping '\', '␤', and ';', all non-alphanumeric values are escaped. Backslashes are doubld and other characters are encoded as hex. The previous version would pass invalid characters through unescaped, including OSC sequences. Although that no longer is allowed, this iteration does not yet enforce particular (e.g. utf-8) encoding and will still misbehave on multibyte sequences.
1 parent 961cd83 commit 530c1b4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/vs/workbench/contrib/terminal/browser/media/shellIntegration.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@ $Global:__VSCodeOriginalPrompt = $function:Prompt
1818
$Global:__LastHistoryId = -1
1919

2020
function Global:__VSCode-Escape-Value([string]$value) {
21-
$value.Replace("\", "\\").Replace("`n", "\x0a").Replace(";", "\x3b")
21+
# NOTE: In PowerShell v6.1+, this can be written `$value -replace '…', { … }` instead of `[regex]::Replace`.
22+
# Replace any non-alphanumeric characters.
23+
[regex]::Replace($value, '[^a-zA-Z0-9]', { param($match)
24+
# Backslashes must be doubled.
25+
if ($match.Value -eq '\') {
26+
'\\'
27+
} else {
28+
# Any other character is encoded as hex.
29+
'\x{0:x2}' -f [int][char]$match.Value
30+
}
31+
})
2232
}
2333

2434
function Global:Prompt() {

0 commit comments

Comments
 (0)