Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Strip comments and PIs from sanitized SVG icons

Juan Denisc2229b6874a2
diff --git a/.github/workflows/frontend-ci.yml b/.github/workflows/frontend-ci.yml
index 4cc01b87..81962877 100644
--- a/.github/workflows/frontend-ci.yml
+++ b/.github/workflows/frontend-ci.yml
@@ -68,6 +68,9 @@ jobs:
- name: Backup request and form browser regressions
working-directory: frontend
run: npm run test:backups
+ - name: SVG sanitizer browser regressions
+ working-directory: frontend
+ run: npm run test:sanitize-svg
- name: Upload Settings screenshots
if: always()
uses: actions/upload-artifact@v4
diff --git a/frontend/package.json b/frontend/package.json
index c4b2fa20..06dab10c 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -16,6 +16,7 @@
"measure:build": "node scripts/measure-build.mjs",
"test:hooks": "node scripts/hooks-browser.mjs",
"test:backups": "node scripts/backups-browser.mjs",
+ "test:sanitize-svg": "node scripts/sanitize-svg-browser.mjs",
"lint:settings-index": "node scripts/check-settings-index.mjs",
"lint:fonts": "node scripts/check-font-integrity.mjs",
"lint:status": "node scripts/check-status-one-door.mjs",
diff --git a/frontend/scripts/sanitize-svg-browser.mjs b/frontend/scripts/sanitize-svg-browser.mjs
new file mode 100644
index 00000000..381a3dcc
--- /dev/null
+++ b/frontend/scripts/sanitize-svg-browser.mjs
@@ -0,0 +1,62 @@
+// sanitizeSvgInner output is rendered with dangerouslySetInnerHTML inside an
+// <svg>, so the HTML parser — not the XML parser that scrubbed it — has the
+// last word. Comments and processing instructions survive XML serialization
+// but close early under HTML rules (`<!-->`, `<?x >`), letting the rest out.
+// Run in a real browser so both parsers are the real ones. No backend needed.
+import assert from 'node:assert/strict';
+import { createServer } from 'vite';
+import { chromium } from 'playwright';
+
+const PAYLOADS = [
+ '<!--><img src=x onerror="window.__xss=1">-->',
+ '<?x ><img src=x onerror="window.__xss=1">?>',
+ '<![CDATA[<img src=x onerror="window.__xss=1">]]>',
+ '<path d="M0 0" onload="window.__xss=1"/>',
+ '<foreignObject><img xmlns="http://www.w3.org/1999/xhtml" src="x" onerror="window.__xss=1"/></foreignObject>',
+ '<g><script>window.__xss=1</script></g>',
+ '<a href="javascript:window.__xss=1"><path d="M0 0"/></a>',
+ '<g><!--><img src=x onerror="window.__xss=1">--></g>',
+];
+
+const server = await createServer({ server: { host: '127.0.0.1', port: 0, strictPort: false, open: false } });
+let browser;
+try {
+ await server.listen();
+ browser = await chromium.launch({ headless: true });
+ const page = await browser.newPage();
+ await page.goto(`${server.resolvedUrls.local[0]}src/utils/sanitizeSvg.js`);
+
+ const results = await page.evaluate(async (payloads) => {
+ const { sanitizeSvgInner } = await import('/src/utils/sanitizeSvg.js');
+ const out = [];
+ for (const payload of payloads) {
+ const host = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
+ document.body.appendChild(host);
+ host.innerHTML = sanitizeSvgInner(payload);
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ out.push({
+ payload,
+ fired: Boolean(window.__xss),
+ foreign: [...host.querySelectorAll('*')]
+ .filter((el) => el.namespaceURI !== 'http://www.w3.org/2000/svg'
+ || [...el.attributes].some((a) => a.name.startsWith('on') || a.name.includes('href')))
+ .map((el) => el.outerHTML),
+ });
+ window.__xss = 0;
+ host.remove();
+ }
+ const kept = sanitizeSvgInner('<path d="M4 6h16" stroke="currentColor"/><circle cx="12" cy="12" r="3"/>');
+ return { out, kept };
+ }, PAYLOADS);
+
+ for (const { payload, fired, foreign } of results.out) {
+ assert.equal(fired, false, `script ran for ${payload}`);
+ assert.deepEqual(foreign, [], `unsafe markup survived for ${payload}`);
+ }
+ assert.match(results.kept, /<path [^>]*d="M4 6h16" stroke="currentColor"/);
+ assert.match(results.kept, /<circle [^>]*cx="12" cy="12" r="3"/);
+ console.log(`SVG sanitizer: ${PAYLOADS.length} bypass payloads inert, safe icons intact.`);
+} finally {
+ await browser?.close();
+ await server.close();
+}
diff --git a/frontend/src/utils/sanitizeSvg.js b/frontend/src/utils/sanitizeSvg.js
index cc33838a..9b1f37e2 100644
--- a/frontend/src/utils/sanitizeSvg.js
+++ b/frontend/src/utils/sanitizeSvg.js
@@ -50,8 +50,17 @@ export function sanitizeSvgInner(svg) {
return result;
}
+// Walk childNodes, not children: comments, processing instructions and CDATA
+// aren't elements, and they survive XML serialization only to close early
+// when the result is re-parsed as HTML (`<!-->`, `<?x >`), letting whatever
+// followed them out. Only allowed elements and plain text are kept.
function scrub(node) {
- for (const child of Array.from(node.children)) {
+ for (const child of Array.from(node.childNodes)) {
+ if (child.nodeType === Node.TEXT_NODE) continue;
+ if (child.nodeType !== Node.ELEMENT_NODE) {
+ child.remove();
+ continue;
+ }
const tag = child.tagName.toLowerCase();
if (!ALLOWED_TAGS.has(tag)) {
child.remove();