Chrome扩展程序 - 内容安全策略 - 执行内嵌代码 [英] Chrome Extension - Content Security Policy - executing inline code

查看:863
本文介绍了Chrome扩展程序 - 内容安全策略 - 执行内嵌代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Chrome扩展中使用了一个外部JavaScript库。
我有内联执行,所以我得到以下类型的错误



(我在控制台上得到的错误)


拒绝执行JavaScript URL,因为它违反了以下
内容安全策略指令:script-src'self'
chrome-extension://。无论是'unsafe-inline'关键字,hash
('sha256 -...')还是一个nonce('nonce -...')都需要启用内联
执行。


错误消息清楚地表明可能有解决办法。



< Chrome内容安全策略说不可能。许多相关问题引用了此链接。



博客这个博客说这是可能的,但可能这只适用于旧的Chrome扩展。



任何可能的解决方法?



PS:不希望/无法更改我正在使用的整个库。

编辑:如何使用散列或随机数启用内联执行。

解决方案

不可以,这是不可能的。自明细版本2以来,Chrome扩展程序明确忽略了$ c> unsafe-inline 。 文档(强调我的):


没有放松对内嵌JavaScript执行限制的机制。 特别是,设置一个包含'unsafe-inline'的脚本策略将无效。 错误消息提到了几种可能的方式,但文档清楚地表明,没有CSP将允许内联脚本,并且忽略不安全内联只是其中一项措施。



更新




从Chrome 46开始,内嵌脚本可以通过指定base64编码哈希列入白名单的政策中的源代码。该散列必须以所使用的散列算法(sha256,sha384或sha512)作为前缀。请参阅示例元素的哈希用法。


请参阅此答案,以便更深入地了解白名单。


I am using an external JavaScript lib in my chrome extension. I has inline execution, so I get following kind of error

(The error I get on console)

Refused to execute JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension://". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

The error message clearly says there is a work-around possible.

Chrome-Content Security Policy says not possible. Many related question cited this link.

Blog This blogger says it is possible, but probably this is applicable to only older chrome extension.

Any work around possible?

PS: don't wanna/can't change the entire library I am using.

EDIT: how to use hash or nonce to enable inline execution.

解决方案

No, this is not possible to relax this policy. unsafe-inline is specifically ignored by Chrome Extensions since manifest version 2.

Documentation (emphasis mine):

There is no mechanism for relaxing the restriction against executing inline JavaScript. In particular, setting a script policy that includes 'unsafe-inline' will have no effect.

The error message mentions several possible ways, but the docs are clear that no CSP will allow inline scripting, and ignoring unsafe-inline is but one of the measures.

Update

As of Chrome 46, inline scripts can be whitelisted by specifying the base64-encoded hash of the source code in the policy. This hash must be prefixed by the used hash algorithm (sha256, sha384 or sha512). See Hash usage for elements for an example.

See this answer for more in-depth look at whitelisting.

这篇关于Chrome扩展程序 - 内容安全策略 - 执行内嵌代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆