如何克服Ajax内容安全策略指令?

时间:2017-10-18 09:45:11

标签: javascript ajax web content-security-policy

我在各种网站上注入一些JS代码(使用Selenium和Python)将POST请求发送到我的本地Web服务器。在一些网站上它工作正常。但大多数情况下,我没有得到请求。因为内容安全策略而计算它。

例如,当我尝试在github.com上使用Chrome中的控制台运行代码时,出现以下错误:

  

拒绝连接“http://10.50.50.127:7777/”,因为它违反了   以下内容安全策略指令:“connect-src'self'   uploads.github.com status.github.com collector.githubapp.com   api.github.com www.google-analytics.com ...“。

我的代码如下所示:

function sendData() {
            var request = new XMLHttpRequest();
            request.open('POST', 'http://10.50.50.127:7777', false);  
            request.send("test");
        }

我自己做了一些研究,并找到了一个可能的解决方案 - 使用本地代理服务器并将数据发送到像“/ test”这样的相对路径。但是从头开始编写代理服务器非常复杂。

那么,我该怎么做才能克服这个内容安全政策?

3 个答案:

答案 0 :(得分:0)

如果您控制了两侧,那么您可以使用。

https://easyxdm.net/wp/

此致

答案 1 :(得分:0)

我知道了!事实证明,您可以禁用所有安全检查:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-web-security')
chrome_options.add_argument('--allow-running-insecure-content')
browser = webdriver.Chrome(chrome_options=chrome_options)

答案 2 :(得分:0)

如果您使用的是Chrome,并且想要禁用内容安全策略,则还可以使用Chrome Web Store中名为Disable Content-Security-Policy的插件。这是Chrome用来禁用标头的插件。 我通过Tampermonkey(Chrome)注入了JS,效果很好。

相关问题