fix(cli): reject invalid UTF-8 JSON file bytes (#613)

* fix(cli): reject invalid UTF-8 JSON file bytes

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>

* test(browser): preserve unknown with-scope loader bindings

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>

---------

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
Rudy Mizrahi Celekli
2026-10-06 02:10:11 +08:00
committed by GitHub
parent 1bd641d39f
commit 22acb0d5d7
3 changed files with 57 additions and 4 deletions
+9 -3
View File
@@ -16,7 +16,7 @@ export const parseCliJsonInput = async (
if (["{", "["].includes(value.trimStart()[0] ?? ""))
return { ok: false, error: inputError(operation) };
try {
const parsed = parseJson(await readFile(value, "utf8"));
const parsed = parseJson(await readFile(value));
return parsed === undefined
? jsonFileError(value, operation, "invalid-json")
: { ok: true, value: parsed };
@@ -25,9 +25,15 @@ export const parseCliJsonInput = async (
}
};
const parseJson = (value: string): unknown => {
const parseJson = (value: string | Uint8Array): unknown => {
try {
return JSON.parse(value);
return JSON.parse(
typeof value === "string"
? value
: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
value,
),
);
} catch {
return undefined;
}