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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: render-html with invalid tags (#1275)
Myles Scolnick7debfd289a19
diff --git a/frontend/src/plugins/core/RenderHTML.tsx b/frontend/src/plugins/core/RenderHTML.tsxindex 5035bac381..d938deef42 100644--- a/frontend/src/plugins/core/RenderHTML.tsx+++ b/frontend/src/plugins/core/RenderHTML.tsx@@ -1,5 +1,6 @@/* Copyright 2024 Marimo. All rights reserved. */import parse, { Element } from "html-react-parser";+import React from "react";interface Options {html: string;@@ -12,6 +13,14 @@ interface Options {export const renderHTML = ({ html }: Options) => {return parse(html, {replace: (domNode) => {+ // Don't render invalid tags+ if (+ domNode instanceof Element &&+ !/^[A-Za-z][\w-]*$/.test(domNode.name)+ ) {+ return React.createElement(React.Fragment);+ }+// For iframe, we just want to use dangerouslySetInnerHTML so:// 1) we can remount the iframe when the src changes// 2) keep event attributes (onload, etc.) since this library removes themdiff --git a/frontend/src/plugins/core/__test__/RenderHTML.test.ts b/frontend/src/plugins/core/__test__/RenderHTML.test.tsnew file mode 100644index 0000000000..9d7cea5c95--- /dev/null+++ b/frontend/src/plugins/core/__test__/RenderHTML.test.ts@@ -0,0 +1,103 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { describe, expect, test } from "vitest";+import { renderHTML } from "../RenderHTML";++describe("RenderHTML", () => {+ test("renders HTML", () => {+ const html = "<h1>Hello</h1>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <h1>+ Hello+ </h1>+ `);+ });++ test("no closing HTML", () => {+ const html = "<h1>Hello";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <h1>+ Hello+ </h1>+ `);+ });++ test("script", () => {+ const html = "<h1>Hello</h1><script>alert('hi')</script>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ [+ <h1>+ Hello+ </h1>,+ <script+ dangerouslySetInnerHTML={+ {+ "__html": "alert('hi')",+ }+ }+ />,+ ]+ `);+ });++ test("iframe", () => {+ const html = '<iframe src="https://example.com" "crossorigin"></iframe>';+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <div+ dangerouslySetInnerHTML={+ {+ "__html": "<iframe src="https://example.com" crossorigin=""></iframe>",+ }+ }+ />+ `);+ });++ test("custom tags - valid", () => {+ let html = "<foobar></foobar>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`<foobar />`);++ html = "<foobar2></foobar2>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`<foobar2 />`);++ html = "<marimo-slider></marimo-slider>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`<marimo-slider />`);+ });++ test("custom tags - invalid", () => {+ let html = "<c&></c&> lorem";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ [+ <React.Fragment />,+ " lorem",+ ]+ `);++ html = "<p><[email protected]></p>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <p>+ <React.Fragment />+ </p>+ `);++ html = "<p><1></p>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <p>+ <1>+ </p>+ `);++ html = "<p><1/></p>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <p>+ <1/>+ </p>+ `);++ html = "<p><a:b></a:b></p>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <p>+ <React.Fragment />+ </p>+ `);+ });+});