mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(plist): disclose lossy stored text decoding (#12268)
* fix(plist): disclose lossy stored text decoding * fix(plist): report lossy UTF-8 marked data --------- Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
2 files changed
+92
-21
No files matched your search
@@ -250,6 +250,44 @@ it("honors a byte order mark in a data object", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["UTF-8 string", Buffer.from([0x72, 0x61, 0xff])],
|
||||
["UTF-16BE string", Buffer.from([0x62, 0, 0x61, 0xd8, 0])],
|
||||
["UTF-16 data", Buffer.from([0x45, 0xff, 0xfe, 0x61, 0, 0x62])],
|
||||
] as const)("reports partial text beside malformed %s", (_name, malformed) => {
|
||||
const bytes = source([
|
||||
dictionary(1, 2),
|
||||
text("key"),
|
||||
text("kept"),
|
||||
malformed,
|
||||
]);
|
||||
expect(decodeBinaryPlistText(bytes, budget)).toEqual({
|
||||
text: "key\nkept\na\ufffd",
|
||||
complete: false,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["valid marked UTF-8", [0xef, 0xbb, 0xbf, 0xc3, 0xa9], "\ufeffé", true],
|
||||
[
|
||||
"lossy marked UTF-8",
|
||||
[0xef, 0xbb, 0xbf, 0x61, 0xff],
|
||||
"\ufeffa\ufffd",
|
||||
false,
|
||||
],
|
||||
["unmarked raw data", [0x61, 0xff], "a\ufffd", true],
|
||||
] as const)(
|
||||
"tracks stored data text coverage for %s",
|
||||
(_name, payload, value, complete) => {
|
||||
const data = Buffer.from([0x40 | payload.length, ...payload]);
|
||||
const bytes = source([dictionary(1, 2), text("key"), data]);
|
||||
expect(decodeBinaryPlistText(bytes, budget)).toEqual({
|
||||
text: `key\n${value}`,
|
||||
complete,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps readable text beside an unreferenced string it cannot read", () => {
|
||||
const bytes = source([
|
||||
dictionary(1, 2),
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
createPlistDuplicateKeyCollector,
|
||||
type PlistDuplicateKeys,
|
||||
} from "./plistDuplicateKeys.js";
|
||||
import { isStrictUtf8 } from "../strictUtf8.js";
|
||||
import { byteOrderMarkEncoding } from "../textByteOrderMark.js";
|
||||
import type { ParsedPropertyList } from "../propertyListKeys.js";
|
||||
import {
|
||||
@@ -191,27 +192,59 @@ const storedPlistPayload = (
|
||||
return { type, payload: bytes.subarray(start, end) };
|
||||
};
|
||||
|
||||
/** The text a data payload holds, honoring a byte order mark; null when no reader scans it. */
|
||||
const storedDataText = (payload: Buffer): string | null => {
|
||||
const encoding = byteOrderMarkEncoding(payload);
|
||||
if (encoding === "utf-32") return null;
|
||||
return encoding === null
|
||||
? payload.toString("utf8")
|
||||
: new TextDecoder(encoding).decode(payload);
|
||||
/** Decode declared wide text while preserving readable portions on a malformed input. */
|
||||
const storedWideText = (
|
||||
payload: Buffer,
|
||||
encoding: "utf-16le" | "utf-16be",
|
||||
ignoreBOM: boolean,
|
||||
): BinaryPlistText => {
|
||||
try {
|
||||
const text = new TextDecoder(encoding, { fatal: true, ignoreBOM }).decode(
|
||||
payload,
|
||||
);
|
||||
return { text, complete: true };
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof TypeError)) throw cause;
|
||||
return {
|
||||
text: new TextDecoder(encoding, { ignoreBOM }).decode(payload),
|
||||
complete: false,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Decode a payload `storedPlistPayload` found; call only once it fits the
|
||||
* budget. Null when its encoding cannot be scanned.
|
||||
*/
|
||||
const storedPlistText = (type: number, payload: Buffer): string | null =>
|
||||
type === 5
|
||||
? payload.toString("latin1")
|
||||
: type === 6
|
||||
? Buffer.from(payload).swap16().toString("utf16le")
|
||||
: type === 4
|
||||
? storedDataText(payload)
|
||||
: payload.toString("utf8");
|
||||
/** Scan data bytes as before unless a byte order mark names a specific encoding. */
|
||||
const storedDataText = (payload: Buffer): BinaryPlistText | null => {
|
||||
const encoding = byteOrderMarkEncoding(payload);
|
||||
if (encoding === "utf-32") return null;
|
||||
if (encoding !== null) return storedWideText(payload, encoding, false);
|
||||
const text = payload.toString("utf8");
|
||||
// This decode preserves a UTF-8 BOM. It declares text just as a wide
|
||||
// mark does; unmarked data remains eligible for best-effort raw scanning.
|
||||
return {
|
||||
text,
|
||||
complete: !text.startsWith("\ufeff") || isStrictUtf8(payload),
|
||||
};
|
||||
};
|
||||
|
||||
/** Decode a stored payload after its byte budget is admitted. */
|
||||
const storedPlistText = (
|
||||
type: number,
|
||||
payload: Buffer,
|
||||
): BinaryPlistText | null => {
|
||||
switch (type) {
|
||||
case 4:
|
||||
return storedDataText(payload);
|
||||
case 5:
|
||||
return { text: payload.toString("latin1"), complete: true };
|
||||
case 6:
|
||||
return storedWideText(payload, "utf-16be", true);
|
||||
default:
|
||||
return {
|
||||
text: payload.toString("utf8"),
|
||||
complete: isStrictUtf8(payload),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/** The text a binary plist stores; `complete` is false when part of it went unread. */
|
||||
export interface BinaryPlistText {
|
||||
@@ -256,8 +289,8 @@ export const decodeBinaryPlistText = (
|
||||
}
|
||||
decoded += stored.payload.length;
|
||||
const text = storedPlistText(stored.type, stored.payload);
|
||||
if (text === null) complete = false;
|
||||
else texts.push(text);
|
||||
if (text?.complete !== true) complete = false;
|
||||
if (text !== null) texts.push(text.text);
|
||||
}
|
||||
return { text: texts.join("\n"), complete };
|
||||
};
|
||||
Reference in new issue
Block a user