fix: content-based SVG detection, guard tests, document REDIS_CACHE_DB

- Detect SVG by content (magic bytes) in addition to MIME type to prevent
  sanitization bypass via mislabeled uploads
- Add guard test verifying DOMPurify actually strips dirty payloads
- Add looksLikeSvg detection test
- Document REDIS_CACHE_DB in .env.example
This commit is contained in:
Thomas Camlong committed 2026-08-07 21:22:25 +02:00
1 parent 33293ba1cf
commit e836d07d4c
4 files changed
+31 -4

No files matched your search

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { purify, sanitizeSvg, svgSanitizeOptions } from "./svg-purify";
import { looksLikeSvg, purify, sanitizeSvg, svgSanitizeOptions } from "./svg-purify";
describe("SVG sanitization", () => {
it("strips script tags from SVG", () => {
@@ -67,6 +67,21 @@ describe("SVG sanitization", () => {
expect(clean).not.toContain("onbegin");
});
it("DOMPurify actually strips dirty payload (guard against no-op shim)", () => {
const dirty = '<svg><script>alert("xss")</script><rect width="1" height="1"/></svg>';
const clean = sanitizeSvg(dirty);
expect(clean).not.toBe(dirty);
expect(clean).not.toContain("<script");
expect(clean).toContain("<rect");
});
it("looksLikeSvg detects SVG content regardless of MIME", () => {
const svg = new TextEncoder().encode('<svg viewBox="0 0 24 24"><path d="M0 0"/></svg>');
const png = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
expect(looksLikeSvg(svg)).toBe(true);
expect(looksLikeSvg(png)).toBe(false);
});
it("preserves well-formed SVG with self-closing children", () => {
const icon =
'<svg viewBox="0 0 24 24"><path d="M12 2L2 22h20Z" fill="currentColor"/><circle cx="12" cy="8" r="2"/></svg>';