fix(node-sea): validate Mach-O segment bounds before blob limits (#12263)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 08:58:48 +00:00
1 parent fe6c9bcf66
commit b74b314fbd
2 files changed
+60 -8

No files matched your search

@@ -331,6 +331,50 @@ it.each([
},
);
it.each(["thin", "universal"] as const)(
"requires a named SEA segment to fit its %s Mach-O image before charging blob bytes",
async (carrier) => {
const image =
carrier === "thin"
? nodeSeaMachO(populated)
: nodeSeaUniversal(populated);
const sliceOffset = carrier === "thin" ? 0 : image.readUInt32BE(16);
const sliceSize = image.length - sliceOffset;
// In a universal image the outer file contains the byte beyond the
// declared slice, so using outer EOF would incorrectly pass this check.
const valid =
carrier === "thin" ? image : Buffer.concat([image, Buffer.alloc(1)]);
const malformed = Buffer.from(valid);
const segment = sliceOffset + 32;
const payloadOffset = malformed.readBigUInt64LE(segment + 40);
const blobLength = Number(malformed.readBigUInt64LE(segment + 72 + 40));
const limits = {
...DEFAULT_NODE_SEA_READER_LIMITS,
blob_bytes: blobLength - 1,
};
const impossibleLength = BigInt(sliceSize) - payloadOffset + 1n;
malformed.writeBigUInt64LE(impossibleLength, segment + 32);
malformed.writeBigUInt64LE(impossibleLength, segment + 48);
const locate = async (bytes: Buffer) => {
const handle = await open(
await writeCarrier("macho-segment", bytes),
"r",
);
try {
return await locateNodeSea(handle, bytes.length, undefined, limits);
} finally {
await handle.close();
}
};
await expect(locate(valid)).rejects.toMatchObject({ reason: "limit" });
await expect(locate(malformed)).rejects.toMatchObject({
reason: "format",
message: expect.stringContaining("segment range is outside the file"),
});
},
);
it("classifies a main script without a JavaScript suffix as javascript-bundle", async () => {
const snapshot = await scanCanonicalArtifactInventory(
await writeCarrier(
+16 -8
View File
@@ -3,7 +3,10 @@ import {
hashArtifactRange,
readArtifactRange as readExact,
} from "../ArtifactCarrierRange.js";
import { readMachoSegmentHeader } from "../../domain/apple/machoSegmentHeader.js";
import {
admitMachoSegmentCommand,
readMachoSegmentHeader,
} from "../../domain/apple/machoSegmentHeader.js";
import {
machoSectionBacking,
readMachoSectionHeader,
@@ -349,6 +352,12 @@ const locateThinMachO = async (
const table = offset + segmentHeader;
if (nsects > Math.floor((size - segmentHeader) / sectionSize))
throw formatFailure(NODE_SEA_MACHO_SEGMENT_TRUNCATED);
const admission = admitMachoSegmentCommand(
segment,
size,
framing.value.wide ? 64 : 32,
sliceSize,
);
for (let index = 0; index < nsects; index += 1) {
const section = table + index * sectionSize;
const parsedSection = readMachoSectionHeader(
@@ -360,18 +369,17 @@ const locateThinMachO = async (
throw formatFailure(NODE_SEA_MACHO_SEGMENT_TRUNCATED);
const record = parsedSection.value;
if (cString(record.nameBytes, 0, 16) !== SEA_SECTION) continue;
if (!admission.ok)
throw formatFailure(`Node SEA ${admission.error.message}`);
const { issue } = machoSectionBacking(record, segment);
if (issue !== null)
throw formatFailure(`Mach-O Node SEA section ${issue}`);
const length = Number(record.size);
const fileOffset = record.offset;
if (!Number.isSafeInteger(length))
throw limitFailure(
"Node SEA blob length exceeds exact-number representation",
);
// The segment and section bounds admit every nonempty payload.
// Empty segments can still declare an offset outside the image.
if (fileOffset > sliceSize) throw formatFailure(NODE_SEA_TRUNCATED);
const length = Number(record.size);
admitBlobBytes(carrier, length);
if (fileOffset > sliceSize || length > sliceSize - fileOffset)
throw formatFailure(NODE_SEA_TRUNCATED);
found.push({ offset: base + fileOffset, length });
}
}