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: injecting style element violates CSP #5946

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
18 changes: 7 additions & 11 deletions src/platforms/platform.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,13 @@ function removeResizeListener(node) {
}
}

function injectCSS(platform, css) {
function injectCSS(css) {
// https://stackoverflow.com/q/3922139
var style = platform._style || document.createElement('style');
if (!platform._style) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

injectCSS() is only called once so we don't have to care about caching created element.

Copy link
Member

@etimberg etimberg Dec 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this cached is for the case of multiple charts on a single page. We only want 1 style element then

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the styles should be injected multiple times? Please note that style.appendChild(document.createTextNode(css)); wasn't part of that if.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the style in platform.initialize() shouldn't be injected multiple times, but injectCSS() could be called multiple time to inject different style under the same style element. That's not the case currently but that could change so injectCSS() still need to be able to be called multiple times (which I guess is still the case).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still possible to call injectCSS() multiple times but that will cause multiple link elements to be added. I didn't benchmark but I guess this is better from a performance perspective cause replacing may cause a more expensive rerendering.

platform._style = style;
css = '/* Chart.js */\n' + css;
style.setAttribute('type', 'text/css');
document.getElementsByTagName('head')[0].appendChild(style);
}

style.appendChild(document.createTextNode(css));
var linkElement = document.createElement('link');
linkElement.setAttribute('rel', 'stylesheet');
linkElement.setAttribute('type', 'text/css');
linkElement.setAttribute('href', 'data:text/css;charset=UTF-8,' + encodeURIComponent(css));
document.getElementsByTagName('head')[0].appendChild(linkElement);
}

module.exports = {
Expand All @@ -328,7 +324,7 @@ module.exports = {
initialize: function() {
var keyframes = 'from{opacity:0.99}to{opacity:1}';

injectCSS(this,
injectCSS(
// DOM rendering detection
// https://davidwalsh.name/detect-node-insertion
'@-webkit-keyframes ' + CSS_RENDER_ANIMATION + '{' + keyframes + '}' +
Expand Down