Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Extract sanitizeHtml into a standalone module (#8726)

Trevor Manzb64ad5952e94
diff --git a/frontend/src/core/dom/outline.ts b/frontend/src/core/dom/outline.ts
index a65473419d..6765107a5e 100644
--- a/frontend/src/core/dom/outline.ts
+++ b/frontend/src/core/dom/outline.ts
@@ -1,6 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { sanitizeHtml } from "@/plugins/core/sanitize";
+import { sanitizeHtml } from "@/plugins/core/sanitize-html";
import { invariant } from "@/utils/invariant";
import { Logger } from "@/utils/Logger";
import type { Outline, OutlineItem } from "../cells/outline";
diff --git a/frontend/src/plugins/core/sanitize-html.ts b/frontend/src/plugins/core/sanitize-html.ts
new file mode 100644
index 0000000000..fc79b2dfae
--- /dev/null
+++ b/frontend/src/plugins/core/sanitize-html.ts
@@ -0,0 +1,50 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+import DOMPurify, { type Config } from "dompurify";
+
+// preserve target=_blank https://github.com/cure53/DOMPurify/issues/317#issuecomment-912474068
+const TEMPORARY_ATTRIBUTE = "data-temp-href-target";
+DOMPurify.addHook("beforeSanitizeAttributes", (node) => {
+ if (node.tagName === "A") {
+ if (!node.hasAttribute("target")) {
+ node.setAttribute("target", "_self");
+ }
+
+ if (node.hasAttribute("target")) {
+ node.setAttribute(TEMPORARY_ATTRIBUTE, node.getAttribute("target") || "");
+ }
+ }
+});
+
+DOMPurify.addHook("afterSanitizeAttributes", (node) => {
+ if (node.tagName === "A" && node.hasAttribute(TEMPORARY_ATTRIBUTE)) {
+ node.setAttribute("target", node.getAttribute(TEMPORARY_ATTRIBUTE) || "");
+ node.removeAttribute(TEMPORARY_ATTRIBUTE);
+ if (node.getAttribute("target") === "_blank") {
+ node.setAttribute("rel", "noopener noreferrer");
+ }
+ }
+});
+
+/**
+ * This removes script tags, form tags, iframe tags, and other potentially dangerous tags
+ */
+export function sanitizeHtml(html: string) {
+ const sanitizationOptions: Config = {
+ // Default to permit HTML, SVG and MathML, this limits to HTML only
+ USE_PROFILES: { html: true, svg: true, mathMl: true },
+ // Allow SVG <use> elements and their href attributes, which are needed
+ // for SVGs that reference <defs> (e.g., Matplotlib SVG output).
+ ADD_TAGS: ["use"],
+ ADD_ATTR: ["href", "xlink:href"],
+ // glue elements like style, script or others to document.body and prevent unintuitive browser behavior in several edge-cases
+ FORCE_BODY: true,
+ CUSTOM_ELEMENT_HANDLING: {
+ tagNameCheck: /^(marimo-[A-Za-z][\w-]*|iconify-icon)$/,
+ attributeNameCheck: /^[A-Za-z][\w-]*$/,
+ },
+ // This flag means we should sanitize such that is it safe for XML,
+ // but this is only used for HTML content.
+ SAFE_FOR_XML: !html.includes("marimo-mermaid"),
+ };
+ return DOMPurify.sanitize(html, sanitizationOptions);
+}
diff --git a/frontend/src/plugins/core/sanitize.ts b/frontend/src/plugins/core/sanitize.ts
index c262edc7d8..8bbdefdb1d 100644
--- a/frontend/src/plugins/core/sanitize.ts
+++ b/frontend/src/plugins/core/sanitize.ts
@@ -1,10 +1,12 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import DOMPurify, { type Config } from "dompurify";
import { atom, useAtomValue } from "jotai";
import { hasRunAnyCellAtom } from "@/components/editor/cell/useRunCells";
import { autoInstantiateAtom } from "@/core/config/config";
import { getInitialAppMode } from "@/core/mode";
+// Re-export so existing consumers don't break.
+export { sanitizeHtml } from "./sanitize-html";
+
/**
* Whether to sanitize the html.
* When running as an app or with auto_instantiate enabled
@@ -42,51 +44,3 @@ const sanitizeHtmlAtom = atom<boolean>((get) => {
export function useSanitizeHtml() {
return useAtomValue(sanitizeHtmlAtom);
}
-
-// preserve target=_blank https://github.com/cure53/DOMPurify/issues/317#issuecomment-912474068
-const TEMPORARY_ATTRIBUTE = "data-temp-href-target";
-DOMPurify.addHook("beforeSanitizeAttributes", (node) => {
- if (node.tagName === "A") {
- if (!node.hasAttribute("target")) {
- node.setAttribute("target", "_self");
- }
-
- if (node.hasAttribute("target")) {
- node.setAttribute(TEMPORARY_ATTRIBUTE, node.getAttribute("target") || "");
- }
- }
-});
-
-DOMPurify.addHook("afterSanitizeAttributes", (node) => {
- if (node.tagName === "A" && node.hasAttribute(TEMPORARY_ATTRIBUTE)) {
- node.setAttribute("target", node.getAttribute(TEMPORARY_ATTRIBUTE) || "");
- node.removeAttribute(TEMPORARY_ATTRIBUTE);
- if (node.getAttribute("target") === "_blank") {
- node.setAttribute("rel", "noopener noreferrer");
- }
- }
-});
-
-/**
- * This removes script tags, form tags, iframe tags, and other potentially dangerous tags
- */
-export function sanitizeHtml(html: string) {
- const sanitizationOptions: Config = {
- // Default to permit HTML, SVG and MathML, this limits to HTML only
- USE_PROFILES: { html: true, svg: true, mathMl: true },
- // Allow SVG <use> elements and their href attributes, which are needed
- // for SVGs that reference <defs> (e.g., Matplotlib SVG output).
- ADD_TAGS: ["use"],
- ADD_ATTR: ["href", "xlink:href"],
- // glue elements like style, script or others to document.body and prevent unintuitive browser behavior in several edge-cases
- FORCE_BODY: true,
- CUSTOM_ELEMENT_HANDLING: {
- tagNameCheck: /^(marimo-[A-Za-z][\w-]*|iconify-icon)$/,
- attributeNameCheck: /^[A-Za-z][\w-]*$/,
- },
- // This flag means we should sanitize such that is it safe for XML,
- // but this is only used for HTML content.
- SAFE_FOR_XML: !html.includes("marimo-mermaid"),
- };
- return DOMPurify.sanitize(html, sanitizationOptions);
-}