diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index b4e9e0b53d..de21e8ee72 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -1,5 +1,4 @@ { - "src/artifacts/DesktopFileInspection.ts": 4, "src/domain/apkSignature/apkSignatureSchemes.ts": 1, "src/domain/apkSignature/apkSigningBlock.ts": 2, "src/domain/apkSignature/apkV1Signature.ts": 5, @@ -18,7 +17,6 @@ "src/domain/apple/propertyListDecodeBudget.ts": 2, "src/domain/binaryTarget.ts": 10, "src/domain/desktop/openPgpKeys.ts": 2, - "src/domain/dosMz.ts": 10, "src/domain/elfProgramHeader.ts": 4, "src/domain/elfRawTableDeclarations.ts": 7, "src/domain/elfRelocation.ts": 6, diff --git a/src/artifacts/DesktopFileInspection.ts b/src/artifacts/DesktopFileInspection.ts index fa66fa05ac..4a0df61638 100644 --- a/src/artifacts/DesktopFileInspection.ts +++ b/src/artifacts/DesktopFileInspection.ts @@ -1,4 +1,5 @@ -import { observeMzWindowsHeader } from "../domain/dosMz.js"; +import { hasDosMzMagic, observeMzWindowsHeader } from "../domain/dosMz.js"; +import { BinaryReader } from "../domain/binaryContainer.js"; import { readPeCoffHeader } from "../domain/peCoffHeader.js"; import { hasGodotPckHeader, @@ -204,8 +205,9 @@ export const inspectDesktopFile = async ( prefix.toString("latin1", 0, 4) === "FORM" && prefix.toString("latin1", 8, 12) === "GEN8" ) { - const containerBytes = prefix.readUInt32LE(4); - const chunkBytes = prefix.readUInt32LE(12); + const form = new BinaryReader(prefix, true, "gamemaker-form"); + const containerBytes = form.u32(4); + const chunkBytes = form.u32(12); if ( containerBytes >= 8 && containerBytes <= size - 8 && @@ -254,7 +256,8 @@ export const inspectDesktopFile = async ( if (!row.issues.includes(cause.message)) row.issues.push(cause.message); } }; - if (prefix.length === 64 && prefix.readUInt16LE(0) === 0x5a4d) + const mz = hasDosMzMagic(prefix); + if (prefix.length === 64 && mz) await observe(() => inspectPeFile( prefix, @@ -266,10 +269,7 @@ export const inspectDesktopFile = async ( signal, ), ); - if ( - (native || (prefix.length >= 2 && prefix.readUInt16LE(0) === 0x5a4d)) && - size >= 12 - ) + if ((native || mz) && size >= 12) await observe(() => inspectPckTrailer(size, read, fact)); }; diff --git a/src/artifacts/PyInstallerArtifactReader.ts b/src/artifacts/PyInstallerArtifactReader.ts index 9014d86f0f..2252de515c 100644 --- a/src/artifacts/PyInstallerArtifactReader.ts +++ b/src/artifacts/PyInstallerArtifactReader.ts @@ -26,7 +26,7 @@ import { } from "./pyinstaller/PyInstallerMetadata.js"; import { artifactExpansionBudgetBytes } from "../domain/artifactExpansionBudget.js"; import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js"; -import { parseMzHeaderEvidence } from "../domain/dosMz.js"; +import { observeMzWindowsHeader } from "../domain/dosMz.js"; import { readFatContainerHeader, readMachoHeaderPrefixFacts, @@ -74,7 +74,7 @@ const carrierPathDialect = async ( }); if (!prefixRead.ok) return "unknown"; const prefix = prefixRead.value; - let windowsHeader = parseMzHeaderEvidence(prefix, fileSize).windowsHeader; + let windowsHeader = observeMzWindowsHeader(prefix, fileSize); if (windowsHeader.state === "signature-needed") { const signatureRead = await readPyInstallerBytes(handle, { position: windowsHeader.offset, @@ -83,10 +83,10 @@ const carrierPathDialect = async ( signal, }); if (!signatureRead.ok) return "unknown"; - windowsHeader = parseMzHeaderEvidence(prefix, fileSize, { + windowsHeader = observeMzWindowsHeader(prefix, fileSize, { offset: windowsHeader.offset, bytes: signatureRead.value, - }).windowsHeader; + }); } if (windowsHeader.state === "declared") return windowsHeader.signature === "PE" ? "windows" : "unknown"; diff --git a/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts b/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts index 8088123dd5..8795172c53 100644 --- a/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts @@ -1124,6 +1124,45 @@ it("reads a PE signature beyond the carrier prefix before projecting paths", asy } }); +it("requires DOS magic before a PE-like record can select Windows path semantics", async () => { + const storedName = String.raw`pkg\data.bin`; + const carrier = cArchiveExecutable({ + carrier: "pe", + layout: "2.1", + pythonVersion: 309, + members: [ + { + name: storedName, + typeCode: "x", + payload: Buffer.from("data"), + compress: false, + }, + ], + }); + const withoutDosMagic = Buffer.from(carrier); + withoutDosMagic.write("XX"); + for (const [bytes, expectedPath] of [ + [carrier, "pkg/data.bin"], + [withoutDosMagic, storedName], + ] as const) { + const reader = new PyInstallerArtifactReader( + await writeArchive("carrier", bytes), + ); + try { + const entries = []; + for await (const entry of reader.entries()) entries.push(entry); + expect(entries).toContainEqual( + expect.objectContaining({ + path: expectedPath, + formatMetadata: expect.objectContaining({ source_name: storedName }), + }), + ); + } finally { + await reader.close(); + } + } +}); + it("rejects a stored member whose declared size disagrees with its bytes", async () => { const path = await writeArchive( "stored-size", diff --git a/src/domain/dosMz.ts b/src/domain/dosMz.ts index 02a0d00f0b..c4716492d0 100644 --- a/src/domain/dosMz.ts +++ b/src/domain/dosMz.ts @@ -1,5 +1,9 @@ +import { BinaryReader } from "./binaryContainer.js"; import { err, ok, type Result } from "./result.js"; +export const hasDosMzMagic = (bytes: Uint8Array): boolean => + bytes[0] === 0x4d && bytes[1] === 0x5a; + /** Source declarations in the fixed DOS header, independent of load-module validity. */ export interface DosMzDeclarations { readonly lastPageBytes: number; @@ -12,18 +16,18 @@ export interface DosMzDeclarations { readonly entryOffset: number; } -const fixedDosMzDeclarations = (bytes: Buffer): DosMzDeclarations => { - const relocationCount = bytes.readUInt16LE(6); - const relocationTableOffset = bytes.readUInt16LE(24); +const fixedDosMzDeclarations = (reader: BinaryReader): DosMzDeclarations => { + const relocationCount = reader.u16(6); + const relocationTableOffset = reader.u16(24); return { - lastPageBytes: bytes.readUInt16LE(2), - pages: bytes.readUInt16LE(4), - headerBytes: bytes.readUInt16LE(8) * 16, + lastPageBytes: reader.u16(2), + pages: reader.u16(4), + headerBytes: reader.u16(8) * 16, relocationCount, relocationTableOffset, relocationTableEnd: relocationTableOffset + relocationCount * 4, - entrySegment: bytes.readUInt16LE(22), - entryOffset: bytes.readUInt16LE(20), + entrySegment: reader.u16(22), + entryOffset: reader.u16(20), }; }; @@ -31,9 +35,9 @@ const fixedDosMzDeclarations = (bytes: Buffer): DosMzDeclarations => { export const readDosMzDeclarations = ( bytes: Buffer, ): Result => - bytes.length < 28 || bytes.toString("latin1", 0, 2) !== "MZ" + bytes.length < 28 || !hasDosMzMagic(bytes) ? err("invalid or truncated DOS MZ header") - : ok(fixedDosMzDeclarations(bytes)); + : ok(fixedDosMzDeclarations(new BinaryReader(bytes, true, "dos-mz"))); /** Derive the initialized source extent from already admitted fixed declarations. */ export const dosMzDeclaredImageByteLength = ({ @@ -124,14 +128,16 @@ const observeWindowsHeader = ( signatureEvidence?: MzWindowsHeaderSignatureEvidence, declarations?: DosMzDeclarations, ): MzWindowsHeaderObservation => { + if (bytes.length >= 2 && !hasDosMzMagic(bytes)) return { state: "absent" }; if (bytes.length < 64) return fileSize >= 64 ? { state: "header-needed" } : { state: "absent" }; + const reader = new BinaryReader(bytes, true, "dos-mz"); const { headerBytes, relocationCount, relocationTableOffset: tableStart, relocationTableEnd: tableEnd, - } = declarations ?? fixedDosMzDeclarations(bytes); + } = declarations ?? fixedDosMzDeclarations(reader); if ( headerBytes !== 0 && relocationCount > 0 && @@ -139,7 +145,7 @@ const observeWindowsHeader = ( tableEnd > 60 ) return { state: "absent" }; - const offset = bytes.readUInt32LE(60); + const offset = reader.u32(60); if (offset === 0) return { state: "absent" }; const inlineSignature = offset < bytes.length @@ -338,11 +344,12 @@ const readRelocations = ( headerBytes: number, moduleBytes: number, ): Result => { + const reader = new BinaryReader(bytes, true, "dos-mz"); const relocations: DosMzRelocation[] = []; for (let index = 0; index < relocationCount; index += 1) { const record = relocationTableOffset + index * 4; - const offset = bytes.readUInt16LE(record); - const segment = bytes.readUInt16LE(record + 2); + const offset = reader.u16(record); + const segment = reader.u16(record + 2); const moduleOffset = segment * 16 + offset; if (moduleOffset + 2 > moduleBytes) return err(`DOS MZ relocation ${index} points outside its load module`);