Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Server-side rendering for comment emails (#70938)

Ngoc Khuat39bc014f14fd
diff --git a/e2e/support/helpers/api/createComment.ts b/e2e/support/helpers/api/createComment.ts
index 47e905590ab..50fa27996e6 100644
--- a/e2e/support/helpers/api/createComment.ts
+++ b/e2e/support/helpers/api/createComment.ts
@@ -6,7 +6,6 @@ export const createComment = ({
child_target_id,
parent_comment_id = null,
content,
- html,
}: Partial<CreateCommentRequest>): Cypress.Chainable<
Cypress.Response<Comment>
> => {
@@ -16,6 +15,5 @@ export const createComment = ({
child_target_id,
parent_comment_id,
content,
- html,
});
};
diff --git a/frontend/src/metabase-types/api/comments.ts b/frontend/src/metabase-types/api/comments.ts
index 276afb47efa..556188f4303 100644
--- a/frontend/src/metabase-types/api/comments.ts
+++ b/frontend/src/metabase-types/api/comments.ts
@@ -53,7 +53,6 @@ export interface CreateCommentRequest {
child_target_id: EntityId | null;
parent_comment_id: CommentId | null;
content: DocumentContent;
- html: string;
}
export interface UpdateCommentRequest {
diff --git a/frontend/src/metabase/comments/components/CommentEditor/CommentEditor.tsx b/frontend/src/metabase/comments/components/CommentEditor/CommentEditor.tsx
index 8e7b6b65930..a1705e16eaa 100644
--- a/frontend/src/metabase/comments/components/CommentEditor/CommentEditor.tsx
+++ b/frontend/src/metabase/comments/components/CommentEditor/CommentEditor.tsx
@@ -43,7 +43,7 @@ interface Props {
readonly?: boolean;
onBlur?: (content: DocumentContent, editor: Editor) => void;
onChange?: (content: DocumentContent) => void;
- onSubmit?: (content: DocumentContent, html: string) => void;
+ onSubmit?: (content: DocumentContent) => void;
onEscape?: () => void;
}
@@ -147,10 +147,8 @@ export const CommentEditor = ({
const content = editor.getJSON() as DocumentContent;
const isEmpty = editor.isEmpty;
- const html = stripInternalIds(editor.getHTML());
-
if (!isEmpty && onSubmit) {
- onSubmit(content, html);
+ onSubmit(content);
editor.commands.clearContent(true);
editor.commands.blur();
}
@@ -227,11 +225,3 @@ export const CommentEditor = ({
</Flex>
);
};
-
-function stripInternalIds(html: string): string {
- const parser = new DOMParser();
- const doc = parser.parseFromString(html, "text/html");
- const elements = doc.body.querySelectorAll("[_id]");
- elements.forEach((el) => el.removeAttribute("_id"));
- return doc.body.innerHTML;
-}
diff --git a/frontend/src/metabase/comments/components/Discussion/Discussion.tsx b/frontend/src/metabase/comments/components/Discussion/Discussion.tsx
index 881b369e511..e23bc728b6d 100644
--- a/frontend/src/metabase/comments/components/Discussion/Discussion.tsx
+++ b/frontend/src/metabase/comments/components/Discussion/Discussion.tsx
@@ -52,14 +52,13 @@ export const Discussion = ({
const [deleteComment] = useDeleteCommentMutation();
const [toggleReaction] = useToggleReactionMutation();
- const handleSubmit = async (doc: DocumentContent, html: string) => {
+ const handleSubmit = async (doc: DocumentContent) => {
const { error } = await createComment({
child_target_id: effectiveChildTargetId,
target_id: targetId,
target_type: targetType,
content: doc,
parent_comment_id: parentCommentId,
- html,
});
if (error) {
diff --git a/frontend/src/metabase/documents/components/CommentsSidesheet/CommentsSidesheet.tsx b/frontend/src/metabase/documents/components/CommentsSidesheet/CommentsSidesheet.tsx
index be1eee67bf8..2bf4a3e0bf0 100644
--- a/frontend/src/metabase/documents/components/CommentsSidesheet/CommentsSidesheet.tsx
+++ b/frontend/src/metabase/documents/components/CommentsSidesheet/CommentsSidesheet.tsx
@@ -156,7 +156,7 @@ export const CommentsSidesheet = ({ params, onClose }: Props) => {
}
});
- const handleSubmit = async (doc: DocumentContent, html: string) => {
+ const handleSubmit = async (doc: DocumentContent) => {
if (!childTargetId || !document) {
return;
}
@@ -167,7 +167,6 @@ export const CommentsSidesheet = ({ params, onClose }: Props) => {
target_type: "document",
content: doc,
parent_comment_id: null,
- html,
});
if (error) {
diff --git a/src/metabase/comments/api.clj b/src/metabase/comments/api.clj
index e39c600f417..8d0fec4c3c8 100644
--- a/src/metabase/comments/api.clj
+++ b/src/metabase/comments/api.clj
@@ -9,6 +9,7 @@
[metabase.channel.render.core :as channel.render]
[metabase.comments.models.comment :as comment]
[metabase.comments.models.comment-reaction :as comment-reaction]
+ [metabase.comments.render :as comments.render]
[metabase.events.core :as events]
[metabase.request.core :as request]
[metabase.users.core :as users]
@@ -52,7 +53,6 @@
[:target_type [:enum "document"]]
[:target_id ms/PositiveInt]
[:content CommentContent]
- [:html :string]
[:child_target_id {:optional true} [:maybe :string]]
[:parent_comment_id {:optional true} [:maybe ms/PositiveInt]]])
@@ -60,7 +60,6 @@
"Schema for updating a comment"
[:map
[:content {:optional true} CommentContent]
- [:html {:optional true} :string]
[:is_resolved {:optional true} :boolean]])
;;; routes
@@ -143,9 +142,9 @@
:document_href (urlpath-for entity)
:created_at (:created_at comment)
:author (:common_name (:creator comment))
- :comment (:content_html comment)
+ :comment (comments.render/content->html (:content comment))
:parent_author (:common_name (:creator parent))
- :parent_comment (:content_html parent)
+ :parent_comment (some-> parent :content comments.render/content->html)
:style {:color_text_dark channel.render/color-text-dark
:color_text_light channel.render/color-text-light
:color_text_medium channel.render/color-text-medium}}]
@@ -166,7 +165,7 @@
"Create a new comment"
[_route-params
_query-params
- {:keys [target_type target_id child_target_id parent_comment_id content html]} :- CreateComment]
+ {:keys [target_type target_id child_target_id parent_comment_id content]} :- CreateComment]
(let [entity (-> (api/read-check (type->model target_type) target_id)
(u/prog1 (api/check-400 (not (entity-archived? <>))
"Cannot comment on archived entities")))
@@ -184,7 +183,6 @@
:child_target_id child_target_id
:parent_comment_id parent_comment_id
:content content
- :content_html html
:creator_id api/*current-user-id*})
(t2/hydrate :creator)
;; New comments always have empty reactions map
@@ -203,7 +201,7 @@
"Update a comment"
[{:keys [comment-id]} :- [:map [:comment-id ms/PositiveInt]]
_query-params
- {:keys [content html is_resolved]} :- UpdateComment]
+ {:keys [content is_resolved]} :- UpdateComment]
(let [comment (api/check-404 (t2/select-one :model/Comment :id comment-id))
entity (-> (api/read-check (type->model (:target_type comment)) (:target_id comment))
(u/prog1 (api/check-400 (not (entity-archived? <>))
@@ -221,7 +219,7 @@
;; Anyone with write permission to target entity can resolve/unresolve
(api/write-check entity))
- (when-let [updates (-> {:content content :html html :is_resolved is_resolved}
+ (when-let [updates (-> {:content content :is_resolved is_resolved}
u/remove-nils
not-empty)]
(t2/update! :model/Comment comment-id updates))
diff --git a/src/metabase/comments/models/comment.clj b/src/metabase/comments/models/comment.clj
index 6f0c4bbd2b3..06cc0d8b1a2 100644
--- a/src/metabase/comments/models/comment.clj
+++ b/src/metabase/comments/models/comment.clj
@@ -12,6 +12,11 @@
(derive :metabase/model)
(derive :hook/timestamped?))
+;; NOTE: the `content_html` column is deprecated and no longer written to.
+;; HTML for email notifications is now rendered server-side from the `content` JSON
+;; via `metabase.comments.render/content->html`. The column can be dropped in a
+;; future migration.
+
(t2/deftransforms :model/Comment
{:content mi/transform-json})
diff --git a/src/metabase/comments/render.clj b/src/metabase/comments/render.clj
new file mode 100644
index 00000000000..50199599304
--- /dev/null
+++ b/src/metabase/comments/render.clj
@@ -0,0 +1,114 @@
+(ns metabase.comments.render
+ "Server-side rendering of TipTap JSON content to safe HTML.
+
+ Renders from the structured content JSON that TipTap produces, with an allowlist of
+ known node types and mark types. Anything not on the allowlist is stripped or rendered
+ as plain text. Nodes are first converted to Hiccup data structures, then compiled to
+ HTML via `hiccup2.core/html`."
+ (:require
+ [hiccup.util :as hiccup.util]
+ [hiccup2.core :as h2]
+ [metabase.channel.urls :as channel.urls]
+ [metabase.util.log :as log]))
+
+;;; ------------------------------------------------- Sanitization -------------------------------------------------
+
+(def ^:private allowed-node-types
+ "Allowlist of TipTap node types we render. Unknown types are stripped."
+ #{"doc" "paragraph" "heading" "bulletList" "orderedList" "listItem"
+ "codeBlock" "blockquote" "horizontalRule" "hardBreak" "text" "smartLink"})
+
+(def ^:private allowed-mark-types
+ "Allowlist of TipTap mark types we render. Unknown marks are stripped.
+ Note: \"link\" is intentionally excluded — the UI only produces smartLinks for entity
+ references, so there is no legitimate source of link marks. Excluding them prevents
+ phishing links from being injected via crafted API requests."
+ #{"bold" "italic" "strike" "code"})
+
+(defn- sanitize-marks
+ "Filter marks to only allowed types."
+ [marks]
+ (when (seq marks)
+ (filterv #(allowed-mark-types (:type %)) marks)))
+
+;;; --------------------------------------------------- Rendering --------------------------------------------------
+
+(def ^:private mark-type->tag
+ {"bold" :strong
+ "italic" :em
+ "strike" :s
+ "code" :code})
+
+(defn- wrap-marks
+ "Wrap hiccup content in tags for each mark (bold, italic, etc.)."
+ [content marks]
+ (reduce (fn [inner {:keys [type]}]
+ (if-let [tag (mark-type->tag type)]
+ [tag inner]
+ inner))
+ content
+ marks))
+
+(declare node->hiccup)
+
+(defn- children->hiccup
+ "Convert a sequence of child nodes to hiccup, skipping nodes with disallowed types."
+ [children]
+ (keep node->hiccup children))
+
+(def ^:private model->url-fn
+ "Map of smart link model types to their URL-building functions.
+ User mentions don't get links — they render as plain text."
+ {"card" channel.urls/card-url
+ "dataset" channel.urls/card-url
+ "dashboard" channel.urls/dashboard-url
+ "collection" channel.urls/collection-url
+ "document" #(format "%s/document/%d" (channel.urls/site-url) %)})
+
+(defn- smart-link->hiccup
+ "Convert a smartLink node to hiccup. Builds URLs from model + entityId rather than
+ trusting the href attribute, which could be crafted to inject phishing links."
+ [{:keys [attrs]}]
+ (let [{:keys [label model entityId]} attrs
+ display-text (or label
+ (when (= model "user") (str "@" entityId))
+ (str model " " entityId))
+ url-fn (model->url-fn model)]
+ (if url-fn
+ [:a {:href (url-fn entityId)} display-text]
+ ;; Unknown model or user mention — render as escaped plain text
+ (hiccup.util/raw-string (hiccup.util/escape-html display-text)))))
+
+(defn node->hiccup
+ "Convert a single TipTap JSON node to a Hiccup data structure. Unknown node types return nil.
+ Doc nodes return a sequence of hiccup forms (not a single form) so the caller can join them."
+ [{:keys [type content text marks attrs] :as node}]
+ (if-not (allowed-node-types type)
+ (do (log/warnf "Ignoring unknown TipTap node type: %s" type)
+ nil)
+ (case type
+ "doc" (children->hiccup content)
+ "paragraph" (into [:p] (children->hiccup content))
+ "heading" (let [level (min (max (get attrs :level 1) 1) 6)
+ tag (keyword (str "h" level))]
+ (into [tag] (children->hiccup content)))
+ "bulletList" (into [:ul] (children->hiccup content))
+ "orderedList" (into [:ol] (children->hiccup content))
+ "listItem" (into [:li] (children->hiccup content))
+ "codeBlock" [:pre [:code (apply str (map :text content))]]
+ "blockquote" (into [:blockquote] (children->hiccup content))
+ "horizontalRule" [:hr]
+ "hardBreak" [:br]
+ "text" (wrap-marks text (sanitize-marks marks))
+ "smartLink" (smart-link->hiccup node))))
+
+(defn content->html
+ "Convert TipTap JSON content to safe HTML. This is the main entry point.
+ Returns nil if content is nil."
+ [content]
+ (when content
+ (let [hiccup (node->hiccup content)]
+ (if (sequential? (first hiccup))
+ ;; doc node returns a seq of hiccup forms
+ (apply str (map #(str (h2/html %)) hiccup))
+ (str (h2/html hiccup))))))
diff --git a/test/metabase/comments/api_test.clj b/test/metabase/comments/api_test.clj
index 59501ca7c76..9ffbc15454f 100644
--- a/test/metabase/comments/api_test.clj
+++ b/test/metabase/comments/api_test.clj
@@ -74,8 +74,7 @@
created (mt/user-http-request :rasta :post 200 "comment/"
{:target_type "document"
:target_id doc-id
- :content content
- :html "<p>New comment</p>"})
+ :content content})
expected1 {:id int?
:content content
:target_type "document"
@@ -103,8 +102,7 @@
{:target_type "document"
:target_id doc-id
:parent_comment_id (:id created)
- :content content2
- :html "<p>Other comment</p>"})
+ :content content2})
expected2 {:id int?
:content content2
:target_type "document"
@@ -129,8 +127,7 @@
{:target_type "document"
:target_id doc-id
:parent_comment_id (:id created)
- :content (tiptap [:p "Third comment in a thread"])
- :html "<p>Third comment in a thread</p>"})]
+ :content (tiptap [:p "Third comment in a thread"])})]
(is (=? {(:email (mt/fetch-user :rasta))
[{:subject "Comment on New Document"
:body [{:content (relaxed-re
@@ -147,8 +144,7 @@
{:target_type "document"
:target_id doc-id
:child_target_id part-id
- :content (tiptap [:p "Part comment"])
- :html "<p>Part comment</p>"})]
+ :content (tiptap [:p "Part comment"])})]
(is (=? {:id int?
:content {:type "doc"}
:target_type "document"
@@ -172,12 +168,59 @@
:target_id doc-id
:content (tiptap
[:smartLink {:model "user"
- :entityId (mt/user->id :crowberto)}])
- :html "<p>Mention of @crowberto :)</p>"})]
+ :entityId (mt/user->id :crowberto)}])})]
(is (=? {(:email (mt/fetch-user :lucky)) [{:subject "Comment on New Document"}]
(:email (mt/fetch-user :crowberto)) [{:subject "Comment on New Document"}]}
(first (swap-vals! mt/inbox empty))))))))))))
+(deftest email-renders-safe-html-test
+ (testing "Notification emails render server-side HTML from content JSON, not client-supplied HTML"
+ (mt/with-temporary-setting-values [site-url "http://localhost:3000"]
+ (mt/with-temp [:model/Document {doc-id :id} {:name "Test Doc"
+ :creator_id (mt/user->id :lucky)}]
+ (mt/with-model-cleanup [:model/Comment :model/Notification]
+ (mt/with-fake-inbox
+ (notification.seed/seed-notification!)
+ (testing "script tags in text are escaped in email"
+ (mt/user-http-request :rasta :post 200 "comment/"
+ {:target_type "document"
+ :target_id doc-id
+ :content (tiptap [:p "<script>alert('xss')</script>"])})
+ (let [email-body (-> @mt/inbox vals first first :body first :content)]
+ (is (not (str/includes? email-body "<script>")))
+ (is (str/includes? email-body "&lt;script&gt;")))
+ (swap! mt/inbox empty))
+
+ (testing "javascript: links in content are stripped in email"
+ (mt/user-http-request :rasta :post 200 "comment/"
+ {:target_type "document"
+ :target_id doc-id
+ :content {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "click here"
+ :marks [{:type "link"
+ :attrs {:href "javascript:alert(1)"}}]}]}]}})
+ (let [email-body (-> @mt/inbox vals first first :body first :content)]
+ (is (not (str/includes? email-body "javascript:")))
+ (is (str/includes? email-body "click here")))
+ (swap! mt/inbox empty))
+
+ (testing "unknown node types are dropped in email"
+ (mt/user-http-request :rasta :post 200 "comment/"
+ {:target_type "document"
+ :target_id doc-id
+ :content {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "safe text"}]}
+ {:type "iframe"
+ :attrs {:src "https://evil.example"}}]}})
+ (let [email-body (-> @mt/inbox vals first first :body first :content)]
+ (is (str/includes? email-body "safe text"))
+ (is (not (str/includes? email-body "iframe")))
+ (is (not (str/includes? email-body "evil.example"))))
+ (swap! mt/inbox empty))))))))
+
(deftest update-comment-test
(testing "PUT /api/comment/:comment-id"
(mt/with-temp [:model/Document {doc-id :id} {}
@@ -269,8 +312,7 @@
(mt/user-http-request :lucky :post 403 "comment/"
{:target_type "document"
:target_id restricted-doc-id
- :content (tiptap "Comment by lucky")
- :html "You shall not pass"}))))
+ :content (tiptap "Comment by lucky")}))))
(testing "PUT /api/comment/:id - users without document access cannot update comments"
(is (= "You don't have permissions to do that."
diff --git a/test/metabase/comments/render_test.clj b/test/metabase/comments/render_test.clj
new file mode 100644
index 00000000000..4b74ead8116
--- /dev/null
+++ b/test/metabase/comments/render_test.clj
@@ -0,0 +1,206 @@
+(ns metabase.comments.render-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.comments.render :as render]
+ [metabase.test :as mt]))
+
+(deftest content->html-basic-test
+ (testing "nil content returns nil"
+ (is (nil? (render/content->html nil))))
+
+ (testing "empty doc"
+ (is (= "" (render/content->html {:type "doc" :content []}))))
+
+ (testing "simple paragraph"
+ (is (= "<p>Hello world</p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "Hello world"}]}]}))))
+
+ (testing "paragraph with no content"
+ (is (= "<p></p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"}]})))))
+
+(deftest content->html-marks-test
+ (testing "bold text"
+ (is (= "<p><strong>bold</strong></p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "bold"
+ :marks [{:type "bold"}]}]}]}))))
+
+ (testing "italic text"
+ (is (= "<p><em>italic</em></p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "italic"
+ :marks [{:type "italic"}]}]}]}))))
+
+ (testing "multiple marks"
+ (is (= "<p><em><strong>bold italic</strong></em></p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "bold italic"
+ :marks [{:type "bold"}
+ {:type "italic"}]}]}]}))))
+
+ (testing "link marks are stripped, text preserved"
+ (is (= "<p>click</p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "click"
+ :marks [{:type "link"
+ :attrs {:href "https://example.com"}}]}]}]}))))
+
+ (testing "strike and code marks"
+ (is (= "<p><s>deleted</s> and <code>code</code></p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "deleted"
+ :marks [{:type "strike"}]}
+ {:type "text" :text " and "}
+ {:type "text"
+ :text "code"
+ :marks [{:type "code"}]}]}]})))))
+
+(deftest content->html-block-nodes-test
+ (testing "heading"
+ (is (= "<h2>Title</h2>"
+ (render/content->html {:type "doc"
+ :content [{:type "heading"
+ :attrs {:level 2}
+ :content [{:type "text" :text "Title"}]}]}))))
+
+ (testing "bullet list"
+ (is (= "<ul><li><p>one</p></li><li><p>two</p></li></ul>"
+ (render/content->html {:type "doc"
+ :content [{:type "bulletList"
+ :content [{:type "listItem"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "one"}]}]}
+ {:type "listItem"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "two"}]}]}]}]}))))
+
+ (testing "ordered list"
+ (is (= "<ol><li><p>first</p></li></ol>"
+ (render/content->html {:type "doc"
+ :content [{:type "orderedList"
+ :content [{:type "listItem"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "first"}]}]}]}]}))))
+
+ (testing "code block"
+ (is (= "<pre><code>fn main()</code></pre>"
+ (render/content->html {:type "doc"
+ :content [{:type "codeBlock"
+ :content [{:type "text" :text "fn main()"}]}]}))))
+
+ (testing "blockquote"
+ (is (= "<blockquote><p>quoted</p></blockquote>"
+ (render/content->html {:type "doc"
+ :content [{:type "blockquote"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "quoted"}]}]}]}))))
+
+ (testing "hard break"
+ (is (= "<p>line1<br />line2</p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "line1"}
+ {:type "hardBreak"}
+ {:type "text" :text "line2"}]}]}))))
+
+ (testing "horizontal rule"
+ (is (= "<hr />"
+ (render/content->html {:type "doc"
+ :content [{:type "horizontalRule"}]})))))
+
+(deftest content->html-smart-link-test
+ (testing "smartLink for card renders URL from model + entityId"
+ (mt/with-temporary-setting-values [site-url "http://localhost:3000"]
+ (is (= "<a href=\"http://localhost:3000/question/42\">My Question</a>"
+ (render/content->html {:type "doc"
+ :content [{:type "smartLink"
+ :attrs {:entityId 42
+ :model "card"
+ :label "My Question"
+ :href "/question/42"}}]})))))
+
+ (testing "smartLink user mention renders as plain text (no link)"
+ (is (= "@42"
+ (render/content->html {:type "doc"
+ :content [{:type "smartLink"
+ :attrs {:entityId 42
+ :model "user"}}]}))))
+
+ (testing "smartLink ignores href — phishing URL cannot be injected"
+ (mt/with-temporary-setting-values [site-url "http://localhost:3000"]
+ (is (= "<a href=\"http://localhost:3000/question/1\">Phishing</a>"
+ (render/content->html {:type "doc"
+ :content [{:type "smartLink"
+ :attrs {:entityId 1
+ :model "card"
+ :label "Phishing"
+ :href "https://evil.example"}}]}))))))
+
+(deftest content->html-xss-test
+ (testing "HTML in text content is escaped"
+ (is (= "<p>&lt;script&gt;alert(1)&lt;/script&gt;</p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "<script>alert(1)</script>"}]}]}))))
+
+ (testing "phishing link via crafted content JSON is stripped"
+ (is (= "<p>Please re-authenticate</p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "Please re-authenticate"
+ :marks [{:type "link"
+ :attrs {:href "https://evil.example"}}]}]}]}))))
+
+ (testing "unknown mark types are stripped"
+ (is (= "<p>text</p>"
+ (render/content->html {:type "doc"
+ :content [{:type "paragraph"
+ :content [{:type "text"
+ :text "text"
+ :marks [{:type "evilMark"}]}]}]}))))
+
+ (testing "HTML in smartLink label is escaped"
+ (mt/with-temporary-setting-values [site-url "http://localhost:3000"]
+ (is (= "<a href=\"http://localhost:3000/question/1\">&lt;img src=x&gt;</a>"
+ (render/content->html {:type "doc"
+ :content [{:type "smartLink"
+ :attrs {:entityId 1
+ :model "card"
+ :label "<img src=x>"}}]})))))
+
+ (testing "smartLink with unknown model renders as plain text"
+ (is (= "My Thing"
+ (render/content->html {:type "doc"
+ :content [{:type "smartLink"
+ :attrs {:entityId 1
+ :model "unknown"
+ :label "My Thing"}}]})))))
+
+(deftest content->html-unknown-nodes-test
+ (testing "unknown node type is dropped entirely"
+ (is (= ""
+ (render/content->html {:type "doc"
+ :content [{:type "futureNodeType"
+ :content [{:type "paragraph"
+ :content [{:type "text" :text "still works"}]}]}]}))))
+
+ (testing "unknown node type without children is dropped"
+ (is (= ""
+ (render/content->html {:type "doc"
+ :content [{:type "futureNodeType"}]})))))
diff --git a/test/metabase/documents/api/collection_test.clj b/test/metabase/documents/api/collection_test.clj
index 4ca2f512358..37712917d0f 100644
--- a/test/metabase/documents/api/collection_test.clj
+++ b/test/metabase/documents/api/collection_test.clj
@@ -74,8 +74,10 @@
:model/Dashboard {dash-id :id} {:collection_id nil
:name "Root Dashboard"}]
(testing "Documents appear alongside cards and dashboards in root"
- (let [items (mt/user-http-request :rasta :get 200 "collection/root/items")
- root-test-items (filter #(#{doc-id card-id dash-id} (:id %))
+ (let [items (mt/user-http-request :rasta :get 200 "collection/root/items")
+ test-ids (set [doc-id card-id dash-id])
+ root-test-items (filter #(and (test-ids (:id %))
+ (#{"document" "card" "dashboard"} (:model %)))
(:data items))]
(is (= #{[doc-id "document"]
[card-id "card"]