Files
SnapOtter/scripts/generate-hostile-fixtures.mjs
T
SnapOtterandGitHub d10d0f544f fix: release QA hardening across processing, media, security, and CI gates (#649)
A release-readiness QA pass over the whole product. The commits split into
defects a user would hit and gates that were reporting green while measuring
nothing.

## Fixes that change behaviour

Rate limiting was bypassable on every install: TRUST_PROXY defaulted to true, so
request.ip came from a client-set header and a forged X-Forwarded-For got past
the login limiter. The default is now a private-network trust list.

A transient Postgres outage stranded in-flight jobs, leaving finished output on
disk with no row pointing at it. A reconciler now resolves those rows and adopts
the bytes rather than dropping the work.

A Redis connection that moved to a new address wedged every read-blocked
consumer, so completions stopped signalling while health still answered 200.
Socket timeouts plus subscriber pings recover it.

Installing more than one AI bundle left the shared venv multi-versioned and
silently broke three tools. The installer now reconciles distributions to one
version each.

Converting an image to JXL at quality 1 through 4 returned a 500, because
libjxl 0.7 rejects the distance those values compute. The quality is floored at
what the encoder honours. A missing ffmpeg was also reported to the user as a
corrupt upload; it now says the engine is unavailable.

RAW uploads reached an unpatched LibRaw on arm64, so it is built from source at
0.22.2, and the release scan was split so it can fail on an unfixed critical
instead of hiding it behind ignore-unfixed.

## Gates that could not fail

Two mutation lanes ran zero mutants because Stryker crawled the gitignored docs
build; coverage discarded its whole report on any failing test; the lint gate
skipped root tests, scripts, and two workspaces; and several generated matrices
counted a host missing ffmpeg as a passing tool. Each now measures what it
claims.

Full evidence and the outstanding release items are tracked locally and are not
part of this branch.
2026-07-27 15:37:30 +08:00

89 lines
3.2 KiB
JavaScript

#!/usr/bin/env node
/**
* Generates the hostile-input fixtures in tests/fixtures/image/hostile/.
*
* Deterministic on purpose: re-running produces byte-identical files so the
* committed fixtures never churn. No dependencies (the bomb PNG is built by
* hand instead of with sharp).
*
* Run from the repo root: node scripts/generate-hostile-fixtures.mjs
*/
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import zlib from "node:zlib";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const outDir = join(root, "tests", "fixtures", "image", "hostile");
mkdirSync(outDir, { recursive: true });
// --- 1. truncated.jpg: a real JPEG cut off at 40% ---------------------------
const realJpeg = readFileSync(
join(root, "tests", "fixtures", "image", "valid", "sample-photo.jpg"),
);
writeFileSync(
join(outDir, "truncated.jpg"),
realJpeg.subarray(0, Math.floor(realJpeg.length * 0.4)),
);
// --- 2. zero-byte.png --------------------------------------------------------
writeFileSync(join(outDir, "zero-byte.png"), Buffer.alloc(0));
// --- 3. garbage.jpg: 8KB of seeded pseudo-random bytes ----------------------
const garbage = Buffer.alloc(8192);
let state = 0xdeadbeef;
for (let i = 0; i < garbage.length; i++) {
// LCG (numerical recipes constants) for deterministic noise
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
garbage[i] = state & 0xff;
}
writeFileSync(join(outDir, "garbage.jpg"), garbage);
// --- 4. png-bytes.jpg: valid PNG content with a lying .jpg extension --------
const realPng = readFileSync(join(root, "tests", "fixtures", "image", "valid", "test-200x150.png"));
writeFileSync(join(outDir, "png-bytes.jpg"), realPng);
// --- 5. bomb-50000x50000.png: tiny file whose header claims 2,500 megapixels
// A decompression-bomb shape: valid signature + IHDR declaring 50000x50000,
// with a minimal IDAT. Anything that trusts the header and allocates dies;
// the API must reject it via its megapixel limit instead.
function crc32(buf) {
let c = 0xffffffff;
const table = new Int32Array(256);
for (let i = 0; i < 256; i++) {
let v = i;
for (let j = 0; j < 8; j++) v = v & 1 ? 0xedb88320 ^ (v >>> 1) : v >>> 1;
table[i] = v;
}
for (let i = 0; i < buf.length; i++) c = table[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
}
function chunk(type, data) {
const typeBuf = Buffer.from(type);
const len = Buffer.alloc(4);
len.writeUInt32BE(data.length);
const crcBuf = Buffer.alloc(4);
crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
return Buffer.concat([len, typeBuf, data, crcBuf]);
}
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(50000, 0); // width
ihdr.writeUInt32BE(50000, 4); // height
ihdr[8] = 8; // bit depth
ihdr[9] = 0; // grayscale
// One deflated row's worth of zeroes; far less data than the header promises.
const idat = zlib.deflateSync(Buffer.alloc(1024));
writeFileSync(
join(outDir, "bomb-50000x50000.png"),
Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", ihdr),
chunk("IDAT", idat),
chunk("IEND", Buffer.alloc(0)),
]),
);
console.log(`Hostile fixtures written to ${outDir}`);