import { gzipSync } from "node:zlib"; import { describe, expect, it, vi } from "vitest"; // Same config mock as svg-sanitize.test.ts: a fixed 10MB SVG cap so the size / // element-count guards are deterministic under the container-free mutation lane. vi.mock("../../../apps/api/src/config.js", () => ({ env: { MAX_SVG_SIZE_MB: 10 }, })); import { decompressSvgz, sanitizeSvg } from "../../../apps/api/src/lib/svg-sanitize.js"; // Helper: run the real sanitizer and return the UTF-8 output string. function clean(svg: string): string { return sanitizeSvg(Buffer.from(svg)).toString("utf-8"); } // Every payload embeds a benign `` sibling. Asserting the // sibling survives proves the regex mutant that would over-strip (or that the // removal ran at all) is not silently eating valid markup, and asserting the // dangerous construct is ABSENT proves the ORIGINAL regex stripped something a // mutated (case-flag-dropped, anchor-flipped, char-class-narrowed) variant // would let through. const KEEP = ''; function expectKept(out: string): void { expect(out).toContain(KEEP); } describe("sanitizeSvg mutation kills: ${KEEP}`); expect(out).not.toContain(" case-insensitively (uppercase)", () => { const out = clean(`${KEEP}`); expect(out.toLowerCase()).not.toContain(" case-insensitively (mixed case)", () => { const out = clean(`${KEEP}`); expect(out.toLowerCase()).not.toContain(" with attributes on the open tag", () => { const out = clean( `${KEEP}`, ); expect(out).not.toContain(" with a whitespace-tolerant end tag", () => { const out = clean(`${KEEP}`); expect(out).not.toContain(" tag with no close (open-tag pattern)", () => { const out = clean(``); expect(out).not.toContain(" tag with no open", () => { const out = clean(`text${KEEP}`); expect(out).not.toContain(" { it("fully removes a nested ipt> payload (one pass is not enough)", () => { // After the first removal of the inner , the outer // ipt>bad${KEEP}`); expect(out).not.toContain(" is stripped", () => { // `on