Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): improve svg security #342

Merged
merged 7 commits into from
Jun 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/utils/src/loader/cdn-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class CdnLoader {
for (const el of elements) {
// Type of SVGGraphicsElement?
if (el instanceof SVGElement && 'getBBox' in el) {
this.stripUnsafeAttributes(el);
this.stripUnsafeNodes(...(el as SVGElement).childNodes);
}
else {
Expand All @@ -24,6 +25,22 @@ export class CdnLoader {
}
}

/**
* Strips any event attributes which could be used to
* maliciously hijack the application.
* @param element Element to check
* @returns {void}
*/
private stripUnsafeAttributes (element: SVGElement): void {
const attributes = element.getAttributeNames();
for (const attribute of attributes) {
// Remove event attributes e.g., `onclick`
if (attribute.startsWith('on')) {
wsuwt marked this conversation as resolved.
Show resolved Hide resolved
element.removeAttribute(attribute);
}
}
}

/**
* Asynchronously tries to load
* @param href The location of the SVG to load
Expand Down