Repository navigation
feat(content): clean pasted content and edit a draft as HTML - #1259
Conversation
Content Editor CE-001 and CE-002. Content becomes a group in the sidebar, like Prompts: Opportunities keeps /dashboard/content, and the new Library (/dashboard/content/library) lists every page a person or the Content Agent writes or changes in Ansvisor, by status (Drafts, In review, Ready, Published), with its page type, method, author and AEO score. - Migration 00203: content_drafts with member RLS (select, insert, update; no delete yet). A guard trigger keeps author, created_by, brand and the scores server-side, so a member can't pass a draft off as the agent's. - Sidebar: each submenu remembers its own open state (Prompts keeps its key), and only the longest matching entry is current, so Opportunities isn't marked on the Library. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor CE-004, stacked on the Library (ansvisor#1256). A draft opens at /dashboard/content/editor/{id}: a header with its title, page type, method and status, whether it's saved, and Mark ready; the toolbar (H1 H2 H3, bold, italic, link, lists, quote, table with add row / add column / delete, image by address) with the length counter against the page type's target; and the page as rich text (TipTap 3.31.4, pinned). - Autosave after a pause in typing; saves never overlap, a failed one keeps the text and offers Retry, and leaving with unsaved text asks. The draft's title is its first H1. - saveContentDraft validates the body (500k chars) and title and says when RLS matched no row; markContentDraftReady moves draft or in review to ready only. - Links: http(s), mailto or a path on the brand's site; javascript:, //host and /\host are refused, in the dialog and when content loads. - The Library gets New content (a blank draft, until the Start screen), and its entry stays current while a draft is open in the editor. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor CE-005 and CE-020, stacked on the editor core (ansvisor#1258). - Paste: from Word, Google Docs, Notion or a web page, headings (H4-H6 as H3), paragraphs, bold, italic, lists, links, tables and quotes stay; fonts, colors, classes, inline styles, scripts, iframes and utm_*, gclid, fbclid on links go. Google Docs' styled bold and italic become strong and em first. Pasted images wait for attachments. Cmd/Ctrl + Shift + V pastes plain text. - </> HTML shows the page's HTML as a draft stores it, one block per line; Back to visual cleans it with the same allow-list and says how many unsafe tags it removed. Schema JSON-LD is kept: a block in the editor, written again from its JSON so it can't carry markup. - The server checks every save against the allow-list again (sanitize-html), so a request made without the editor can't store a script, a handler or a javascript: link. - dompurify 3.4.16 and sanitize-html 2.18.0, pinned. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@gkhngyk third Content Editor PR: paste cleaning (CE-005) and the HTML view (CE-020). It's stacked on #1258 → #1256, so please merge those first. One allow-list serves the browser (DOMPurify) and the server's check on every save (sanitize-html, in web/ since the save is a server action). Schema JSON-LD is kept, its JSON parsed and written again so it can't carry markup; the break-out attempts are in the tests. Both packages are pinned to versions at least two weeks old. Could you take a look? |
sanitize-html 2.18.0 needs Node 22.12 or later, and CI and the web Docker image run Node 20, so yarn install refused it. 2.17.0 has no engine limit and the same API; @types/sanitize-html 2.16.0 matches it (both on htmlparser2 8). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Links in a draft looked like the text around them (dark, barely underlined), so a link just added seemed not to be there. They're blue and underlined now. With the cursor in a link, the toolbar shows its address, which opens it in a new tab; Cmd/Ctrl + click on it in the page does too. A click alone still puts the cursor in it to edit. A path on the brand's own site is shown, not opened: it has no address here. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-html # Conflicts: # web/messages/en.json # web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx # web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx # web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
|
@gkhngyk CI fix (ab0bda2): sanitize-html 2.18.0 needs Node ≥ 22.12, and CI and the web Docker image run Node 20, so |
…rary # Conflicts: # supabase/schema.sql
ansvisor#1257 took 00203 (citation gap rollups) on main, so the content drafts table moves to the next free number. The file is otherwise unchanged; schema.sql is rebuilt. Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
left a comment
There was a problem hiding this comment.
A few fixes before merge:
-
The JSON-LD regex backtracks catastrophically, and the server runs it on raw input (
web/src/lib/content/html-allowlist.ts:114-128, called fromsanitize-draft-html.ts:20, i.e. on everysaveContentDraftatcontent-drafts.ts:135).<script\b[^>]*\btype…["']?[^>]*>has two[^>]*runs around\btype. On input with many<script type=application/ld+jsonand no>, the cost grows cubically. Measured in Node: about 2 s at 16.5k characters and about 16 s at 33k. The Zod cap is 500k characters, so one save request from any member can keep a server action busy until the function timeout, and they can repeat it. (Pasting the same text freezes the browser tab too, throughcleanHtml.) Replace the regex with a linear scan:indexOf('<script'), thenindexOf('>')for the end of the tag, test only that tag's slice fortype=application/ld+json, thenindexOf('</script'). Or let htmlparser2, already installed withsanitize-html, find the elements. Add a test that runs a pathological 500k input under a time budget. #1262'sdetectPageTypereusesJSON_LD_SCRIPT, so the fix covers it too. -
The server re-check is advisory, since
body_htmlcan be written without the action (supabase/migrations/00204_content_drafts.sql:120-138). The member update policy lets any member PATCHcontent_drafts.body_htmldirectly through PostgREST with their own session, so "a request made without the editor can't store a script" doesn't hold. Nothing renders the stored HTML raw today (every reader parses it through TipTap's schema), so this is fine for now. Either move draft writes behind the server (revokeinsert/update (body_html)fromauthenticatedand write with the admin client after the membership check), or say insanitize-draft-html.tsthat stored HTML is untrusted. That way later readers (Copy HTML, the API/MCP exports later in the stack) won't assume it's clean.
…unsaved text ready
- saveContentDraft takes the version the editor last saw (updated_at and
a SHA-256 of the page) and writes with .eq('updated_at', base). When
another write came first, the page tells which kind: the same page means
only other columns changed (Mark ready, the type), and the save goes over
them; another page comes back `conflict` and nothing is overwritten. The
header then says "Changed elsewhere · Reload" and autosave stops.
- Results are typed ({ ok: false, reason }) since Next.js hides server
action messages in production: a page over 500,000 characters says "Too
long to save" (and isn't sent at all), not "Couldn't save · Retry".
- Mark ready settles first: it waits for a save under way and isn't run
when the page can't be saved.
- Nit: the title limit comes from 00204.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-html # Conflicts: # web/src/lib/actions/content-drafts.test.ts # web/src/lib/actions/content-drafts.ts
…king regex The JSON-LD pattern had two [^>]* runs around \btype, so input with many unclosed `<script type=application/ld+json ` tags took cubic time (about 16 s at 33k characters), and the server runs it on every save. It is now a scan (indexOf for the tag, its end and </script>, the type tested on the tag alone), linear in the length; a 500,000-character page built to backtrack takes milliseconds. The paste cleaner uses the same scan. sanitize-draft-html.ts now says stored HTML is untrusted: a member can write body_html through PostgREST past this check, so readers parse or clean it again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@gkhngyk Review fixes for #1259 are in c997d1b (merged up the stack):
|
gkhngyk
left a comment
There was a problem hiding this comment.
The scan is linear now, but it introduces one fix before merge:
- I timed
jsonLdScripts/extractKeptBlocksin Node on 500,000-character adversarial inputs: unclosed tags, scripts without an end tag,<scriptwith no>, one huge tag full oftype, whitespace runs andtype=runs,</scriptwithout>,<scripts. Each finished in 0.2 to 22 ms. Four times the input took 3.5 to 6 times as long. The old regex took 16 s at 33k characters. The "stored HTML is untrusted" note is fine as the resolution of the second item.
toLowerCase()changes the string's length, so the offsets taken fromlowerdon't line up withhtml(web/src/lib/content/html-allowlist.ts:125, used at:144and byextractKeptBlocks).'İ'.toLowerCase()is two UTF-16 units (i+ U+0307). Everyİbefore a JSON-LD script shiftsstart,endandtextby one. The JSON then fails to parse and the block is dropped, the tail of the tag is kept, and that many characters after the script are cut.- Run through the save path with sanitize-html, a page with three
İin its headings and text before a JSON-LD block is stored without the schema and without the paragraph after it. - This happens on every save, in Copy HTML (#1260), in paste and the HTML view, and in #1262's schema detection, for any page with a Turkish capital İ before its schema.
- Fix: lowercase only ASCII, which keeps the length and stays linear:
const lower = html.replace(/[A-Z]+/g, (s) => s.toLowerCase());(I checked it on the sample above and on the 500k inputs: 0.4 ms). Add a test withİbefore a JSON-LD block.
- Run through the save path with sanitize-html, a page with three
ansvisor#1256 and ansvisor#1258 are squash-merged on main; this keeps only this branch's own change on top. Claude-Session: https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A
gkhngyk
left a comment
There was a problem hiding this comment.
The fixes check out. Merging with the stack.
Summary
Content Editor (spec v2), PR 3 of the approved plan: CE-005 (paste cleaning) and CE-020 (HTML view).
One allow-list, used twice (
lib/content/html-allowlist.ts).on*, scripts, iframes, objects, forms.safeLinkHref(feat(content): write and save drafts in the content editor #1258), andutm_*,gclidandfbclidare dropped.Paste (CE-005, spec §9) via TipTap's
transformPastedHTML, withlib/content/clean-html.ts(DOMPurify, its own instance so the hooks stay local).<b id="docs-internal-guid-…">wrapper is unwrapped.font-weight:700,font-style:italic) become<strong>and<em>before styles go. Without this, bold and italic would be lost.mso-styles and<o:p>go; empty paragraphs are dropped.data-pm-slice, so pasting part of a list still works.HTML view (CE-020, spec §12).
</> HTMLin the toolbar shows the page's HTML as a draft stores it, one block per line. The editor's own table widths and link targets are left out.Schema JSON-LD is kept, as the spec asks.
<script type="application/ld+json">with the JSON parsed and written again,<,>and&escaped. Nothing but JSON can sit in the element.json-ld-node.ts), so it doesn't count toward the length.Server re-check.
saveContentDraftrunssanitizeDraftHtml(sanitize-html,lib/content/sanitize-draft-html.ts) on every save. A request made without the editor can't store a script, a handler or ajavascript:link. It's tested with break-out attempts: a</script>inside JSON-LD,//host,data:images.New dependencies (approved in the plan), pinned exact, each at least two weeks old:
dompurify3.4.16 (MPL-2.0 or Apache-2.0)sanitize-html2.17.0 (MIT). Not 2.18.0, which needs Node 22.12 or later, while CI and the web Docker image run Node 20.@types/sanitize-html2.16.0 (dev), matching 2.17.0; both use htmlparser2 8.sanitize-htmllives inweb/because the save is a Next.js server action.yarn.lockonly gains entries;nanoid's entry merges a key at the same version.Next. On the user's request, HTML blocks come next in their own PR: as many custom-code blocks as needed per draft, kept verbatim and never run in Ansvisor, with one-click Copy HTML per block and for the whole page.
Related issue
Content Editor spec v2 (CE-005, CE-020). Stacked on #1258 → #1256.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
?utm_source=x, a bulleted list, a table, an image) and paste it on an empty line.utm_source.<h4>Care</h4><p style="color:red">x <script>alert(1)</script></p><iframe src="https://example.com"></iframe>and click Back to visual.<script type="application/ld+json">{"@type":"Product","name":"X"}</script>in the HTML view: it shows as "Schema · Product (JSON-LD)".Tests:
html-allowlist.test.ts: links, JSON-LD.clean-html.test.ts: a Google Docs paste, a Word paste, unsafe tags, JSON-LD, images,data-pm-slice, source formatting.sanitize-draft-html.test.ts: the server check, including break-outs.content-drafts.test.ts: save sanitizes.page.test.tsx: HTML view apply, unedited return, JSON-LD block.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) (0 errors; the 6 warnings are in untouched files, as on main)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)yarn test(1,960 tests) andyarn buildpass.🤖 Generated with Claude Code