【问题标题】:'unsafe-eval' on chrome extensionchrome 扩展上的“不安全评估”
【发布时间】:2014-10-07 18:24:03
【问题描述】:

我正在尝试运行以下命令:

chrome.tabs.onCreated.addListener(function (tab){
    if (tab.url.indexOf(".salesforce.com/") != -1 || tab.url.indexOf(".force.com/") != -1) {
        chrome.tabs.executeScript(tab.id, {
            "file": "loadScript.js"
        }, function () {
            console.log("Script Executed .. ");
        });
    } else {
        var wrongTab = chrome.i18n.getMessage("wrongTab");
        console.log(wrongTab);
        alert(wrongTab);
    }
});

应该(理论上)在页面加载时运行 loadScript.js 文件.... loadScript.js 文件如下,这应该将文件附加到正在运行的页面,而不是附加到后台页面目前:

/* Create a scriipt element in head of HTML and put /soap/ajax/31.0/connection.js in the src  */
var connectJsUrl = "/connection.js";

function loadScript(url, callback) {
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.src = url;
    var done = false;
    script.onload = script.onreadystatechange = function() {
        if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
            done = true;
            callback();
            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
        }
    };
    head.appendChild(script);
}

loadScript(connectJsUrl, function() {
    console.log("Script Confirmed...")
});

/* Check to see if the file have been appended correctly and works correctly */
var JSFile = "chrome-extension://" + window.location.host + connectJsUrl;
var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
if (req == null) {
    console.log("Error: XMLHttpRequest failed to initiate.");
};
req.onload = function() {
    try {
        eval(req.responseText);
    } catch (e) {
        console.log("There was an error in the script file.");
    }
};
try {
    req.open("GET", JSFile, true);
    req.send(null);
} catch (e) {
    console.log("Error retrieving data httpReq. Some browsers only accept cross-domain request with HTTP.");
};

我还是 Chrome 扩展和 .js 的新手,如果我犯了一个愚蠢的错误,请原谅:)

我从中得到的只有以下几点: 拒绝将字符串评估为 JavaScript,因为 'unsafe-eval' 不是以下内容安全策略指令中允许的脚本来源:“script-src 'self' chrome-extension-resource:”。

【问题讨论】:

    标签: javascript google-chrome-extension


    【解决方案1】:

    为防止跨站点脚本,Google 已阻止 eval 功能。

    要解决这个问题,将此代码添加到 ma​​nifest.json

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

    如果您需要进一步解释,请发表评论

    【讨论】:

    • 谢谢先生!我将它添加到 manifest.json 中,它一开始不起作用。但是在我强制重启 Chrome 扩展程序后,它工作得很好!
    • 解决了我的问题。在 Chrome 扩展选项页面中运行 Vue/Vuetify。
    • 是否有详细说明 unsafe-eval 的官方文档?我在 chrome 扩展文档的 CSP 政策部分没有找到它。
    • 拒绝将字符串评估为 JavaScript,因为 'unsafe-eval' 不是以下内容安全策略指令中允许的脚本来源:“script-src 'self'”。
    • 在清单 3 中,不允许使用 unsafe-eval
    【解决方案2】:

    重要

    如前所述,将其添加到您的 manifest.json:

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

    确保将“manifest_version”设置为 2 aka

    //this
    "manifest_version": 2
    

    出于某些安全原因,适用于 manifest_version 3 的 Chrome 扩展程序不支持不安全的评估。

    还要确保重新加载您的扩展。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      相关资源
      最近更新 更多