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:
+9
-3
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user