简体   繁体   English

Web扩展-Iframe Source执行脚本时出现内容安全策略错误

[英]Web Extension - Content Security Policy Error when Iframe Source Executes Script

I have a firefox web extension that uses a content script to inject HTML into a webpage when a button is clicked. 我有一个firefox Web扩展程序,该扩展程序使用内容脚本在单击按钮时将HTML注入网页。 The HTML that is injected consists of an iFrame nested within several divs. 注入的HTML由嵌套在多个div中的iFrame组成。

Here's the relevant portion of the content script: 这是内容脚本的相关部分:

var iFrame = document.createElement("iFrame");
iFrame.id = "contentFrame";
iFrame.style.cssText = "width: 100%; height: 100%; border: none;";
iFrame.src = browser.extension.getURL("inject-content/inject.html");

var boxDiv = document.createElement("div");
boxDiv.style.cssText = "left: calc(100% - 390px); position: fixed; top: 0px; width: 390px; z-index: 1;"

var zeroDiv = document.createElement("div");
zeroDiv.style.cssText = "position: fixed; width: 0px; height: 0px; top: 0px; left: 0px; z-index: 2147483647;";

var outerDiv = document.createElement("div");
outerDiv.id = outerDivID;

boxDiv.appendChild(iFrame);
zeroDiv.appendChild(boxDiv);
outerDiv.appendChild(zeroDiv);
document.body.appendChild(outerDiv);

As indicated by the code, the source of the iFrame is a file called "inject.html". 如代码所示,iFrame的源是一个名为“ inject.html”的文件。 Two important features of inject.html are: inject.html的两个重要功能是:

1) A script tag (inside the header) that refers to the file for a javascript library in the same directory. 1)脚本标记(位于标头内),指向同一目录中javascript库的文件。

<script src="perfect-scrollbar.js"></script>

2) A piece of inline javascript that uses "perfect-scrollbar.js". 2)使用“ perfect-scrollbar.js”的一段内联JavaScript。 Also, for reference, here is the library itself: https://github.com/utatti/perfect-scrollbar 另外,作为参考,这里是库本身: https : //github.com/utatti/perfect-scrollbar

<script>
console.log("Hello World");

var perfectScrollbar = new PerfectScrollbar("#container");
</script>

When I directly open the file from my computer (ie - right-click, open with Chrome), it works fine. 当我直接从计算机上打开文件时(即-右键单击,使用Chrome打开),它可以正常工作。 However, when I use my extension in Firefox, I get the following error: 但是,当我在Firefox中使用扩展程序时,出现以下错误:

Content Security Policy: The page's settings blocked the loading of a resource at self (“script-src”). 内容安全策略:页面的设置阻止自我加载资源(“ script-src”)。

Source: console.log("hello world"); 来源:console.log(“ hello world”);

va.... va ....

I read through the documentation and it seems like in-line javascript is not allowed by the default content security policy. 我通读了文档 ,似乎默认内容安全策略不允许使用串联javascript。

Relevant documentation: 相关文件:

The default content security policy for extensions is: 扩展的默认内容安全策略是:

"script-src 'self'; object-src 'self';"

This will be applied to any extension that has not explicitly set its own content security policy using the content_security_policy manifest.json key. 这将适用于任何未使用content_security_policy manifest.json键显式设置其自身的内容安全策略的扩展。 It has the following consequences: 它具有以下后果:

You may only load and resources that are local to the extension. 您只能加载扩展名本地的资源。

The extension is not allowed to evaluate strings as JavaScript. 该扩展名不允许将字符串评估为JavaScript。

Inline JavaScript is not executed. 内联JavaScript不执行。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#Unsafe_inline_script https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#Unsafe_inline_script

I would solve the problem of inline javascript by using import statements, but according to Mozilla, those are not supported in Firefox right now: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import 我可以通过使用import语句解决内联javascript的问题,但是根据Mozilla的说法,Firefox目前不支持这些语句: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements /进口

According to the documentation, it is possible to allow some inline Javascript by creating a sha-256 hash of your script. 根据文档,可以通过创建脚本的sha-256哈希来允许某些内联Javascript。

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/content_security_policy https://developer.mozilla.org/zh-CN/Add-ons/WebExtensions/manifest.json/content_security_policy

Allow the extension to execute inline scripts, by supplying the hash of the script in the "script-src" directive. 通过在“ script-src”指令中提供脚本的哈希,允许扩展执行内联脚本。

Allow the inline script: <script>alert('Hello, world.');</script> : 允许内联脚本: <script>alert('Hello, world.');</script>

"content_security_policy": "script-src 'self' 'sha256-qznLcsROx4GACP2dm0UCKCzCG+HiZ1guq6ZZDob/Tng='; object-src 'self'"

and this 和这个

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#Unsafe_inline_script https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#Unsafe_inline_script

Alternatively, you can create hashes from your inline scripts. 或者,您可以从内联脚本创建哈希。 CSP supports sha256, sha384 and sha512. CSP支持sha256,sha384和sha512。

Content-Security-Policy: script-src 'sha256-B2yPHKaXnvFWtRChIbabYmUBFZdVfKKXHbWtWidDVF8='

When generating the hash, don't include the <script> tags and note that capitalization and whitespace matter, including leading or trailing whitespace. 生成哈希值时,请勿包含<script>标记,并注意大小写和空格很重要,包括前导或尾随空格。

So, I created a sha-256 hash of my script by highlighting the following text in "inject.html": 因此,我通过突出显示“ inject.html”中的以下文本来创建脚本的sha-256哈希:

突出示例

and using a SHA-256 calculator (I chose base-64 to remain consistent with the example): https://hash.online-convert.com/sha256-generator 并使用SHA-256计算器(我选择了base-64以便与示例保持一致): https : //hash.online-convert.com/sha256-generator

and changing my "manifest.json" file to: 并将我的“ manifest.json”文件更改为:

{
    "manifest_version": 2,
    "name": "Summarizer",
    "version": "1.0",

    "description": "Summarizes webpages",

    "permissions": [
        "activeTab",
        "tabs",
        "storage",
        "downloads",
        "*://*.smmry.com/*"
    ],

    "icons":
    {
        "48": "icons/border-48.png"
    },

    "browser_action":
    {
        "browser_style": true,
        "default_popup": "popup/choose_length_page.html",
        "default_icon":
        {
            "16": "icons/summarizer-icon-16.png",
            "32": "icons/summarizer-icon-32.png"
        }
    },

    "web_accessible_resources": [
        "inject-content/inject.html"
    ],

    "content_security_policy": "script-src 'self' 'sha256-GdCLzZEX8DPwLRiIBZvv6ffymh4hz/9NgjmzyPv+lGM='; object-src 'self'"

}

Yet, my error still remained! 但是,我的错误仍然存​​在!

As such, my question is, 因此,我的问题是

How can I allow my inline Javascript to execute without having this error? 如何允许我的内联Javascript执行而不会出现此错误? Is there a way to do this without having a hash? 有没有办法做到这一点而没有哈希?

Ideally, I'd like to avoid using inline javascript altogether, and instead move all of my inline javascript into the file of my content script. 理想情况下,我想避免完全使用嵌入式JavaScript,而是将所有嵌入式JavaScript移到内容脚本文件中。 Since Firefox doesn't support import statements, is it possible for me to do that, and if so how? 由于Firefox不支持import语句,我可以这样做吗? I have no experience with tools such as Babel. 我没有使用Babel这样的工具的经验。

Although the webpage says you can have trailing/leading whitespace, I was having issues with that. 尽管网页上说您可以使用尾随/前导空格,但我对此有疑问。 The solution turned out to be putting all my code on one line (and re-calculating the base 64 SHA-256 hash). 原来,该解决方案是将我所有的代码放在一行上(然后重新计算基本的64 SHA-256哈希值)。

<script>console.log("Hello World");var perfectScrollbar = new PerfectScrollbar("#container",{suppressScrollX: true});</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM