浏览器扩展:eval()用户输入

时间:2017-01-23 14:48:25

标签: javascript google-chrome-extension eval sandbox firefox-webextensions

在Chrome扩展程序和Firefox WebExtensions中使用eval()(在用户输入上)是否有解决方法?

背景

我目前正在为Chrome和Firefox编写浏览器扩展程序。其中一个重要部分是用户可以指定某些过滤器。我可以轻松做到的是:
让用户在一个文本框中输入一个RegEx字符串并运行我想要过滤的内容,但不是每个人都非常了解RegEx,有些事情根本就不可能。

为了简化操作,我们假设我们正在循环几个文件夹,其中包含一些文件,我们想要使用过滤器挑选出有趣的文件。这可能看起来像这样:

filter() {
    let interestingFiles = []
    folders.forEach( folder => {
        let files = folder.files
        files.forEach( file => {
            if (file.name.includes('important')) {
                interestingFiles.push(file)
            } else if (file.sizeMB > 500) {
                interestingFiles.push(file)                
            } else if (file.sizeMB > folder.getBiggest()/100*15) {
                interestingFiles.push(file)
            }
        })
    })
    return interestingFiles
}

我想允许我的扩展程序的用户指定类似的过滤器。这样做的显而易见的方法是为用户提供一个文本框,让他/她输入代码,然后在该用户输入上运行eval()。 但是,由于安全问题,浏览器扩展程序拒绝运行eval() 现在,我的问题是,是否有解决方法?我的意思是我真的不想为此编写自己的JavaScript解析器,甚至不想使用现有的解析器。如果我可以简单地创建一个孤立的范围,eval()可以安全地执行,这将是很好的。

顺便说一下,在内容脚本中不会发生该过滤器的执行,它会在浏览器动作弹出窗口中发生,因此它可以访问后台API。

我尝试过的东西不起作用

  • 直接使用eval()
    Error: call to eval() blocked by CSP
    Content Security Policy: The page’s settings blocked the loading of a resource at self (“script-src moz-extension://57f1d9cf-71b6-4d64-9b8e-6c5b90ad7341”).
  • Components.utils.SandboxComponents.utils.evalInSandbox
    var mySandbox = new Components.utils.Sandbox("about:newtab");
    var result = Components.utils.evalInSandbox("string to eval", mySandbox);

我似乎无法使用它们,但它们似乎也被弃用了:

    TypeError: Components.utils is undefined [Learn More]
    The Components object is deprecated. It will soon be removed.
  • 我还尝试使用iframe并在
  • 中执行eval()
    document.body.innerHTML = '<html><head></head><body><iframe id="myIframe" src="about:blank"></iframe></body></html>'
    document.getElementById('myIframe').contentWindow.eval('var aa = 1;')

但这只会导致CSP错误:

    12:19:26.787 Error: call to eval() blocked by CSP
    12:19:26.789 Content Security Policy: The page’s settings blocked the loading of a resource at self (“script-src moz-extension://f0155032-64bd-40c5-8b09-3bd7a92dc90e”).  
 
    about:blank : Unable to run script because scripts are blocked internally.  (unknown)
    Use of getPreventDefault() is deprecated.  Use defaultPrevented instead.  samples
    about:blank : Unable to run script because scripts are blocked internally. (unknown)
    The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.  eventpage.html
    Content Security Policy: The page’s settings blocked the loading of a resource at self (“script-src moz-extension://6b9a242e-f1db-4efd-a4cc-85d0d5beb79c”).  sandbox.html:17
    Use of getPreventDefault() is deprecated.  Use defaultPrevented instead.  newtab
    about:blank : Unable to run script because scripts are blocked internally.  (unknown)  

我也找不到任何关于Mozilla MDN网站上清单的“沙盒”属性的提及。

0 个答案:

没有答案