mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 14:59:05 +00:00
refactor(pkg): fold the CPIO scan into the prefix reader (#16921)
This commit is contained in:
1 parent
f9f1cb726d
commit
8aace05d16
1 file changed
+179
-196
+179
-196
@@ -282,190 +282,6 @@ const resolveHardLinks = (
|
||||
return aliasBytes;
|
||||
};
|
||||
|
||||
const parseCpioOdcInto = (
|
||||
bytes: Buffer,
|
||||
members: CpioMember[],
|
||||
hardLinks: Map<string, LinkRecord[]>,
|
||||
remainingMembers: number = MACOS_PKG_MAX_CPIO_MEMBERS,
|
||||
packageMaxMembers: number = Math.max(
|
||||
MACOS_PKG_MAX_CPIO_MEMBERS,
|
||||
Math.floor(remainingMembers),
|
||||
),
|
||||
): Result<readonly CpioMember[], MacosPkgParseError> => {
|
||||
const effectiveMax = Math.max(0, Math.floor(packageMaxMembers));
|
||||
const memberLimit = Math.max(
|
||||
0,
|
||||
Math.min(Math.floor(remainingMembers), effectiveMax),
|
||||
);
|
||||
let cursor = 0;
|
||||
for (;;) {
|
||||
if (cursor + CPIO_ODC_HEADER_BYTES > bytes.length)
|
||||
return err(
|
||||
truncatedInput(MACOS_PKG_FORMAT, "cpio odc header is truncated", {
|
||||
field: "cpio",
|
||||
offset: cursor,
|
||||
}),
|
||||
);
|
||||
const header = bytes.subarray(cursor, cursor + CPIO_ODC_HEADER_BYTES);
|
||||
const magic = header.subarray(0, 6).toString("ascii");
|
||||
if (magic !== CPIO_ODC_MAGIC)
|
||||
return err(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc magic mismatch at offset ${String(cursor)}`,
|
||||
{ field: "cpio", offset: cursor },
|
||||
),
|
||||
);
|
||||
const mode = octalField(header, 18, 6, "mode");
|
||||
if (!mode.ok) return mode;
|
||||
const uid = octalField(header, 24, 6, "uid");
|
||||
if (!uid.ok) return uid;
|
||||
const gid = octalField(header, 30, 6, "gid");
|
||||
if (!gid.ok) return gid;
|
||||
const namesize = octalField(header, 59, 6, "namesize");
|
||||
if (!namesize.ok) return namesize;
|
||||
const filesize = octalField(header, 65, 11, "filesize");
|
||||
if (!filesize.ok) return filesize;
|
||||
if (namesize.value < 1 || namesize.value > MACOS_PKG_MAX_CPIO_NAME_BYTES)
|
||||
return err(
|
||||
macosPkgLimitFailure(
|
||||
`cpio odc namesize ${String(namesize.value)} is outside the ${String(MACOS_PKG_MAX_CPIO_NAME_BYTES)}-byte allowance`,
|
||||
{
|
||||
name: "macos_pkg_cpio_name_bytes",
|
||||
maximum: MACOS_PKG_MAX_CPIO_NAME_BYTES,
|
||||
required: namesize.value,
|
||||
},
|
||||
"namesize",
|
||||
),
|
||||
);
|
||||
cursor += CPIO_ODC_HEADER_BYTES;
|
||||
const nameOffset = cursor;
|
||||
if (cursor + namesize.value > bytes.length)
|
||||
return err(
|
||||
truncatedInput(MACOS_PKG_FORMAT, "cpio odc name is truncated", {
|
||||
field: "name",
|
||||
offset: cursor,
|
||||
}),
|
||||
);
|
||||
const nameBytes = bytes.subarray(cursor, cursor + namesize.value);
|
||||
cursor += namesize.value;
|
||||
const nul = nameBytes.indexOf(0);
|
||||
const rawNameBytes = nameBytes.subarray(
|
||||
0,
|
||||
nul === -1 ? nameBytes.length : nul,
|
||||
);
|
||||
let name: string;
|
||||
try {
|
||||
name = decodeStrictUtf8(rawNameBytes, "preserve");
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc member name at offset ${String(nameOffset)} is not valid UTF-8`,
|
||||
{ field: "name", offset: nameOffset, cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
if (name === TRAILER) {
|
||||
if (nul !== nameBytes.length - 1)
|
||||
return err(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
"cpio odc trailer name is not exactly NUL-terminated",
|
||||
{ field: "name", offset: nameOffset },
|
||||
),
|
||||
);
|
||||
if (filesize.value !== 0)
|
||||
return err(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
"cpio odc trailer declares a non-zero size",
|
||||
{
|
||||
field: "cpio",
|
||||
offset: cursor,
|
||||
},
|
||||
),
|
||||
);
|
||||
// Writers may pad the last output block with zeros. Other bytes are
|
||||
// outside the archive we inventoried, even if they form another cpio.
|
||||
let offset = cursor;
|
||||
while (offset < bytes.length && bytes[offset] === 0) offset++;
|
||||
if (offset < bytes.length)
|
||||
return err(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc has non-zero data at offset ${String(offset)} after its trailer; remaining bytes were not inventoried`,
|
||||
{ field: "cpio", offset },
|
||||
),
|
||||
);
|
||||
return ok(members);
|
||||
}
|
||||
if (cursor + filesize.value > bytes.length)
|
||||
return err(
|
||||
truncatedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc member ${name} is truncated`,
|
||||
{ field: "filesize", offset: cursor },
|
||||
),
|
||||
);
|
||||
if (members.length >= memberLimit)
|
||||
return err(
|
||||
macosPkgLimitFailure(
|
||||
memberLimit < effectiveMax
|
||||
? `cpio odc archives exceed the ${String(effectiveMax)}-member package allowance; raise artifact_limits.expanded_bytes to admit more cpio members`
|
||||
: `cpio odc archive declares more than ${String(effectiveMax)} members; raise artifact_limits.expanded_bytes to admit more cpio members`,
|
||||
{ name: "macos_pkg_cpio_members", maximum: effectiveMax },
|
||||
"cpio",
|
||||
),
|
||||
);
|
||||
const kind = memberKind(mode.value);
|
||||
const body = bytes.subarray(cursor, cursor + filesize.value);
|
||||
cursor += filesize.value;
|
||||
let symlinkTarget: string | null = null;
|
||||
let contents: Buffer | null = null;
|
||||
if (kind === "symlink") {
|
||||
if (body.length > MACOS_PKG_MAX_SYMLINK_TARGET_BYTES)
|
||||
return err(
|
||||
macosPkgLimitFailure(
|
||||
`cpio odc symlink target for ${name} exceeds ${String(MACOS_PKG_MAX_SYMLINK_TARGET_BYTES)} bytes`,
|
||||
{
|
||||
name: "macos_pkg_symlink_target_bytes",
|
||||
maximum: MACOS_PKG_MAX_SYMLINK_TARGET_BYTES,
|
||||
required: body.length,
|
||||
},
|
||||
"link",
|
||||
),
|
||||
);
|
||||
contents = Buffer.from(body);
|
||||
symlinkTarget = observedSymlinkTarget(contents).text;
|
||||
} else if (kind === "file") contents = Buffer.from(body);
|
||||
if (kind === "file" && contents !== null) {
|
||||
const links = octalField(header, 36, 6, "nlink");
|
||||
if (!links.ok) return links;
|
||||
const device = octalField(header, 6, 6, "dev");
|
||||
if (!device.ok) return device;
|
||||
const inode = octalField(header, 12, 6, "ino");
|
||||
if (!inode.ok) return inode;
|
||||
if (links.value > 1) {
|
||||
const key = `${String(device.value)}:${String(inode.value)}`;
|
||||
const records = hardLinks.get(key) ?? [];
|
||||
records.push({ index: members.length, linkCount: links.value });
|
||||
hardLinks.set(key, records);
|
||||
}
|
||||
}
|
||||
members.push({
|
||||
name,
|
||||
kind,
|
||||
mode: mode.value,
|
||||
uid: uid.value,
|
||||
gid: gid.value,
|
||||
size: filesize.value,
|
||||
symlinkTarget,
|
||||
contents,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export interface CpioOdcPrefix {
|
||||
/** Members read completely before the archive ended or failed. */
|
||||
readonly members: readonly CpioMember[];
|
||||
@@ -489,19 +305,186 @@ export const parseCpioOdcPrefix = (
|
||||
): CpioOdcPrefix => {
|
||||
const members: CpioMember[] = [];
|
||||
const hardLinks = new Map<string, LinkRecord[]>();
|
||||
const parsed = parseCpioOdcInto(
|
||||
bytes,
|
||||
members,
|
||||
hardLinks,
|
||||
remainingMembers,
|
||||
packageMaxMembers,
|
||||
const finish = (failure: CpioOdcPrefix["failure"]): CpioOdcPrefix => {
|
||||
// Some writers store a hard-linked body only once. Resolve aliases
|
||||
// within this archive, sharing the retained byte buffer.
|
||||
let aliasBytes = 0;
|
||||
for (const records of hardLinks.values())
|
||||
aliasBytes += resolveHardLinks(members, records);
|
||||
return { members, aliasBytes, failure };
|
||||
};
|
||||
const effectiveMax = Math.max(0, Math.floor(packageMaxMembers));
|
||||
const memberLimit = Math.max(
|
||||
0,
|
||||
Math.min(Math.floor(remainingMembers), effectiveMax),
|
||||
);
|
||||
// Some writers store a hard-linked body only once. Resolve aliases
|
||||
// within this archive, sharing the retained byte buffer.
|
||||
let aliasBytes = 0;
|
||||
for (const records of hardLinks.values())
|
||||
aliasBytes += resolveHardLinks(members, records);
|
||||
return { members, aliasBytes, failure: parsed.ok ? null : parsed.error };
|
||||
let cursor = 0;
|
||||
for (;;) {
|
||||
if (cursor + CPIO_ODC_HEADER_BYTES > bytes.length)
|
||||
return finish(
|
||||
truncatedInput(MACOS_PKG_FORMAT, "cpio odc header is truncated", {
|
||||
field: "cpio",
|
||||
offset: cursor,
|
||||
}),
|
||||
);
|
||||
const header = bytes.subarray(cursor, cursor + CPIO_ODC_HEADER_BYTES);
|
||||
const magic = header.subarray(0, 6).toString("ascii");
|
||||
if (magic !== CPIO_ODC_MAGIC)
|
||||
return finish(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc magic mismatch at offset ${String(cursor)}`,
|
||||
{ field: "cpio", offset: cursor },
|
||||
),
|
||||
);
|
||||
const mode = octalField(header, 18, 6, "mode");
|
||||
if (!mode.ok) return finish(mode.error);
|
||||
const uid = octalField(header, 24, 6, "uid");
|
||||
if (!uid.ok) return finish(uid.error);
|
||||
const gid = octalField(header, 30, 6, "gid");
|
||||
if (!gid.ok) return finish(gid.error);
|
||||
const namesize = octalField(header, 59, 6, "namesize");
|
||||
if (!namesize.ok) return finish(namesize.error);
|
||||
const filesize = octalField(header, 65, 11, "filesize");
|
||||
if (!filesize.ok) return finish(filesize.error);
|
||||
if (namesize.value < 1 || namesize.value > MACOS_PKG_MAX_CPIO_NAME_BYTES)
|
||||
return finish(
|
||||
macosPkgLimitFailure(
|
||||
`cpio odc namesize ${String(namesize.value)} is outside the ${String(MACOS_PKG_MAX_CPIO_NAME_BYTES)}-byte allowance`,
|
||||
{
|
||||
name: "macos_pkg_cpio_name_bytes",
|
||||
maximum: MACOS_PKG_MAX_CPIO_NAME_BYTES,
|
||||
required: namesize.value,
|
||||
},
|
||||
"namesize",
|
||||
),
|
||||
);
|
||||
cursor += CPIO_ODC_HEADER_BYTES;
|
||||
const nameOffset = cursor;
|
||||
if (cursor + namesize.value > bytes.length)
|
||||
return finish(
|
||||
truncatedInput(MACOS_PKG_FORMAT, "cpio odc name is truncated", {
|
||||
field: "name",
|
||||
offset: cursor,
|
||||
}),
|
||||
);
|
||||
const nameBytes = bytes.subarray(cursor, cursor + namesize.value);
|
||||
cursor += namesize.value;
|
||||
const nul = nameBytes.indexOf(0);
|
||||
const rawNameBytes = nameBytes.subarray(
|
||||
0,
|
||||
nul === -1 ? nameBytes.length : nul,
|
||||
);
|
||||
let name: string;
|
||||
try {
|
||||
name = decodeStrictUtf8(rawNameBytes, "preserve");
|
||||
} catch (cause: unknown) {
|
||||
return finish(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc member name at offset ${String(nameOffset)} is not valid UTF-8`,
|
||||
{ field: "name", offset: nameOffset, cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
if (name === TRAILER) {
|
||||
if (nul !== nameBytes.length - 1)
|
||||
return finish(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
"cpio odc trailer name is not exactly NUL-terminated",
|
||||
{ field: "name", offset: nameOffset },
|
||||
),
|
||||
);
|
||||
if (filesize.value !== 0)
|
||||
return finish(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
"cpio odc trailer declares a non-zero size",
|
||||
{
|
||||
field: "cpio",
|
||||
offset: cursor,
|
||||
},
|
||||
),
|
||||
);
|
||||
// Writers may pad the last output block with zeros. Other bytes are
|
||||
// outside the archive we inventoried, even if they form another cpio.
|
||||
let offset = cursor;
|
||||
while (offset < bytes.length && bytes[offset] === 0) offset++;
|
||||
if (offset < bytes.length)
|
||||
return finish(
|
||||
malformedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc has non-zero data at offset ${String(offset)} after its trailer; remaining bytes were not inventoried`,
|
||||
{ field: "cpio", offset },
|
||||
),
|
||||
);
|
||||
return finish(null);
|
||||
}
|
||||
if (cursor + filesize.value > bytes.length)
|
||||
return finish(
|
||||
truncatedInput(
|
||||
MACOS_PKG_FORMAT,
|
||||
`cpio odc member ${name} is truncated`,
|
||||
{ field: "filesize", offset: cursor },
|
||||
),
|
||||
);
|
||||
if (members.length >= memberLimit)
|
||||
return finish(
|
||||
macosPkgLimitFailure(
|
||||
memberLimit < effectiveMax
|
||||
? `cpio odc archives exceed the ${String(effectiveMax)}-member package allowance; raise artifact_limits.expanded_bytes to admit more cpio members`
|
||||
: `cpio odc archive declares more than ${String(effectiveMax)} members; raise artifact_limits.expanded_bytes to admit more cpio members`,
|
||||
{ name: "macos_pkg_cpio_members", maximum: effectiveMax },
|
||||
"cpio",
|
||||
),
|
||||
);
|
||||
const kind = memberKind(mode.value);
|
||||
const body = bytes.subarray(cursor, cursor + filesize.value);
|
||||
cursor += filesize.value;
|
||||
let symlinkTarget: string | null = null;
|
||||
let contents: Buffer | null = null;
|
||||
if (kind === "symlink") {
|
||||
if (body.length > MACOS_PKG_MAX_SYMLINK_TARGET_BYTES)
|
||||
return finish(
|
||||
macosPkgLimitFailure(
|
||||
`cpio odc symlink target for ${name} exceeds ${String(MACOS_PKG_MAX_SYMLINK_TARGET_BYTES)} bytes`,
|
||||
{
|
||||
name: "macos_pkg_symlink_target_bytes",
|
||||
maximum: MACOS_PKG_MAX_SYMLINK_TARGET_BYTES,
|
||||
required: body.length,
|
||||
},
|
||||
"link",
|
||||
),
|
||||
);
|
||||
contents = Buffer.from(body);
|
||||
symlinkTarget = observedSymlinkTarget(contents).text;
|
||||
} else if (kind === "file") contents = Buffer.from(body);
|
||||
if (kind === "file" && contents !== null) {
|
||||
const links = octalField(header, 36, 6, "nlink");
|
||||
if (!links.ok) return finish(links.error);
|
||||
const device = octalField(header, 6, 6, "dev");
|
||||
if (!device.ok) return finish(device.error);
|
||||
const inode = octalField(header, 12, 6, "ino");
|
||||
if (!inode.ok) return finish(inode.error);
|
||||
if (links.value > 1) {
|
||||
const key = `${String(device.value)}:${String(inode.value)}`;
|
||||
const records = hardLinks.get(key) ?? [];
|
||||
records.push({ index: members.length, linkCount: links.value });
|
||||
hardLinks.set(key, records);
|
||||
}
|
||||
}
|
||||
members.push({
|
||||
name,
|
||||
kind,
|
||||
mode: mode.value,
|
||||
uid: uid.value,
|
||||
gid: gid.value,
|
||||
size: filesize.value,
|
||||
symlinkTarget,
|
||||
contents,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user