Skip to content

Commit a3f378c

Browse files
committed
Validate palettes in indexed REPL screenshots
1 parent 7b46784 commit a3f378c

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎examples/repl-bench/lib/publication-files-deno.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,26 @@ export function png(bytes: Uint8Array): boolean {
192192
const compressed = new Uint8Array(bytes.length);
193193
let compressedLength = 0;
194194
let finalIdatStart = 0;
195+
const indexed = view.getUint8(25) === 3;
196+
const maxPaletteBytes = 3 * Math.min(256, 2 ** view.getUint8(24));
197+
let hasPalette = false;
195198
for (let at = 8; at + 12 <= bytes.length; ) {
196199
const length = view.getUint32(at);
197200
const type = view.getUint32(at + 4);
198201
const end = at + length + 12;
199202
if (end > bytes.length || crc32(bytes.subarray(at + 4, end - 4)) !== view.getUint32(end - 4)) {
200203
return false;
201204
}
205+
if (indexed && type === 0x504c5445) {
206+
if (hasPalette || length === 0 || length % 3 !== 0 || length > maxPaletteBytes) {
207+
return false;
208+
}
209+
hasPalette = true;
210+
}
202211
if (type === 0x49444154) {
212+
if (indexed && !hasPalette) {
213+
return false;
214+
}
203215
finalIdatStart = compressedLength;
204216
compressed.set(bytes.subarray(at + 8, end - 4), compressedLength);
205217
compressedLength += length;

‎examples/repl-bench/tests/publish-evidence.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,49 @@ describe("REPL UI evidence publication", () => {
390390
expect(host.bytes.get(urls[0])).toEqual(padded);
391391
});
392392

393+
it("requires a well-formed palette for indexed PNG evidence", function* () {
394+
const input = yield* useFixture();
395+
const host = modeledHost(() => snapshot(input));
396+
const rejected = [
397+
// missing
398+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAACklEQVR4nGNgAAAAAgABSK+kcQAAAABJRU5ErkJggg==",
399+
// duplicate
400+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAA1BMVEX/AAAZ4gk3AAAAA1BMVEX/AAAZ4gk3AAAACklEQVR4nGNgAAAAAgABSK+kcQAAAABJRU5ErkJggg==",
401+
// late
402+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAACklEQVR4nGNgAAAAAgABSK+kcQAAAANQTFRF/wAAGeIJNwAAAABJRU5ErkJggg==",
403+
// empty
404+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAAFBMVEVLqIlVAAAACklEQVR4nGNgAAAAAgABSK+kcQAAAABJRU5ErkJggg==",
405+
// partial entry
406+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAABFBMVEUAAAAAtKOuegAAAApJREFUeJxjYAAAAAIAAUivpHEAAAAASUVORK5CYII=",
407+
// too many entries for the bit depth
408+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAACVBMVEUAAAAAAAAAAACDY+nAAAAACklEQVR4nGNgAAAAAgABSK+kcQAAAABJRU5ErkJggg==",
409+
];
410+
const control =
411+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAA1BMVEX/AAAZ4gk3AAAACklEQVR4nGNgAAAAAgABSK+kcQAAAABJRU5ErkJggg==";
412+
for (const encoded of [...rejected, control]) {
413+
const bytes = Uint8Array.from(atob(encoded), (character) => character.charCodeAt(0));
414+
const digest = sha256(bytes);
415+
yield* until(writeFile(input.paths[0], bytes));
416+
yield* writeTextFile(
417+
input.path,
418+
JSON.stringify({
419+
...input.receipt,
420+
captures: [{ ...input.receipt.captures[0], sha256: digest }, input.receipt.captures[1]],
421+
}),
422+
);
423+
if (encoded !== control) {
424+
expect(String(yield* raised(run(input, host, "dry-run")))).toContain(
425+
"Screenshot checkpoint-before must be a PNG matching its approved SHA-256.",
426+
);
427+
expect(host.calls).toEqual([]);
428+
} else {
429+
expect((yield* run(input, host)).status).toBe("complete");
430+
expect(host.edits[0].images[0].sha256).toBe(digest);
431+
expect(host.bytes.get(urls[0])).toEqual(bytes);
432+
}
433+
}
434+
});
435+
393436
it("publishes only staged images and preserves bytes on both sides of the evidence", function* () {
394437
const input = yield* useFixture();
395438
const section = yield* preview(input);

0 commit comments

Comments
 (0)