From 3f91a4566039e8aff32822cd7b8f8b53bf09e724 Mon Sep 17 00:00:00 2001 From: Bastien Chanot Date: Sun, 12 Jul 2026 03:32:50 +0200 Subject: [PATCH] fix(editor): sanitize user-imported SVGs before DOM insertion SvgEditorRoute.loadSVG inserted dropped SVG files with no stripping; pathology-editor stripped descendants but missed the root element's on* attributes and javascript: hrefs. Both now remove script/ foreignObject nodes, on* handlers (root included) and javascript: hrefs before appendChild. Impact bounded (no auth/backend), MEDIUM. --- public/pathology-editor.html | 7 +++++-- src/app/routes/SvgEditorRoute.tsx | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/public/pathology-editor.html b/public/pathology-editor.html index e8b973ba..adb79e21 100644 --- a/public/pathology-editor.html +++ b/public/pathology-editor.html @@ -1125,9 +1125,12 @@ clearChildren(canvasInner); var imported = document.importNode(svg, true); imported.querySelectorAll('script, foreignObject').forEach(function (e) { e.remove(); }); - imported.querySelectorAll('*').forEach(function (e) { + [imported].concat(Array.prototype.slice.call(imported.querySelectorAll('*'))).forEach(function (e) { for (var i = e.attributes.length - 1; i >= 0; i--) { - if (e.attributes[i].name.match(/^on/i)) e.removeAttribute(e.attributes[i].name); + var a = e.attributes[i]; + var isHandler = a.name.match(/^on/i); + var isJsHref = a.name.match(/href$/i) && a.value.match(/^\s*javascript:/i); + if (isHandler || isJsHref) e.removeAttribute(a.name); } }); canvasInner.appendChild(imported); diff --git a/src/app/routes/SvgEditorRoute.tsx b/src/app/routes/SvgEditorRoute.tsx index 5d5224df..d4a8460f 100644 --- a/src/app/routes/SvgEditorRoute.tsx +++ b/src/app/routes/SvgEditorRoute.tsx @@ -495,6 +495,19 @@ function initSvgEditor( }, sig) } + /** Strip active content from an untrusted SVG: scripts, foreignObject, + * event-handler attributes (root included) and javascript: hrefs. */ + function sanitizeSvg(root: Element) { + root.querySelectorAll('script, foreignObject').forEach((el) => el.remove()) + for (const el of [root, ...root.querySelectorAll('*')]) { + for (const attr of [...el.attributes]) { + const isHandler = /^on/i.test(attr.name) + const isJsHref = /href$/i.test(attr.name) && /^\s*javascript:/i.test(attr.value) + if (isHandler || isJsHref) el.removeAttribute(attr.name) + } + } + } + function loadSVG(svgText: string) { const parser = new DOMParser() const doc = parser.parseFromString(svgText, 'image/svg+xml') @@ -505,8 +518,10 @@ function initSvgEditor( return } + const imported = document.importNode(parsedSvg, true) + sanitizeSvg(imported) canvasInner.textContent = '' - canvasInner.appendChild(document.importNode(parsedSvg, true)) + canvasInner.appendChild(imported) svgRoot = canvasInner.querySelector('svg') if (!svgRoot) { alert(t('svg_editor.alert_invalid_svg'))