plotly.js icon indicating copy to clipboard operation
plotly.js copied to clipboard

Plotly uses inline CSS

Open sanmai-NL opened this issue 8 years ago • 27 comments

Which necessitates the unsafe-inline Content Security Policy directive, or another workaround. Can we get rid of the inline styling somehow?

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='), or a nonce ('nonce-...') is required to enable inline execution.

l.addStyleRule @ plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7
plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7 Uncaught TypeError: Cannot read property 'insertRule' of null
    at Object.l.addStyleRule (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at Object.1.../src/lib (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at i (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7
    at Object.724.../build/plotcss (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at i (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7
    at Object.12.../src/core (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at i (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7
    at Object.20../aggregate (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at i (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at t (plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7)
    at plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7
    at plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7
    at plotly__SHA256:3b219dbb9a6101432f4c65d4d4f35a0c5bff2ed2b9d43a007d46faa937b2ac81.mjs:7

sanmai-NL avatar Feb 09 '18 09:02 sanmai-NL

Yes, looks like it is supported in d3 (v3). I get 🟢 using this snippet (using inline script for testing we are not using inline style 🙈):

<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="Content-Security-Policy" content="style-src 'self'">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.0.0/d3.js"></script>
   </head>
   <body>
      <div id="root">Hello world</div>
      <script>d3.select("#root").style("background-color", "green");</script>
   </body>
</html>

anders-kiaer avatar Jun 14 '21 20:06 anders-kiaer

Great! Looks like someone should tackle the addStyleRule and then see what remains I guess. Maybe just removing that function or replacing it with a return would be enough to find the next violation?

nicolaskruchten avatar Jun 14 '21 21:06 nicolaskruchten

I hope this is not off-topic, but my issue is somewhat connected. I want to use CSS on Plotly.js, but I find absolutely no resources for this.

I'm using:

    "plotly.js": "^2.18.0",
    "react": "^18.2.0",

andmy plots are not coming from backend, but rather from React, via:

import Plot from "react-plotly.js";

I have a plot whose title looks like:

        title: `<i><b>${title}</b></i>`,

and I wanted it to look like:

        title: `<span class='boldit'>${title}</span>`,

where the style is:

.boldit {
  font-style: italic;
  font-weight: bold;
}

The only way I could apply something was to hack into the DevTools and figure out that the title has a style gtitle, but this has a bit of inline style that always supersedes my CSS, like font-weight: normal;.

Is there a way I could control all these feature of the title without inlining HTML and styles?

ricardo-reis-1970 avatar Feb 20 '23 14:02 ricardo-reis-1970

I was searching a way to add styling to the plots as @ricardo-reis-1970 mentioned. We have a webpage with theming and I have to change a lot of colors if the page is dark. I don't want this information duplicated in the code (js/css). I'm used to zenUML.com and their way of styling all and everything with css and this would be very cool. Perhaps having a mode of "remove-all-inner-styling" would already help. I don't know if the paths for the traces has something like id="trace-0" and similar for other parts of the plot. This would be very cool. A CSS template for this case (no styling in the svg would result in a transparent svg?) would be good.

eqvis avatar May 12 '23 08:05 eqvis

if someone wouldn't mind adding this to the code, it's kinda magical.

                function l(t, e, r) {
                    var n = "plotly.js-style-" + t
                      , a = document.getElementById(n);
                    a || ((a = document.createElement("style")).setAttribute("id", n),
>>>                 a.setAttribute("nonce", document.querySelector("script[nonce]")?.nonce || ""),
                    a.appendChild(document.createTextNode("")),
                    document.head.appendChild(a));
                    var o = a.sheet;
                    o.insertRule ? o.insertRule(e + "{" + r + "}", 0) : o.addRule ? o.addRule(e, r, 0) : i.warn("addStyleRule failed")
                }

FirefighterBlu3 avatar Oct 03 '23 00:10 FirefighterBlu3

Just another vote for implementing https://github.com/plotly/plotly.js/pull/6239 or something similar. The organization I work for has infrastructural security restrictions that simply won't allow unsafe-inline in responses. This is a blocker for us and I suspect many large security conscious organizations.

sesa529917 avatar Mar 13 '24 16:03 sesa529917

Allowing styles based on returned hashes works, example: style-src 'sha-hash1' 'sha-hash2' 'sha-...'. Though I don't know whether those are the same for all chart types, might be too much work.

orazio1234 avatar Aug 05 '24 12:08 orazio1234