【问题标题】:chrome extension policy declaration not workingchrome 扩展策略声明不起作用
【发布时间】:2016-06-07 02:27:39
【问题描述】:

我在我的 chrome 扩展程序中加载外部 js 文件时遇到问题。这是我的清单中的 csp 条目:

"content_security_policy": "script-src 'self' 'unsafe-eval' 'unsafe-inline' http://proto.office.atlassian.com; object-src 'self'" 

这是我在 popup.html 中调用脚本的方式:

<script src="http://proto.office.atlassian.com/prototypes.js"></script>

这是我得到的错误:

Refused to load the script 'http://proto.office.atlassian.com/prototypes.js' because it violates the following Content Security Policy directive: "script-src 'self'"

我已确认我的 CORS 已在服务器上正确设置,我可以通过 XMLHttpRequest 拉出脚本就好了,但我似乎无法通过脚本标签加载一个或一旦我抓住它就对其进行评估.任何帮助将不胜感激:)

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension chromium


    【解决方案1】:

    content security policy 必须在清单中明确允许外部脚本。

    如果您需要一些外部 JavaScript 或对象资源,您可以通过将应接受的脚本的安全来源列入白名单来在有限程度上放宽政策...

    允许通过 HTTPS 从 example.com 加载脚本资源的宽松策略定义可能如下所示:

    "content_security_policy":"script-src 'self' https://example.com; object-src 'self'"
    

    脚本只能通过 HTTPS 加载到扩展中,因此您必须通过 HTTPS 加载 jQuery CDN 资源:

    <script src="https://ajax.googleapis.com/..."></script>
    {
    "manifest_version": 2,
    "name": "One-click Kittens",
    "description": "This extension demonstrates a 'browser action' with kittens.",
    "version": "1.0",
    "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
    },
    "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
    }
    

    【讨论】:

    • 啊,谢谢,没有意识到它必须是 https。在我的服务器上设置 ssl,一切正常 :)
    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 2021-06-24
    • 2013-02-01
    • 1970-01-01
    • 2013-12-24
    • 2017-05-26
    • 2012-01-22
    • 2012-08-08
    相关资源
    最近更新 更多