fix(clipboard): use base64 encoding for PowerShell read path to prevent ANSI codepage corruption#37212
Open
annguyenNous wants to merge 1 commit into
Open
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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