fix(targets): recognize Windows headers after short DOS stubs

Observe MZ header declarations and signature uncertainty at the domain boundary, then reuse those facts in bounded target resolution, inventory, and Go PE parsing. Preserve DOS relocation ownership and fail closed when the relevant bytes remain unexamined.

Validation: 59 domain tests, 130 target/Go boundary tests, and public CLI inspection of a clang/lld-link PE DLL with a short DOS header. Fixes #2172.
This commit is contained in:
morluto
2026-10-13 03:47:45 +08:00
parent f611d8ce72
commit c9fed50691
7 changed files with 314 additions and 57 deletions
@@ -18,7 +18,10 @@ import {
zipPackageFormatForPath,
type ZipPackageFormat,
} from "../../domain/zipPackageFormat.js";
import { mzWindowsHeaderOffset, parseDosMzHeader } from "../../domain/dosMz.js";
import {
observeMzWindowsHeader,
parseDosMzHeader,
} from "../../domain/dosMz.js";
/** Mutable internal occurrence used until root-bound IDs are known. */
export interface MutableOccurrence {
@@ -450,17 +453,19 @@ export const classifyArtifactBytes = (
prefix.length < Math.min(fileSize, 64)
)
return "unknown";
const windowsOffset = mzWindowsHeaderOffset(prefix);
if (windowsOffset !== null) {
const windowsHeader = observeMzWindowsHeader(prefix, fileSize);
if (windowsHeader.state === "signature-needed") return "unknown";
if (windowsHeader.state === "declared") {
const windowsOffset = windowsHeader.offset;
if (
windowsHeader.signature === "PE" &&
windowsOffset >= 64 &&
windowsOffset <= prefix.length - 4 &&
prefix
.subarray(windowsOffset, windowsOffset + 4)
.equals(Buffer.from([0x50, 0x45, 0, 0]))
windowsOffset <= prefix.length - 4
)
return "pe";
} else if (parseDosMzHeader(prefix, fileSize).ok) return "dos-mz";
return "unknown";
}
if (parseDosMzHeader(prefix, fileSize).ok) return "dos-mz";
return "unknown";
}
return "file";