Merge pull request #841 from morluto/fix/plist-non-json-values
fix(native): inspect plists containing data, dates, and non-finite reals
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { projectPlistValue } from "./plistValue.js";
|
||||
|
||||
it("projects plist data, dates, and non-finite reals as typed values", () => {
|
||||
expect(
|
||||
projectPlistValue({
|
||||
blob: new Uint8Array([0, 1, 2]),
|
||||
when: new Date("2020-01-01T00:00:00Z"),
|
||||
nested: [{ ratio: 0.5, missing: Number.NaN, unbounded: -Infinity }],
|
||||
plain: { "": "empty key", flag: true, count: 42 },
|
||||
}),
|
||||
).toEqual({
|
||||
value: {
|
||||
blob: { $plist_type: "data", base64: "AAEC" },
|
||||
when: { $plist_type: "date", iso8601: "2020-01-01T00:00:00.000Z" },
|
||||
nested: [
|
||||
{
|
||||
ratio: 0.5,
|
||||
missing: { $plist_type: "real", value: null },
|
||||
unbounded: { $plist_type: "real", value: null },
|
||||
},
|
||||
],
|
||||
plain: { "": "empty key", flag: true, count: 42 },
|
||||
},
|
||||
unknownRealCount: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects values a plist decoder cannot produce", () => {
|
||||
expect(() => projectPlistValue({ value: Symbol("x") })).toThrow();
|
||||
const deep: unknown[] = [];
|
||||
let cursor = deep;
|
||||
for (let depth = 0; depth < 200; depth += 1) {
|
||||
const next: unknown[] = [];
|
||||
cursor.push(next);
|
||||
cursor = next;
|
||||
}
|
||||
expect(() => projectPlistValue(deep)).toThrow(RangeError);
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import type { JsonValue } from "./jsonValue.js";
|
||||
|
||||
const MAX_PLIST_DEPTH = 128;
|
||||
|
||||
/** A decoded plist projected into JSON, with values JSON cannot express. */
|
||||
export interface ProjectedPlistValue {
|
||||
readonly value: JsonValue;
|
||||
/** Reals decoded as NaN, which the decoder also uses for ±infinity. */
|
||||
readonly unknownRealCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Project a decoded plist into JSON. Data and dates become explicit
|
||||
* `$plist_type` objects; a non-finite real becomes an explicit unknown
|
||||
* because the XML decoder reports NaN and ±infinity alike.
|
||||
*/
|
||||
export const projectPlistValue = (value: unknown): ProjectedPlistValue => {
|
||||
let unknownRealCount = 0;
|
||||
const project = (item: unknown, depth: number): JsonValue => {
|
||||
if (depth > MAX_PLIST_DEPTH)
|
||||
throw new RangeError(
|
||||
`Plist exceeds ${String(MAX_PLIST_DEPTH)} nesting levels`,
|
||||
);
|
||||
if (item instanceof Uint8Array)
|
||||
return {
|
||||
$plist_type: "data",
|
||||
base64: Buffer.from(item).toString("base64"),
|
||||
};
|
||||
if (item instanceof Date)
|
||||
return { $plist_type: "date", iso8601: item.toISOString() };
|
||||
if (typeof item === "number" && !Number.isFinite(item)) {
|
||||
unknownRealCount += 1;
|
||||
return { $plist_type: "real", value: null };
|
||||
}
|
||||
if (Array.isArray(item))
|
||||
return item.map((entry: unknown) => project(entry, depth + 1));
|
||||
if (item !== null && typeof item === "object")
|
||||
return Object.fromEntries(
|
||||
Object.entries(item).map(([key, entry]) => [
|
||||
key,
|
||||
project(entry, depth + 1),
|
||||
]),
|
||||
);
|
||||
return z.union([z.string(), z.number(), z.boolean(), z.null()]).parse(item);
|
||||
};
|
||||
const projected = project(value, 0);
|
||||
return { value: projected, unknownRealCount };
|
||||
};
|
||||
Reference in New Issue
Block a user