mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(capture): validate PNG headers at the shared boundary (#12310)
fix(screenshot): validate IHDR encoding fields at capture
This commit is contained in:
1 parent
daf5e91f39
commit
c5709b8697
9 files changed
+53
-52
No files matched your search
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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()
|
||||
|
||||
+21
-12
@@ -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<PngIhdrFacts, string> => {
|
||||
@@ -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. */
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user