Skip to content

Commit

Permalink
fix: handle javascript uris in removexss plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Nov 4, 2023
1 parent 5980f4e commit dff8cd9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
64 changes: 43 additions & 21 deletions src/lib/svgo-xss.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { CustomPlugin } from "svgo";

/**
* Remove possible XSS attacks
* Remove possible XSS attacks.
*
* * Remove <script> elements.
* * Removes known event attributes.
* * Removes JavaScript URIs.
*
* Sometimes it's not enough just to remove <script> tag, XSS may be hidden under event listeners
* @author Katya Pavlenko (@cakeinpanic)
*
* Based on https://github.com/svg/svgo/pull/1664
* Based on https://github.com/svg/svgo/blob/main/plugins/removeScriptElement.js
*/
export const xss = {
name: "removeXSS",
Expand All @@ -22,9 +25,33 @@ export const xss = {
return;
}
for (const event of ALL_EVENTS) {
for (const [name] of Object.entries(node.attributes)) {
if (name === event) {
delete node.attributes[name];
if (node.attributes[event] != null) {
delete node.attributes[event];
}
}
},
exit: (node, parentNode) => {
if (node.name !== "a") {
return;
}

for (const attr of Object.keys(node.attributes)) {
if (attr === "href" || attr.endsWith(":href")) {
if (
node.attributes[attr] == null ||
!node.attributes[attr].trimStart().startsWith("javascript:")
) {
continue;
}

const index = parentNode.children.indexOf(node);
parentNode.children.splice(index, 1, ...node.children);

for (const child of node.children) {
Object.defineProperty(child, "parentNode", {
writable: true,
value: parentNode,
});
}
}
}
Expand All @@ -35,24 +62,18 @@ export const xss = {
} satisfies CustomPlugin;

const ALL_EVENTS = [
"onbegin",
"onend",
"onrepeat",
"onabort",
"onerror",
"onresize",
"onscroll",
"onunload",
"onactivate",
"onbegin",
"onend",
"onrepeat",
"oncancel",
"oncanplay",
"oncanplaythrough",
"onchange",
"onclick",
"onclose",
"oncopy",
"oncuechange",
"oncut",
"ondblclick",
"ondrag",
"ondragend",
Expand All @@ -63,9 +84,12 @@ const ALL_EVENTS = [
"ondrop",
"ondurationchange",
"onemptied",
"onend",
"onended",
"onerror",
"onfocus",
"onfocusin",
"onfocusout",
"oninput",
"oninvalid",
"onkeydown",
Expand All @@ -83,11 +107,13 @@ const ALL_EVENTS = [
"onmouseover",
"onmouseup",
"onmousewheel",
"onpaste",
"onpause",
"onplay",
"onplaying",
"onprogress",
"onratechange",
"onrepeat",
"onreset",
"onresize",
"onscroll",
Expand All @@ -100,12 +126,8 @@ const ALL_EVENTS = [
"onsuspend",
"ontimeupdate",
"ontoggle",
"onunload",
"onvolumechange",
"onwaiting",
"oncopy",
"oncut",
"onpaste",
"onactivate",
"onfocusin",
"onfocusout",
"onzoom",
];
8 changes: 5 additions & 3 deletions test/assets/xss.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dff8cd9

Please sign in to comment.