如何在 chrome 扩展 manifest.json 文件中设置多个内容安全策略

how to set multiple Content Security Policies in a chrome extension manifest.json file

Google chrome documentation 中,我发现我可以添加内容安全策略以允许外部 javascript 文件在我的扩展上工作。

但我找不到如何添加多个。它是字符串数组吗?

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

我试过像那样放置多行,但它不起作用。出现错误:

拒绝加载脚本 https://example.com because it violates the following Content Security Policy directive: "script-src 'self' https://example.com”。请注意,'script-src-elem' 未明确设置,因此 'script-src' 用作备用。

CSP 策略是单个字符串(包含以分号分隔的 指令 及其参数列表)。它适用于所有扩展页面。

如果您需要一个具有多个来源的政策,您可以这样做。事实上,您已经知道了:'self'https://example.com 是两个来源。

阅读一般 CSP 和 script-src 指令,例如on the MDN.

Syntax

One or more sources can be allowed for the script-src policy:

Content-Security-Policy: script-src <source>;
Content-Security-Policy: script-src <source> <source>;

所以你只需要space-将它们分隔在script-src和分号之间。

确保您的来源不包含路径。
例如。 https://example.com 可以,但 https://example.com/https://example.com/script.js 不行。

如果您需要针对不同页面的多个独立策略,恐怕您不能那样做。