TypeScript
fix(screenshots): reject corrupt PNGs during validation (#23161)
Victor Vogel6262c9a2ba51
diff --git a/scripts/mobile-showcase.test.ts b/scripts/mobile-showcase.test.tsindex d862678ff6e..7e6c9e0f0e6 100644--- a/scripts/mobile-showcase.test.ts+++ b/scripts/mobile-showcase.test.ts@@ -2,11 +2,13 @@import { assert, it } from "@effect/vitest";import { PNG } from "pngjs";+import type { ShowcaseStoreAssetSpec } from "./mobile-showcase.config.ts";import {encodeAndroidPairingUrls,normalizeStorePng,parsePairingCredentialOutput,readPngMetadata,+ validateStoreAsset,} from "./mobile-showcase.ts";function rgbaPng(width: number, height: number): Buffer {@@ -26,6 +28,55 @@ it("converts simulator RGBA captures to upload-safe 24-bit RGB PNGs", () => {});});+const storeSpec: ShowcaseStoreAssetSpec = {+ store: "apple",+ directory: "fixture",+ width: 2,+ height: 3,+ minimumUploadCount: 1,+ maximumUploadCount: 10,+};++it("validates a complete RGB PNG from an offset byte view", () => {+ const bytes = normalizeStorePng(rgbaPng(2, 3));+ const padded = Buffer.concat([Buffer.from([0]), bytes, Buffer.from([0])]);+ assert.deepStrictEqual(validateStoreAsset(storeSpec, padded.subarray(1, -1)), {+ width: 2,+ height: 3,+ bitDepth: 8,+ colorType: 2,+ hasAlpha: false,+ });+});++it("rejects PNG metadata without image data during store validation", () => {+ const bytes = normalizeStorePng(rgbaPng(2, 3));+ assert.throws(+ () => validateStoreAsset(storeSpec, bytes.subarray(0, 26), "threads.png"),+ /threads\.png is not a valid PNG/,+ );+ assert.throws(() => validateStoreAsset(storeSpec, bytes.subarray(0, 33)), /is not a valid PNG/);+});++it("rejects a PNG truncated inside its image data", () => {+ const bytes = normalizeStorePng(rgbaPng(2, 3));+ const imageData = bytes.indexOf("IDAT");+ assert.notEqual(imageData, -1);+ assert.throws(+ () => validateStoreAsset(storeSpec, bytes.subarray(0, imageData + 5)),+ /is not a valid PNG/,+ );+});++it("rejects a PNG with a corrupt image-data checksum", () => {+ const bytes = normalizeStorePng(rgbaPng(2, 3));+ const imageData = bytes.indexOf("IDAT");+ assert.notEqual(imageData, -1);+ const checksum = imageData + 4 + bytes.readUInt32BE(imageData - 4);+ bytes[checksum] = bytes.readUInt8(checksum) ^ 1;+ assert.throws(() => validateStoreAsset(storeSpec, bytes), /is not a valid PNG/);+});+it("reads multiline JSON from the pairing CLI", () => {assert.equal(parsePairingCredentialOutput('server log\n{\n "credential": "PAIR-ME"\n}\n'),diff --git a/scripts/mobile-showcase.ts b/scripts/mobile-showcase.tsindex 17a609b9894..425c371fd0c 100644--- a/scripts/mobile-showcase.ts+++ b/scripts/mobile-showcase.ts@@ -206,6 +206,11 @@ export function validateStoreAsset(throw new Error(`${label} must use Google Play's recommended portrait 9:16 aspect ratio.`);}}+ try {+ PNG.sync.read(Buffer.from(bytes));+ } catch (cause) {+ throw new Error(`${label} is not a valid PNG.`, { cause });+ }return metadata;}