fix: catalog reads project files through Base64, shared PowerShell helper
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
cfd9ffe943
commit
df4605bac1
4 files changed
+121
-57
No files matched your search
@@ -18,6 +18,7 @@
|
||||
// was read and nothing fit.
|
||||
|
||||
import { GROUP_BUILTIN, GROUP_PROJECT, GROUP_USER, rank } from "./suggest.js";
|
||||
import { decode } from "./powershell.js";
|
||||
|
||||
/**
|
||||
* @typedef {import("./suggest.js").Item} Item
|
||||
@@ -277,17 +278,26 @@ async function tryRead(exec, shell, path, cwd) {
|
||||
return String(result.stdout ?? "");
|
||||
}
|
||||
|
||||
/** The PowerShell that reads one file; the path goes in as a literal. */
|
||||
function readScript(path) {
|
||||
const literal = path.replace(/'/g, "''");
|
||||
/**
|
||||
* The PowerShell that reads one file.
|
||||
*
|
||||
* The path comes from a project that may have been cloned from anywhere, and
|
||||
* PowerShell reads U+2018..U+201B as quotes too: the path is never in the
|
||||
* script as text, only as Base64 that the script decodes (`powershell.js`).
|
||||
*
|
||||
* @param {string} path
|
||||
* @returns {string}
|
||||
*/
|
||||
export function readScript(path) {
|
||||
return (
|
||||
"[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); " +
|
||||
// A file that is gone writes nothing and says nothing - the command
|
||||
// would be a ghost. Without the encoding, Windows PowerShell reads a file
|
||||
// without a BOM as the system's own code page, and the text comes out
|
||||
// garbled.
|
||||
`if (-not (Test-Path -LiteralPath '${literal}')) { exit 1 }; ` +
|
||||
`Get-Content -Raw -Encoding utf8 -ErrorAction Stop -LiteralPath '${literal}'`
|
||||
`$path = ${decode(path)}; ` +
|
||||
"if (-not (Test-Path -LiteralPath $path)) { exit 1 }; " +
|
||||
"Get-Content -Raw -Encoding utf8 -ErrorAction Stop -LiteralPath $path"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
// An empty or missing <bytes> means the size could not be read: the caller
|
||||
// does not let such a file through (fail closed).
|
||||
|
||||
import { decode } from "./powershell.js";
|
||||
|
||||
/** How long a pasted image stays in the paste folder. */
|
||||
export const PASTE_MAX_AGE_MS = 7 * 864e5;
|
||||
|
||||
@@ -254,55 +256,3 @@ function toBytes(text) {
|
||||
const value = Number(text);
|
||||
return Number.isFinite(value) && value >= 0 ? value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// powershell.js - how text from the outside gets into a PowerShell script.
|
||||
//
|
||||
// Pure: no gpui-kit, no gpui-base, no herdr. Every script the mod hands to
|
||||
// `exec("powershell", ["-Command", script])` (clipboard.js, catalog.js) builds
|
||||
// what it needs from a path or a name that is NOT the mod's own - a file the
|
||||
// person dropped, a file in a cloned project - through `decode`, and never by
|
||||
// putting that text between quotes.
|
||||
|
||||
/**
|
||||
* 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} a PowerShell expression
|
||||
*/
|
||||
export 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