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

CSP blocks a line of code #221

Open
habibmousavi opened this issue Jul 13, 2022 · 1 comment
Open

CSP blocks a line of code #221

habibmousavi opened this issue Jul 13, 2022 · 1 comment

Comments

@habibmousavi
Copy link

While extending my deep appreciations to the author, I wanted to ask him to rewrite a line of code, as it is getting blocked by the CSP directives even though we use a nonce. The line is the following
ripple.setAttribute('style', convertStyle(rippleStyle));
This just throws the following error:
Content Security Policy: The page’s settings blocked the loading of a resource at inline (“style-src”).
I'd be really grateful if some action is taken to fix this issue.

@marconue
Copy link

marconue commented Feb 9, 2023

You can create a new .js file with the following code, it's a little "hack" that allows you to replace the .setAttribute, more info in the webpage https://csplite.com/csp/test343/

`var setAttribute_ = Element.prototype.setAttribute; // Save source of Elem.setAttribute funct
Element.prototype.setAttribute = function (attr, val) {
if (attr.toLowerCase() !== 'style') {
setAttribute_.apply(this, [attr, val]); // Call the saved native Elem.setAttribute funct
}
else { // 'color:red; background-color:#ddd' -> el.style.color='red'; el.style.backgroundColor='#ddd';
var arr = val.split(';').map((el, index) => el.trim());
for (var i = 0, tmp; i < arr.length; ++i) {
if (! /:/.test(arr[i])) continue; // Empty or wrong
tmp = arr[i].split(':').map((el, index) => el.trim());
this.style[camelize(tmp[0])] = tmp[1];
//console.log(camelize(tmp[0]) + ' = '+ tmp[1]);
}
}
}

function camelize(str) {
return str
.split('-') // Parse 'my-long-word' to ['my', 'long', 'word']
.map(
// Coverts all elements to uppercase except first: ['my', 'long', 'word'] -> ['my', 'Long', 'Word']
(word, index) => index == 0 ? word : word[0].toUpperCase() + word.slice(1)
)
.join(''); // Join ['my', 'Long', 'Word'] to 'myLongWord'
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants