How to detect Content Security Policy (CSP)

后端 未结 6 1532
长发绾君心
长发绾君心 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:11

    You can try to catch a CSP violation error using an event "securitypolicyviolation"

    From: https://developer.mozilla.org/en-US/docs/Web/API/SecurityPolicyViolationEvent

    example:

    document.addEventListener("securitypolicyviolation", (e) => {
      console.log(e.blockedURI);    
      console.log(e.violatedDirective);    
      console.log(e.originalPolicy);
    });
    

提交回复
热议问题