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:
FactorandOvis Fast committed 2026-11-19 11:43:06 +01:00
1 parent 1ed04fe9be
commit 6360c75ff8
3 files changed
+128 -40

No files matched your search

+37 -7
View File
@@ -18,11 +18,29 @@ test("clipboardScript: files first (with sizes, directories apart), then image,
assert.ok(s.includes(".Length"));
assert.ok(s.includes("IMAGE"));
assert.ok(s.includes("NONE"));
assert.ok(s.includes("C:\\out dir"));
assert.equal(decodedStrings(s).includes("C:\\out dir"), true);
});
test("clipboardScript quotes a path with an apostrophe", () => {
assert.ok(clipboardScript("C:\\o'brien", "1").includes("C:\\o''brien"));
// What a script carries as Base64 (the only way text from outside gets in).
const decodedStrings = (script) =>
[...script.matchAll(/FromBase64String\('([A-Za-z0-9+/=]*)'\)/g)].map((m) => Buffer.from(m[1], "base64").toString("utf8"));
const NASTY = ["C:\\it\u2019s.txt", "C:\\it\u2018s.txt", "C:\\a\u201As\u201Bb.txt", "C:\\o'brien.txt", "C:\\$env:USERNAME.txt", 'C:\\"; Remove-Item x #.txt', "C:\\zażółć gęślą.txt"];
test("clipboardScript: the folder is Base64, never a literal (any quote, $, or text)", () => {
for (const dir of NASTY) {
const s = clipboardScript(dir, "1");
assert.deepEqual(decodedStrings(s), [dir]);
for (const raw of ["\u2019", "\u2018", "\u201A", "\u201B", "$env:USERNAME", "Remove-Item x", "o'brien", "zażółć"]) {
if (dir.includes(raw)) assert.equal(s.includes(raw), false, `${raw} in the script`);
}
}
});
test("clipboardScript: the stamp can only be harmless characters", () => {
const s = clipboardScript("C:\o", "1'$(x)'");
assert.equal(s.includes("$(x)"), false);
assert.ok(s.includes("paste-1x.png"));
});
test("parseClipboardResult: image with its size", () => {
@@ -92,10 +110,12 @@ test("parsePickerResult: path and bytes, cancelled -> []", () => {
assert.deepEqual(parsePickerResult(null), []);
});
test("sizeScript: one call for the whole batch, paths quoted", () => {
const s = sizeScript(["C:\\a b\\it's.txt", "D:\\x.png"]);
assert.ok(s.includes("'C:\\a b\\it''s.txt'"));
assert.ok(s.includes("'D:\\x.png'"));
test("sizeScript: one call for the whole batch, the paths only as Base64", () => {
const s = sizeScript(NASTY);
assert.deepEqual(decodedStrings(s), [NASTY.join("\n")]);
for (const raw of ["\u2019", "\u2018", "\u201A", "\u201B", "$env:USERNAME", "Remove-Item x", "o'brien", "zażółć"]) {
assert.equal(s.includes(raw), false, `${raw} in the script`);
}
assert.ok(s.includes("-LiteralPath"));
assert.ok(s.includes("DIR"));
assert.ok(s.includes("SIZE"));
@@ -136,3 +156,13 @@ test("supportsImages", () => {
assert.equal(supportsImages(null), false);
assert.equal(supportsImages(""), false);
});
test("sizeScript: an empty batch decodes to nothing", () => {
assert.deepEqual(decodedStrings(sizeScript([])), [""]);
});
test("Base64 of every length of tail round-trips (padding)", () => {
for (const text of ["", "a", "ab", "abc", "abcd", "ęą", "😀x", "a\nb"]) {
assert.deepEqual(decodedStrings(sizeScript([text])), [text]);
}
});