【问题标题】:How to overcome Ajax Content Security Policy directive?如何克服 Ajax 内容安全策略指令?
【发布时间】:2017-10-18 09:45:11
【问题描述】:

我在各种网站(使用 Selenium 和 Python)上注入一些 JS 代码,以将 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”这样的相对路径。但是从头开始编写代理服务器是相当复杂的。

那么,我可以做些什么来克服这个内容安全政策?

【问题讨论】:

    标签: javascript ajax web content-security-policy


    【解决方案1】:

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

    【讨论】:

    • 非常感谢。真的有帮助。我正在为调用我的后端的 WhatsApp 制作 Tampermonkey 脚本被卡住了半小时
    【解决方案2】:

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

    https://easyxdm.net/wp/

    问候

    【讨论】:

    • 很遗憾,我无法控制后端。
    • 您是否正在对您的服务器进行 ajax 调用?
    • 网站不是我的,所以后台无法访问
    【解决方案3】:

    我想通了!原来你可以禁用所有的安全检查:

    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)
    

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2018-02-14
      • 1970-01-01
      • 2018-12-25
      • 2014-06-06
      • 2021-12-07
      • 2018-08-14
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多