How to detect Content Security Policy (CSP)

后端 未结 6 1533
长发绾君心
长发绾君心 2021-01-03 21:19

I noticed that GitHub and Facebook are both implementing this policy now, which restricts third party scripts from being run within their experience/site.

Is there a

6条回答
  •  春和景丽
    2021-01-03 22:07

    Currently, there is no way to do so in shipping browsers.

    However, something such as the following should work, per spec, and does in Chrome with experimental web platform features enabled in chrome://flags/:

    function detectCSPInUse() {
      return "securityPolicy" in document ? document.securityPolicy.isActive : false;
    }
    

    The SecurityPolicy interface (what you get from document.securityPolicy if it is implemented) has a few attributes that give more detail as to what is currently allowed.

提交回复
热议问题