2017-10-18 54 views
0

我在各种网站上注入了一些JS代码(使用Selenium和Python)将POST请求发送到我的本地Web服务器。在一些网站上它工作正常。但大多数情况下,我没有收到请求。认为这是因为内容安全策略。如何克服Ajax内容安全策略指令?

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

Refused to connect to ' http://10.50.50.127:7777/ ' because it violates the following Content Security Policy directive: "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”这样的相对路径。但从头开始编写代理服务器非常复杂。

那么,我能做些什么来克服这个内容安全策略呢?

回答

0

如果你控制双方,那么你可以使用。

https://easyxdm.net/wp/

问候

+0

可悲的是,我没有控制后端。 –

+0

你正在对你的服务器进行ajax调用吗? –

+0

该网站不是我的,所以我无法访问后端端 –

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)