fix(unityfs): share bounded header probing with desktop inspection (#12169)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 03:34:19 +00:00
1 parent 3d313b56bc
commit 2b7d54257f
5 files changed
+86 -14

No files matched your search

+14 -3
View File
@@ -25,6 +25,7 @@ import { hasMachoMagic } from "../domain/apple/machoContainer.js";
import {
hasUnityFsHeader,
readUnityFsHeaderFacts,
UNITYFS_HEADER_PREFIX_BYTES,
} from "../domain/unityfsFormat.js";
import { sha256Hex } from "../kernel/sha256Hex.js";
@@ -69,7 +70,7 @@ class DesktopProbeLimit extends ArtifactReaderFailure {
}
}
/** Inspect selected ranges on an already admitted descriptor; never hash payloads. */
/** Inspect and hash selected ranges on an already admitted descriptor. */
export const inspectDesktopFile = async (
file: FileHandle,
row: DesktopFile,
@@ -171,8 +172,18 @@ export const inspectDesktopFile = async (
},
});
}
if (prefix.length >= 20 && hasUnityFsHeader(prefix)) {
const unityfsHeader = readUnityFsHeaderFacts(prefix);
if (hasUnityFsHeader(prefix)) {
let unityfsHeader = readUnityFsHeaderFacts(prefix);
const headerPrefixBytes = Math.min(size, UNITYFS_HEADER_PREFIX_BYTES);
// The generic probe can end inside a version string or the fixed tail.
// Continue only for UnityFS and charge the additional range to the same budget.
if (unityfsHeader === undefined && prefix.length < headerPrefixBytes)
unityfsHeader = readUnityFsHeaderFacts(
Buffer.concat([
prefix,
await read(prefix.length, headerPrefixBytes - prefix.length),
]),
);
if (unityfsHeader !== undefined)
fact({
kind: "unityfs-header",
+4 -7
View File
@@ -12,6 +12,7 @@ import {
} from "../domain/binaryContainer.js";
import { setImmediate } from "node:timers/promises";
import {
hasUnityFsHeader,
readUnityFsHeaderFacts,
UNITYFS_COMPRESSION_LZ4,
UNITYFS_COMPRESSION_LZ4HC,
@@ -20,8 +21,7 @@ import {
UNITYFS_FLAG_COMPRESSION_MASK,
UNITYFS_FORMAT_VERSION_MAX,
UNITYFS_FORMAT_VERSION_MIN,
UNITYFS_HEADER_STRING_MAX,
UNITYFS_SIGNATURE,
UNITYFS_HEADER_PREFIX_BYTES,
unityFsLayoutFlags,
} from "../domain/unityfsFormat.js";
import { strictUtf8Decoder } from "../domain/strictUtf8.js";
@@ -42,8 +42,6 @@ const directoryLimit = (): ArtifactReaderFailure =>
{ boundary: "memory" },
);
/** Signature, version, two bounded strings and the fixed tail; reads are bounded. */
const HEADER_PREFIX_MAX = 8 + 4 + UNITYFS_HEADER_STRING_MAX * 2 + 20;
const UTF8 = strictUtf8Decoder("preserve");
export interface UnityFsHeader {
@@ -112,12 +110,11 @@ export const readUnityFsHeader = async (
const prefix = await readArtifactFileBytes(
handle,
0,
Math.min(size, HEADER_PREFIX_MAX),
Math.min(size, UNITYFS_HEADER_PREFIX_BYTES),
size,
signal,
);
if (prefix.length < 20 || !prefix.subarray(0, 8).equals(UNITYFS_SIGNATURE))
return undefined;
if (prefix.length < 20 || !hasUnityFsHeader(prefix)) return undefined;
const facts = readUnityFsHeaderFacts(prefix);
if (facts === undefined)
throw unityFsFormatFailure("header is truncated or exceeds its bounds");
+12 -4
View File
@@ -105,7 +105,15 @@ export const unityFsLayoutFlags = (
};
/** Producer version strings are short; the bound keeps a corrupt header local. */
export const UNITYFS_HEADER_STRING_MAX = 1024;
const UNITYFS_HEADER_STRING_MAX = 1024;
const UNITYFS_VERSION_STRINGS_OFFSET = UNITYFS_SIGNATURE.length + 4;
const UNITYFS_HEADER_TAIL_BYTES = 8 + 4 + 4 + 4;
/** A prefix covering the signature, version, both bounded strings and fixed tail. */
export const UNITYFS_HEADER_PREFIX_BYTES =
UNITYFS_VERSION_STRINGS_OFFSET +
2 * UNITYFS_HEADER_STRING_MAX +
UNITYFS_HEADER_TAIL_BYTES;
/** Declared header fields; support and extent admission belong to callers. */
export interface UnityFsHeaderFacts {
@@ -132,7 +140,7 @@ export const readUnityFsHeaderFacts = (
): UnityFsHeaderFacts | undefined => {
if (!hasUnityFsHeader(bytes)) return undefined;
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
let cursor = 12;
let cursor = UNITYFS_VERSION_STRINGS_OFFSET;
const readString = (): string | undefined => {
const start = cursor;
while (
@@ -157,7 +165,7 @@ export const readUnityFsHeaderFacts = (
if (
playerVersion === undefined ||
engineVersion === undefined ||
cursor + 20 > bytes.length
cursor + UNITYFS_HEADER_TAIL_BYTES > bytes.length
)
return undefined;
const fixed = cursor;
@@ -169,6 +177,6 @@ export const readUnityFsHeaderFacts = (
compressedBlocksInfoSize: view.getUint32(fixed + 8),
uncompressedBlocksInfoSize: view.getUint32(fixed + 12),
flags: view.getUint32(fixed + 16),
afterFixed: fixed + 20,
afterFixed: fixed + UNITYFS_HEADER_TAIL_BYTES,
};
};