fix(browser): validate PNG chunks and preserve unsupported encodings

Validate chunk checksums, type bytes, ordering and end boundaries before pixel comparison. Distinguish malformed PNGs and corrupt DEFLATE streams from recognized unsupported encodings through CLI and MCP error projection.

Adapted from #1375 with additional decoder and public boundary regressions. Eighteen decoder tests and six compiled CLI/MCP cases passed.

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Rudy Celekli
2026-10-11 11:07:15 +08:00
committed by morluto
co-authored by morluto
parent 21dbcf7a14
commit 4e4b15b825
6 changed files with 505 additions and 26 deletions
+225
View File
@@ -3,6 +3,7 @@ import { crc32, deflateSync } from "node:zlib";
import { describe, expect, it } from "vitest";
import { comparePngScreenshots } from "./PngVisualDiff.js";
import { AnalysisUnsupportedTargetError } from "../domain/analysisErrorCore.js";
import {
compareWebScreenshotsInputSchema,
createWebScreenshotArtifact,
@@ -149,6 +150,230 @@ describe("PNG visual diff", () => {
});
});
describe("PNG chunk integrity", () => {
it.each(["IHDR", "IDAT", "IEND"])(
"rejects a corrupt %s chunk checksum",
(type) => {
const bytes = png(1, 1, [0, 0, 0, 255]);
let offset = 8;
while (offset < bytes.length) {
const length = bytes.readUInt32BE(offset);
if (bytes.subarray(offset + 4, offset + 8).toString("ascii") === type) {
bytes[offset + 8 + length] = (bytes[offset + 8 + length] ?? 0) ^ 1;
break;
}
offset += length + 12;
}
const image = createWebScreenshotArtifact(bytes);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: image,
after: image,
}),
),
).toThrow("Invalid PNG chunk checksum");
},
);
it("reports a CRC-valid but corrupt compressed stream as invalid PNG data", () => {
const header = Buffer.from(png(1, 1, [0, 0, 0, 255]).subarray(16, 29));
const image = createWebScreenshotArtifact(
Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", header),
chunk("IDAT", Buffer.from("bad")),
chunk("IEND", Buffer.alloc(0)),
]),
);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
),
).toThrow("Invalid PNG image data");
});
it("rejects high-bit bytes in a CRC-valid PNG chunk name", () => {
const bytes = png(1, 1, [0, 0, 0, 255]);
const headerChunk = Buffer.from(bytes.subarray(8, 33));
headerChunk[4] = 0xc9;
headerChunk.writeUInt32BE(crc32(headerChunk.subarray(4, 21)), 21);
const malformed = createWebScreenshotArtifact(
Buffer.concat([bytes.subarray(0, 8), headerChunk, bytes.subarray(33)]),
);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: malformed,
after: malformed,
}),
),
).toThrow("Invalid PNG chunk type");
});
it("rejects duplicate image headers and unknown critical chunks while preserving ancillary chunks", () => {
const bytes = png(1, 1, [0, 0, 0, 255]);
const duplicate = Buffer.concat([
bytes.subarray(0, 33),
bytes.subarray(8, 33),
bytes.subarray(33),
]);
const unknown = Buffer.concat([
bytes.subarray(0, 33),
chunk("ABCD", Buffer.alloc(0)),
bytes.subarray(33),
]);
for (const malformed of [duplicate, unknown]) {
const image = createWebScreenshotArtifact(malformed);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: image,
after: image,
}),
),
).toThrow(/Invalid PNG header order|Unsupported critical PNG chunk/u);
}
const ancillary = createWebScreenshotArtifact(
Buffer.concat([
bytes.subarray(0, 33),
chunk("tEXt", Buffer.from("Comment\0original capture")),
bytes.subarray(33),
]),
);
expect(
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: ancillary,
after: createWebScreenshotArtifact(bytes),
}),
).status,
).toBe("identical");
});
it("rejects bytes after IEND and reports valid unsupported PNG encodings separately", () => {
const bytes = png(1, 1, [0, 0, 0, 255]);
const trailing = createWebScreenshotArtifact(
Buffer.concat([bytes, Buffer.from([0])]),
);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: trailing,
after: trailing,
}),
),
).toThrow("Invalid PNG end chunk");
const grayscaleHeader = Buffer.from(bytes.subarray(16, 29));
grayscaleHeader[9] = 0;
const grayscale = createWebScreenshotArtifact(
Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", grayscaleHeader),
chunk("IDAT", deflateSync(Buffer.from([0, 30]))),
chunk("IEND", Buffer.alloc(0)),
]),
);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: grayscale,
after: grayscale,
}),
),
).toThrow(AnalysisUnsupportedTargetError);
});
});
describe("PNG chunk ordering", () => {
it("rejects malformed palettes, interrupted data and misplaced transparency while retaining valid chunks", () => {
const header = Buffer.from(png(1, 1, [10, 20, 30, 255]).subarray(16, 29));
header[9] = 2;
const ihdr = chunk("IHDR", header);
const compressed = deflateSync(Buffer.from([0, 10, 20, 30]));
const idat = chunk("IDAT", compressed);
const iend = chunk("IEND", Buffer.alloc(0));
const palette = chunk("PLTE", Buffer.from([10, 20, 30]));
const transparency = chunk("tRNS", Buffer.from([0, 10, 0, 20, 0, 30]));
const ancillary = chunk("tEXt", Buffer.from("Comment\0retained"));
const image = (...chunks: readonly Buffer[]) =>
createWebScreenshotArtifact(
Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
...chunks,
]),
);
const original = image(ihdr, idat, iend);
const cases = [
[ihdr, chunk("PLTE", Buffer.alloc(1)), idat, iend],
[ihdr, palette, palette, idat, iend],
[ihdr, idat, palette, iend],
[ihdr, transparency, palette, idat, iend],
[ihdr, transparency, transparency, idat, iend],
[ihdr, idat, transparency, iend],
[
ihdr,
chunk("IDAT", compressed.subarray(0, 6)),
ancillary,
chunk("IDAT", compressed.subarray(6)),
iend,
],
[ihdr, idat, chunk("IEND", Buffer.from([1]))],
];
for (const chunks of cases) {
const malformed = image(...chunks);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: malformed,
after: original,
}),
),
).toThrow(
/Invalid PNG palette|Invalid PNG transparency order|Nonconsecutive PNG image data|Invalid PNG end chunk/u,
);
}
const indexedHeader = Buffer.from(header);
indexedHeader[8] = 1;
indexedHeader[9] = 3;
const emptyTransparency = image(
chunk("IHDR", indexedHeader),
chunk("PLTE", Buffer.from([10, 20, 30])),
chunk("tRNS", Buffer.alloc(0)),
chunk("IDAT", deflateSync(Buffer.from([0, 0]))),
iend,
);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: emptyTransparency,
after: original,
}),
),
).toThrow("Unsupported PNG transparency");
for (const valid of [
image(ihdr, palette, idat, iend),
image(ihdr, ancillary, idat, iend),
image(
ihdr,
chunk("IDAT", compressed.subarray(0, 6)),
chunk("IDAT", compressed.subarray(6)),
iend,
),
]) {
expect(
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: valid,
after: original,
}),
).status,
).toBe("identical");
}
});
});
const artifact = (width: number, height: number, pixels: readonly number[]) =>
createWebScreenshotArtifact(png(width, height, pixels));