Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep allow-listed inline HTML when editing in the rich editor (#44487)

Renato Atilioc202c6429617
diff --git a/frontend/discourse-markdown-it/src/engine.js b/frontend/discourse-markdown-it/src/engine.js
index 9a81a426b03..86f954d9068 100644
--- a/frontend/discourse-markdown-it/src/engine.js
+++ b/frontend/discourse-markdown-it/src/engine.js
@@ -58,6 +58,7 @@ export default function makeEngine(
allowLister.allowListFeature(feature, info);
});
+ options.allowLister = allowLister;
options.sanitizer = options.discourse.sanitizer = options.discourse.sanitize
? (a) => sanitize(a, allowLister)
: (a) => a;
diff --git a/frontend/discourse-markdown-it/src/index.js b/frontend/discourse-markdown-it/src/index.js
index e2f9c21c429..1718db0c14c 100644
--- a/frontend/discourse-markdown-it/src/index.js
+++ b/frontend/discourse-markdown-it/src/index.js
@@ -50,6 +50,10 @@ export default class DiscourseMarkdownIt {
this.options = options;
}
+ get allowLister() {
+ return this.options.allowLister;
+ }
+
get linkify() {
return this.options.engine.linkify;
}
diff --git a/frontend/discourse/app/static/prosemirror/core/parser.js b/frontend/discourse/app/static/prosemirror/core/parser.js
index c6b5b07fc44..7d33de7321c 100644
--- a/frontend/discourse/app/static/prosemirror/core/parser.js
+++ b/frontend/discourse/app/static/prosemirror/core/parser.js
@@ -40,6 +40,19 @@ export default class Parser {
// Adding function parse handlers directly
Object.assign(parser.tokenHandlers, this.postParseTokens);
+ // An inline node left open by unbalanced HTML must not outlive its block.
+ // The length guard spares extensions that swap in a single-entry stack.
+ for (const [type, handler] of Object.entries(parser.tokenHandlers)) {
+ parser.tokenHandlers[type] = (state, token, tokens, i) => {
+ if (token.block) {
+ while (state.stack.length > 1 && state.top().type.isInline) {
+ state.closeNode();
+ }
+ }
+ return handler(state, token, tokens, i);
+ };
+ }
+
try {
return parser.parse(text);
} catch (e) {
diff --git a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
index 34870f44c55..012a3562191 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/html-inline.js
@@ -1,6 +1,8 @@
+import { isAllowListedAttr } from "pretty-text/sanitizer";
import { Fragment } from "prosemirror-model";
import { TextSelection } from "prosemirror-state";
import { i18n } from "discourse-i18n";
+import { getAllowLister } from "../lib/markdown-it";
const HTML_INLINE_MARKS = {
s: "strikethrough",
@@ -12,7 +14,7 @@ const HTML_INLINE_MARKS = {
code: "code",
};
-const ALLOWED_INLINE = [
+const INLINE_TAGS = [
"kbd",
"sup",
"sub",
@@ -28,30 +30,161 @@ const ALLOWED_INLINE = [
"span",
];
-const ALLOWED_TAG_ATTRS = {
- span: ["lang"],
- ruby: ["lang"],
- rb: ["lang"],
- rt: ["lang"],
-};
+const VOID_TAGS = [
+ "area",
+ "base",
+ "br",
+ "col",
+ "embed",
+ "hr",
+ "img",
+ "input",
+ "link",
+ "meta",
+ "param",
+ "source",
+ "track",
+ "wbr",
+];
+
+// Tags that open an HTML block at a line start (markdown-it's `html_blocks`
+// plus pre/script/style/textarea), so they are only kept inside table cells.
+const HTML_BLOCK_TAGS = [
+ "address",
+ "article",
+ "aside",
+ "base",
+ "basefont",
+ "blockquote",
+ "body",
+ "caption",
+ "center",
+ "col",
+ "colgroup",
+ "dd",
+ "details",
+ "dialog",
+ "dir",
+ "div",
+ "dl",
+ "dt",
+ "fieldset",
+ "figcaption",
+ "figure",
+ "footer",
+ "form",
+ "frame",
+ "frameset",
+ "h1",
+ "h2",
+ "h3",
+ "h4",
+ "h5",
+ "h6",
+ "head",
+ "header",
+ "hr",
+ "html",
+ "iframe",
+ "legend",
+ "li",
+ "link",
+ "main",
+ "menu",
+ "menuitem",
+ "nav",
+ "noframes",
+ "ol",
+ "optgroup",
+ "option",
+ "p",
+ "param",
+ "pre",
+ "script",
+ "search",
+ "section",
+ "style",
+ "summary",
+ "table",
+ "tbody",
+ "td",
+ "textarea",
+ "tfoot",
+ "th",
+ "thead",
+ "title",
+ "tr",
+ "track",
+ "ul",
+];
+
+// Allow-listed by the cook, but would load media or frames in the editor.
+const EMBEDDED_TAGS = ["audio", "iframe", "picture", "video"];
+
+function insideTableCell(state) {
+ return state.stack.some(({ type }) =>
+ ["cell", "header_cell"].includes(type.spec.tableRole)
+ );
+}
+
+function isAllowListedTag(state, tagName) {
+ if (
+ VOID_TAGS.includes(tagName) ||
+ EMBEDDED_TAGS.includes(tagName) ||
+ !getAllowLister().getAllowList().tagList[tagName]
+ ) {
+ return false;
+ }
+
+ return !HTML_BLOCK_TAGS.includes(tagName) || insideTableCell(state);
+}
function extractHtmlAttrs(element, tagName) {
- const allowed = ALLOWED_TAG_ATTRS[tagName];
- if (!allowed) {
+ const allowLister = getAllowLister();
+ const forTag = allowLister.getAllowList().attrList[tagName];
+ if (!forTag) {
return null;
}
+
const attrs = {};
let hasAny = false;
- for (const attr of allowed) {
- const value = element.getAttribute(attr);
- if (value != null) {
- attrs[attr] = value;
+ for (const { name, value } of element.attributes) {
+ if (
+ isAllowListedAttr(forTag, name, value) ||
+ allowLister.getCustom().some((fn) => fn(tagName, name, value))
+ ) {
+ attrs[name] = value;
hasAny = true;
}
}
return hasAny ? attrs : null;
}
+function closeHtmlInline(state, tagName) {
+ const { html_inline } = state.schema.nodes;
+ let depth = -1;
+
+ for (let i = state.stack.length - 1; i > 0; i--) {
+ const { type, attrs } = state.stack[i];
+ if (!type.isInline) {
+ break;
+ }
+ if (type === html_inline && attrs.tag === tagName) {
+ depth = i;
+ break;
+ }
+ }
+
+ if (depth === -1) {
+ return false;
+ }
+
+ while (state.stack.length > depth) {
+ state.closeNode();
+ }
+ return true;
+}
+
function serializeHtmlAttrs(htmlAttrs) {
if (!htmlAttrs) {
return "";
@@ -64,11 +197,18 @@ function serializeHtmlAttrs(htmlAttrs) {
.join("");
}
-const ALL_ALLOWED_TAGS = [...Object.keys(HTML_INLINE_MARKS), ...ALLOWED_INLINE];
+const INPUT_RULE_TAGS = [...Object.keys(HTML_INLINE_MARKS), ...INLINE_TAGS];
-// Publishing tools stamp `lang` on every span; an authored one arrives as a token.
-// A span kept as a node also loses the whitespace sitting at its edges.
-const DOM_PARSED_INLINE = ALLOWED_INLINE.filter((tag) => tag !== "span");
+// Pasted DOM is mostly cooked output, where the allow-list is far too wide.
+// Spans are left out: publishing tools stamp `lang` on every one of them.
+const DOM_PARSED_INLINE = [
+ ...INLINE_TAGS.filter((tag) => tag !== "span"),
+ "abbr",
+];
+
+// Only lists have no markdown form inside a cell. A selector starting with
+// `ul`/`ol` would stop ProseMirror from normalizing malformed nested lists.
+const DOM_PARSED_IN_CELL = ["li", "ul", "ol", "dl", "dt", "dd"];
const SMALL = "small";
@@ -241,25 +381,34 @@ const extension = {
defining: true,
content: "inline*",
attrs: { tag: {}, htmlAttrs: { default: null } },
- parseDOM: DOM_PARSED_INLINE.map((tag) => ({
- tag,
- getAttrs: (element) => ({
+ parseDOM: [
+ // Outranks the list rules so a pasted list stays inline HTML in a cell.
+ {
+ tag: DOM_PARSED_IN_CELL.join(","),
+ priority: 60,
+ context: "table_cell//|table_header_cell//",
+ getAttrs: (element) => {
+ const tag = element.tagName.toLowerCase();
+ return { tag, htmlAttrs: extractHtmlAttrs(element, tag) };
+ },
+ },
+ ...DOM_PARSED_INLINE.map((tag) => ({
tag,
- htmlAttrs: extractHtmlAttrs(element, tag),
- }),
- })),
- toDOM: (node) => {
- const domAttrs = node.attrs.htmlAttrs
- ? { ...node.attrs.htmlAttrs }
- : {};
- return [node.attrs.tag, domAttrs, 0];
- },
+ getAttrs: (element) => ({
+ tag,
+ htmlAttrs: extractHtmlAttrs(element, tag),
+ }),
+ })),
+ ],
+ toDOM: (node) => [node.attrs.tag, { ...node.attrs.htmlAttrs }, 0],
},
},
parse: {
html_inline: (state, token) => {
- const openMatch = token.content.match(/^<([a-z]+)(\s[^>]*)?\/?>$/i);
- const closeMatch = token.content.match(/^<\/([a-z]+)>$/i);
+ const openMatch = token.content.match(
+ /^<([a-z][a-z0-9]*)(\s[^>]*)?\/?>$/i
+ );
+ const closeMatch = token.content.match(/^<\/([a-z][a-z0-9]*)>$/i);
if (openMatch) {
const tagName = openMatch[1].toLowerCase();
@@ -270,6 +419,12 @@ const extension = {
return;
}
+ const markName = HTML_INLINE_MARKS[tagName];
+ if (markName) {
+ state.openMark(state.schema.marks[markName].create());
+ return;
+ }
+
if (hasAttributes) {
const parser = new DOMParser();
const doc = parser.parseFromString(token.content, "text/html");
@@ -299,7 +454,7 @@ const extension = {
return;
}
- if (ALLOWED_INLINE.includes(tagName)) {
+ if (isAllowListedTag(state, tagName)) {
const htmlAttrs = extractHtmlAttrs(element, tagName);
state.openNode(state.schema.nodes.html_inline, {
tag: tagName,
@@ -310,13 +465,7 @@ const extension = {
}
}
- const markName = HTML_INLINE_MARKS[tagName];
- if (markName) {
- state.openMark(state.schema.marks[markName].create());
- return;
- }
-
- if (ALLOWED_INLINE.includes(tagName)) {
+ if (isAllowListedTag(state, tagName)) {
state.openNode(state.schema.nodes.html_inline, {
tag: tagName,
});
@@ -328,6 +477,10 @@ const extension = {
if (closeMatch) {
const tagName = closeMatch[1].toLowerCase();
+ if (closeHtmlInline(state, tagName)) {
+ return;
+ }
+
if (tagName === "a") {
state.closeMark(state.schema.marks.link);
return;
@@ -336,11 +489,6 @@ const extension = {
const markName = HTML_INLINE_MARKS[tagName];
if (markName) {
state.closeMark(state.schema.marks[markName].create());
- return;
- }
-
- if (ALLOWED_INLINE.includes(tagName)) {
- state.closeNode();
}
}
},
@@ -354,7 +502,7 @@ const extension = {
},
},
inputRules: {
- match: new RegExp(`<(${ALL_ALLOWED_TAGS.join("|")})>$`, "i"),
+ match: new RegExp(`<(${INPUT_RULE_TAGS.join("|")})>$`, "i"),
handler: (state, match, start, end) => {
const tag = match[1];
diff --git a/frontend/discourse/app/static/prosemirror/lib/markdown-it.js b/frontend/discourse/app/static/prosemirror/lib/markdown-it.js
index 970f69ec9a1..44174523205 100644
--- a/frontend/discourse/app/static/prosemirror/lib/markdown-it.js
+++ b/frontend/discourse/app/static/prosemirror/lib/markdown-it.js
@@ -29,6 +29,8 @@ export const parse = (text) => getEngine().parse(text);
/** @returns {import("discourse/lib/link-matcher").LinkMatcher} */
export const getLinkify = () => getEngine().linkify;
+export const getAllowLister = () => getEngine().allowLister;
+
export const isWhiteSpace = (str, index) =>
!str || getEngine().options.engine.utils.isWhiteSpace(str.charCodeAt(index));
diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
index 4cf5cd2cc22..e0115df967b 100644
--- a/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/html-inline-test.js
@@ -1,6 +1,10 @@
+import { settled } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
-import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";
+import {
+ setupRichEditor,
+ testMarkdown,
+} from "discourse/tests/helpers/rich-editor-helper";
module(
"Integration | Component | prosemirror-editor - html-inline extension",
@@ -121,10 +125,85 @@ module(
"<p>Line1<br>Line2</p>",
"Line1\nLine2",
],
+ "list HTML inside a table cell": [
+ "| Column 1 | Column 2 |\n|---|---|\n| An HTML-built list | <ul><li>line 1</li><li>line 2</li></ul> |\n",
+ (assert) => {
+ assert.dom("td ul li").exists({ count: 2 });
+ assert.dom("td ul li:first-child").hasText("line 1");
+ },
+ "| Column 1 | Column 2 |\n|----|----|\n| An HTML-built list | <ul><li>line 1</li><li>line 2</li></ul> |\n\n",
+ ],
+ "unclosed inline HTML does not swallow the next cell": [
+ "| a | b |\n|---|---|\n| <ul><li>x | y |\n",
+ (assert) => {
+ assert.dom("tbody td").exists({ count: 2 });
+ assert.dom("tbody td:first-child ul li").hasText("x");
+ assert.dom("tbody td:last-child").hasText("y");
+ },
+ "| a | b |\n|----|----|\n| <ul><li>x</li></ul> | y |\n\n",
+ ],
+ "stray closing tag is ignored": ["a</li>b", "<p>ab</p>", "ab"],
+ "closing an outer tag closes the inner ones": [
+ "<kbd><sup>x</kbd> y",
+ "<p><kbd><sup>x</sup></kbd> y</p>",
+ "<kbd><sup>x</sup></kbd> y",
+ ],
+ "anchor without href keeps its name": [
+ 'see <a name="top">here</a>',
+ '<p>see <a name="top">here</a> </p>',
+ 'see <a name="top">here</a> ',
+ ],
+ "allow-listed tags and attributes inside a table round-trip": [
+ '| a |\n|---|\n| <ol start="3"><li>c</li></ol> <div data-x="1" onclick="y()">d</div> <abbr title="T">e</abbr> <h2>f</h2> |\n',
+ (assert) => {
+ assert.dom("td ol").hasAttribute("start", "3");
+ assert.dom("td div[data-x='1']").doesNotHaveAttribute("onclick");
+ assert.dom("td abbr").hasAttribute("title", "T");
+ assert.dom("td h2").hasText("f");
+ },
+ '| a |\n|----|\n| <ol start="3"><li>c</li></ol> <div data-x="1">d</div> <abbr title="T">e</abbr> <h2>f</h2> |\n\n',
+ ],
+ "mark tags keep being marks when they carry attributes": [
+ '<strong class="x">bold</strong> <code class="lang-js">c</code>',
+ "<p><strong>bold</strong> <code>c</code></p>",
+ "**bold** `c`",
+ ],
+ "tags the cook strips, embedded media and block-level tags outside a table are dropped":
+ [
+ 'a <blink>b</blink> <iframe src="https://example.com"></iframe> <div>c</div> <ul><li>d</li></ul> e',
+ "<p>a b c d e</p>",
+ "a b c d e",
+ ],
}).forEach(([name, [markdown, html, expectedMarkdown]]) => {
test(name, async function (assert) {
await testMarkdown(assert, markdown, html, expectedMarkdown);
});
});
+
+ test("a list pasted into a table cell stays inline HTML", async function (assert) {
+ const [editor] = await setupRichEditor(assert, "");
+
+ editor.view.pasteHTML(
+ "<table><tbody><tr><td><ul><li>a</li><li>b</li></ul></td></tr></tbody></table>"
+ );
+ await settled();
+
+ assert.dom(".ProseMirror table ul li").exists({ count: 2 });
+ assert.true(editor.value.includes("| <ul><li>a</li><li>b</li></ul>"));
+ });
+
+ test("pasted phrasing tags keep allow-listed attributes", async function (assert) {
+ const [editor] = await setupRichEditor(assert, "");
+
+ editor.view.pasteHTML(
+ 'x <abbr title="T">a</abbr> <kbd>k</kbd> <blink>b</blink>'
+ );
+ await settled();
+
+ assert.strictEqual(
+ editor.value,
+ 'x <abbr title="T">a</abbr> <kbd>k</kbd> b'
+ );
+ });
}
);
diff --git a/frontend/pretty-text/addon/sanitizer.js b/frontend/pretty-text/addon/sanitizer.js
index d3fdb08e482..a0d8c119023 100644
--- a/frontend/pretty-text/addon/sanitizer.js
+++ b/frontend/pretty-text/addon/sanitizer.js
@@ -89,6 +89,17 @@ function testDataAttribute(forTag, name, value) {
});
}
+export function isAllowListedAttr(forTag, name, value) {
+ const forAttr = forTag[name];
+
+ return !!(
+ (forAttr && (forAttr.includes("*") || forAttr.includes(value))) ||
+ (!name.includes("data-html-") &&
+ name.startsWith("data-") &&
+ (forTag["data-*"] || testDataAttribute(forTag, name, value)))
+ );
+}
+
export function sanitize(text, allowLister) {
if (!text) {
return "";
@@ -119,13 +130,8 @@ export function sanitize(text, allowLister) {
onIgnoreTagAttr(tag, name, value) {
const forTag = allowList.attrList[tag];
if (forTag) {
- const forAttr = forTag[name];
-
if (
- (forAttr && (forAttr.includes("*") || forAttr.includes(value))) ||
- (!name.includes("data-html-") &&
- name.startsWith("data-") &&
- (forTag["data-*"] || testDataAttribute(forTag, name, value))) ||
+ isAllowListedAttr(forTag, name, value) ||
(tag === "a" &&
name === "href" &&
hrefAllowed(value, extraHrefMatchers)) ||