The capture never fetches external images, so placehold.co placeholders came out as a blank grey box (Chromium) or alt text (Firefox) and the reviewer reported a false "screenshot mismatch". checkUrl now maps a strictly parsed placehold.co URL (https, host exactly placehold.co, /WxH with 1-4 digits, up to two 3/6-digit hex colours, optional image extension) to a locally generated SVG data URL of the same intrinsic size: URL bg colour (default #cccccc), ?text= label (default "W x H") in the fg colour (default #969696), XML-escaped. Off-spec placehold.co URLs become a plain #cccccc box (no label), or the old 1x1 grey image when the size is unknown. Works for <img> (own CSS: width, aspect-ratio, object-fit) and CSS url() backgrounds. Only an SVG byte-identical to our generator passes the second sanitiser pass. makePolicy/allow-list, sandbox and CSP unchanged; fetchBounded refuses placehold.co outright. e2e: fidelity samples the boxes/labels, security asserts zero placehold.co requests (incl. hostile ?text=) in Chromium and Firefox. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
7.0 KiB
TypeScript
108 lines
7.0 KiB
TypeScript
import { describe, expect, test } from 'vitest';
|
||
import { parsePlaceholdUrl, placeholderDataUrl, isPlaceholderDataUrl } from './capture-placeholder';
|
||
import { sanitizeCaptureHtml, checkUrl, makePolicy, BLOCKED_IMAGE } from './sanitize-capture-html';
|
||
import { fetchBounded } from './self-review-capture';
|
||
|
||
const svgOf = (d: string) => new TextDecoder().decode(Uint8Array.from(atob(d.split(',')[1]), (c) => c.charCodeAt(0)));
|
||
const dom = (h: string) => new DOMParser().parseFromString(h, 'text/html');
|
||
|
||
describe('parsePlaceholdUrl (strict)', () => {
|
||
test('size, colours and text', () => {
|
||
expect(parsePlaceholdUrl('https://placehold.co/800x1000/1f3a5f/fbfaf7?text=Team+working+together')).toEqual({
|
||
kind: 'label', w: 800, h: 1000, bg: '#1f3a5f', fg: '#fbfaf7', text: 'Team working together',
|
||
});
|
||
});
|
||
test('defaults: #cccccc on #969696, label = W x H', () => {
|
||
expect(parsePlaceholdUrl('https://placehold.co/600x400')).toEqual({ kind: 'label', w: 600, h: 400, bg: '#cccccc', fg: '#969696', text: '600 × 400' });
|
||
expect(parsePlaceholdUrl('https://placehold.co/600x400/000')).toMatchObject({ bg: '#000000', fg: '#969696' });
|
||
});
|
||
test('percent-decoding, format suffix, 3-digit hex', () => {
|
||
expect(parsePlaceholdUrl('https://placehold.co/300x200/abc/FFF.png?text=Caf%C3%A9%20%26%20%3Cb%3E')).toMatchObject({
|
||
kind: 'label', bg: '#aabbcc', fg: '#ffffff', text: 'Café & <b>',
|
||
});
|
||
});
|
||
test('not placehold.co -> null (today\'s behaviour)', () => {
|
||
for (const u of ['https://example.com/800x600', 'https://placehold.co.evil.example/800x600', 'https://evil.example/placehold.co/800x600',
|
||
'https://via.placeholder.com/800x600', '//placehold.co/800x600', 'placehold.co/800x600', 'assets/x.png', '/beacon']) {
|
||
expect(parsePlaceholdUrl(u)).toBeNull();
|
||
}
|
||
});
|
||
test('placehold.co but anything off-spec -> plain neutral box, no label', () => {
|
||
expect(parsePlaceholdUrl('http://placehold.co/800x600/000/fff?text=hi')).toEqual({ kind: 'neutral', w: 800, h: 600 });
|
||
expect(parsePlaceholdUrl('https://placehold.co/800x600/orange/white?text=hi')).toEqual({ kind: 'neutral', w: 800, h: 600 });
|
||
expect(parsePlaceholdUrl('https://placehold.co/800x600/0000/fff')).toEqual({ kind: 'neutral', w: 800, h: 600 });
|
||
expect(parsePlaceholdUrl('https://user@placehold.co/800x600')).toEqual({ kind: 'neutral', w: 800, h: 600 });
|
||
expect(parsePlaceholdUrl('https://placehold.co:8443/800x600')).toEqual({ kind: 'neutral', w: 800, h: 600 });
|
||
expect(parsePlaceholdUrl('https://placehold.co/800x600/000/fff/extra')).toEqual({ kind: 'neutral', w: 800, h: 600 });
|
||
expect(parsePlaceholdUrl('https://placehold.co/12345x600')).toEqual({ kind: 'neutral', w: 0, h: 0 });
|
||
expect(parsePlaceholdUrl('https://placehold.co/0x600')).toEqual({ kind: 'neutral', w: 0, h: 0 });
|
||
expect(parsePlaceholdUrl('https://placehold.co/600')).toEqual({ kind: 'neutral', w: 0, h: 0 });
|
||
expect(parsePlaceholdUrl('https://PLACEHOLD.CO/800x600')).toMatchObject({ kind: 'label' }); // hosts are case-insensitive
|
||
});
|
||
});
|
||
|
||
describe('placeholderDataUrl', () => {
|
||
test('a local SVG of the image size, bg fill and escaped label', () => {
|
||
const d = placeholderDataUrl('https://placehold.co/800x1000/1f3a5f/fbfaf7?text=%3Cscript%3Ealert(1)%3C/script%3E+%26+"q"')!;
|
||
expect(d.startsWith('data:image/svg+xml;base64,')).toBe(true);
|
||
const svg = svgOf(d);
|
||
expect(svg).toMatch(/width="800" height="1000"/);
|
||
expect(svg).toContain('fill="#1f3a5f"');
|
||
expect(svg).toContain('fill="#fbfaf7"');
|
||
expect(svg).toContain('<script>alert(1)</script> & "q"');
|
||
expect(svg.replace('xmlns="http://www.w3.org/2000/svg"', '')).not.toMatch(/<script|https?:|href/);
|
||
expect(isPlaceholderDataUrl(d)).toBe(true);
|
||
});
|
||
test('neutral box: size kept, #cccccc, no text; unknown size -> the 1x1 blocked image', () => {
|
||
const svg = svgOf(placeholderDataUrl('https://placehold.co/800x600/orange/white?text=hi')!);
|
||
expect(svg).toMatch(/width="800" height="600"/);
|
||
expect(svg).toContain('fill="#cccccc"');
|
||
expect(svg).not.toContain('<text');
|
||
expect(placeholderDataUrl('https://placehold.co/600?text=x')).toBe(BLOCKED_IMAGE);
|
||
expect(placeholderDataUrl('https://example.com/a.png')).toBeNull();
|
||
});
|
||
test('only our own generated SVG passes isPlaceholderDataUrl', () => {
|
||
expect(isPlaceholderDataUrl('data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=')).toBe(false);
|
||
const forged = btoa('<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><image href="https://evil.example/x"/></svg>');
|
||
expect(isPlaceholderDataUrl(`data:image/svg+xml;base64,${forged}`)).toBe(false);
|
||
const d = placeholderDataUrl('https://placehold.co/80x60?text=ok')!;
|
||
const tampered = svgOf(d).replace('</svg>', '<image href="https://evil.example/x"/></svg>');
|
||
expect(isPlaceholderDataUrl(`data:image/svg+xml;base64,${btoa(tampered)}`)).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe('sanitiser + capture use the local placeholder, never the network', () => {
|
||
test('img src and CSS background url() become the local SVG', () => {
|
||
const r = sanitizeCaptureHtml('<html><body><img id="i" src="https://placehold.co/800x600/1f3a5f/fbfaf7?text=Team" srcset="https://placehold.co/1600x1200 2x" style="width:100%;aspect-ratio:4/3;object-fit:cover">'
|
||
+ '<div id="b" style="background-image:url(https://placehold.co/1200x400/222/eee?text=Hero);background-color:#333;height:40px"></div>'
|
||
+ '<img id="x" src="https://example.com/a.png"></body></html>') as any;
|
||
expect(r.ok).toBe(true);
|
||
const d = dom(r.html);
|
||
const src = d.getElementById('i')!.getAttribute('src')!;
|
||
expect(isPlaceholderDataUrl(src)).toBe(true);
|
||
expect(svgOf(src)).toContain('>Team</text>');
|
||
expect(d.getElementById('i')!.getAttribute('style')).toMatch(/object-fit:\s*cover/);
|
||
const bg = /url\("([^"]+)"\)/.exec(d.getElementById('b')!.getAttribute('style')!)![1]; // jsdom: longhands only (its CSSOM re-serialises a position shorthand unstably)
|
||
expect(isPlaceholderDataUrl(bg)).toBe(true);
|
||
expect(svgOf(bg)).toContain('fill="#222222"');
|
||
expect(d.getElementById('x')!.getAttribute('src')).toBe(BLOCKED_IMAGE); // any other external image: unchanged behaviour
|
||
expect(r.html).not.toContain('placehold.co');
|
||
});
|
||
test('checkUrl returns the data URL (not the URL), and the policy is not widened', () => {
|
||
const pol = makePolicy();
|
||
expect(pol.allow.size).toBe(0);
|
||
expect(checkUrl('https://placehold.co/80x60', pol)!.startsWith('data:image/svg+xml;base64,')).toBe(true);
|
||
expect(checkUrl('https://placehold.co/80x60', pol, true)).toBeNull(); // never a font
|
||
});
|
||
test('fetchBounded never fetches a placehold.co URL', async () => {
|
||
let calls = 0;
|
||
const orig = globalThis.fetch;
|
||
globalThis.fetch = (async () => { calls++; return new Response('x'); }) as typeof fetch;
|
||
try {
|
||
expect(await fetchBounded('https://placehold.co/80x60', makePolicy())).toBeNull();
|
||
expect(await fetchBounded('http://placehold.co/80x60', makePolicy())).toBeNull();
|
||
} finally { globalThis.fetch = orig; }
|
||
expect(calls).toBe(0);
|
||
});
|
||
});
|