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

Allow usage of this library without forcing CSP unsafe-inline #12

Closed
tzimmermann opened this issue Apr 6, 2020 · 3 comments · Fixed by #14
Closed

Allow usage of this library without forcing CSP unsafe-inline #12

tzimmermann opened this issue Apr 6, 2020 · 3 comments · Fixed by #14

Comments

@tzimmermann
Copy link
Contributor

We are currently trying to enforce stricter Content Security Policies on our webapp.
The webapp uses echarts-for-react, and this one in turn uses size-sensor.

Unfortunately, we are forced to set style-src: 'unsafe-inline' due to this dependency, because there is a code snippet in object.js that sets an inline style here:

obj.setAttribute('style', SensorStyle);

It would be really good if we could enhance the security of our webapp by removing unsafe-inline and still be able to use echarts.

I see a couple of ways to fix this:

  • Put styles into a separate CSS file and use CSS classes to reference them.
  • Create separate <style> tags in the same document and add a nonce attribute to them that is populated from a <meta> property on the page (ass JSS does it)
@hustcc
Copy link
Owner

hustcc commented Apr 6, 2020

The second way means add style as below:

<style>
   .size-sensor-object {
     // ...
   }
</style>

then use class name instead of inline style? What the meaning of a nonce attribute?

@tzimmermann
Copy link
Contributor Author

then use class name instead of inline style? What the meaning of a nonce attribute?

It is basically a way to whitelist which scripts/styles are allowed to be executed on a page to protect against certain scripting attacks.

The nonce is a random token that is generated on the server new for every page visit.

Look for example here for a detailed explanation: https://stackoverflow.com/a/42924000

We would need a way to "inject" our nonce attribute from the document into the <style> tag that size-sensor generates. Not sure what is best practice here for other libraries, but I quite like JSS's approach, they read the value from the document's <meta> tag.

@hustcc
Copy link
Owner

hustcc commented Apr 7, 2020

@tzimmermann can give me a pr for this? I would like to see whether it will increase the complexity of usage.

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

Successfully merging a pull request may close this issue.

2 participants