fix(formats): preserve high bits in Windows signature checks

Decode MZ and Windows signature bytes without ASCII high-bit masking. Move malformed signature cases into the shared Go diagnostics fixtures so provider, compiled CLI, and MCP workflows cover NE, LE, LX, and PE lookalikes; remove the superseded provider-only cases.

Validation: runtime build, check:ci, and focused DOS/Go/target/CLI/MCP tests. Windows-only reference-import cases await CI.
This commit is contained in:
morluto
2026-10-13 04:24:29 +08:00
parent fd9588089d
commit ac57c88efe
4 changed files with 31 additions and 28 deletions
+19 -11
View File
@@ -170,18 +170,26 @@ describe("MZ Windows header observations", () => {
});
});
it("keeps a complete non-Windows signature candidate as DOS", () => {
const bytes = dosMz(64);
const offset = 32;
bytes.writeUInt32LE(offset, 60);
bytes.write("PEZZ", offset, "ascii");
it.each(["PEZZ", "NE", "LE", "LX", "PE"])(
"keeps a non-Windows %s signature candidate as DOS",
(signature) => {
const bytes = dosMz(64);
const offset = 32;
bytes.writeUInt32LE(offset, 60);
const candidate = Buffer.from(
[...Buffer.from(signature)].map((byte) =>
signature === "PEZZ" ? byte : byte | 0x80,
),
);
bytes.set(candidate, offset);
expect(observeMzWindowsHeader(bytes)).toEqual({ state: "absent" });
expect(parseExecutableHeader(bytes, "x64")).toMatchObject({
ok: true,
value: { format: "dos-mz", architecture: "x86" },
});
});
expect(observeMzWindowsHeader(bytes)).toEqual({ state: "absent" });
expect(parseExecutableHeader(bytes, "x64")).toMatchObject({
ok: true,
value: { format: "dos-mz", architecture: "x86" },
});
},
);
it("accepts a short-header candidate only when it points at a Windows signature", () => {
const stub = dosMz(64);
+3 -3
View File
@@ -62,7 +62,7 @@ export const observeMzWindowsHeader = (
const observedPePrefix =
available !== undefined &&
available.length >= 2 &&
available.toString("ascii", 0, 2) === "PE";
available.toString("latin1", 0, 2) === "PE";
if (fileSize - offset < (observedPePrefix ? 4 : 2))
return { state: "absent" };
return { state: "signature-needed", offset };
@@ -72,7 +72,7 @@ const windowsHeaderSignature = (
bytes: Buffer | undefined,
): "PE" | "NE" | "LE" | "LX" | "other" | "unobserved" => {
if (bytes === undefined || bytes.length < 2) return "unobserved";
const prefix = bytes.toString("ascii", 0, 2);
const prefix = bytes.toString("latin1", 0, 2);
if (prefix === "NE" || prefix === "LE" || prefix === "LX") return prefix;
if (prefix !== "PE") return "other";
if (bytes.length < 4) return "unobserved";
@@ -91,7 +91,7 @@ export const parseDosMzHeader = (
fileSize = bytes.length,
signatureAtOffset?: Buffer,
): Result<DosMzHeader, string> => {
if (bytes.length < 28 || bytes.toString("ascii", 0, 2) !== "MZ")
if (bytes.length < 28 || bytes.toString("latin1", 0, 2) !== "MZ")
return err("invalid or truncated DOS MZ header");
if (!Number.isSafeInteger(fileSize) || fileSize < bytes.length)
return err("invalid DOS MZ file length");