Sitesmith: allow-list cleaner for model responses before they reach the canvas; export strips attack-only CSS (G8)
clean-ai-response.ts runs first in useApplyAiResponse().apply() and in
applyRegeneratedSection(): link props allow ''|#anchor|relative|mailto:|tel:|
https:|http: (never //host, backslash, whitespace/control chars, > 2048);
images only assets/<file> or https on images.pexels.com / placehold.co;
videos assets/ or YouTube/Vimeo; the model can never set form destinations,
relay settings or head code (patches keep the user's values); style names
must be CSS identifiers and values may not carry url() (except assets/),
@import, expression(, behavior, -moz-binding, javascript:, < > { } ; \;
anchor ids [A-Za-z0-9_-]. HtmlBlock code keeps its existing sanitisers.
Export boundary: stripLegacyCssVectors() in cssValue/cssPropsToString
(byte-identical for ordinary content: OLS fixture + default design measured).
Server-side mirror not implemented (deferred).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
93b3cc35af
commit
2b8b0557b2
8 files changed
+408
-3
No files matched your search
@@ -183,3 +183,41 @@ describe('AI apply-response (real @craftjs/core editor)', () => {
|
||||
expect(harness.container.textContent).toContain('Anchor Heading');
|
||||
});
|
||||
});
|
||||
|
||||
describe('AI apply-response: the allow-list cleaner runs before anything reaches the canvas (G8)', () => {
|
||||
test('a section with a disallowed link and an off-list image lands without those props', async () => {
|
||||
harness = renderEditorHarness({ initialState: INITIAL_STATE });
|
||||
const applyRef = mountApplyFn(harness);
|
||||
const resp: SitesmithResponse = {
|
||||
type: 'replace', scope: 'section', message: 'm',
|
||||
pages: [{ name: 'section', tree: { type: { resolvedName: 'Container' }, props: { tag: 'div' }, nodes: [
|
||||
{ type: { resolvedName: 'ButtonLink' }, props: { text: 'Cleaned Button', href: 'javascript:void(0)' } },
|
||||
{ type: { resolvedName: 'ImageBlock' }, props: { src: 'https://example.com/a.png', alt: 'Cleaned Image' } },
|
||||
] } }],
|
||||
};
|
||||
await harness.act(async () => { await applyRef.current!(resp); });
|
||||
const ser = JSON.parse(harness.getSerialized());
|
||||
const props = Object.values(ser).map((n: any) => n.props);
|
||||
const btn = props.find((p: any) => p.text === 'Cleaned Button') as any;
|
||||
const img = props.find((p: any) => p.alt === 'Cleaned Image') as any;
|
||||
expect(btn).toBeDefined();
|
||||
expect(String(btn.href ?? '')).not.toContain('javascript');
|
||||
expect(img).toBeDefined();
|
||||
expect(String(img.src ?? '')).not.toContain('example.com');
|
||||
});
|
||||
|
||||
test('update_props cannot change a form destination; other props still apply', async () => {
|
||||
const state = JSON.parse(INITIAL_STATE);
|
||||
state.ROOT.nodes.push('form-1');
|
||||
state['form-1'] = { type: { resolvedName: 'ContactForm' }, isCanvas: false, props: { node_id: 'form-1', heading: 'Old', recipientEmail: 'owner@example.com' },
|
||||
displayName: 'Contact Form', custom: {}, hidden: false, parent: 'ROOT', nodes: [], linkedNodes: {} };
|
||||
harness = renderEditorHarness({ initialState: JSON.stringify(state) });
|
||||
const applyRef = mountApplyFn(harness);
|
||||
await harness.act(async () => {
|
||||
await applyRef.current!({ type: 'patch', message: 'm', ops: [{ op: 'update_props', node_id: 'form-1', props: { heading: 'New heading', recipientEmail: 'other@example.com' } }] });
|
||||
});
|
||||
const p = harness.query.node('form-1').get().data.props;
|
||||
expect(p.heading).toBe('New heading');
|
||||
expect(p.recipientEmail).toBe('owner@example.com');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user