fix: pass paths to PowerShell as Base64, validate files one by one
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
1ed04fe9be
commit
6360c75ff8
3 files changed
+128
-40
No files matched your search
@@ -33,7 +33,7 @@ const IMAGE_APPS = new Set(["claude-code", "codex", "gemini", "opencode"]);
|
||||
* @returns {string}
|
||||
*/
|
||||
export function clipboardScript(outDir, stamp) {
|
||||
const dir = quote(outDir);
|
||||
const dir = decode(outDir);
|
||||
const name = `paste-${String(stamp).replace(/[^\w-]/g, "")}.png`;
|
||||
|
||||
return [
|
||||
@@ -150,11 +150,13 @@ export function parsePickerResult(res) {
|
||||
* @returns {string}
|
||||
*/
|
||||
export function sizeScript(paths) {
|
||||
const list = (Array.isArray(paths) ? paths : []).map(quote).join(", ");
|
||||
const list = (Array.isArray(paths) ? paths : []).map(String).join("\n");
|
||||
return [
|
||||
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
|
||||
"$tab = [char]9",
|
||||
`$paths = @(${list})`,
|
||||
// One path per line (a file name has no newline), as Base64: no byte of
|
||||
// a name is ever part of the script itself.
|
||||
`$paths = @(${decode(list)}.Split([char]10) | Where-Object { $_ -ne '' })`,
|
||||
"foreach ($p in $paths) {",
|
||||
" if (Test-Path -LiteralPath $p -PathType Container) {",
|
||||
' Write-Output ("DIR" + $tab + $p)',
|
||||
@@ -253,7 +255,54 @@ function toBytes(text) {
|
||||
return Number.isFinite(value) && value >= 0 ? value : null;
|
||||
}
|
||||
|
||||
/** A PowerShell single-quoted string. */
|
||||
function quote(text) {
|
||||
return `'${String(text).replace(/'/g, "''")}'`;
|
||||
/**
|
||||
* PowerShell that yields `text` - without the text itself being in the
|
||||
* script.
|
||||
*
|
||||
* A path or a name comes from the outside, and PowerShell reads more than the
|
||||
* ASCII apostrophe as a quote (U+2018, U+2019, U+201A, U+201B too), so no
|
||||
* quoting is safe enough. The text goes in as Base64 of its UTF-8 bytes - an
|
||||
* alphabet with no quote, no `$`, no space - and is decoded by PowerShell.
|
||||
*
|
||||
* @param {string} text
|
||||
* @returns {string}
|
||||
*/
|
||||
function decode(text) {
|
||||
return `[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64(utf8(text))}'))`;
|
||||
}
|
||||
|
||||
/** The UTF-8 bytes of a text (a lone surrogate becomes U+FFFD). */
|
||||
function utf8(text) {
|
||||
const bytes = [];
|
||||
for (const char of String(text)) {
|
||||
let code = char.codePointAt(0);
|
||||
if (code >= 0xd800 && code <= 0xdfff) code = 0xfffd;
|
||||
if (code < 0x80) {
|
||||
bytes.push(code);
|
||||
} else if (code < 0x800) {
|
||||
bytes.push(0xc0 | (code >> 6), 0x80 | (code & 63));
|
||||
} else if (code < 0x10000) {
|
||||
bytes.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63));
|
||||
} else {
|
||||
bytes.push(0xf0 | (code >> 18), 0x80 | ((code >> 12) & 63), 0x80 | ((code >> 6) & 63), 0x80 | (code & 63));
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
/** Base64 of bytes (no Buffer, no btoa: the mods do not have to have them). */
|
||||
function base64(bytes) {
|
||||
let out = "";
|
||||
for (let i = 0; i < bytes.length; i += 3) {
|
||||
const a = bytes[i];
|
||||
const b = bytes[i + 1];
|
||||
const c = bytes[i + 2];
|
||||
out += BASE64[a >> 2];
|
||||
out += BASE64[((a & 3) << 4) | ((b ?? 0) >> 4)];
|
||||
out += b === undefined ? "=" : BASE64[((b & 15) << 2) | ((c ?? 0) >> 6)];
|
||||
out += c === undefined ? "=" : BASE64[c & 63];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user