diff --git a/docs/guides/android/adb-device-analysis.md b/docs/guides/android/adb-device-analysis.md index 493c1ebdb7..dc18d0778e 100644 --- a/docs/guides/android/adb-device-analysis.md +++ b/docs/guides/android/adb-device-analysis.md @@ -122,9 +122,9 @@ existing analysis families directly, for example | `start_adb_activity` | **mutates device** | Starts an intent (`am start -W -a` with optional URI, component, and up to 16 typed extras); preserves the activity manager's verdict | | `stop_adb_app` | **mutates device** | Force-stops every process of a package, ending a dynamic-analysis run | -Screen capture validates the PNG signature, IHDR framing, dimensions, and IHDR -checksum before publishing. It does not validate encoding fields or decode and -validate image contents; that limitation is retained in Evidence. +Screen capture validates the PNG signature, IHDR framing and checksum, dimensions, +and encoding fields before publishing. Evidence records that pixel data remains +undecoded. When `push_adb_file` receives an existing directory, adb places the local file inside it under the local basename; Evidence reports that effective file path diff --git a/src/adb/AdbProvider.ts b/src/adb/AdbProvider.ts index d2d2c78ca0..b1f18ef3d4 100644 --- a/src/adb/AdbProvider.ts +++ b/src/adb/AdbProvider.ts @@ -3189,7 +3189,7 @@ export class AdbProvider { { limitations: [ "The capture is one frame at read time; no recording or interaction occurred", - "PNG signature, IHDR framing, checksum, and dimensions are validated; encoding and image contents are not validated", + "PNG signature, IHDR framing, checksum, dimensions, and encoding fields are validated. Pixel data remains undecoded", ...(usedExclusiveFallback ? [ "The exclusive-create fallback makes the output path visible while its bytes are being written", diff --git a/src/domain/native/nativeUiObservation.ts b/src/domain/native/nativeUiObservation.ts index 5d3446ae4f..26beb89b91 100644 --- a/src/domain/native/nativeUiObservation.ts +++ b/src/domain/native/nativeUiObservation.ts @@ -78,7 +78,7 @@ const nativeUiScreenshotSchema = z .string() .min(4) .describe( - "Canonical base64 encoding of PNG bytes; the decoded bytes must have a PNG signature and IHDR chunk.", + "Canonical base64 encoding of PNG bytes; the decoded bytes must have a PNG signature and a valid IHDR chunk.", ), sha256: z .string() diff --git a/src/domain/pngHeader.ts b/src/domain/pngHeader.ts index 021edacc97..96d43056cd 100644 --- a/src/domain/pngHeader.ts +++ b/src/domain/pngHeader.ts @@ -13,12 +13,13 @@ export interface PngIhdrFacts { readonly height: number; readonly bitDepth: number; readonly colorType: number; - readonly compressionMethod: number; - readonly filterMethod: number; readonly interlaceMethod: number; } -/** Decode IHDR declarations and admit dimensions; encoding support belongs to consumers. */ +/** + * Admit the declared IHDR fields (PNG specification, ยง11.2.1). + * Image data and codec support are checked by consumers that decode it. + */ export const readPngIhdrFacts = ( data: Buffer, ): Result => { @@ -32,15 +33,23 @@ export const readPngIhdrFacts = ( height > 0x7fff_ffff ) return err("Invalid PNG header"); - return ok({ - width, - height, - bitDepth: data[8]!, - colorType: data[9]!, - compressionMethod: data[10]!, - filterMethod: data[11]!, - interlaceMethod: data[12]!, - }); + const bitDepth = data[8]!; + const colorType = data[9]!; + const interlaceMethod = data[12]!; + const validDepth = + (colorType === 0 && [1, 2, 4, 8, 16].includes(bitDepth)) || + (colorType === 2 && [8, 16].includes(bitDepth)) || + (colorType === 3 && [1, 2, 4, 8].includes(bitDepth)) || + (colorType === 4 && [8, 16].includes(bitDepth)) || + (colorType === 6 && [8, 16].includes(bitDepth)); + if ( + !validDepth || + data[10] !== 0 || + data[11] !== 0 || + (interlaceMethod !== 0 && interlaceMethod !== 1) + ) + return err("Invalid PNG header"); + return ok({ width, height, bitDepth, colorType, interlaceMethod }); }; /** Read screenshot dimensions from a byte-faithful PNG header, not a full image decode. */ diff --git a/src/domain/pngVisualDiff.test.ts b/src/domain/pngVisualDiff.test.ts index 90d1d88b91..9a989dd3a6 100644 --- a/src/domain/pngVisualDiff.test.ts +++ b/src/domain/pngVisualDiff.test.ts @@ -37,12 +37,12 @@ describe("PNG header admission", () => { ); it.each([24, 25, 26, 27, 28])( - "preserves header dimensions for unknown encoding field at %s", + "rejects unknown IHDR encoding fields at %s", (offset) => { const bytes = png(1, 1, [0, 0, 0, 255]); bytes[offset] = 255; bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); - expect(readPngHeaderDimensions(bytes)).toEqual({ width: 1, height: 1 }); + expect(readPngHeaderDimensions(bytes)).toBeUndefined(); const image = createWebScreenshotArtifact(bytes); expect(() => comparePngScreenshots( @@ -357,14 +357,17 @@ describe("PNG chunk integrity", () => { 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)), - ]), - ); + const grayscaleBytes = 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(readPngHeaderDimensions(grayscaleBytes)).toEqual({ + width: 1, + height: 1, + }); + const grayscale = createWebScreenshotArtifact(grayscaleBytes); expect(() => comparePngScreenshots( compareWebScreenshotsInputSchema.parse({ diff --git a/src/domain/pngVisualDiff.ts b/src/domain/pngVisualDiff.ts index 436a8ff5af..3578eeecbf 100644 --- a/src/domain/pngVisualDiff.ts +++ b/src/domain/pngVisualDiff.ts @@ -372,28 +372,7 @@ const inflatePng = (compressed: Buffer, maxOutputLength: number): Buffer => { const parseHeader = (data: Buffer) => { const parsed = readPngIhdrFacts(data); if (!parsed.ok) throw new TypeError(parsed.error); - const { - width, - height, - bitDepth, - colorType, - compressionMethod, - filterMethod, - interlaceMethod, - } = parsed.value; - const validDepth = - (colorType === 0 && [1, 2, 4, 8, 16].includes(bitDepth)) || - (colorType === 2 && [8, 16].includes(bitDepth)) || - (colorType === 3 && [1, 2, 4, 8].includes(bitDepth)) || - (colorType === 4 && [8, 16].includes(bitDepth)) || - (colorType === 6 && [8, 16].includes(bitDepth)); - if ( - !validDepth || - compressionMethod !== 0 || - filterMethod !== 0 || - (interlaceMethod !== 0 && interlaceMethod !== 1) - ) - throw new TypeError("Invalid PNG header"); + const { width, height, bitDepth, colorType, interlaceMethod } = parsed.value; return { width, height, diff --git a/src/native/NativeUiObservation.test.ts b/src/native/NativeUiObservation.test.ts index 0085cef5aa..4c040340fd 100644 --- a/src/native/NativeUiObservation.test.ts +++ b/src/native/NativeUiObservation.test.ts @@ -81,11 +81,15 @@ it.each([ }, ); describe("native UI screenshot validation", () => { - it.each([24, 32, "crc"])( + it.each([24, 32, "crc", "invalid filter"])( "rejects incomplete or corrupt IHDR despite matching native digest: %s", async (kind) => { const bytes = Buffer.from(validPng); if (kind === "crc") bytes[29] = (bytes[29] ?? 0) ^ 1; + if (kind === "invalid filter") { + bytes[27] = 1; + bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); + } const payload = typeof kind === "number" ? bytes.subarray(0, kind) : bytes; expect((await observeWithScreenshot(screenshotOf(payload))).ok).toBe( diff --git a/tests/boundary/cli/adbCli.test.ts b/tests/boundary/cli/adbCli.test.ts index ed34604ba2..1de2f352fb 100644 --- a/tests/boundary/cli/adbCli.test.ts +++ b/tests/boundary/cli/adbCli.test.ts @@ -35,6 +35,7 @@ it "zero height", "invalid chunk type", "invalid checksum", + "invalid truecolor depth", "truncated header", "valid", ] as const)( @@ -49,6 +50,7 @@ it if (kind === "zero width") png.writeUInt32BE(0, 16); if (kind === "zero height") png.writeUInt32BE(0, 20); if (kind === "invalid chunk type") png[12] = 0xc9; + if (kind === "invalid truecolor depth") png[24] = 4; if (kind !== "invalid checksum") png.writeUInt32BE(crc32(png.subarray(12, 29)), 29); else png[29] = png[29]! ^ 0xff; @@ -100,7 +102,7 @@ exit 1 ).toMatchObject({ width: 1, height: 1 }); expect(output.limitations).toContainEqual( expect.stringMatching( - /PNG signature.*IHDR.*checksum.*dimensions.*validated.*encoding.*image contents.*not validated/iu, + /PNG signature.*IHDR.*encoding fields.*validated.*pixel data.*undecoded/iu, ), ); expect( diff --git a/tests/boundary/mcp/browserMcp.test.ts b/tests/boundary/mcp/browserMcp.test.ts index f327b3bd4e..b955b948a8 100644 --- a/tests/boundary/mcp/browserMcp.test.ts +++ b/tests/boundary/mcp/browserMcp.test.ts @@ -1041,7 +1041,7 @@ const artifactOf = (value: unknown): unknown => { return normalized.artifact; }; -it.each([24, 32, "crc"])( +it.each([24, 32, "crc", "invalid interlace"])( "rejects corrupt captured PNG through MCP: %s", async (kind) => { const bytes = Buffer.from( @@ -1049,6 +1049,10 @@ it.each([24, 32, "crc"])( "base64", ); if (kind === "crc") bytes[29] = (bytes[29] ?? 0) ^ 1; + if (kind === "invalid interlace") { + bytes[28] = 2; + bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); + } const data = ( typeof kind === "number" ? bytes.subarray(0, kind) : bytes ).toString("base64");