From b74b314fbddffadb731d7cff43ea214f7808133d Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:58:48 -0500 Subject: [PATCH] fix(node-sea): validate Mach-O segment bounds before blob limits (#12263) --- src/artifacts/NodeSeaArtifactReader.test.ts | 44 +++++++++++++++++++++ src/artifacts/nodeSea/NodeSeaLocation.ts | 24 +++++++---- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/artifacts/NodeSeaArtifactReader.test.ts b/src/artifacts/NodeSeaArtifactReader.test.ts index 3812867afb..23a71e144b 100644 --- a/src/artifacts/NodeSeaArtifactReader.test.ts +++ b/src/artifacts/NodeSeaArtifactReader.test.ts @@ -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( diff --git a/src/artifacts/nodeSea/NodeSeaLocation.ts b/src/artifacts/nodeSea/NodeSeaLocation.ts index 6353d8869a..ae7d8ea4fc 100644 --- a/src/artifacts/nodeSea/NodeSeaLocation.ts +++ b/src/artifacts/nodeSea/NodeSeaLocation.ts @@ -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 }); } }