Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(windows): keep multi-file CF_HDROP copies and non-ASCII paths intact

review81a553e350ff
diff --git a/src/main/clipboard-files-win.test.ts b/src/main/clipboard-files-win.test.ts
index 50485ed11..cc858c863 100644
--- a/src/main/clipboard-files-win.test.ts
+++ b/src/main/clipboard-files-win.test.ts
@@ -1,5 +1,8 @@
+import { execFileSync } from 'child_process'
import { describe, expect, it, vi } from 'vitest'
import {
+ WINDOWS_FILE_DROP_READ_PATHS,
+ asciiJson,
WINDOWS_FILE_DROP_SCRIPT,
WINDOWS_FILE_DROP_TIMEOUT_MS,
windowsPowerShellPath,
@@ -28,7 +31,7 @@ describe('writeWindowsFileDrop', () => {
expect(calls[0].args).toEqual(['-NoProfile', '-NonInteractive', '-STA', '-Command', WINDOWS_FILE_DROP_SCRIPT])
for (const arg of calls[0].args) for (const p of paths) expect(arg).not.toContain(p)
expect(calls[0].options).toEqual({ timeout: WINDOWS_FILE_DROP_TIMEOUT_MS, windowsHide: true })
- expect(stdin.end).toHaveBeenCalledWith(JSON.stringify(paths))
+ expect(stdin.end).toHaveBeenCalledWith(asciiJson(paths))
})
it('maps a nonzero exit or a timeout kill to false', async () => {
@@ -57,3 +60,49 @@ describe('windowsPowerShellPath', () => {
expect(windowsPowerShellPath({ WINDIR: 'E:\\W' })).toBe('E:\\W\\System32\\WindowsPowerShell\\v1.0\\powershell.exe')
})
})
+
+describe('asciiJson', () => {
+ it('escapes every non-ASCII unit so an OEM code page cannot mangle it', () => {
+ const paths = ['C:\\caf\u00e9\\\u65e5\u672c.png', 'C:\\\ud83d\ude00.txt']
+ const wire = asciiJson(paths)
+ expect(wire).toMatch(/^[\x00-\x7e]*$/)
+ expect(JSON.parse(wire)).toEqual(paths)
+ })
+})
+
+function hasPwsh(): boolean {
+ try {
+ execFileSync('pwsh', ['-NoProfile', '-Command', 'exit 0'], { stdio: 'ignore', timeout: 20_000 })
+ return true
+ } catch {
+ return false
+ }
+}
+
+// The parse half of the real script, run under a real PowerShell. `-NoEnumerate` reproduces
+// Windows PowerShell 5.1's ConvertFrom-Json (an array is ONE pipeline object), which is the shape
+// that used to fold a multi-file selection into a single space-joined path.
+describe.skipIf(!hasPwsh())('WINDOWS_FILE_DROP_READ_PATHS under pwsh', () => {
+ const run = (paths: unknown, emulate51: boolean): string[] => {
+ const script =
+ (emulate51
+ ? WINDOWS_FILE_DROP_READ_PATHS.replace('ConvertFrom-Json -InputObject', 'ConvertFrom-Json -NoEnumerate -InputObject')
+ : WINDOWS_FILE_DROP_READ_PATHS) +
+ '; foreach ($x in $p) { [Console]::Out.Write((($x.ToCharArray() | ForEach-Object { [int]$_ }) -join ",") + ";") }'
+ const out = execFileSync('pwsh', ['-NoProfile', '-NonInteractive', '-Command', script], {
+ input: asciiJson(paths),
+ encoding: 'utf8',
+ timeout: 30_000
+ })
+ return out
+ .split(';')
+ .filter(Boolean)
+ .map((codes) => String.fromCharCode(...codes.split(',').map(Number)))
+ }
+
+ it.each([false, true])('keeps a multi-file selection as separate paths (5.1 semantics: %s)', (emulate51) => {
+ const paths = ['C:\\a b.png', 'C:\\caf\u00e9 $(x) & "q".txt', '\\\\srv\\share\\\ud83d\ude00']
+ expect(run(paths, emulate51)).toEqual(paths)
+ expect(run(['C:\\only.png'], emulate51)).toEqual(['C:\\only.png'])
+ }, 60_000)
+})
diff --git a/src/main/clipboard-files-win.ts b/src/main/clipboard-files-win.ts
index 00e9abc25..bddbb9d4c 100644
--- a/src/main/clipboard-files-win.ts
+++ b/src/main/clipboard-files-win.ts
@@ -8,17 +8,47 @@ import { win32 } from 'path'
* - The explicit `System32\WindowsPowerShell\v1.0` path pins 5.1 (always present, .NET Framework,
* WinForms available) — `pwsh` 7's `Set-Clipboard` has no `-Path`, and PATH is not trusted.
* - `-STA`: the OLE clipboard refuses an MTA thread.
- * - The PATHS NEVER RIDE ARGV OR THE SCRIPT TEXT: they arrive as JSON on stdin, so there is no
- * quoting to get wrong and nothing for a crafted file name to inject into.
+ * - The PATHS NEVER RIDE ARGV OR THE SCRIPT TEXT: they arrive as ASCII-escaped JSON on stdin
+ * (`asciiJson`), so there is no quoting to get wrong and nothing for a crafted file name to
+ * inject into.
*/
-export const WINDOWS_FILE_DROP_SCRIPT =
+/**
+ * The parse half of the script, kept separate so a test can run it under a real PowerShell.
+ *
+ * - `ConvertFrom-Json -InputObject … | ForEach-Object { $_ }`: Windows PowerShell 5.1's
+ * ConvertFrom-Json writes a JSON array as ONE pipeline object (7.x enumerates it), so
+ * `@($json | ConvertFrom-Json)` is a one-element array holding the real array, and `[string]$x`
+ * then joins every path with spaces into ONE bogus path. Re-emitting through ForEach-Object
+ * flattens it on both versions.
+ * - Every element must be a string, and there must be at least one: anything else exits nonzero.
+ */
+export const WINDOWS_FILE_DROP_READ_PATHS =
'$ErrorActionPreference = "Stop"; ' +
- '$p = @([Console]::In.ReadToEnd() | ConvertFrom-Json); ' +
- 'Add-Type -AssemblyName System.Windows.Forms; ' +
+ '$p = @(ConvertFrom-Json -InputObject ([Console]::In.ReadToEnd()) | ForEach-Object { $_ }); ' +
+ 'if ($p.Count -lt 1) { exit 2 }; ' +
+ 'foreach ($x in $p) { if ($x -isnot [string]) { exit 3 } }'
+
+export const WINDOWS_FILE_DROP_SCRIPT =
+ WINDOWS_FILE_DROP_READ_PATHS +
+ '; Add-Type -AssemblyName System.Windows.Forms; ' +
'$c = New-Object System.Collections.Specialized.StringCollection; ' +
- 'foreach ($x in $p) { [void]$c.Add([string]$x) }; ' +
+ 'foreach ($x in $p) { [void]$c.Add($x) }; ' +
'[System.Windows.Forms.Clipboard]::SetFileDropList($c)'
+/**
+ * The stdin payload: JSON with every non-ASCII UTF-16 unit as a `\uXXXX` escape. Windows
+ * PowerShell 5.1 decodes redirected stdin with the console's OEM code page (437/850/932…), not
+ * UTF-8, so a raw `é` or `日本` in a path would arrive mangled and name a file that does not exist.
+ * Pure ASCII reads identically under every code page, and ConvertFrom-Json turns the escapes back
+ * into the exact UTF-16 string (surrogate pairs included).
+ */
+export function asciiJson(value: unknown): string {
+ return JSON.stringify(value).replace(
+ /[\u007f-\uffff]/g,
+ (ch) => `\\u${ch.charCodeAt(0).toString(16).padStart(4, '0')}`
+ )
+}
+
/** PowerShell cold start is ~0.5–1.5 s; past this the copy is reported as failed, never hung. */
export const WINDOWS_FILE_DROP_TIMEOUT_MS = 5000
@@ -55,7 +85,7 @@ export function writeWindowsFileDrop(
}
// A spawn failure surfaces in the callback; swallow the paired EPIPE on stdin.
child.stdin.on?.('error', () => undefined)
- child.stdin.end(JSON.stringify(paths))
+ child.stdin.end(asciiJson(paths))
} catch {
resolve(false)
}
diff --git a/src/main/clipboard-files.test.ts b/src/main/clipboard-files.test.ts
index a70a9532c..d15d7a964 100644
--- a/src/main/clipboard-files.test.ts
+++ b/src/main/clipboard-files.test.ts
@@ -126,6 +126,23 @@ describe('validateClipboardFilePaths', () => {
expect(validateClipboardFilePaths(['C:\\a'], 'win32', () => true)).toEqual(['C:\\a'])
expect(validateClipboardFilePaths([], 'darwin', () => true)).toBeNull()
})
+
+ it('accepts only fully qualified Windows paths (drive or UNC), never a driveless rooted one', () => {
+ const ok = (p: string) => validateClipboardFilePaths([p], 'win32', () => true)
+ expect(ok('C:/a/b.png')).toEqual(['C:/a/b.png'])
+ expect(ok('\\\\srv\\share\\c.txt')).toEqual(['\\\\srv\\share\\c.txt'])
+ expect(ok('\\a.png')).toBeNull()
+ expect(ok('/a.png')).toBeNull()
+ expect(ok('C:a.png')).toBeNull()
+ expect(ok('\\\\srv')).toBeNull()
+ // POSIX keeps accepting a leading slash, byte for byte.
+ expect(validateClipboardFilePaths(['/a.png'], 'linux', () => true)).toEqual(['/a.png'])
+ })
+
+ it('dedupes case-insensitively on Windows only', () => {
+ expect(validateClipboardFilePaths(['C:\\A.png', 'c:\\a.png'], 'win32', () => true)).toEqual(['C:\\A.png'])
+ expect(validateClipboardFilePaths(['/A.png', '/a.png'], 'darwin', () => true)).toEqual(['/A.png', '/a.png'])
+ })
})
describe('fileListPropertyList', () => {
diff --git a/src/main/clipboard-files.ts b/src/main/clipboard-files.ts
index 6fdb9f7c8..c33c87062 100644
--- a/src/main/clipboard-files.ts
+++ b/src/main/clipboard-files.ts
@@ -1,4 +1,4 @@
-import { posix, win32 } from 'path'
+import { posix } from 'path'
export const FILE_LIST_PASTEBOARD_TYPE = 'NSFilenamesPboardType'
const MAX_CLIPBOARD_FILES = 64
@@ -33,6 +33,15 @@ export function fileListPropertyList(paths: string[]): Buffer {
)
}
+/**
+ * `path.win32.isAbsolute` also accepts ROOTED-but-driveless paths (`\foo`, `/foo`), which resolve
+ * against whatever drive the process is on — not a path a drop list should carry. Only a drive
+ * path (`C:\…`) or a UNC path (`\\server\share\…`) is fully qualified.
+ */
+function isFullyQualifiedWin32(value: string): boolean {
+ return /^[A-Za-z]:[\\/]/.test(value) || /^[\\/]{2}[^\\/]+[\\/][^\\/]+/.test(value)
+}
+
/**
* Validate a renderer-supplied file selection. Inputs cross an IPC trust boundary, so paths are
* capped, absolute-only (in the dialect of the platform that will read them — `path.win32` on
@@ -46,20 +55,24 @@ export function validateClipboardFilePaths(
isFile: (path: string) => boolean
): string[] | null {
if (!Array.isArray(input)) return null
- const isAbsolute = platform === 'win32' ? win32.isAbsolute : posix.isAbsolute
+ const win = platform === 'win32'
+ const isAbsolute = win ? isFullyQualifiedWin32 : posix.isAbsolute
+ // NTFS is case-insensitive: `C:\A.png` and `c:\a.png` are one file, and a drop list naming it
+ // twice makes the paste ask to overwrite the file with itself.
+ const key = (value: string): string => (win ? value.toLowerCase() : value)
const paths: string[] = []
const seen = new Set<string>()
for (const value of input) {
if (typeof value !== 'string' || !isAbsolute(value)) return null
- if (seen.has(value)) continue
+ if (seen.has(key(value))) continue
if (paths.length >= MAX_CLIPBOARD_FILES) return null
try {
if (!isFile(value)) return null
} catch {
return null
}
- seen.add(value)
+ seen.add(key(value))
paths.push(value)
}
return paths.length ? paths : null