test(browser): protect PNG scanline filter reconstruction (#12050)

This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-19 00:12:41 -05:00
1 parent e70c4c963a
commit 3329d6da95
3 files changed
+196

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { digestCanonicalValue } from "../../../src/domain/canonicalDigest.js";
import { createWebScreenshotArtifact } from "../../../src/domain/webScreenshot.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
const corpus = JSON.parse(
await readFile(
new URL("../../fixtures/pngFilters/vectors.json", import.meta.url),
"utf8",
),
) as {
vectors: {
mode: string;
reference: string;
filtered: string;
changed: string;
delta: number;
}[];
};
// Both channel layouts must remain covered independently of fixture contents.
expect(corpus.vectors.map(({ mode }) => mode).sort()).toEqual(["RGB", "RGBA"]);
for (const vector of corpus.vectors) {
cliTest(
`compares independently decoded ${vector.mode} scanline filters against literal pixels`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-png-filter-oracle-");
const artifacts = [vector.reference, vector.filtered, vector.changed].map(
(encoded) =>
createWebScreenshotArtifact(Buffer.from(encoded, "base64")),
);
const paths = artifacts.map((_, index) => join(root, `${index}.json`));
for (const [index, artifact] of artifacts.entries())
await writeFile(paths[index]!, JSON.stringify(artifact));
for (const [index, expected] of [
[
1,
{
status: "identical",
changed_pixels: 0,
maximum_channel_delta: 0,
changed_ratio: 0,
mean_absolute_channel_delta: 0,
},
],
[
2,
{
status: "different",
changed_pixels: 1,
maximum_channel_delta: vector.delta,
changed_ratio: 1 / 36,
mean_absolute_channel_delta: vector.delta / (36 * 4),
},
],
] as const) {
const result = await cli.run({
arguments: [
"compare-web-screenshots",
paths[0]!,
paths[index]!,
"--json",
],
environment: process.env,
});
expect(result.exitCode, result.stderr).toBe(0);
expect(result.signal).toBeNull();
expect(result.json).toMatchObject({
operation: "compare_web_screenshots",
confidence: "derived",
authority: "analyst-inference",
parameters: {
before_artifact_sha256: artifacts[0]!.sha256,
after_artifact_sha256: artifacts[index]!.sha256,
comparison_input_sha256: digestCanonicalValue({
before: artifacts[0]!,
after: artifacts[index]!,
channel_threshold: 0,
}),
},
normalized_result: {
...expected,
before: { width: 6, height: 6 },
after: { width: 6, height: 6 },
compared_pixels: 36,
channel_threshold: 0,
},
});
}
for (const [index, artifact] of artifacts.entries())
expect(await readFile(paths[index]!, "utf8")).toBe(
JSON.stringify(artifact),
);
},
);
}
+71
View File
@@ -0,0 +1,71 @@
"""Regenerate portable PNG filter vectors; requires Pillow 13.0.0, not REA.
PNG encoding follows https://www.w3.org/TR/png-3/#9Filters. Pillow must
independently decode every image to the exact intended RGB/RGBA samples.
Run from any directory; normal tests read the checked-in vectors only.
Apply the repository JSON formatter to vectors.json after regenerating.
"""
import base64
import hashlib
import io
import json
import pathlib
import struct
import zlib
from PIL import Image, __version__
def chunk(kind, payload):
data = kind + payload
return struct.pack(">I", len(payload)) + data + struct.pack(">I", zlib.crc32(data))
def png(pixels, channels, filters):
rows = []
stride = 6 * channels
for y, kind in enumerate(filters):
row = bytearray([kind])
for x in range(stride):
a = pixels[y * stride + x - channels] if x >= channels else 0
b = pixels[(y - 1) * stride + x] if y else 0
c = pixels[(y - 1) * stride + x - channels] if y and x >= channels else 0
prediction = a + b - c
distances = [abs(prediction - value) for value in (a, b, c)]
paeth = (a, b, c)[distances.index(min(distances))]
predictor = (0, a, b, (a + b) // 2, paeth)[kind]
row.append((pixels[y * stride + x] - predictor) % 256)
rows.append(row)
header = struct.pack(">IIBBBBB", 6, 6, 8, 2 if channels == 3 else 6, 0, 0, 0)
encoded = (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header)
+ chunk(b"IDAT", zlib.compress(b"".join(rows))) + chunk(b"IEND", b""))
image = Image.open(io.BytesIO(encoded))
assert image.mode == ("RGB" if channels == 3 else "RGBA")
assert image.tobytes() == pixels
return base64.b64encode(encoded).decode("ascii")
vectors = []
for channels in (3, 4):
pixels = bytearray(hashlib.shake_256(b"rea-png-filter-oracle").digest(36 * channels))
# Paeth ties at row 4, pixel 1: above/upper-left and left/upper-left.
for y, x, values in ((3, 0, (20, 20)), (3, 1, (40, 10)), (4, 0, (10, 40))):
pixels[(y * 6 + x) * channels:(y * 6 + x) * channels + 2] = bytes(values)
original = bytes(pixels)
pixels[-channels] ^= 0x20
changed = bytes(pixels)
vectors.append({
"mode": "RGB" if channels == 3 else "RGBA",
"decoded_sha256": hashlib.sha256(original).hexdigest(),
"filters": [4, 1, 2, 3, 4, 0],
"reference": png(original, channels, [0] * 6),
"filtered": png(original, channels, [4, 1, 2, 3, 4, 0]),
"changed": png(changed, channels, [4, 1, 2, 3, 4, 0]),
"delta": abs(changed[-channels] - original[-channels]),
})
pathlib.Path(__file__).with_name("vectors.json").write_text(
json.dumps({"oracle": "Pillow " + __version__, "vectors": vectors}, indent=2) + "\n"
)
print("Pillow", __version__, "verified all 6 PNGs, 36 pixels each, exact full samples")
+23
View File
@@ -0,0 +1,23 @@
{
"oracle": "Pillow 13.0.0",
"vectors": [
{
"mode": "RGB",
"decoded_sha256": "d4264b9fdc83d11d191bb5916362448596339b87d7f5aeccf14e91d86cc37143",
"filters": [4, 1, 2, 3, 4, 0],
"reference": "iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAIAAABvrngfAAAAfUlEQVR4nAFyAI3/AGCRW6lv+l5leYuwsNvVArm/awB426z49SjMklcFECck6FhgZ4YAIc7mQKFQbodUr78GaAn46tULABQU+SgKcxKmqe35EEstyrMPrQAKKJwEZNHKwwGQh9Zz8duNQxYABhstASHMjRofvHb+bqZr7+Tx+Hc0sa1dmJsAAAAASUVORK5CYII=",
"filtered": "iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAIAAABvrngfAAAAfUlEQVR4nAFyAI3/BGCRW0nen7X2fy1LN1AlUt7qaQF426yAGnzUnS85ftAf2DE8fy4CqfM6SKwoovX9qq/fRCGgim6FAwSthv6wz8deRo1HuaGsRhmOQwT2FKPcPF7GHTCjjtUoxAXaUkwABhstASHMjRofvHb+bqZr7+TxDSY1wg0/XHgAAAAASUVORK5CYII=",
"changed": "iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAIAAABvrngfAAAAfUlEQVR4nAFyAI3/BGCRW0nen7X2fy1LN1AlUt7qaQF426yAGnzUnS85ftAf2DE8fy4CqfM6SKwoovX9qq/fRCGgim6FAwSthv6wz8deRo1HuaGsRhmOQwT2FKPcPF7GHTCjjtUoxAXaUkwABhstASHMjRofvHb+bqZrz+TxDMY1op5us3kAAAAASUVORK5CYII=",
"delta": 32
},
{
"mode": "RGBA",
"decoded_sha256": "bbe054ef4031891c77d12f7810dc19181257ecab6e3a6156ee41635ad56fa59b",
"filters": [4, 1, 2, 3, 4, 0],
"reference": "iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAoUlEQVR4nAGWAGn/AGCRW6lv+l5leYuwsNvVArm/a3jbrPj1KADMklcFECck6FhgZ4YhzuZAoVBuh1SvvwYAaAn46tULsrH5WYlzEqap7fkQSy3Ksw+tABQUnAQoCsrDAZCH1nPx241DFgYbLQEhzAAKKB+8dv5upmvv5PEwST81VyAA9C0unwkA+3dfF4TXx3xtvoVVoNn+n8koDSAFhZS65LVFDAG1rAwAAAAASUVORK5CYII=",
"filtered": "iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAoUlEQVR4nAGWAGn/BGCRW6kPaQO8CpFSS2JKUgnklnYi7Y19TQHMklcFRJXN40g5Q57Jbn+6gIKIR7NfUX8CnHeh5cXkjsmh+SLt8djDrVjA3aZ2BFCnA+AQII+0+yNpcV/eO2pWQ6yNlnO+p50XaAT2FIO4TtZP4/Xxdi69WFuoJwr62eotfhUA+3dfF4TXx3xtvoVVoNn+n8koDSAFhZS6K/BKKpxAWVYAAAAASUVORK5CYII=",
"changed": "iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAoUlEQVR4nAGWAGn/BGCRW6kPaQO8CpFSS2JKUgnklnYi7Y19TQHMklcFRJXN40g5Q57Jbn+6gIKIR7NfUX8CnHeh5cXkjsmh+SLt8djDrVjA3aZ2BFCnA+AQII+0+yNpcV/eO2pWQ6yNlnO+p50XaAT2FIO4TtZP4/Xxdi69WFuoJwr62eotfhUA+3dfF4TXx3xtvoVVoNn+n8koDSAlhZS6LHBKSlRmeWEAAAAASUVORK5CYII=",
"delta": 32
}
]
}