Olddevs UI4.14.0
ReferenceRichTextView

Schema and security

Schema and security

The Schema is the only source of the Editor's allowed markup: the Editor drops everything that is not in it, both on load and on paste. RichTextView does not check the Schema: it shows HTML without parsing it.

The allowlist of tags and attributes (the table mirrors src/editor/core/schema.ts and is checked by a test; the list grows with the set of Blocks and Marks):

WhatAllowed
Blocksp, h1, h2, h3, h4, ul, ol, li, blockquote, hr, pre, table, tbody, tr, th, td; label, input, span — only inside a task list item; div — there too, plus the Column layout and its columns (a Column layout only at the top level, a column only directly inside it) and the Wrapper (at the top level and directly in a column, not in another Wrapper), and video (the same div with the value "video" of data-block and an ordinary link inside; there is no player in the Document, an iframe is always stripped); details — the Details Block, with summary and a content div inside it (both only directly in details; outside it the tags are removed); figure — an image, with img and an optional figcaption inside (they do not exist outside figure); formulas — an inline span and a block div with the attributes "data-type" and "data-latex", both elements empty; footnotes — the list at the end of the Document is section with the value "footnotes" of data-block, only at the top level, holding an ol of li with a p and the backlink; the reference is the sup and a that are in Marks
Marksstrong, em, u, s, code, sup, sub, mark (only with a color), br, a, time (a date mention)
Attributesol[start] — only when numbering does not start at 1; ul[data-type], li[data-type], li[data-checked] — task list; input[type], input[checked] — its checkbox; code[class] — only language-<name> with a canonical name from the Editor code language table. A plain text block has no class; span[data-color], mark[data-color] — text color and highlight, only names from that tag's palette: text colors on span, highlights on mark; span[style], mark[style] — the author's custom color, only "color:#rrggbb" and "background-color:#rrggbb", and not together with "data-color"; p[data-align], h1[data-align], h2[data-align], h3[data-align], h4[data-align] — Block alignment, center or right; th[colspan], td[colspan] — horizontal merge, an integer from 2 to 20; th[rowspan], td[rowspan] — vertical, from 2 to 50; th[data-align], td[data-align] — column alignment, center or right, on each of its cells; a[href] — http:, https: and mailto: by default, plus the footnote anchors below. Pass { allowRelativeLinks: true } to sanitizeRichText to keep relative paths and fragments; a[rel] — noopener noreferrer on ordinary links; img[src] — only absolute "http" or "https", no "data:" or "blob:"; img[alt] — always, empty for an image without a description; img[data-width] — 25, 33, 50, 75 or 100; figure[data-align] — image alignment, "left" or "right", the text wraps around the image (the centre has no attribute); div[data-block] — the Column layout ("layout"), its column ("column"), the Wrapper ("wrapper") and video ("video"); div[data-layout] — the Column layout variant, column shares in percent: "100", "50-50", "33-33-33", "25-25-25-25", "60-40" or "40-60"; div[data-tone] — the Callout tone, only on it: "neutral", "accent", "success", "warning" or "danger" (no tone, no attribute); div[data-type] — the content wrapper of a Details Block ("detailsContent", only directly in details) and a block formula ("block-math"); span[data-type] — an inline formula, only the value "inline-math"; span[data-latex], div[data-latex] — the LaTeX source of a formula, only next to its "data-type", and the only attribute value where "<" is allowed; div[data-provider] — the service of a video Block, only on it: "youtube" or "twitch"; div[data-src] — the link to a video of that service in canonical form, only on a video Block and only "https": on www.youtube.com (a video or a playlist) or on www.twitch.tv and clips.twitch.tv (a recording, a channel, a clip); details[open] — an open Block, only an empty "open"; sup[data-footnote-ref] — a footnote reference, the key of its footnote: eight lowercase letters and digits; a[id], a[role] — only on the anchor inside the reference ("fnref-" and the key, "doc-noteref") and on the backlink ("doc-backlink"); li[id] — "fn-" and the key, only on a footnote in the list; section[data-block], section[role], section[aria-label] — the footnote list: "footnotes", "doc-endnotes" and its name; the Schema retains only these footnote id values. A footnote reference links to #fn-<key> and its backlink links to #fnref-<key> with the same key
Datestime[datetime] — a date mention, only on time: exactly YYYY-MM-DD or YYYY-MM-DDTHH:MM±HH:MM and a real calendar date; the element holds only the author's text
Languagespan[lang] — the language of a fragment: any BCP 47 tag (en, pt-BR, zh-Hant-TW), written in its canonical case, and only on a plain span
Columnscolgroup, col — column widths of a table: one colgroup, the first child of a table, with 2 to 20 col directly in it. A colgroup that breaks the rule of the next row is removed whole, and the table is sized by its content
Sharescol[data-width] — a column's share of the table's width in percent: a multiple of 5 from 5 to 95, and the shares of one colgroup add up to 100

Tiptap writes a task list item like this: <li data-type="taskItem" data-checked="true"><label><input type="checkbox" checked><span></span></label><div><p>…</p></div></li>. sanitizeRichText lets input, label, span and div through only inside such an item, and input only with type="checkbox".

The Editor writes a table like this: <table><tbody><tr><th><p>…</p></th></tr><tr><td colspan="2" data-align="center"><p>…</p></td></tr></tbody></table>. The header row is the first row made of th, and a cell has only three attributes: colspan and rowspan (merging, an integer from 2 to RICH_TEXT_SCHEMA.tableSpans: 20 horizontally and 50 vertically; 1 is not written) and data-align (column alignment, center or right, on every cell of the column). A row fully covered by a merge from above stays an empty <tr></tr>. A table whose author set the column widths also has a colgroup before the body: <table><colgroup><col data-width="35"><col data-width="65"></colgroup><tbody>…, one col per column, each a share of the table's width in percent (a multiple of 5 from 5 to 95, summing to 100: RICH_TEXT_SCHEMA.tableColumnShares); there is no style. thead, tfoot and caption are not in the Schema, and by default DOMPurify removes the contents of thead along with the tag: the header of a third-party table written in thead disappears. The Editor does not write thead, so for its HTML this is no loss. tbody, tr, th and td outside a table are dropped by the HTML parser before sanitizing (the text stays), style and colwidth are not allowed through, and colspan and rowspan are let through only by the sanitizeRichText hook: on th and td, and only as digits within the Schema's limits; the bare config does not know them at all. The hook also keeps a colgroup only as the first one directly in a table, with 2 to 20 col, each with a valid share, adding up to 100; anything else, a style on a col included, removes the whole colgroup and leaves the table sized by its content (the bare config lets the tags and data-width through unchecked: they are harmless, and the Editor reads such a list as no widths). Without such a check, colspan="10000" in third-party HTML would expand into 10,000 empty cells for everyone who opens the Document.

There are no other classes, style (except the custom color, see below), id, role or aria-* (except the footnote markup, see "Footnotes") or data-* (except twelve Schema names: data-type of the task list, of the content wrapper of a Details Block and of formulas, the data-latex of a formula, data-checked of the task list, data-color for color, data-align for alignment, data-width for image width, data-block and data-layout of the Column layout, data-tone of the Wrapper, data-provider and data-src of the video block, and data-footnote-ref of a footnote reference) in the output HTML. An h1 appears in a Document only if the Editor has the heading1 Tool (on by default in full); a screen that puts an H1 above the Editor turns this Tool off.

An image is written by the Editor like this: <figure><img src="https://…" alt="…" data-width="50"><figcaption>…</figcaption></figure>. figcaption is present only when the caption is not empty, data-width only on an image with a set width (25, 33, 50, 75 or 100 percent of the width of its container, RICH_TEXT_SCHEMA.imageWidths; without it, the image keeps its own size, no wider than the Document), data-align="left" or "right" on the figure only on an image the text wraps around (RICH_TEXT_SCHEMA.imageAlignments; the centre has no attribute). The View draws the wrapping with the same styles as the Editor, from 30 rem of its own width: in a narrower container the image stays on a line of its own, in the centre, and alt is always present and empty for an image without a description. The URL must be an absolute http or https URL (RICH_TEXT_SCHEMA.imageProtocols): the Editor never writes data: (base64) or blob:, and an image never has style, width, height or class — neither on img nor on figure. The Editor does not output a bare <img> outside figure; sanitizeRichText removes it.

Color and alignment are names, not CSS values. data-color accepts only the names of the Editor's palette, and there are two palettes, one per tag. On span (text color): accent, success, warning, danger, lime, muted (RICH_TEXT_SCHEMA.textColors); on mark (highlight): gray, brown, orange, yellow, green, blue, purple, pink, red (RICH_TEXT_SCHEMA.markerColors). The Editor menu offers the bright yellow, green and pink; the other six stay readable and are still rendered with their previous values. A name from another tag's palette is not allowed on a tag. Documents saved before the highlight palette contain six old names on mark; they are converted (RICH_TEXT_SCHEMA.legacyMarkerColors, the table is in the Editor documentation): sanitizeRichText does this with a hook, and RichTextView itself renders an old name in the color it is converted to. data-align is center or right (the Editor does not write left and reads it as the absence of the attribute; RICH_TEXT_SCHEMA.alignments). The theme supplies the concrete colors, so the same saved text stays legible in dark, light and dim: RichTextView and the Editor style data-color with the same classes (prose.ts). DOMPurify does not check attribute values — the sanitizeRichText hooks do.

A fragment's language is lang, not a name from a list. The Editor's language Tool writes <span lang="en">; sanitizeRichText keeps span[lang] with any tag of the BCP 47 shape (en, pt-BR, zh-Hant-TW; the case is brought to en-US) and removes a value that is not a tag, such as x-none or an empty string, with the wrapper: the text stays. The server needs no Options of the screen for this. RichTextView draws nothing for the mark (the dotted underline is the Editor's alone): a screen reader reads the fragment by its language, and the browser spell-checks and hyphenates it by it (WCAG 3.1.2). The mark is the outermost one: <span lang><span data-color><mark>….

The order of Marks on a highlight. The Editor writes the highlight as the outermost Mark after the text color (<span data-color><mark><sup>…), so the highlight does not shift on superscripts and does not break on a link. RichTextView does not rebuild the HTML: a Document saved earlier as <sup><mark>…</mark></sup> is shown in the old order, and the part of the highlight on the superscript rises with it. The order is corrected when the Document is opened and saved in the Editor.

A custom color is the only exception to "there is no style". Besides the palette colors, the author can pick any color in ColorPicker ("Custom color…" in the Editor's color menu). Such a color has no name, and HTML stores its value in style, so it is written into the Document strictly like this: <span style="color:#ff0000"> for text and <mark style="background-color:#ffee00"> for a highlight. The rule is narrow and is recorded in the Schema (RICH_TEXT_SCHEMA.customColor.styles, regular expressions as strings): lowercase letters, exactly six digits, no spaces and no ;, one declaration per tag, and never together with data-color on the same element. The Editor drops any other style (#f00, rgb(…), red, a second declaration next to it, style on another tag) on load and paste, and sanitizeRichText strips it the same way: the hook checks the tag and the value against the same expressions. The bare richTextSanitizeConfig() has no style at all: DOMPurify's ALLOWED_ATTR is shared by all tags, and without a hook it would let any CSS through.

The author's color does not change with the theme. Palette colors are tokens, and one saved text stays legible in dark, light and dim. #ff0000 is a value: it is the same in every theme, and a color-and-background pair may lose contrast where the author never saw it. The Editor warns about this on selection (4.5:1 contrast with the current theme's background; for a highlight, the body text on it is checked), but it does not forbid it and does not recompute the color when the theme changes. If a Document is shown in several themes, use the palette.

The Editor writes a link like <a href="https://…" rel="noopener noreferrer">…</a>. It accepts http:, https: and mailto: by default. allowRelativeLinks also admits relative paths and fragments, which resolve against the page that displays the Document. The page determines how links open.

Sanitization on the consumer's server

Client-side sanitizing does not protect against XSS: a request that bypasses the interface can carry anything. Sanitize HTML on the server on save (and ideally on output too, if the database holds old records). DOMPurify is not among the kit's dependencies — you install it yourself, and the kit provides the Schema's policy through the sanitizeRichText helper:

npm install dompurify jsdom   # or isomorphic-dompurify
import { JSDOM } from "jsdom";
import createDOMPurify from "dompurify";
import { sanitizeRichText } from "@olddevs/ui/editor/schema";

const DOMPurify = createDOMPurify(new JSDOM("").window);

export function sanitizeDocument(html: string): string {
  return sanitizeRichText(html, DOMPurify);
}

sanitizeRichText(html, purify) takes a DOMPurify instance as an argument — the kit does not import it, the instance type is structural (RichTextPurifier), and dompurify with jsdom or isomorphic-dompurify fit without casts. For the duration of one call, the helper installs the Schema hooks on the instance and removes them when the call returns: you use one instance for everything (comments, emails), so the Editor's rules do not fire when other code sanitizes with that instance, and your hooks stay in place. The Editor's markup comes through byte for byte; the hooks strip everything else: the text from a stripped tag stays, as in the Editor.

Use sanitizeRichText(html, DOMPurify, { allowRelativeLinks: true }) to keep the relative links an opted-in Editor saves. Include tools in the same options object when the Document uses custom Tools.

The hooks check the following:

  • label, input, span and div — only the markup of a task list item; input — only type="checkbox"; ul[data-type], li[data-type], li[data-checked] — only the task list values;
  • ol[start] — only on ol;
  • code[class] — only the canonical language-<name> from RICH_TEXT_SCHEMA.codeLanguages: an alias (language-py), an unknown language, a second class and a class on another tag are stripped, and so are hljs-* tokens;
  • span[data-color] and mark[data-color] — only names from that tag's palette; old highlight names are converted (legacyMarkerColors); a mark without a color is stripped;
  • style — only color:#rrggbb on span and background-color:#rrggbb on mark (RICH_TEXT_SCHEMA.customColor.styles), and never together with data-color;
  • data-align — center or right on p, h1–h4, th, td;
  • a formula — span[data-type="inline-math"] or div[data-type="block-math"] with data-latex: the pair is open only on these tags, the element is empty (rendered KaTeX markup is removed), and without data-latex it loses its wrapper; < is allowed in data-latex, and it is the only Schema attribute value that may contain <; the pairs ]>, -->, --!> and </ get a space in it before DOMPurify's check (it would cut the whole attribute otherwise);
  • a date mention — time[datetime] with a value that is exactly YYYY-MM-DD or YYYY-MM-DDTHH:MM±HH:MM and a real calendar date (2026-02-29, month 13, hour 24, Z and seconds are not); the element keeps only text, and a time in any other form loses its wrapper and keeps its words;
  • colspan and rowspan — only on th and td, an integer no greater than RICH_TEXT_SCHEMA.tableSpans;
  • column widths — colgroup only as the first child of a table, with 2 to 20 col directly in it, each with data-width, a multiple of 5 from 5 to 95, and the shares add up to 100: otherwise the whole colgroup is stripped. data-width on a col has this rule, on an img another (a preset), and on any other tag it is stripped;
  • a link — href follows the same URL policy as the Editor, rel always noopener noreferrer, an unexpected target is stripped, a link without a valid URL becomes text;
  • an image — img with an absolute http/https directly inside figure (no data: or blob:), data-width is a preset, alt is always present; an img outside figure is removed, and a figcaption outside figure loses its tag.

Checked with the real DOMPurify 3.4.16 on jsdom in tests/editor-sanitize.test.tsx: a Document with every Block and Mark of the Editor comes through unchanged, while forged input is stripped — Tailwind classes (class="fixed inset-0"), style on p, a data: image, a javascript: link, a name from another palette, a label outside an item, colspan="10000" and others.

Custom Tools: the third argument

If the Editor works with custom Tools (RichTextEditor, "Custom Tool"), the server passes the same tools array to sanitizeRichText. The description of a custom Tool is data, and the module with it is imported on the server: sanitizing reads only the Schema rules (blocks, marks) and does not touch the Extensions. To keep Tiptap out of the server graph too, keep the description in a module with no extension imports — the Extensions are then supplied by a Loader (RichTextEditor, "Extensions Loader") or built next to the Editor.

import { sanitizeRichText } from "@olddevs/ui/editor/schema";
import { EDITOR_TOOLS } from "./editor-tools"; // the same array as in the Editor's `tools`

export function sanitizeDocument(html: string): string {
  return sanitizeRichText(html, DOMPurify, EDITOR_TOOLS);
}
  • A custom Block comes through as div[data-block="<name>"], an inline Atom as span[data-block="<name>"], a custom Mark as span[data-mark="<name>"]. A Block of the wrong kind (an Atom in a div, a block-level Block in a span) and an unknown name lose their tag; the text stays.
  • Custom attributes data-<name> — only on their own element and only with a value that follows the rule: from a list, matching a pattern, or a URL that follows the link policy (absolute http, https, mailto). A value with markup (<) is never allowed through. An invalid value is stripped together with the attribute, and the element itself stays — the Editor does the same.
  • Anything extra on a custom Block's element — class, style, data-color, handlers, an attribute of another Block — are stripped.
  • The top slot — only the Document's top level: a nested Block (in a quote, a Wrapper, another Block) loses its tag, like a Column layout.
  • Names of the kit's Tools do not narrow the allowlist. ["bold", acmeCard] sanitizes the same as [...RICH_TEXT_PRESETS.full, acmeCard]: screens with different sets write one Document, and the server must not strip a table that a full screen wrote. The server ignores rules with a name that lacks a Consumer prefix: the Editor rejects such a set with a field error, and on the server it must not create a way around the kit's rules.
  • Without the third argument sanitizing is the same as before custom Tools: custom Blocks are stripped like any other third-party HTML.

richTextSchema(tools) returns the same rules as data — for your own sanitizer or a check on the database side: customBlocks (kind, slot, attributes — with defaults filled in) and customMarks. There are no Block traits (container, turnInto, plainAtom) in it: that is the Editor's behavior, not what arrives in HTML. Checked in tests/editor-sanitize-tools.test.tsx: an Editor Document with custom Blocks, Atom and Mark comes through sanitizing byte for byte, and forged input is stripped the same way the Editor strips it.

richTextSanitizeConfig() is restrictive by default. This is a config for anyone who passes it to DOMPurify directly, without hooks. DOMPurify's shared ALLOWED_ATTR (one list for all tags, and it does not check values) includes only what is harmless on any tag and with any value: data-type, data-checked, data-color, data-align, data-width, data-block, data-layout, data-tone, data-provider, data-latex, open, datetime, start, alt, rel and href. style, class, src, data-src, colspan, rowspan, type, checked, lang, id, role, aria-label, data-footnote-ref and the input and img tags are allowed only by sanitizeRichText, after checking the tag and the value: without a check they would let through any CSS and class, a base64 image, 10,000 empty cells and any id on any tag. The bare config is therefore safe, but it loses part of the Editor's markup — the author's color, the code block language, cell merging, checkboxes and images. ALLOW_DATA_ATTR and ALLOW_ARIA_ATTR are off: by default DOMPurify lets any data-* and aria-* through. Every call returns new arrays.

An allowlist of your own. RICH_TEXT_SCHEMA.attributes is data: it names the attributes, but not the tag, the place and the shape of value each one is allowed in. That rule lives in the hooks of sanitizeRichText, so copying the names into another sanitizer's allowlist is safe only for the attributes that are harmless whatever stands in them. Sanitize with sanitizeRichText (or richTextSanitizeConfig() together with its hooks) whenever you can. If you cannot:

  • Do not copy id, role, aria-label or data-footnote-ref as plain allowed attributes. They are the footnote markup, and plain they pass on every tag with any value: an author's id clobbers the page's named elements (DOM clobbering) or collides with its anchors, and role and aria-label rename any element for assistive technology. sanitizeRichText admits each one only in its exact shape (fn-<key>, fnref-<key>, doc-noteref, doc-endnotes, doc-backlink), on its own tag. Leave them out and the footnotes keep their text and lose the jumps.
  • span[lang] is better with the hook. It is inert, but a plain copy lets any tag and any string change the language a screen reader speaks in; the bare config leaves it out.
  • time[datetime], col[data-width], figure[data-align] and img[data-width] are safe as plain data. They carry no script, address or style, and the kit's styles select only their fixed values; any other value does nothing. The bare config has them. img itself and its src still need the hooks.

The bare config's ALLOWED_URI_REGEXP admits only http:, https: and mailto:. The hooked sanitizer also handles footnote anchors and the relative-link opt-in. DOMPurify checks every allowed attribute against this expression except the "URI-safe" ones, and a strict expression would strip start="3" or data-color="danger"; so the config declares all attributes except URLs safe (ADD_URI_SAFE_ATTR). An image URL cannot be restricted by the expression: DOMPurify lets data: in the src of an <img> through, bypassing ALLOWED_URI_REGEXP — so src is allowed only by the hook.

The sanitizing helpers and the Schema come from the separate lightweight entry point @olddevs/ui/editor/schema: it holds the Schema as data (RICH_TEXT_SCHEMA, richTextSchema), sanitizeRichText, richTextSanitizeConfig(), their types and the Text card helper markRange for describing a custom Tool. It has no React and no Tiptap, so a server without the Editor does not need to install the engine. @olddevs/ui/editor still exports the same names, but on a server without a bundler this entry point would load the whole Editor with its engine.

On this page