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;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { decode } from "../herdr/herdr-composer/mods/herdr-composer/powershell.js";
|
||||
import { readScript } from "../herdr/herdr-composer/mods/herdr-composer/catalog.js";
|
||||
|
||||
const NASTY = [
|
||||
".claude/commands/it\u2019s.md",
|
||||
".claude/commands/it\u2018s.md",
|
||||
".claude/commands/a\u201Ab\u201Bc.md",
|
||||
".claude/commands/o'brien.md",
|
||||
".claude/commands/$env:USERNAME.md",
|
||||
'.claude/commands/"; calc #.md',
|
||||
".claude/skills/zażółć gęślą/SKILL.md",
|
||||
];
|
||||
|
||||
const b64s = (script) =>
|
||||
[...script.matchAll(/FromBase64String\('([A-Za-z0-9+/=]*)'\)/g)].map((m) => Buffer.from(m[1], "base64").toString("utf8"));
|
||||
|
||||
test("decode: PowerShell that yields the text, with the text itself nowhere in it", () => {
|
||||
for (const text of NASTY) {
|
||||
const expr = decode(text);
|
||||
assert.deepEqual(b64s(expr), [text]);
|
||||
assert.equal(expr.includes(text), false);
|
||||
assert.match(expr, /^\[System\.Text\.Encoding\]::UTF8\.GetString\(\[System\.Convert\]::FromBase64String\('[A-Za-z0-9+/=]*'\)\)$/);
|
||||
}
|
||||
});
|
||||
|
||||
test("decode: every length of tail, emoji, newline, empty", () => {
|
||||
for (const text of ["", "a", "ab", "abc", "abcd", "ęą", "😀x", "a\nb"]) {
|
||||
assert.deepEqual(b64s(decode(text)), [text]);
|
||||
}
|
||||
});
|
||||
|
||||
test("catalog readScript: the path of a project file is only ever Base64", () => {
|
||||
for (const path of NASTY) {
|
||||
const script = readScript(path);
|
||||
assert.deepEqual(b64s(script), [path]);
|
||||
for (const raw of ["\u2019", "\u2018", "\u201A", "\u201B", "$env:USERNAME", "calc", "o'brien", "zażółć"]) {
|
||||
if (path.includes(raw)) assert.equal(script.includes(raw), false, `${raw} in the script`);
|
||||
}
|
||||
// the script still reads the file
|
||||
assert.ok(script.includes("Get-Content"));
|
||||
assert.ok(script.includes("-LiteralPath"));
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user