Extjs、Chrome扩展和内容安全策略

Extjs, Chrome Extension and Content Security Policy

本文关键字:安全策略 扩展 Chrome Extjs      更新时间:2023-09-26

我正在尝试用Ext JS 5.1.0开发一个Google Chrome扩展。

当我试图将ext-all.js添加到default_popup html中时,我发现Google chrome扩展不能再使用动态脚本评估技术,如eval()或new Function(),也不能将js代码字符串传递给将导致使用eval(()的函数,如setTimeout()。

因此,在设置过程中,googlechrome调试器返回以下错误:

拒绝将字符串求值为JavaScript,因为"unsafe eval"为不是以下内容安全中允许的脚本源策略指令:"script src'self'chrome扩展资源:"
ext-all-debug.js:8742 ext.ClassManager.ext.apply.getInstantiator

这是的错误代码

        getInstantiator: function(length) {
            var instantiators = this.instantiators,
                instantiator, i, args;
            instantiator = instantiators[length];
            if (!instantiator) {
                i = length;
                args = [];
                for (i = 0; i < length; i++) {
                    args.push('a[' + i + ']');
                }
                // The problem is here 
                instantiator = instantiators[length] = new Function('c','a','return new c(' + args.join(',') + ')');
                instantiator.name = "Ext.create" + length;
            }
            return instantiator;
        },

我找到了一个改变content_security_policy 的解决方案

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

将这一行添加到manifest.json中允许使用动态脚本评估技术(但这很危险)。

所以,我想保留标准的谷歌铬安全权限。有没有办法解决这个问题?

您可以看看这里概述的sandbox方法:使用Sencha Ext JS 构建应用程序

这是关于Chrome应用程序的,但原则仍然适用。您可以在清单中创建一个具有sandbox属性的沙盒页面,将其嵌入到页面中,并使用postMessage安全地与其通信。沙盒页面无法运行提升权限的Chrome API,这使得eval使用起来更安全。

同样,Chrome文档中有一篇命名恰当的文章:在Chrome扩展中使用eval。安全地