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

Inline Style doesn't work with Content Security Policy #2087

Closed
michaelbaisch opened this issue Dec 17, 2021 · 5 comments · Fixed by #2115
Closed

Inline Style doesn't work with Content Security Policy #2087

michaelbaisch opened this issue Dec 17, 2021 · 5 comments · Fixed by #2115

Comments

@michaelbaisch
Copy link
Contributor

michaelbaisch commented Dec 17, 2021

Hello,

after enabling CSP I get this error in the browser console when opening administrate:

Refused to apply a stylesheet because its hash, its nonce, or 'unsafe-inline' does not appear in the style-src directive of the Content Security Policy.

Referencing this line:

<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">

Maybe this simple style could be transferred to the administrate css?

Greetings,
Michael

@pablobm
Copy link
Collaborator

pablobm commented Dec 28, 2021

My understanding of CSP is rusty at the moment 😓 How did you enable CSP in your app? Do you know what the alternatives are?

@michaelbaisch
Copy link
Contributor Author

That's pretty straight forward - see https://edgeguides.rubyonrails.org/security.html#content-security-policy

In config/initializers/content_security_policy.rb active this:

Rails.application.config.content_security_policy do |policy|
  policy.default_src :self, :https
  policy.font_src    :self, :https, :data
  policy.img_src     :self, :https, :data
  policy.object_src  :none
  policy.script_src  :self, :https
  policy.style_src   :self, :https
end

If you want to allow inline style and js (but only in a separate tag) you need to activate nonce generation:

Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }

Rails.application.config.content_security_policy_nonce_directives = %w(script-src style-src)

This way you could so things like:

<%= javascript_tag nonce: true do %>
<% end -%>

<style type="text/css" nonce="<%= content_security_policy_nonce %>">
</style>

But as I said earlier it would be a cleaner solution if that display: none would be in a regular css file and not inline.

@pablobm
Copy link
Collaborator

pablobm commented Jan 11, 2022

I see, that makes sense. I wonder if the style was put inline originally due to some browser incompatibility or some such?

Browsers are better-behaved creatures these days, so perhaps we can try move this into the stylesheets as you first proposed. Would you be able to create a PR?

michaelbaisch added a commit to michaelbaisch/administrate that referenced this issue Jan 14, 2022
nickcharlton pushed a commit that referenced this issue Jan 24, 2022
@kaka-ruto
Copy link

It was the grammarly browser extension for me that I had to disable 🤦🏽‍♂️

@pablobm
Copy link
Collaborator

pablobm commented Jul 19, 2024

@kaka-ruto - Thank you for sharing that! 🙌 It may be helpful to other people who end up here in the future.

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.

3 participants