Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(content): clean pasted content and edit a draft as HTML (#1259)
Cihanf110f9266935
diff --git a/web/messages/en.json b/web/messages/en.jsonindex 271d6f2d4..f86843bd9 100644--- a/web/messages/en.json+++ b/web/messages/en.json@@ -3112,6 +3112,21 @@"newPage": "New page","placeholder": "Start with the page's H1, then write…","documentLabel": "Page content",+ "jsonLd": "Schema · {type} (JSON-LD)",+ "jsonLdUntyped": "Schema (JSON-LD)",+ "paste": {+ "cleaned": "Pasted · formatting cleaned (headings, lists, links and tables kept)",+ "cleanedNoImages": "Pasted · formatting cleaned. Images were left out; add them with Insert an image.",+ "plain": "Pasted as plain text"+ },+ "html": {+ "title": "HTML view",+ "hint": "paste or edit HTML; it applies when you switch back. Scripts, iframes and inline styles are removed; schema JSON-LD is kept.",+ "back": "Back to visual",+ "label": "The page's HTML",+ "applied": "HTML applied · formatting cleaned",+ "appliedUnsafe": "HTML applied · {count, plural, one {# unsafe tag} other {# unsafe tags}} removed"+ },"markReady": "Mark ready","markedReady": "Marked ready. Publish it on your site when you're ready.","markReadyFailed": "Couldn't mark it ready. Try again.",@@ -3147,6 +3162,8 @@"addRow": "Add a row below","addColumn": "Add a column to the right","deleteTable": "Delete the table",+ "html": "Edit as HTML",+ "htmlBack": "Back to visual","openLink": "Open the link in a new tab (or ⌘/Ctrl + click it in the page)","counter": "{count} / {target} characters"},diff --git a/web/package.json b/web/package.jsonindex afb349bcb..f5f5e4232 100644--- a/web/package.json+++ b/web/package.json@@ -41,6 +41,7 @@"ai": "^6.0.116","class-variance-authority": "^0.7.1","clsx": "^2.1.1",+ "dompurify": "3.4.16","fflate": "^0.8.3","lucide-react": "^0.577.0","marked": "^18.0.0",@@ -56,6 +57,7 @@"recharts": "^3.8.0","remark-breaks": "^4.0.0","remark-gfm": "^4.0.1",+ "sanitize-html": "2.17.0","shadcn": "^4.0.2","shiki": "^4.0.2","sonner": "^2.0.7",@@ -72,6 +74,7 @@"@types/node": "^20","@types/react": "^19","@types/react-dom": "^19",+ "@types/sanitize-html": "2.16.0","eslint": "^9","eslint-config-next": "16.1.6","eslint-config-prettier": "^10.1.8",diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsxindex 193d673c3..60fd7172d 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx@@ -1,6 +1,6 @@'use client';-import { useState } from 'react';+import { useEffect, useState } from 'react';import { useTranslations } from 'next-intl';import { toast } from 'sonner';import { EditorContent, useEditor } from '@tiptap/react';@@ -16,7 +16,9 @@ import { buttonVariants } from '@/components/ui/button-variants';import { DraftMethodBadge, DraftStatusBadge } from '@/components/content/draft-badges';import { markContentDraftReady } from '@/lib/actions/content-drafts';import { CONTENT_LIBRARY_PATH, isPageType, type ContentDraft } from '@/lib/content/drafts';+import { cleanHtml, formatHtmlSource } from '@/lib/content/clean-html';import { draftTitleFromDoc, lengthTarget, safeLinkHref } from '@/lib/content/editor';+import { JsonLd } from '@/lib/content/json-ld-node';import { cn } from '@/lib/utils';import { EditorToolbar } from './_toolbar';import { useDraftAutosave, type SaveState } from './_use-autosave';@@ -42,6 +44,11 @@ const DOCUMENT_CLASS = cn(* draft's title, page type and method, whether it's saved, and Mark ready;* the toolbar; and the page itself, rich text that saves as you write. The* draft's title is its first heading, as the Library lists it.+ *+ * A paste is cleaned on the way in (CE-005): headings, lists, links and+ * tables stay; fonts, colors, styles and tracking parameters go; ⌘/Ctrl ++ * Shift + V pastes plain text. The HTML view (CE-020) shows the page's+ * source, and cleans it the same way when it switches back.*/export function DraftEditor({ draft }: { draft: ContentDraft }) {const t = useTranslations('contentEditor.editor');@@ -50,6 +57,9 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {const [title, setTitle] = useState(draft.title);const [status, setStatus] = useState(draft.status);const [marking, setMarking] = useState(false);+ // The HTML view's text while it is open; null in the visual editor.+ const [htmlSource, setHtmlSource] = useState<string | null>(null);+ const [htmlEdited, setHtmlEdited] = useState(false);const editor = useEditor({extensions: [@@ -69,12 +79,28 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {Image,CharacterCount,Placeholder.configure({ placeholder: t('placeholder') }),+ JsonLd.configure({+ label: (type) => (type ? t('jsonLd', { type }) : t('jsonLdUntyped')),+ }),],content: draft.bodyHtml,// Rendered on the client only, so the server's HTML can't disagree with it.immediatelyRender: false,editorProps: {attributes: { class: DOCUMENT_CLASS, 'aria-label': t('documentLabel') },+ transformPastedHTML: (html) => {+ // A copy from this editor keeps its images, and needs no word about it.+ if (html.includes('data-pm-slice')) return cleanHtml(html, { keepImages: true }).html;+ // Images from elsewhere wait for attachments (CE-006): Docs' and+ // Word's are temporary addresses or embedded data.+ const cleaned = cleanHtml(html, { keepImages: false });+ toast(cleaned.imagesRemoved ? t('paste.cleanedNoImages') : t('paste.cleaned'));+ return cleaned.html;+ },+ transformPastedText: (text, plain) => {+ if (plain) toast(t('paste.plain'));+ return text;+ },// A click on a link puts the cursor in it, to edit it; ⌘/Ctrl + click// opens it, as in other editors. A path on the brand's own site has no// address here to open.@@ -93,6 +119,37 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {},});+ const openHtmlView = () => {+ if (!editor) return;+ setHtmlSource(formatHtmlSource(editor.getHTML()));+ setHtmlEdited(false);+ };+ const applyHtmlView = () => {+ if (!editor || htmlSource === null) return;+ // Nothing edited: back to the page as it was, with nothing to save.+ if (!htmlEdited) {+ setHtmlSource(null);+ return;+ }+ const cleaned = cleanHtml(htmlSource, { keepImages: true });+ // Emits an update, so the title and the autosave follow.+ editor.commands.setContent(cleaned.html);+ setHtmlSource(null);+ toast(+ cleaned.unsafeRemoved+ ? t('html.appliedUnsafe', { count: cleaned.unsafeRemoved })+ : t('html.applied'),+ );+ };++ // HTML edited but not applied isn't saved yet: leaving asks first.+ useEffect(() => {+ if (!htmlEdited || htmlSource === null) return;+ const warn = (e: BeforeUnloadEvent) => e.preventDefault();+ window.addEventListener('beforeunload', warn);+ return () => window.removeEventListener('beforeunload', warn);+ }, [htmlEdited, htmlSource]);+const canMarkReady = status === 'draft' || status === 'in_review';const markReady = async () => {setMarking(true);@@ -171,10 +228,43 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {</div></header>- {editor && <EditorToolbar editor={editor} target={lengthTarget(draft.pageType)} />}+ {editor && (+ <EditorToolbar+ editor={editor}+ target={lengthTarget(draft.pageType)}+ htmlView={htmlSource !== null}+ onToggleHtml={htmlSource === null ? openHtmlView : applyHtmlView}+ />+ )}++ {htmlSource !== null && (+ <div className="flex items-center gap-2 border-b bg-muted/40 px-4 py-1.5 text-xs text-muted-foreground">+ <span>+ <span className="font-medium text-foreground">{t('html.title')}</span> ·{' '}+ {t('html.hint')}+ </span>+ <Button size="sm" className="ml-auto h-7" onClick={applyHtmlView}>+ {t('html.back')}+ </Button>+ </div>+ )}<div className="min-h-0 flex-1 overflow-y-auto">- <div className="mx-auto max-w-[720px] px-7 pt-7 pb-20">+ {htmlSource !== null && (+ <textarea+ value={htmlSource}+ onChange={(e) => {+ setHtmlSource(e.target.value);+ setHtmlEdited(true);+ }}+ spellCheck={false}+ aria-label={t('html.label')}+ className="block h-full min-h-[600px] w-full resize-none bg-muted/20 px-7 py-5 font-mono text-[12.5px] leading-relaxed outline-none"+ />+ )}+ <div+ className={cn('mx-auto max-w-[720px] px-7 pt-7 pb-20', htmlSource !== null && 'hidden')}+ ><EditorContent editor={editor} /></div></div>diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsxindex a027a1b68..35fe4a554 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx@@ -37,10 +37,21 @@ import { cn } from '@/lib/utils';/*** The editor's toolbar (CE-004, spec §6): H1 H2 H3 · B I link · lists,- * quote · table, image, and the length counter against the page type's- * target. Inside a table it adds a row, a column, or removes the table.+ * quote · table, image · </> HTML, and the length counter against the page+ * type's target. Inside a table it adds a row, a column, or removes the+ * table. While the HTML view is open, only its own button works.*/-export function EditorToolbar({ editor, target }: { editor: Editor; target: number }) {+export function EditorToolbar({+ editor,+ target,+ htmlView,+ onToggleHtml,+}: {+ editor: Editor;+ target: number;+ htmlView: boolean;+ onToggleHtml: () => void;+}) {const t = useTranslations('contentEditor.editor.toolbar');const [dialog, setDialog] = useState<'link' | 'image' | null>(null);@@ -138,14 +149,28 @@ export function EditorToolbar({ editor, target }: { editor: Editor; target: numb<div key={i} className="flex items-center gap-0.5">{i > 0 && <span aria-hidden="true" className="mx-1.5 h-5 w-px bg-border" />}{group.map((tool) => (- <ToolButton key={tool.key} label={t(tool.key)} pressed={tool.on} onClick={tool.run}>+ <ToolButton+ key={tool.key}+ label={t(tool.key)}+ pressed={!htmlView && tool.on}+ disabled={htmlView}+ onClick={tool.run}+ ><tool.icon className="h-4 w-4" /></ToolButton>))}</div>))}- {state.href && <LinkTarget href={state.href} />}- {state.inTable && (+ <span aria-hidden="true" className="mx-1.5 h-5 w-px bg-border" />+ <ToolButton+ label={htmlView ? t('htmlBack') : t('html')}+ pressed={htmlView}+ onClick={onToggleHtml}+ >+ <span className="font-mono text-[11.5px] font-medium"></> HTML</span>+ </ToolButton>+ {state.href && !htmlView && <LinkTarget href={state.href} />}+ {state.inTable && !htmlView && (<div className="flex items-center gap-0.5"><span aria-hidden="true" className="mx-1.5 h-5 w-px bg-border" />{tableTools.map((tool) => (@@ -208,11 +233,13 @@ function LinkTarget({ href }: { href: string }) {function ToolButton({label,pressed,+ disabled,onClick,children,}: {label: string;pressed?: boolean;+ disabled?: boolean;onClick: () => void;children: React.ReactNode;}) {@@ -222,11 +249,12 @@ function ToolButton({title={label}aria-label={label}aria-pressed={pressed}+ disabled={disabled}// Keep the editor's selection: a click on the toolbar mustn't move focus first.onMouseDown={(e) => e.preventDefault()}onClick={onClick}className={cn(- 'flex h-7 min-w-7 items-center justify-center rounded-md px-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50',+ 'flex h-7 min-w-7 items-center justify-center rounded-md px-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-40',pressed && 'bg-muted text-foreground',)}>diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsxindex c25e0371b..e7a79e937 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx@@ -30,7 +30,7 @@ const brandState = vi.hoisted(() => ({vi.mock('@/stores/use-brand-store', () => ({useBrandStore: (selector: (state: typeof brandState) => unknown) => selector(brandState),}));-const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() }));+const toast = vi.hoisted(() => Object.assign(vi.fn(), { success: vi.fn(), error: vi.fn() }));vi.mock('sonner', () => ({ toast }));(@@ -78,9 +78,10 @@ beforeEach(() => {root = createRoot(container);getContentDraft.mockReset();markContentDraftReady.mockReset();- saveContentDraft.mockReset();+ saveContentDraft.mockReset().mockResolvedValue({ updatedAt: '2026-10-20T10:00:00Z' });brandState.activeBrandId = 'b1';brandState.setActiveBrand.mockReset();+ toast.mockReset();toast.success.mockReset();});@@ -144,6 +145,55 @@ describe('the content editor', () => {expect(button('Mark ready')!.disabled).toBe(true);});+ it('shows the page as HTML, and cleans it when it switches back', async () => {+ getContentDraft.mockResolvedValue(+ draft({+ bodyHtml:+ '<h1>Trail Runner X</h1><script type="application/ld+json">{"@type":"Product"}</script>',+ }),+ );+ await renderPage();+ // Schema JSON-LD shows as a labelled block, not as text.+ expect(container.querySelector('[data-json-ld]')!.textContent).toBe(+ '{ } Schema · Product (JSON-LD)',+ );++ await act(async () => button('Edit as HTML')!.click());+ const source = container.querySelector('textarea')!;+ expect(source.value).toBe(+ '<h1>Trail Runner X</h1>\n<script type="application/ld+json">{"@type":"Product"}</script>',+ );+ // The other tools wait while the HTML view is open.+ expect(button('Bold')!.disabled).toBe(true);++ const setValue = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value')!.set!;+ await act(async () => {+ setValue.call(+ source,+ `${source.value}<p style="color:red">New <script>alert(1)</script>line</p>`,+ );+ source.dispatchEvent(new Event('input', { bubbles: true }));+ });+ await act(async () => button('Back to visual')!.click());++ expect(container.querySelector('textarea')).toBeNull();+ expect(toast).toHaveBeenCalledWith('HTML applied · 1 unsafe tag removed');+ const page = container.querySelector('.ProseMirror')!;+ expect(page.querySelector('p')!.outerHTML).toBe('<p>New line</p>');+ expect(page.querySelector('[data-json-ld]')).not.toBeNull();+ expect(page.textContent).not.toContain('alert');+ });++ it('goes back from an unedited HTML view without saving anything', async () => {+ getContentDraft.mockResolvedValue(draft());+ await renderPage();+ await act(async () => button('Edit as HTML')!.click());+ await act(async () => button('Back to visual')!.click());+ expect(container.querySelector('textarea')).toBeNull();+ expect(toast).not.toHaveBeenCalled();+ expect(container.querySelector('header')!.textContent).toContain('Saved');+ });+it('shows the link the cursor is in, to open in a new tab', async () => {getContentDraft.mockResolvedValue(draft({ bodyHtml: '<p><a href="https://gartner.com/x">Gartner</a> says so.</p>' }),diff --git a/web/src/lib/actions/content-drafts.test.ts b/web/src/lib/actions/content-drafts.test.tsindex 00cecfdab..29ff33d87 100644--- a/web/src/lib/actions/content-drafts.test.ts+++ b/web/src/lib/actions/content-drafts.test.ts@@ -87,6 +87,18 @@ describe('saveContentDraft', () => {});});+ it('checks the body against the draft allow-list again before saving it', async () => {+ updated = { updated_at: '2026-10-20T15:00:00Z' };+ const result = await saveContentDraft(DRAFT, {+ title: 'Hi',+ bodyHtml: '<p onclick="steal()">Hi</p><script>alert(1)</script>',+ base: await base(),+ });+ expect(calls[0].value).toEqual({ title: 'Hi', body_html: '<p>Hi</p>' });+ // The fingerprint is of the page as stored.+ expect(result).toMatchObject({ ok: true, bodyHash: await bodyHash('<p>Hi</p>') });+ });+it('goes over a change to the draft’s other columns, the page being the same', async () => {// Marked ready meanwhile: a newer updated_at, the page as the editor saw it.updates = [null, { updated_at: '2026-10-20T15:01:00Z' }];diff --git a/web/src/lib/actions/content-drafts.ts b/web/src/lib/actions/content-drafts.tsindex 22f4fe2f7..206efd7b2 100644--- a/web/src/lib/actions/content-drafts.ts+++ b/web/src/lib/actions/content-drafts.ts@@ -16,6 +16,7 @@ import {bodyHash,type SaveDraftResult,} from '@/lib/content/editor';+import { sanitizeDraftHtml } from '@/lib/content/sanitize-draft-html';/*** Content drafts (Content Editor, CE-002), read through the signed-in member's@@ -129,10 +130,11 @@ const saveInput = z.object({const SAVE_ATTEMPTS = 3;/**- * The editor's autosave: the body and the title read from it, written only- * over the version the editor last saw (`base`). When another write came- * first, the page tells which kind: the same page means only the draft's- * other columns changed (Mark ready, its type), and the save goes over them;+ * The editor's autosave: the body, checked against the draft allow-list+ * again (sanitizeDraftHtml), and the title read from it, written only over+ * the version the editor last saw (`base`). When another write came first,+ * the page tells which kind: the same page means only the draft's other+ * columns changed (Mark ready, its type), and the save goes over them;* another page means someone else changed it (another tab, a teammate, the* Content Agent), and nothing is overwritten. A save RLS refused, or of a* draft that no longer exists, comes back not_found rather than saved.@@ -146,7 +148,8 @@ export async function saveContentDraft(}const parsed = saveInput.safeParse(input);if (!draftId.safeParse(id).success || !parsed.success) return { ok: false, reason: 'invalid' };- const { title, bodyHtml, base } = parsed.data;+ const { title, base } = parsed.data;+ const bodyHtml = sanitizeDraftHtml(parsed.data.bodyHtml);const supabase = await createClient();let expected = base.updatedAt;for (let attempt = 0; attempt < SAVE_ATTEMPTS; attempt++) {diff --git a/web/src/lib/content/clean-html.test.ts b/web/src/lib/content/clean-html.test.tsnew file mode 100644index 000000000..67cbd8def--- /dev/null+++ b/web/src/lib/content/clean-html.test.ts@@ -0,0 +1,93 @@+import { describe, expect, it } from 'vitest';+import { cleanHtml, formatHtmlSource } from './clean-html';++// A section copied from Google Docs: the whole copy in a non-bold <b>, bold+// and italic as styles, styled links with tracking parameters, an H5, a+// table, an image.+const GOOGLE_DOCS =+ '<meta charset="utf-8"><b style="font-weight:normal;" id="docs-internal-guid-1a2b">' ++ '<h1 dir="ltr" style="line-height:1.38"><span style="font-size:20pt;font-family:Arial;font-weight:700">Lisbon guide</span></h1>' ++ '<p dir="ltr" style="line-height:1.38;margin-top:0pt"><span style="font-weight:700;color:#000">Best time</span>' ++ '<span style="font-weight:400"> is </span><span style="font-style:italic">spring</span>. ' ++ '<a href="https://visitlisboa.com/en?utm_source=docs&lang=en&gclid=abc" style="text-decoration:none">' ++ '<span style="color:#1155cc">Visit Lisboa</span></a></p>' ++ '<ul style="margin-top:0"><li dir="ltr" style="list-style-type:disc"><p dir="ltr"><span>Alfama</span></p></li></ul>' ++ '<h5 dir="ltr"><span>Getting around</span></h5>' ++ '<div dir="ltr" align="left"><table style="border:none"><colgroup><col width="200"></colgroup><tbody><tr>' ++ '<td style="border-left:solid #000 1pt"><p dir="ltr"><span>Tram 28</span></p></td></tr></tbody></table></div>' ++ '<p><span><img src="https://lh7-rt.googleusercontent.com/docsz/abc" width="300"></span></p></b>';++describe('cleaning a paste', () => {+ it('keeps headings, bold, italic, links, lists and tables from Google Docs, and nothing else', () => {+ const { html, imagesRemoved, unsafeRemoved } = cleanHtml(GOOGLE_DOCS, { keepImages: false });+ expect(html).toBe(+ '<h1>Lisbon guide</h1>' ++ '<p><strong>Best time</strong> is <em>spring</em>. ' ++ '<a href="https://visitlisboa.com/en?lang=en">Visit Lisboa</a></p>' ++ '<ul><li><p>Alfama</p></li></ul>' ++ '<h3>Getting around</h3>' ++ '<table><tbody><tr><td><p>Tram 28</p></td></tr></tbody></table>',+ );+ expect(imagesRemoved).toBe(1);+ expect(unsafeRemoved).toBe(0);+ });++ it('keeps Word’s bold and drops its classes, fonts and empty o:p', () => {+ const word =+ '<p class="MsoNormal" style="mso-line-height:12pt"><b><span style="font-family:Calibri">Hello</span></b>' ++ '<span lang="EN-US"> world<o:p></o:p></span></p><p class="MsoNormal"><o:p> </o:p></p>';+ expect(cleanHtml(word, { keepImages: false }).html).toBe('<p><b>Hello</b> world</p>');+ });++ it('takes out scripts, iframes and event handlers, and counts them', () => {+ const { html, unsafeRemoved } = cleanHtml(+ '<script>alert(1)</script><p onclick="x()">Hi <a href="javascript:alert(1)">there</a></p>' ++ '<iframe src="https://evil.com"></iframe><img src="x" onerror="alert(1)">',+ { keepImages: true },+ );+ expect(html).toBe('<p>Hi <a>there</a></p>');+ expect(unsafeRemoved).toBe(2);+ });++ it('keeps schema JSON-LD, written again from its JSON', () => {+ const { html, unsafeRemoved } = cleanHtml(+ '<p>Shoe</p><script type="application/ld+json">\n{ "@type": "Product", "name": "X" }\n</script>',+ { keepImages: true },+ );+ expect(html).toBe(+ '<p>Shoe</p><script type="application/ld+json">{"@type":"Product","name":"X"}</script>',+ );+ expect(unsafeRemoved).toBe(0);+ });++ it('keeps web images in the HTML view, never embedded data', () => {+ const { html, imagesRemoved } = cleanHtml(+ '<p><img src="https://cdn.acme.com/a.png" alt="Shoe" style="width:10px"></p>' ++ '<p><img src="data:image/png;base64,AAAA"></p>',+ { keepImages: true },+ );+ expect(html).toBe('<p><img src="https://cdn.acme.com/a.png" alt="Shoe"></p>');+ expect(imagesRemoved).toBe(1);+ });++ it('keeps how a copy from the editor itself was cut', () => {+ const { html } = cleanHtml('<ul data-pm-slice="3 3 []"><li><p>One</p></li></ul>', {+ keepImages: true,+ });+ expect(html).toBe('<ul data-pm-slice="3 3 []"><li><p>One</p></li></ul>');+ });+});++describe('formatHtmlSource', () => {+ it('puts one block per line, as a draft stores it', () => {+ expect(+ formatHtmlSource(+ '<h1>A</h1><p>B <strong>C</strong> <a target="_blank" rel="noopener" href="/x">D</a></p>' ++ '<table style="min-width: 50px"><colgroup><col style="min-width: 25px"></colgroup>' ++ '<tbody><tr><td><p>E</p></td></tr></tbody></table>',+ ),+ ).toBe(+ '<h1>A</h1>\n<p>B <strong>C</strong> <a href="/x">D</a></p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>E</p>\n</td>\n</tr>\n</tbody>\n</table>',+ );+ });+});diff --git a/web/src/lib/content/clean-html.ts b/web/src/lib/content/clean-html.tsnew file mode 100644index 000000000..b5abc3b77--- /dev/null+++ b/web/src/lib/content/clean-html.ts@@ -0,0 +1,138 @@+import DOMPurify from 'dompurify';+import {+ ALLOWED_ATTRIBUTES,+ ALLOWED_TAGS,+ DEMOTED_HEADINGS,+ JSON_LD_SLOT,+ UNSAFE_TAGS,+ cleanHref,+ extractJsonLd,+ restoreJsonLd,+} from './html-allowlist';++/**+ * Cleans HTML in the browser before the editor takes it (Content Editor+ * CE-005, CE-020): a paste from Word, Google Docs, Notion or a web page, and+ * the HTML view when it switches back. Keeps what html-allowlist.ts allows;+ * the server checks every save again (sanitize-draft-html.ts).+ */++export interface CleanedHtml {+ html: string;+ /** Scripts, iframes, objects… taken out (JSON-LD isn't one). */+ unsafeRemoved: number;+ /** Images left out (a paste), or ones that weren't http(s). */+ imagesRemoved: number;+}++// data-i marks a JSON-LD slot; data-pm-slice is how a copy from the editor+// itself says which lists or quotes it was cut out of.+const ALLOWED_ATTR = [+ ...new Set(Object.values(ALLOWED_ATTRIBUTES).flat()),+ 'data-i',+ 'data-pm-slice',+];++/**+ * Bold and italic set by style, as Google Docs writes them, become <strong>+ * and <em>, so they outlive the styles. Google Docs also wraps a whole copy+ * in <b style="font-weight:normal">, which isn't bold at all.+ */+function keepStyledEmphasis(root: HTMLElement) {+ for (const b of root.querySelectorAll<HTMLElement>('b[id^="docs-internal-guid"]')) {+ b.replaceWith(...b.childNodes);+ }+ for (const el of root.querySelectorAll<HTMLElement>('[style]')) {+ if (el.tagName === 'B' && /font-weight\s*:\s*(normal|[1-5]00)\b/i.test(el.style.cssText)) {+ el.replaceWith(...el.childNodes);+ continue;+ }+ const weight = el.style.fontWeight;+ // A heading is bold already; its spans only say so again.+ const bold =+ (weight === 'bold' || weight === 'bolder' || Number(weight) >= 600) &&+ !el.closest('h1, h2, h3, h4, h5, h6');+ const italic = el.style.fontStyle === 'italic';+ if (!bold && !italic) continue;+ const inner = document.createElement('span');+ inner.append(...el.childNodes);+ let wrapped: HTMLElement = inner;+ if (italic) {+ const em = document.createElement('em');+ em.append(wrapped);+ wrapped = em;+ }+ if (bold) {+ const strong = document.createElement('strong');+ strong.append(wrapped);+ wrapped = strong;+ }+ el.append(wrapped);+ }+}++function demoteHeadings(root: HTMLElement) {+ for (const tag of DEMOTED_HEADINGS) {+ for (const heading of root.querySelectorAll(tag)) {+ const h3 = document.createElement('h3');+ h3.append(...heading.childNodes);+ heading.replaceWith(h3);+ }+ }+}++export function cleanHtml(input: string, { keepImages }: { keepImages: boolean }): CleanedHtml {+ const { html, blocks } = extractJsonLd(input);+ const doc = new DOMParser().parseFromString(html, 'text/html');+ // Counted in the whole document: a script before any text parses into <head>.+ const unsafeRemoved = doc.querySelectorAll(UNSAFE_TAGS.join(',')).length;+ keepStyledEmphasis(doc.body);+ demoteHeadings(doc.body);++ // A purifier of its own, so these hooks don't touch other uses of DOMPurify.+ const purify = DOMPurify(window);+ let imagesRemoved = 0;+ purify.addHook('afterSanitizeAttributes', (node) => {+ if (node.tagName === 'A') {+ const href = cleanHref(node.getAttribute('href') ?? '');+ if (href) node.setAttribute('href', href);+ else node.removeAttribute('href');+ } else if (node.tagName === 'IMG') {+ const src = node.getAttribute('src') ?? '';+ if (!keepImages || !/^https?:\/\//i.test(src)) {+ imagesRemoved += 1;+ node.remove();+ }+ } else if (node.hasAttribute('data-i') && node.tagName.toLowerCase() !== JSON_LD_SLOT) {+ node.removeAttribute('data-i');+ }+ });+ const clean = purify.sanitize(doc.body.innerHTML, {+ ALLOWED_TAGS: [...ALLOWED_TAGS, JSON_LD_SLOT],+ ALLOWED_ATTR,+ ALLOW_DATA_ATTR: false,+ // Links and image addresses are checked by the hook above, more strictly+ // than DOMPurify's own rule (which still stops javascript:).+ });++ return {+ html: restoreJsonLd(clean, blocks).replace(/<p>(\s| |<br>)*<\/p>/g, ''),+ unsafeRemoved,+ imagesRemoved,+ };+}++const BLOCKS = 'h[1-6]|p|ul|ol|li|blockquote|table|thead|tbody|tr|th|td|script';+const BEFORE_OPENING_BLOCK = new RegExp(`><(?=(?:${BLOCKS})\\b)`, 'g');+const AFTER_CLOSING_BLOCK = new RegExp(`(</(?:${BLOCKS})>)(?=<)`, 'g');++/**+ * The HTML view's text: the page's HTML as a draft stores it (without the+ * editor's own table widths and link targets), one block per line, so it+ * reads like source. Inline tags stay on their line.+ */+export function formatHtmlSource(html: string): string {+ return cleanHtml(html, { keepImages: true })+ .html.replace(AFTER_CLOSING_BLOCK, '$1\n')+ .replace(BEFORE_OPENING_BLOCK, '>\n<');+}diff --git a/web/src/lib/content/html-allowlist.test.ts b/web/src/lib/content/html-allowlist.test.tsnew file mode 100644index 000000000..650dd49b4--- /dev/null+++ b/web/src/lib/content/html-allowlist.test.ts@@ -0,0 +1,100 @@+import { describe, expect, it } from 'vitest';+import {+ cleanHref,+ extractJsonLd,+ jsonLdType,+ normalizeJsonLd,+ restoreJsonLd,+} from './html-allowlist';++describe('cleanHref', () => {+ it('drops utm_*, gclid and fbclid and keeps every other parameter', () => {+ expect(+ cleanHref('https://acme.com/p?utm_source=x&id=3&utm_medium=y&gclid=z&fbclid=w#top'),+ ).toBe('https://acme.com/p?id=3#top');+ expect(cleanHref('/pricing?utm_campaign=fall&plan=pro')).toBe('/pricing?plan=pro');+ expect(cleanHref('https://acme.com/?UTM_Source=x')).toBe('https://acme.com/');+ });++ it('keeps mailto, and refuses what the toolbar refuses', () => {+ expect(cleanHref('mailto:[email protected]')).toBe('mailto:[email protected]');+ expect(cleanHref('javascript:alert(1)')).toBeNull();+ expect(cleanHref('//evil.com')).toBeNull();+ });+});++describe('normalizeJsonLd', () => {+ it('writes the JSON again with <, > and & escaped', () => {+ expect(normalizeJsonLd('{ "@type": "Product", "name": "A & B </script><b>" }')).toBe(+ '{"@type":"Product","name":"A \\u0026 B \\u003c/script\\u003e\\u003cb\\u003e"}',+ );+ expect(JSON.parse(normalizeJsonLd('[{"@type":"FAQPage"}]')!)).toEqual([{ '@type': 'FAQPage' }]);+ });++ it('refuses anything that isn’t a JSON object or list', () => {+ expect(normalizeJsonLd('alert(1)')).toBeNull();+ expect(normalizeJsonLd('"text"')).toBeNull();+ expect(normalizeJsonLd('null')).toBeNull();+ expect(normalizeJsonLd(`{"a":"${'x'.repeat(50_001)}"}`)).toBeNull();+ });++ it('names the schema type for the editor’s label', () => {+ expect(jsonLdType('{"@type":"Product"}')).toBe('Product');+ expect(jsonLdType('[{"@type":["FAQPage","WebPage"]}]')).toBe('FAQPage');+ expect(jsonLdType('{}')).toBeNull();+ });+});++describe('extractJsonLd and restoreJsonLd', () => {+ it('keeps valid schema blocks through cleaning, as script elements of our own', () => {+ const { html, blocks } = extractJsonLd(+ '<p>a</p><script type="application/ld+json" onload="x()">{"@type":"Product"}</script>' ++ "<script type='application/ld+json'>not json</script><script>alert(1)</script>",+ );+ expect(blocks).toEqual(['{"@type":"Product"}']);+ expect(html).toBe('<p>a</p><jsonld-slot data-i="0"></jsonld-slot><script>alert(1)</script>');+ expect(restoreJsonLd('<p>a</p><jsonld-slot data-i="0"></jsonld-slot>', blocks)).toBe(+ '<p>a</p><script type="application/ld+json">{"@type":"Product"}</script>',+ );+ });++ it('reads tags in any case, and skips other tags and plain scripts', () => {+ const { html, blocks } = extractJsonLd(+ '<SCRIPT TYPE="Application/LD+JSON">{"@type":"Product"}</SCRIPT>' ++ '<scripts type="application/ld+json">{}</scripts>' ++ '<script>var s = "<script type=application/ld+json>{}";</script><p>b</p>',+ );+ expect(blocks).toEqual(['{"@type":"Product"}']);+ expect(html).toBe(+ '<jsonld-slot data-i="0"></jsonld-slot><scripts type="application/ld+json">{}</scripts>' ++ '<script>var s = "<script type=application/ld+json>{}";</script><p>b</p>',+ );+ });++ it('finds a block after letters whose lower case is longer, as Turkish İ', () => {+ const { html, blocks } = extractJsonLd(+ '<h1>İstanbul İçin İyi Kahve</h1><script type="application/ld+json">{"@type":"Product"}</script><p>Sonra.</p>',+ );+ expect(blocks).toEqual(['{"@type":"Product"}']);+ expect(html).toBe(+ '<h1>İstanbul İçin İyi Kahve</h1><jsonld-slot data-i="0"></jsonld-slot><p>Sonra.</p>',+ );+ });++ it('takes time in proportion to the length on input built to backtrack', () => {+ const unclosed = '<script type=application/ld+json '.repeat(15_000);+ const unended = '<script type="application/ld+json">{"a":1}'.repeat(11_000);+ for (const input of [unclosed, unended]) {+ expect(input.length).toBeGreaterThan(450_000);+ const started = performance.now();+ const { html, blocks } = extractJsonLd(input);+ expect(performance.now() - started).toBeLessThan(500);+ expect(blocks).toEqual([]);+ expect(html).toBe(input);+ }+ });++ it('fills a slot nobody kept with nothing', () => {+ expect(restoreJsonLd('<jsonld-slot data-i="7"></jsonld-slot><p>a</p>', [])).toBe('<p>a</p>');+ });+});diff --git a/web/src/lib/content/html-allowlist.ts b/web/src/lib/content/html-allowlist.tsnew file mode 100644index 000000000..77c4584bb--- /dev/null+++ b/web/src/lib/content/html-allowlist.ts@@ -0,0 +1,184 @@+import { safeLinkHref } from './editor';++/**+ * What a draft's HTML may hold (Content Editor CE-005 and CE-020, spec §9 and+ * §12). The browser cleans a paste and the HTML view with it (clean-html.ts),+ * and the server checks every save against it again (sanitize-draft-html.ts):+ * headings, paragraphs, bold, italic, lists, links, tables, quotes, images+ * and schema JSON-LD. Fonts, colors, classes, inline styles, scripts and+ * iframes go.+ */++export const ALLOWED_TAGS = [+ 'h1',+ 'h2',+ 'h3',+ 'p',+ 'br',+ 'strong',+ 'b',+ 'em',+ 'i',+ 'a',+ 'ul',+ 'ol',+ 'li',+ 'blockquote',+ 'table',+ 'thead',+ 'tbody',+ 'tr',+ 'th',+ 'td',+ 'img',+];++/** Attributes kept, per tag; every other one (style, class, id, on*…) goes. */+export const ALLOWED_ATTRIBUTES: Record<string, string[]> = {+ a: ['href'],+ img: ['src', 'alt', 'title'],+ th: ['colspan', 'rowspan'],+ td: ['colspan', 'rowspan'],+};++/** Headings below H3 become H3: the editor has three levels. */+export const DEMOTED_HEADINGS = ['h4', 'h5', 'h6'];++/** Elements whose removal a toast counts as "unsafe". */+export const UNSAFE_TAGS = [+ 'script',+ 'iframe',+ 'frame',+ 'frameset',+ 'object',+ 'embed',+ 'applet',+ 'form',+ 'base',+];++const TRACKING_PARAM = /^(utm_.+|gclid|fbclid)$/i;++/**+ * A link as a draft keeps it: one the toolbar would accept (safeLinkHref),+ * without utm_*, gclid or fbclid, which only say where someone once clicked.+ * Null when it isn't a link.+ */+export function cleanHref(href: string): string | null {+ const safe = safeLinkHref(href);+ if (!safe || safe.startsWith('mailto:')) return safe;+ const relative = safe.startsWith('/');+ const url = new URL(safe, 'https://relative.invalid');+ for (const key of [...url.searchParams.keys()]) {+ if (TRACKING_PARAM.test(key)) url.searchParams.delete(key);+ }+ return relative ? `${url.pathname}${url.search}${url.hash}` : url.href;+}++/** The largest schema block a draft keeps, in characters. */+const MAX_JSON_LD_LENGTH = 50_000;++/**+ * A JSON-LD block as a draft stores it: parsed and written again, so only+ * JSON goes into the script element, with <, > and & escaped so it can never+ * close the element or start another. Null when it isn't a JSON object or+ * list, or is too large.+ */+export function normalizeJsonLd(text: string): string | null {+ if (text.length > MAX_JSON_LD_LENGTH) return null;+ let value: unknown;+ try {+ value = JSON.parse(text);+ } catch {+ return null;+ }+ if (typeof value !== 'object' || value === null) return null;+ return JSON.stringify(value)+ .replace(/</g, '\\u003c')+ .replace(/>/g, '\\u003e')+ .replace(/&/g, '\\u0026');+}++/** The schema type a JSON-LD block describes ("Product", "FAQPage"), for its label. */+export function jsonLdType(json: string): string | null {+ try {+ const value = JSON.parse(json) as Record<string, unknown> | Record<string, unknown>[];+ const first = Array.isArray(value) ? value[0] : value;+ const type = first?.['@type'];+ return typeof type === 'string' ? type : Array.isArray(type) ? String(type[0]) : null;+ } catch {+ return null;+ }+}++/** A script tag's attributes that say it holds schema JSON-LD. */+const JSON_LD_TYPE = /\btype\s*=\s*["']?application\/ld\+json/;++/**+ * The schema JSON-LD scripts in the HTML: where each starts and ends, and+ * its text. Found by a scan, not a regex: a pattern over a script tag's+ * attributes backtracks on input with many unclosed tags (cubic time), and+ * the server runs this on every save. Each step moves past what it read, so+ * the time grows with the length only.+ */+export function jsonLdScripts(html: string): { start: number; end: number; text: string }[] {+ // ASCII letters only: lower-casing all of it changes the length ('İ'+ // becomes two units), and the positions found must fit the HTML.+ const lower = html.replace(/[A-Z]+/g, (s) => s.toLowerCase());+ const found: { start: number; end: number; text: string }[] = [];+ let from = 0;+ for (;;) {+ const open = lower.indexOf('<script', from);+ if (open === -1) break;+ // "<scripts>" or "<script-x>" is another tag.+ if (/[^\s>/]/.test(lower.charAt(open + 7))) {+ from = open + 7;+ continue;+ }+ const tagEnd = lower.indexOf('>', open);+ if (tagEnd === -1) break;+ // A script's text isn't markup: the next tag is its end tag.+ const close = lower.indexOf('</script', tagEnd + 1);+ if (close === -1) break;+ const closeEnd = lower.indexOf('>', close);+ if (closeEnd === -1) break;+ if (JSON_LD_TYPE.test(lower.slice(open, tagEnd))) {+ found.push({ start: open, end: closeEnd + 1, text: html.slice(tagEnd + 1, close) });+ }+ from = closeEnd + 1;+ }+ return found;+}++/** Where a kept JSON-LD block waits while the rest of the HTML is cleaned. */+export const JSON_LD_SLOT = 'jsonld-slot';+const SLOT_PATTERN = new RegExp(`<${JSON_LD_SLOT} data-i="(\\d+)"></${JSON_LD_SLOT}>`, 'g');++/**+ * Takes the schema JSON-LD blocks out of the HTML before it is cleaned, as+ * slots, so the cleaner, which removes every script, keeps them. A block+ * that isn't valid JSON is dropped with its script.+ */+export function extractJsonLd(html: string): { html: string; blocks: string[] } {+ const blocks: string[] = [];+ let out = '';+ let last = 0;+ for (const script of jsonLdScripts(html)) {+ out += html.slice(last, script.start);+ const json = normalizeJsonLd(script.text.trim());+ if (json) {+ blocks.push(json);+ out += `<${JSON_LD_SLOT} data-i="${blocks.length - 1}"></${JSON_LD_SLOT}>`;+ }+ last = script.end;+ }+ return { html: out + html.slice(last), blocks };+}++/** Puts the kept blocks back as script elements of our own, with only their JSON. */+export function restoreJsonLd(html: string, blocks: string[]): string {+ return html.replace(SLOT_PATTERN, (_, i: string) => {+ const json = blocks[Number(i)];+ return json ? `<script type="application/ld+json">${json}</script>` : '';+ });+}diff --git a/web/src/lib/content/json-ld-node.ts b/web/src/lib/content/json-ld-node.tsnew file mode 100644index 000000000..fd3509428--- /dev/null+++ b/web/src/lib/content/json-ld-node.ts@@ -0,0 +1,58 @@+import { Node } from '@tiptap/core';+import { jsonLdType, normalizeJsonLd } from './html-allowlist';++export interface JsonLdOptions {+ /** The block's label in the editor, from its schema type ("Schema · Product"). */+ label: (type: string | null) => string;+}++/**+ * Schema JSON-LD in a draft (Content Editor CE-020: "schema JSON-LD is+ * kept"). In the page's HTML it is the script element; in the editor, a+ * labelled block that isn't text, so it neither counts toward the length nor+ * gets typed into, and is edited in the HTML view. Its JSON is checked and+ * written again on the way in (normalizeJsonLd), so it can't carry markup.+ */+export const JsonLd = Node.create<JsonLdOptions>({+ name: 'jsonLd',+ group: 'block',+ atom: true,+ selectable: true,+ draggable: false,++ addOptions() {+ return { label: (type) => (type ? `Schema · ${type}` : 'Schema') };+ },++ addAttributes() {+ return { json: { default: '{}', rendered: false } };+ },++ parseHTML() {+ return [+ {+ tag: 'script[type="application/ld+json"]',+ getAttrs: (el) => {+ const json = normalizeJsonLd((el.textContent ?? '').trim());+ return json ? { json } : false;+ },+ },+ ];+ },++ renderHTML({ node }) {+ return ['script', { type: 'application/ld+json' }, node.attrs.json as string];+ },++ addNodeView() {+ return ({ node }) => {+ const dom = document.createElement('div');+ dom.contentEditable = 'false';+ dom.dataset.jsonLd = '';+ dom.className =+ 'not-prose my-4 flex items-center gap-2 rounded-md border border-dashed bg-muted/40 px-3 py-2 font-mono text-xs text-muted-foreground';+ dom.textContent = `{ } ${this.options.label(jsonLdType(node.attrs.json as string))}`;+ return { dom };+ };+ },+});diff --git a/web/src/lib/content/sanitize-draft-html.test.ts b/web/src/lib/content/sanitize-draft-html.test.tsnew file mode 100644index 000000000..bd8520b98--- /dev/null+++ b/web/src/lib/content/sanitize-draft-html.test.ts@@ -0,0 +1,62 @@+import { describe, expect, it } from 'vitest';+import { sanitizeDraftHtml } from './sanitize-draft-html';++/**+ * What a save can't store, whoever sends it: the editor sends clean HTML,+ * a hand-made request may not.+ */+describe('sanitizeDraftHtml', () => {+ it('keeps what the editor writes', () => {+ const html =+ '<h1>Pipeline forecasting</h1><p><strong>Bold</strong> and <em>italic</em> ' ++ '<a href="https://gartner.com/x">Gartner</a></p><ul><li><p>One</p></li></ul>' ++ '<blockquote><p>Quote</p></blockquote>' ++ '<table><tbody><tr><th colspan="1" rowspan="1"><p>A</p></th></tr></tbody></table>' ++ '<p><img src="https://cdn.acme.com/a.png" alt="A" /></p>';+ expect(sanitizeDraftHtml(html)).toBe(html);+ });++ it('drops scripts, frames, styles, handlers and javascript: links', () => {+ expect(+ sanitizeDraftHtml(+ '<script>alert(1)</script><p style="color:red" class="x" onclick="x()">Hi ' ++ '<a href="javascript:alert(1)" onmouseover="x()">there</a></p>' ++ '<iframe src="https://evil.com"></iframe><style>p{}</style>' ++ '<img src="data:image/png;base64,AAAA"><img src="https://a.co/i.png" onerror="x()">' ++ '<a href="//evil.com">far</a>',+ ),+ ).toBe('<p>Hi <a>there</a></p><img src="https://a.co/i.png" /><a>far</a>');+ });++ it('turns H4–H6 into H3 and drops tracking parameters from links', () => {+ expect(+ sanitizeDraftHtml('<h5>Deep</h5><a href="https://acme.com/?utm_source=x&id=2">a</a>'),+ ).toBe('<h3>Deep</h3><a href="https://acme.com/?id=2">a</a>');+ });++ it('keeps schema JSON-LD, and nothing that tries to break out of it', () => {+ expect(+ sanitizeDraftHtml(+ '<script type="application/ld+json">{"@type":"Product","name":"</script><script>alert(1)</script>"}</script>' ++ '<script type="application/ld+json">{"@type":"FAQPage","x":"a<b"}</script>',+ ),+ ).toBe(+ // The first block ends at the first </script>, so its JSON is cut and+ // dropped; the script after it is removed; the second block is kept.+ '"}<script type="application/ld+json">{"@type":"FAQPage","x":"a\\u003cb"}</script>',+ );+ });++ it('keeps the schema and the text after it on a page with Turkish İ before it', () => {+ const html =+ '<h1>İzmir İçin İpuçları</h1><script type="application/ld+json">{"@type":"FAQPage"}</script><p>After the schema.</p>';+ expect(sanitizeDraftHtml(html)).toBe(html);+ });++ it('cleans a 500,000-character page built to backtrack within the save’s time', () => {+ const input = '<script type=application/ld+json '.repeat(15_000);+ const started = performance.now();+ sanitizeDraftHtml(input);+ expect(performance.now() - started).toBeLessThan(2000);+ });+});diff --git a/web/src/lib/content/sanitize-draft-html.ts b/web/src/lib/content/sanitize-draft-html.tsnew file mode 100644index 000000000..0936c60a2--- /dev/null+++ b/web/src/lib/content/sanitize-draft-html.ts@@ -0,0 +1,45 @@+import sanitizeHtml from 'sanitize-html';+import {+ ALLOWED_ATTRIBUTES,+ ALLOWED_TAGS,+ DEMOTED_HEADINGS,+ JSON_LD_SLOT,+ cleanHref,+ extractJsonLd,+ restoreJsonLd,+} from './html-allowlist';++/**+ * The server's check of a draft's HTML before it is saved (Content Editor+ * CE-005, spec §12: "The server re-checks the HTML before saving"). The+ * editor sends clean HTML already; this keeps a save made without it to the+ * same allow-list, so the save action never stores a script, an event+ * handler or a javascript: link. Schema JSON-LD stays, written again from+ * its JSON.+ *+ * Stored HTML is still untrusted: a member can also write+ * content_drafts.body_html straight through PostgREST (the update policy in+ * 00204), past this check. Whatever reads it parses it through the editor's+ * schema or cleans it again; nothing renders or hands it on raw.+ */+export function sanitizeDraftHtml(html: string): string {+ const { html: withSlots, blocks } = extractJsonLd(html);+ const clean = sanitizeHtml(withSlots, {+ allowedTags: [...ALLOWED_TAGS, JSON_LD_SLOT],+ allowedAttributes: { ...ALLOWED_ATTRIBUTES, [JSON_LD_SLOT]: ['data-i'] },+ allowedSchemes: ['http', 'https', 'mailto'],+ allowedSchemesByTag: { img: ['http', 'https'] },+ allowProtocolRelative: false,+ transformTags: {+ ...Object.fromEntries(DEMOTED_HEADINGS.map((tag) => [tag, 'h3'])),+ a: (tagName, attribs) => {+ const href = cleanHref(attribs.href ?? '');+ const kept: sanitizeHtml.Attributes = href ? { href } : {};+ return { tagName, attribs: kept };+ },+ },+ // An image whose address was refused is no image at all.+ exclusiveFilter: (frame) => frame.tag === 'img' && !frame.attribs.src,+ });+ return restoreJsonLd(clean, blocks);+}diff --git a/web/yarn.lock b/web/yarn.lockindex 1a9da1cdc..8f1a68773 100644--- a/web/yarn.lock+++ b/web/yarn.lock@@ -2505,6 +2505,13 @@dependencies:csstype "^3.2.2"+"@types/[email protected]":+ version "2.16.0"+ resolved "https://registry.yarnpkg.com/@types/sanitize-html/-/sanitize-html-2.16.0.tgz#860d72c1ba8a5d044946f37559cc359c0a13b24e"+ integrity sha512-l6rX1MUXje5ztPT0cAFtUayXF06DqPhRyfVXareEN5gGCFaP/iwsxIyKODr9XDhfxPpN6vXUFNfo5kZMXCxBtw==+ dependencies:+ htmlparser2 "^8.0.0"+"@types/statuses@^2.0.6":version "2.0.6"resolved "https://registry.npmjs.org/@types/statuses/-/statuses-2.0.6.tgz"@@ -3588,7 +3595,7 @@ deep-is@^0.1.3:resolved "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz"integrity sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==-deepmerge@^4.3.1:+deepmerge@^4.2.2, deepmerge@^4.3.1:version "4.3.1"resolved "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz"integrity sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==@@ -3668,6 +3675,34 @@ doctrine@^2.1.0:dependencies:esutils "^2.0.2"+dom-serializer@^2.0.0:+ version "2.0.0"+ resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-2.0.0.tgz#e41b802e1eedf9f6cae183ce5e622d789d7d8e53"+ integrity sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==+ dependencies:+ domelementtype "^2.3.0"+ domhandler "^5.0.2"+ entities "^4.2.0"++domelementtype@^2.3.0:+ version "2.3.0"+ resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-2.3.0.tgz#5c45e8e869952626331d7aab326d01daf65d589d"+ integrity sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==++domhandler@^5.0.2, domhandler@^5.0.3:+ version "5.0.3"+ resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-5.0.3.tgz#cc385f7f751f1d1fc650c21374804254538c7d31"+ integrity sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==+ dependencies:+ domelementtype "^2.3.0"++ version "3.4.16"+ resolved "https://registry.yarnpkg.com/dompurify/-/dompurify-3.4.16.tgz#c51b3709ea15ee9abb07e804f2ef30ea9b953128"+ integrity sha512-sqo+pNp3qRhCIpbgRi1y8Tgk27Bo2Ry7w0dC1NBeNTdZChWjz9Xb/KOoZbRP/R6pQZ80Qw8YhXw13hWWBbMRnQ==+ optionalDependencies:+ "@types/trusted-types" "^2.0.7"+dompurify@^3.3.2:version "3.4.3"resolved "https://registry.yarnpkg.com/dompurify/-/dompurify-3.4.3.tgz#3ef336e7a757c3bf1efbd3781afb149a3ae5cfa4"@@ -3675,6 +3710,15 @@ dompurify@^3.3.2:optionalDependencies:"@types/trusted-types" "^2.0.7"+domutils@^3.0.1:+ version "3.2.2"+ resolved "https://registry.yarnpkg.com/domutils/-/domutils-3.2.2.tgz#edbfe2b668b0c1d97c24baf0f1062b132221bc78"+ integrity sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw==+ dependencies:+ dom-serializer "^2.0.0"+ domelementtype "^2.3.0"+ domhandler "^5.0.3"+dotenv@^17.2.1:version "17.3.1"resolved "https://registry.npmjs.org/dotenv/-/dotenv-17.3.1.tgz"@@ -3742,6 +3786,11 @@ enhanced-resolve@^5.19.0:graceful-fs "^4.2.4"tapable "^2.3.0"+entities@^4.2.0, entities@^4.4.0:+ version "4.5.0"+ resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48"+ integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==+entities@^6.0.0:version "6.0.1"resolved "https://registry.yarnpkg.com/entities/-/entities-6.0.1.tgz#c28c34a43379ca7f61d074130b2f5f7020a30694"@@ -4785,6 +4834,16 @@ html-void-elements@^3.0.0:resolved "https://registry.yarnpkg.com/html-void-elements/-/html-void-elements-3.0.0.tgz#fc9dbd84af9e747249034d4d62602def6517f1d7"integrity sha512-bEqo66MRXsUGxWHV5IP0PUiAWwoEjba4VCzg0LjFJBpchPaTfyfCKTG6bc5F8ucKec3q5y6qOdGyYTSBEvhCrg==+htmlparser2@^8.0.0:+ version "8.0.2"+ resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-8.0.2.tgz#f002151705b383e62433b5cf466f5b716edaec21"+ integrity sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==+ dependencies:+ domelementtype "^2.3.0"+ domhandler "^5.0.3"+ domutils "^3.0.1"+ entities "^4.4.0"+http-errors@^2.0.0, http-errors@^2.0.1, http-errors@~2.0.1:version "2.0.1"resolved "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz"@@ -5124,6 +5183,11 @@ is-plain-obj@^4.0.0, is-plain-obj@^4.1.0:resolved "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-4.1.0.tgz"integrity sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==+is-plain-object@^5.0.0:+ version "5.1.0"+ resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-5.1.0.tgz#22ed7b5e856bb57966348e5b9468dada42be87f4"+ integrity sha512-bUi/yjmtKYcRVUtWRGr0UA6xEFh2I6zWUwMrUXB3s7bmYCaZ8a+0ZsTRkrawh/mzlSD1Y0Ph8bp/U+TvBpWDNw==+is-potential-custom-element-name@^1.0.1:version "1.0.1"resolved "https://registry.yarnpkg.com/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz#171ed6f19e3ac554394edf78caa05784a45bebb5"@@ -6184,7 +6248,7 @@ nanoid@^3.3.11:resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.3.11.tgz"integrity sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w==-nanoid@^3.3.16:+nanoid@^3.3.16, nanoid@^3.3.20:version "3.3.20"resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.20.tgz#dfd4444d85aa10111ef66ba85ed72e08ba0855d8"integrity sha512-uKdg2G3GNCKQn9byYOpxbGqrT2fGO5KRt5J/8b3pok8rT6qxGWF6hxMyJiEYtAf+FVyYuD9hRaDqX5uPFYJ4ZQ==@@ -6551,6 +6615,11 @@ parse-ms@^4.0.0:resolved "https://registry.npmjs.org/parse-ms/-/parse-ms-4.0.0.tgz"integrity sha512-TXfryirbmq34y8QBwgqCVLi+8oA3oWx2eAnSn62ITyEhEYaWRlVZ2DvMM9eZbMs/RfxPu/PK/aBLyGj4IrqMHw==+parse-srcset@^1.0.2:+ version "1.0.2"+ resolved "https://registry.yarnpkg.com/parse-srcset/-/parse-srcset-1.0.2.tgz#f2bd221f6cc970a938d88556abc589caaaa2bde1"+ integrity sha512-/2qh0lav6CmI15FzA3i/2Bzk2zCgQhGMkvhOhKNcBVQ1ldgpbfiNTVslmooUmWJcADi1f1kIeynbDRVzNlfR6Q==+parse-svg-path@^0.1.2:version "0.1.2"resolved "https://registry.yarnpkg.com/parse-svg-path/-/parse-svg-path-0.1.2.tgz#7a7ec0d1eb06fa5325c7d3e009b859a09b5d49eb"@@ -6685,6 +6754,15 @@ [email protected]:picocolors "^1.1.1"source-map-js "^1.2.1"+postcss@^8.3.11:+ version "8.5.31"+ resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.5.31.tgz#d44731fa02f8ae8371629ec7220e2ae0dbdf23ec"+ integrity sha512-YUbWYCgErwnMAVkjUnYa+KlMBzGpHwiIt/DE7f0G33AuX2LH9UcTk6KgWxd3zRU+pN3u/C+Xl3EnMLM4Hea7Sg==+ dependencies:+ nanoid "^3.3.20"+ picocolors "^1.1.1"+ source-map-js "^1.2.2"+postcss@^8.5.6:version "8.5.8"resolved "https://registry.npmjs.org/postcss/-/postcss-8.5.8.tgz"@@ -7310,6 +7388,18 @@ safe-regex-test@^1.0.3, safe-regex-test@^1.1.0:resolved "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz"integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==+ version "2.17.0"+ resolved "https://registry.yarnpkg.com/sanitize-html/-/sanitize-html-2.17.0.tgz#a8f66420a6be981d8fe412e3397cc753782598e4"+ integrity sha512-dLAADUSS8rBwhaevT12yCezvioCA+bmUTPH/u57xKPT8d++voeYE6HeluA/bPbQ15TwDBG2ii+QZIEmYx8VdxA==+ dependencies:+ deepmerge "^4.2.2"+ escape-string-regexp "^4.0.0"+ htmlparser2 "^8.0.0"+ is-plain-object "^5.0.0"+ parse-srcset "^1.0.2"+ postcss "^8.3.11"+saxen@^11.1.0:version "11.2.0"resolved "https://registry.yarnpkg.com/saxen/-/saxen-11.2.0.tgz#fec7492a2606c01dda58b3426be696de36a4952c"@@ -7582,6 +7672,11 @@ source-map-js@^1.2.1:resolved "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz"integrity sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==+source-map-js@^1.2.2:+ version "1.2.2"+ resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.2.tgz#79fb88d97535a21cb27162cb05d211b67acea4ab"+ integrity sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw==+source-map@~0.6.1:version "0.6.1"resolved "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz"