How to detect Content Security Policy (CSP)

后端 未结 6 1530
长发绾君心
长发绾君心 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 21:58

    What about this. For slow connections, the timeout should probably be raised. Onload is what I used to detect it and it seems to work. If it loads then CSP obviously isn't enabled or it is configured improperly.

    var CSP = 0;
    frame = document.createElement('script');
    frame.setAttribute('id', 'theiframe');
    frame.setAttribute('src', location.protocol+'//example.com/');
    frame.setAttribute('onload', 'CSP=1;');
    document.body.appendChild(frame);
    setTimeout(function(){if (0 == CSP){alert("CSP IS ENABLED");}}, 250);
    

提交回复
热议问题