fix(browser): compare PNG IHDR bytes without ASCII folding (#2099)
* fix(browser): compare PNG IHDR bytes without ASCII folding Screenshot checks already compare the PNG signature as raw bytes, then decoded the chunk type with an encoding that maps high-bit bytes onto IHDR. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(browser): satisfy the strict index check in the IHDR test Co-authored-by: Cursor <cursoragent@cursor.com> * refactor(browser): share byte-faithful PNG header facts --------- Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: REA preparation lane <rea-preparation@localhost>
This commit is contained in:
co-authored by
Cursor
REA preparation lane
parent
c59fbf5e9c
commit
fdd8f82602
@@ -0,0 +1,35 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { readPngHeaderDimensions } from "./pngHeader.js";
|
||||
|
||||
const png = Buffer.from(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jv4sAAAAASUVORK5CYII=",
|
||||
"base64",
|
||||
);
|
||||
|
||||
it("reads encoded dimensions without changing screenshot bytes", () => {
|
||||
const bytes = Buffer.from(png);
|
||||
bytes.writeUInt32BE(257, 16);
|
||||
bytes.writeUInt32BE(129, 20);
|
||||
const original = Buffer.from(bytes);
|
||||
expect(readPngHeaderDimensions(bytes)).toEqual({ width: 257, height: 129 });
|
||||
expect(bytes.equals(original)).toBe(true);
|
||||
});
|
||||
|
||||
it.each([12, 13, 14, 15])("rejects ASCII-folded IHDR byte at %s", (offset) => {
|
||||
const bytes = Buffer.from(png);
|
||||
bytes[offset] = (bytes[offset] ?? 0) | 0x80;
|
||||
expect(bytes.subarray(12, 16).toString("ascii")).toBe("IHDR");
|
||||
expect(readPngHeaderDimensions(bytes)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("rejects incomplete headers, invalid signatures and zero dimensions", () => {
|
||||
expect(readPngHeaderDimensions(png.subarray(0, 23))).toBeUndefined();
|
||||
const badSignature = Buffer.from(png);
|
||||
badSignature[0] = 0;
|
||||
expect(readPngHeaderDimensions(badSignature)).toBeUndefined();
|
||||
for (const offset of [16, 20]) {
|
||||
const zero = Buffer.from(png);
|
||||
zero.writeUInt32BE(0, offset);
|
||||
expect(readPngHeaderDimensions(zero)).toBeUndefined();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user