Review findings on clean-ai-response.ts: 1. update_props is now cleaned with the TARGET node's component (resolved on the live canvas by apply()); an unresolvable target gets the strictest rules (no code, url/src only assets/). code is kept only on an HtmlBlock. 2. cssValueAllowed() is a positive grammar: numbers/lengths/hex/keywords, an allow-list of functions (gradients, colour functions, calc/clamp, var(--*), transforms, filters), url() only of assets/<file>, quoted strings only plain font names; image-set(, src(, cross-fade(, element(, paint(, attr(, env(, ':' or '//' outside url(assets/..), backslashes and control chars rejected. Regression: every style/colour value in the templates, section families and palettes passes (clean-ai-response.ordinary.test.ts). Also: tricky link forms (//host, backslashes, whitespace/control chars, mixed-case schemes, > 2048) rejected; comments state the server-side mirror is not implemented (deferred). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
12 KiB
TypeScript
192 lines
12 KiB
TypeScript
import { describe, test, expect } from 'vitest';
|
|
import { allowUrl, cssValueAllowed, cleanAiProps, cleanAiResponse } from './clean-ai-response';
|
|
|
|
describe('allowUrl: link props', () => {
|
|
test.each(['', '#about', '/contact', 'about.html', 'mailto:hello@example.com', 'tel:+1 555 0100', 'https://example.com/a?b=1', 'http://example.com/'])('allows %s', (v) => {
|
|
expect(allowUrl('link', v)).toBe(v);
|
|
});
|
|
test.each(['javascript:alert(1)', 'data:text/html,hi', 'vbscript:x', 'file:///etc/passwd', '//example.com/x', 'ftp://example.com/', 'https://user:pw@example.com/', 'a b', '#a"b'])('rejects %s', (v) => {
|
|
expect(allowUrl('link', v)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('allowUrl: tricky link forms', () => {
|
|
test.each([
|
|
['protocol-relative', '//example.com/path'],
|
|
['protocol-relative after a slash and backslash', '/\\example.com/path'],
|
|
['backslash form', '\\\\example.com\\path'],
|
|
['leading space', ' //example.com'.slice(0) + ' x'],
|
|
['tab inside', 'java\tscript:x'],
|
|
['newline inside', '/a\nb'],
|
|
['NUL', '/a\u0000b'],
|
|
['mixed-case scheme', 'JavaScript:x'],
|
|
['upper-case data scheme', 'DATA:text/plain,x'],
|
|
['over-long', '/' + 'a'.repeat(2100)],
|
|
['quote', '/a"b'],
|
|
])('rejects %s', (_label, v) => {
|
|
expect(allowUrl('link', v)).toBeNull();
|
|
});
|
|
test('a single leading slash is relative; mixed-case https is still https', () => {
|
|
expect(allowUrl('link', '/about')).toBe('/about');
|
|
expect(allowUrl('link', 'HTTPS://example.com/')).toBe('HTTPS://example.com/');
|
|
});
|
|
test('the image and video rules reject the same tricky forms', () => {
|
|
for (const v of ['//images.pexels.com/a.jpg', 'HTTPS://example.com/a.jpg', 'assets/a\\b.jpg', 'assets/' + 'a'.repeat(250)]) {
|
|
expect(allowUrl('image', v), v).toBeNull();
|
|
expect(allowUrl('video', v), v).toBeNull();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('allowUrl: image and video props', () => {
|
|
test('images: own assets and the supported stock/placeholder hosts only', () => {
|
|
expect(allowUrl('image', 'assets/hero.jpg')).toBe('assets/hero.jpg');
|
|
expect(allowUrl('image', 'https://images.pexels.com/photos/1/a.jpeg')).toBe('https://images.pexels.com/photos/1/a.jpeg');
|
|
expect(allowUrl('image', 'https://placehold.co/600x400?text=Hero')).toBe('https://placehold.co/600x400?text=Hero');
|
|
for (const bad of ['https://example.com/a.png', 'http://images.pexels.com/a.jpg', 'data:image/png;base64,AAAA', '/api/assets-proxy/?site_id=1&filename=a.jpg', 'assets/../x.jpg', 'images/a.jpg']) {
|
|
expect(allowUrl('image', bad), bad).toBeNull();
|
|
}
|
|
});
|
|
test('videos: own assets or YouTube/Vimeo', () => {
|
|
expect(allowUrl('video', 'https://www.youtube.com/watch?v=abc')).not.toBeNull();
|
|
expect(allowUrl('video', 'https://vimeo.com/123')).not.toBeNull();
|
|
expect(allowUrl('video', 'assets/clip.mp4')).toBe('assets/clip.mp4');
|
|
expect(allowUrl('video', 'https://example.com/clip.mp4')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('cssValueAllowed', () => {
|
|
test.each(['#112233', 'var(--wsb-primary)', 'linear-gradient(135deg, #111, #222)', '24px', 'color-mix(in srgb, var(--wsb-primary) 10%, transparent)', 'url(assets/bg.jpg)', "url('assets/bg.jpg')", 0])('allows %s', (v) => {
|
|
expect(cssValueAllowed(v)).toBe(true);
|
|
});
|
|
test.each(['url(https://example.com/x.png)', 'url(javascript:x)', '@import "x.css"', 'expression(alert(1))', 'red; color: blue', 'red}', '</style>', 'behavior: url(x.htc)'])('rejects %s', (v) => {
|
|
expect(cssValueAllowed(v)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('cleanAiProps', () => {
|
|
test('keeps ordinary props unchanged', () => {
|
|
const p = { text: 'Book a table', href: '#contact', style: { color: 'var(--wsb-text)', padding: '14px 28px' }, anchorId: 'menu' };
|
|
expect(cleanAiProps(p, 'ButtonLink')).toEqual(p);
|
|
});
|
|
test('drops disallowed URLs, CSS and ids; keeps the rest', () => {
|
|
const out = cleanAiProps({
|
|
text: 'Hi', href: 'javascript:alert(1)', anchorId: 'about us',
|
|
style: { color: 'red', background: 'url(https://example.com/x.png)', 'bad key': 'x', behavior: 'x' },
|
|
}, 'ButtonLink');
|
|
expect(out).toEqual({ text: 'Hi', style: { color: 'red' } });
|
|
});
|
|
test('nested arrays: links, images, slides', () => {
|
|
expect(cleanAiProps({ links: [{ text: 'A', href: '/a' }, { text: 'B', href: 'data:text/html,x' }] }, 'Navbar'))
|
|
.toEqual({ links: [{ text: 'A', href: '/a' }, { text: 'B' }] });
|
|
expect(cleanAiProps({ images: [{ src: 'assets/a.jpg', alt: 'a' }, { src: 'https://example.com/b.jpg', alt: 'b' }, 'assets/c.jpg', 'https://example.com/d.jpg'] }, 'Gallery'))
|
|
.toEqual({ images: [{ src: 'assets/a.jpg', alt: 'a' }, { alt: 'b' }, 'assets/c.jpg'] });
|
|
});
|
|
test('the model may not set form destinations, relay settings or head code', () => {
|
|
const out = cleanAiProps({ recipientEmail: 'x@example.com', webhookUrl: 'https://example.com/h', destinationType: 'webhook', thankYouUrl: 'https://example.com/', action: 'https://example.com/', headCode: '<meta>', heading: 'Contact us' }, 'ContactForm');
|
|
expect(out).toEqual({ heading: 'Contact us' });
|
|
});
|
|
test('event-handler-like and non-identifier prop names are dropped', () => {
|
|
expect(cleanAiProps({ onClick: 'x', 'data-x': 'y', text: 't' }, 'TextBlock')).toEqual({ text: 't' });
|
|
});
|
|
test('VideoBlock url, CallToAction image bgValue, icon class', () => {
|
|
expect(cleanAiProps({ videoUrl: 'https://example.com/v.mp4', poster: 'assets/p.jpg' }, 'VideoBlock')).toEqual({ poster: 'assets/p.jpg' });
|
|
expect(cleanAiProps({ bgType: 'image', bgValue: 'https://example.com/b.jpg' }, 'CallToAction')).toEqual({ bgType: 'image' });
|
|
expect(cleanAiProps({ bgType: 'gradient', bgValue: 'linear-gradient(90deg, #000, #fff)' }, 'CallToAction')).toEqual({ bgType: 'gradient', bgValue: 'linear-gradient(90deg, #000, #fff)' });
|
|
expect(cleanAiProps({ icon: 'fa-solid fa-star' }, 'Icon')).toEqual({ icon: 'fa-solid fa-star' });
|
|
expect(cleanAiProps({ icon: 'fa-star" x="y' }, 'Icon')).toEqual({});
|
|
});
|
|
test('HtmlBlock code passes through to its own sanitisers unchanged', () => {
|
|
expect(cleanAiProps({ code: '<p>Hello</p>' }, 'HtmlBlock')).toEqual({ code: '<p>Hello</p>' });
|
|
});
|
|
});
|
|
|
|
describe('cleanAiResponse', () => {
|
|
test('replace: every tree (pages, header, footer) is cleaned; input not mutated', () => {
|
|
const resp = {
|
|
type: 'replace' as const, scope: 'site' as const, message: 'm',
|
|
header: { tree: { type: { resolvedName: 'Navbar' }, props: { logoUrl: 'javascript:x', logoText: 'Brand' } } },
|
|
pages: [{ name: 'Home', tree: { type: { resolvedName: 'Container' }, props: {}, nodes: [{ type: { resolvedName: 'ImageBlock' }, props: { src: 'https://example.com/a.png', alt: 'a' } }] } }],
|
|
};
|
|
const snap = JSON.stringify(resp);
|
|
const out = cleanAiResponse(resp);
|
|
expect(JSON.stringify(resp)).toBe(snap);
|
|
if (out.type !== 'replace') throw new Error('type');
|
|
expect(out.header!.tree.props).toEqual({ logoText: 'Brand' });
|
|
expect(out.pages[0].tree.nodes![0].props).toEqual({ alt: 'a' });
|
|
});
|
|
test('patch: update_props cannot change the form destination (the user value stays)', () => {
|
|
const out = cleanAiResponse({ type: 'patch', message: 'm', ops: [{ op: 'update_props', node_id: 'f1', props: { recipientEmail: 'x@example.com', heading: 'Write to us' } }] });
|
|
if (out.type !== 'patch') throw new Error('type');
|
|
expect(out.ops[0]).toEqual({ op: 'update_props', node_id: 'f1', props: { heading: 'Write to us' } });
|
|
});
|
|
test('ask passes through', () => {
|
|
const a = { type: 'ask' as const, question: 'Which colours?' };
|
|
expect(cleanAiResponse(a)).toBe(a);
|
|
});
|
|
});
|
|
|
|
describe('patches are cleaned with the TARGET component\'s rules (review finding 1)', () => {
|
|
const patch = (props: Record<string, unknown>) => ({ type: 'patch' as const, message: 'm', ops: [{ op: 'update_props' as const, node_id: 't', props }] });
|
|
const propsOf = (r: ReturnType<typeof cleanAiResponse>) => (r.type === 'patch' && r.ops[0].op === 'update_props' ? r.ops[0].props : null);
|
|
const as = (name: string | undefined) => () => name;
|
|
test('HtmlBlock: code kept only when the target IS an HtmlBlock', () => {
|
|
expect(propsOf(cleanAiResponse(patch({ code: '<p>x</p>' }), as('HtmlBlock')))).toEqual({ code: '<p>x</p>' });
|
|
expect(propsOf(cleanAiResponse(patch({ code: '<p>x</p>', text: 'a' }), as('TextBlock')))).toEqual({ text: 'a' });
|
|
expect(propsOf(cleanAiResponse(patch({ code: '<p>x</p>' }), as(undefined)))).toEqual({});
|
|
expect(propsOf(cleanAiResponse(patch({ code: '<p>x</p>' })))).toEqual({});
|
|
});
|
|
test('VideoBlock: url is a video URL on a VideoBlock; unresolvable target -> strictest (assets only)', () => {
|
|
const yt = 'https://www.youtube.com/watch?v=abc';
|
|
expect(propsOf(cleanAiResponse(patch({ url: yt }), as('VideoBlock')))).toEqual({ url: yt });
|
|
expect(propsOf(cleanAiResponse(patch({ url: yt }), as(undefined)))).toEqual({});
|
|
expect(propsOf(cleanAiResponse(patch({ url: 'assets/clip.mp4' }), as(undefined)))).toEqual({ url: 'assets/clip.mp4' });
|
|
expect(propsOf(cleanAiResponse(patch({ videoUrl: 'https://example.com/v.mp4' }), as('VideoBlock')))).toEqual({});
|
|
});
|
|
test('ContactForm / FormContainer: destinations are never patchable, resolved or not', () => {
|
|
for (const name of ['ContactForm', 'FormContainer', 'SubscribeForm', undefined]) {
|
|
expect(propsOf(cleanAiResponse(patch({ recipientEmail: 'a@example.com', action: '/x', webhookUrl: 'https://example.com/h', heading: 'Hi' }), as(name)))).toEqual({ heading: 'Hi' });
|
|
}
|
|
});
|
|
test('ButtonLink href and ImageBlock src', () => {
|
|
expect(propsOf(cleanAiResponse(patch({ href: 'https://example.com/' }), as('ButtonLink')))).toEqual({ href: 'https://example.com/' });
|
|
expect(propsOf(cleanAiResponse(patch({ href: 'javascript:x' }), as(undefined)))).toEqual({});
|
|
const px = 'https://images.pexels.com/photos/1/a.jpeg';
|
|
expect(propsOf(cleanAiResponse(patch({ src: px }), as('ImageBlock')))).toEqual({ src: px });
|
|
expect(propsOf(cleanAiResponse(patch({ src: px }), as(undefined)))).toEqual({});
|
|
expect(propsOf(cleanAiResponse(patch({ src: 'https://example.com/a.png' }), as('ImageBlock')))).toEqual({});
|
|
});
|
|
test('Navbar: links[].href and logoUrl / logoImage', () => {
|
|
expect(propsOf(cleanAiResponse(patch({ logoUrl: '/', logoImage: 'assets/logo.png', links: [{ text: 'A', href: '#a' }, { text: 'B', href: 'vbscript:x' }] }), as('Navbar'))))
|
|
.toEqual({ logoUrl: '/', logoImage: 'assets/logo.png', links: [{ text: 'A', href: '#a' }, { text: 'B' }] });
|
|
});
|
|
test('a componentOf that throws is treated as unresolvable (strict)', () => {
|
|
expect(propsOf(cleanAiResponse(patch({ code: 'x', src: 'https://images.pexels.com/a.jpg' }), () => { throw new Error('gone'); }))).toEqual({});
|
|
});
|
|
test('replace_node / insert_* trees carry their component names', () => {
|
|
const r = cleanAiResponse({ type: 'patch', message: 'm', ops: [
|
|
{ op: 'insert_after', node_id: 'a', tree: { type: { resolvedName: 'HtmlBlock' }, props: { code: '<p>ok</p>' } } },
|
|
{ op: 'replace_node', node_id: 'b', tree: { type: { resolvedName: 'Container' }, props: { code: '<p>no</p>', tag: 'div' } } },
|
|
] });
|
|
if (r.type !== 'patch') throw new Error('type');
|
|
expect((r.ops[0] as any).tree.props).toEqual({ code: '<p>ok</p>' });
|
|
expect((r.ops[1] as any).tree.props).toEqual({ tag: 'div' });
|
|
});
|
|
});
|
|
|
|
describe('cssValueAllowed: positive grammar (review finding 2)', () => {
|
|
test.each([
|
|
'red', 'transparent', 'inherit', '#0f172a', 'rgb(0 0 0 / 50%)', 'rgba(15, 23, 42, 0.8)', 'hsl(210 40% 98%)',
|
|
'1.5rem', '-0.02em', '100%', '0 4px 12px rgba(0,0,0,.1)', 'clamp(2.25rem, 6vw, 4rem)', 'calc(100% - 2 * 24px)',
|
|
'var(--wsb-primary)', 'var(--wsb-radius, 8px)', 'linear-gradient(135deg, var(--wsb-primary) 0%, #111 100%)',
|
|
'radial-gradient(circle at center, #fff, transparent)', 'conic-gradient(from 90deg, red, blue)',
|
|
"'Playfair Display', serif", '"Open Sans", sans-serif', '4 / 3', 'translateY(-4px)', 'cubic-bezier(0.4, 0, 0.2, 1)',
|
|
'repeat(3, minmax(0, 1fr))', 'url(assets/bg.jpg)', "url('assets/bg.jpg') center / cover no-repeat",
|
|
])('allows %s', (v) => expect(cssValueAllowed(v)).toBe(true));
|
|
test.each([
|
|
'image-set("assets/a.jpg" 1x)', '-webkit-image-set(url(assets/a.jpg) 1x)', 'src("assets/a.jpg")', 'cross-fade(20% red, blue)',
|
|
'element(#x)', 'paint(foo)', 'attr(data-x)', 'env(safe-area-inset-top)', "'https//example.com'", '"a:b"', 'x//y',
|
|
'url(https://example.com/x.png)', 'url(//example.com/x.png)', 'red \\9', 'red\u0001', 'var(x)', 'red !important',
|
|
])('rejects %s', (v) => expect(cssValueAllowed(v)).toBe(false));
|
|
});
|