Refused to apply inline style because it violates the following Content Security Policy directive

后端 未结 6 2347
心在旅途
心在旅途 2020-12-08 09:08

So, in about 1 hour my extensions failed hard.

I was doing my extension and it was doing what I pretended. I made some changes, and as I didnt liked I deleted them,

相关标签:
6条回答
  • 2020-12-08 09:39

    You can use in Content-security-policy add "img-src 'self' data:;" And Use outline CSS.Don't use Inline CSS.It's secure from attackers.

    0 讨论(0)
  • 2020-12-08 09:43

    You can also relax your CSP for styles by adding style-src 'self' 'unsafe-inline';

    "content_security_policy": "default-src 'self' style-src 'self' 'unsafe-inline';" 
    

    This will allow you to keep using inline style in your extension.

    Important note

    As others have pointed out, this is not recommended, and you should put all your CSS in a dedicated file. See the OWASP explanation on why CSS can be a vector for attacks (kudos to @ KayakinKoder for the link).

    0 讨论(0)
  • 2020-12-08 09:48

    As the error message says, you have an inline style, which CSP prohibits. I see at least one (list-style: none) in your HTML. Put that style in your CSS file instead.

    To explain further, Content Security Policy does not allow inline CSS because it could be dangerous. From An Introduction to Content Security Policy:

    "If an attacker can inject a script tag that directly contains some malicious payload .. the browser has no mechanism by which to distinguish it from a legitimate inline script tag. CSP solves this problem by banning inline script entirely: it’s the only way to be sure."

    0 讨论(0)
  • 2020-12-08 09:48

    Another method is to use the CSSOM (CSS Object Model), via the style property on a DOM node.

    var myElem = document.querySelector('.my-selector');
    myElem.style.color = 'blue';
    

    More details on CSSOM: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.style

    As mentioned by others, enabling unsafe-line for css is another method to solve this.

    0 讨论(0)
  • 2020-12-08 09:49

    Well, I think it is too late and many others have the solution so far.

    But I hope this can Help:

    I'm using react for an identity server so 'unsafe-inline' is not an option at all. If you look at your console and actually read the CSP docs, you might find that there are three options for solving the issue:

    1. 'unsafe-inline' as it says is unsafe if your project is using CSPs is for one reason and it is like throwing out the complete policy, will be the same to no have CSP policy at all

      1. 'sha-XXXCODE' this is good, safe but not optimal because there is a lot of manual work and every compilation the SHA might change so it will become easily a nightmare, use only when the script or style is unlikely to change and there are few references

      2. Nonce. This is the winner!

    Nonce works in the similar way as scripts

    CSP HEADER ///csp stuff nonce-12331

    <script nonce="12331">
       //script content
    </script>
    
    

    Because the nonce in the csp is the same that the tag, the script will be executed

    In the case of inline styles, the nonce also came in the form of attribute so the same rules apply.

    so generate the nonce and put it on your inline scritps

    If you are using webpack maybe you are using the style-loader

    the following code will do the trick

    
    module.exports = {
      module: {
        rules: [
          {
            test: /\.css$/i,
            use: [
              {
                loader: 'style-loader',
                options: {
                  attributes: {
                    nonce: '12345678',
                  },
                },
              },
              'css-loader',
            ],
          },
        ],
      },
    };
    
    
    0 讨论(0)
  • 2020-12-08 09:50

    As per http://content-security-policy.com/ The best place to start:

        default-src 'none'; 
        script-src 'self'; 
        connect-src 'self'; 
        img-src 'self'; 
        style-src 'self';
        font-src 'self';
    

    Never inline styles or scripts as it undermines the purpose of CSP. You can use a stylesheet to set a style property and then use a function in a .js file to change the style property (if need be).

    0 讨论(0)
提交回复
热议问题