fix(clipboard): use base64 encoding for PowerShell read path to prevent ANSI codepage corruption - #37212
Conversation
…nt ANSI codepage corruption PowerShell's Get-Clipboard -Raw outputs text in the system's ANSI codepage (e.g. CP1252, CP936), not UTF-8. When Node.js reads this with encoding: 'utf8', non-ASCII characters (CJK, emoji, accented chars) are corrupted. The write path already solved this by base64-encoding UTF-8 bytes and passing them via -Command argument (see comment at line 94-98). This fix applies the same approach to the read path: - Change PowerShell read command to base64-encode the clipboard content using [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(...)) - Add base64 flag to read command type for PowerShell entries - Decode base64 result in readClipboardText when flag is set Also adds child.unref() to spawned clipboard child processes in the write path to prevent delaying process.exit() when the app exits mid-clipboard-write.
|
Thanks for addressing the PowerShell read path. The premise remains on current main: Problems
Suggested changes
GitHub currently reports this PR as conflicting, but the target functions remain recognizable on current main, so this looks mechanically salvageable. Automated hermes-sweeper review. |
|
Merged in #81963 — your base64 clipboard strategy is on main with authorship preserved. It was the strongest of three competing approaches: encoding the clipboard bytes as base64 inside PowerShell sidesteps the console codepage entirely, where chcp/OutputEncoding forcing is fragile under -NoProfile. Covers TUI + desktop composer paste. Thanks! |
Problem
PowerShell's
Get-Clipboard -Rawoutputs text in the system's ANSI codepage (e.g. CP1252 for Western, CP936 for Chinese), not UTF-8. When Node.js reads this withencoding: 'utf8', non-ASCII characters are corrupted:The write path already solved this exact problem by base64-encoding UTF-8 bytes and passing them via
-Commandargument (see the detailed comment at lines 94-98). The read path has no equivalent mitigation — it relies onencoding: 'utf8'which only works for ASCII clipboard content.Fix
Apply the same base64 approach to the read path, symmetric with the write path:
[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((Get-Clipboard -Raw)))— this converts clipboard text to UTF-8 bytes, then base64-encodes thembase64flag to the read command type for PowerShell entriesreadClipboardTextwhen flag is set:Buffer.from(result.stdout.trim(), 'base64').toString('utf8')Also adds
child.unref()to spawned clipboard child processes in the write path to preventprocess.exit()from being blocked by slowpowershell.exestartup on WSL.Before vs After
Tests