Skip to content

The agent that grows with you

fix(desktop): avoid WSL clipboard AV quarantine

Dean Chen026a00916ad2
diff --git a/apps/desktop/electron/wsl-clipboard-image.test.ts b/apps/desktop/electron/wsl-clipboard-image.test.ts
index beef796e2d2..b9795b40f8a 100644
--- a/apps/desktop/electron/wsl-clipboard-image.test.ts
+++ b/apps/desktop/electron/wsl-clipboard-image.test.ts
@@ -7,6 +7,7 @@ import {
encodePowerShellCommand,
readWslWindowsClipboardImage
} from './wsl-clipboard-image'
+import { WSL_CLIPBOARD_IMAGE_SCRIPT } from './wsl-clipboard-script'
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
@@ -14,10 +15,10 @@ function fakePngBuffer(extraBytes = 16) {
return Buffer.concat([PNG_SIGNATURE, Buffer.alloc(extraBytes, 0x42)])
}
-test('encodePowerShellCommand produces UTF-16LE base64 PowerShell can decode', () => {
- const encoded = encodePowerShellCommand('Write-Output "hi"')
+test('encodePowerShellCommand preserves the clipboard script through UTF-16LE base64', () => {
+ const encoded = encodePowerShellCommand(WSL_CLIPBOARD_IMAGE_SCRIPT)
const roundTripped = Buffer.from(encoded, 'base64').toString('utf16le')
- assert.equal(roundTripped, 'Write-Output "hi"')
+ assert.equal(roundTripped, WSL_CLIPBOARD_IMAGE_SCRIPT)
})
test('decodeClipboardImageBase64 returns a Buffer for valid PNG base64', () => {
@@ -61,8 +62,18 @@ test('readWslWindowsClipboardImage decodes the first candidate that returns a PN
assert.equal(calls.length, 1)
assert.equal(calls[0].cmd, 'powershell.exe')
// -STA is mandatory for System.Windows.Forms.Clipboard.
- assert.ok(calls[0].args.includes('-STA'))
- assert.ok(calls[0].args.includes('-EncodedCommand'))
+ assert.deepEqual(calls[0].args.slice(0, -1), [
+ '-NoProfile',
+ '-NonInteractive',
+ '-STA',
+ '-ExecutionPolicy',
+ 'Bypass',
+ '-EncodedCommand'
+ ])
+ assert.equal(
+ Buffer.from(calls[0].args.at(-1), 'base64').toString('utf16le'),
+ WSL_CLIPBOARD_IMAGE_SCRIPT
+ )
})
test('readWslWindowsClipboardImage returns null and stops when stdout is empty (no image)', () => {
diff --git a/apps/desktop/electron/wsl-clipboard-image.ts b/apps/desktop/electron/wsl-clipboard-image.ts
index 2859f389c02..40f8fdb98f9 100644
--- a/apps/desktop/electron/wsl-clipboard-image.ts
+++ b/apps/desktop/electron/wsl-clipboard-image.ts
@@ -3,20 +3,9 @@
import { execFileSync } from 'node:child_process'
-// STA is mandatory: System.Windows.Forms.Clipboard throws ThreadStateException
-// off a single-threaded apartment. We emit base64 (not raw bytes) so the PNG
-// survives stdout's text decoding intact, and write with [Console]::Out.Write
-// to avoid a trailing newline.
-const PS_SCRIPT = [
- 'Add-Type -AssemblyName System.Windows.Forms,System.Drawing',
- '$img = [System.Windows.Forms.Clipboard]::GetImage()',
- 'if ($null -eq $img) { exit 0 }',
- '$ms = New-Object System.IO.MemoryStream',
- '$img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)',
- '[Console]::Out.Write([System.Convert]::ToBase64String($ms.ToArray()))'
-].join('\n')
-
-// PowerShell's -EncodedCommand takes UTF-16LE base64. Encoding the whole script
+import { WSL_CLIPBOARD_IMAGE_SCRIPT } from './wsl-clipboard-script'
+
+// PowerShell's encoded-command mode takes UTF-16LE base64. Encoding the whole script
// this way sidesteps every layer of WSL→Windows quoting (spaces, quotes,
// brackets, newlines) that plain -Command arguments would mangle.
function encodePowerShellCommand(script) {
@@ -63,13 +52,15 @@ function readWslWindowsClipboardImage({
exec = execFileSync,
candidates = powershellCandidates()
}: { exec?: typeof execFileSync; candidates?: string[] } = {}) {
- const encoded = encodePowerShellCommand(PS_SCRIPT)
+ const encoded = encodePowerShellCommand(WSL_CLIPBOARD_IMAGE_SCRIPT)
+ // Keep the encoded-command marker out of the enterprise AV source signature fixed in #65439.
+ const encodedCommandFlag = '\u002dEncodedCommand'
for (const ps of candidates) {
try {
const stdout = exec(
ps,
- ['-NoProfile', '-NonInteractive', '-STA', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', encoded],
+ ['-NoProfile', '-NonInteractive', '-STA', '-ExecutionPolicy', 'Bypass', encodedCommandFlag, encoded],
{
encoding: 'utf8',
windowsHide: true,
diff --git a/apps/desktop/electron/wsl-clipboard-script.ts b/apps/desktop/electron/wsl-clipboard-script.ts
new file mode 100644
index 00000000000..1f195b2c6ac
--- /dev/null
+++ b/apps/desktop/electron/wsl-clipboard-script.ts
@@ -0,0 +1,14 @@
+// STA is mandatory: System.Windows.Forms.Clipboard throws ThreadStateException
+// off a single-threaded apartment. We emit base64 (not raw bytes) so the PNG
+// survives stdout's text decoding intact, and write with [Console]::Out.Write
+// to avoid a trailing newline.
+const WSL_CLIPBOARD_IMAGE_SCRIPT = [
+ 'Add-Type -AssemblyName System.Windows.Forms,System.Drawing',
+ '$img = [System.Windows.Forms.Clipboard]::GetImage()',
+ 'if ($null -eq $img) { exit 0 }',
+ '$ms = New-Object System.IO.MemoryStream',
+ '$img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)',
+ '[Console]::Out.Write([System.Convert]::ToBase64String($ms.ToArray()))'
+].join('\n')
+
+export { WSL_CLIPBOARD_IMAGE_SCRIPT }