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: preserve SVG <use> elements and href attributes in sanitization (#8520)
24f2006299e90e721dbf04
diff --git a/frontend/src/plugins/core/__test__/sanitize.test.ts b/frontend/src/plugins/core/__test__/sanitize.test.tsindex 0ac651aa5e..6dca5840bf 100644--- a/frontend/src/plugins/core/__test__/sanitize.test.ts+++ b/frontend/src/plugins/core/__test__/sanitize.test.ts@@ -288,9 +288,54 @@ describe("sanitizeHtml", () => {expect(sanitizeHtml(html)).toMatchInlineSnapshot(`"<div>Text</div>"`);});- test("removes use element from SVG", () => {+ test("preserves use element in SVG", () => {const html = '<svg><use xlink:href="#icon"></use></svg>';- expect(sanitizeHtml(html)).toMatchInlineSnapshot(`"<svg></svg>"`);+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<svg><use xlink:href="#icon"></use></svg>"`,+ );+ });++ test("preserves SVG defs and use pattern", () => {+ const html = [+ '<svg width="60" height="60">',+ '<circle cx="30" cy="30" r="30" fill="orange"></circle>',+ '<defs><circle id="myCircle" cx="0" cy="0" r="10" fill="green"></circle></defs>',+ '<use href="#myCircle" x="20" y="20"></use>',+ "</svg>",+ ].join("");+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<svg width="60" height="60"><circle cx="30" cy="30" r="30" fill="orange"></circle><defs><circle id="myCircle" cx="0" cy="0" r="10" fill="green"></circle></defs><use href="#myCircle" x="20" y="20"></use></svg>"`,+ );+ });++ test("strips javascript: href from SVG use element", () => {+ const html = '<svg><use href="javascript:alert(1)"></use></svg>';+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<svg><use></use></svg>"`,+ );+ });++ test("strips javascript: xlink:href from SVG use element", () => {+ const html = '<svg><use xlink:href="javascript:alert(1)"></use></svg>';+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<svg><use></use></svg>"`,+ );+ });++ test("preserves external href on SVG use element", () => {+ const html =+ '<svg><use href="https://example.com/sprite.svg#icon"></use></svg>';+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<svg><use href="https://example.com/sprite.svg#icon"></use></svg>"`,+ );+ });++ test("preserves external xlink:href on SVG use element", () => {+ const html =+ '<svg><use xlink:href="https://example.com/sprite.svg#icon"></use></svg>';+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<svg><use xlink:href="https://example.com/sprite.svg#icon"></use></svg>"`,+ );});test("removes javascript in SVG href", () => {diff --git a/frontend/src/plugins/core/sanitize.ts b/frontend/src/plugins/core/sanitize.tsindex 4a724bf012..c262edc7d8 100644--- a/frontend/src/plugins/core/sanitize.ts+++ b/frontend/src/plugins/core/sanitize.ts@@ -74,6 +74,10 @@ export function sanitizeHtml(html: string) {const sanitizationOptions: Config = {// Default to permit HTML, SVG and MathML, this limits to HTML onlyUSE_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-casesFORCE_BODY: true,CUSTOM_ELEMENT_HANDLING: {