javascript chrome扩展上的“unsafe-eval”

2hh7jdfx  于 2022-12-21  发布在  Java
关注(0)|答案(4)|浏览(683)

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

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”不是以下内容安全策略指令中允许的脚本源:“脚本源代码”自“chrome扩展资源:“。

e4yzc0pl

e4yzc0pl1#

为了防止跨站点脚本,Google屏蔽了eval函数。
要解决此问题,请将此代码添加到manifest.json

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

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

pxiryf3j

pxiryf3j2#

重要提示

如前所述,将此添加到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扩展不支持不安全的评估。
同时确保重新加载您的扩展。

tpgth1q7

tpgth1q73#

用于清单V3

您不能在manifest v3中运行带有不安全eval的代码,如果您正在使用webpack或vite等任何捆绑程序,您可以更改代码以不使用eval,或者检查包捆绑(如果它包含任何eval),以下是您不应在manifest 3中使用的语法列表

  • 求值()
  • 设置超时()
  • 设置间隔()
  • 函数()
  • 设置立即()
  • 执行脚本()

将content_security_policy与unsafe-eval一起添加是不安全的,因为站点可能易于XSS attack
但是如果您碰巧使用了任何wasm代码,那么下面的配置可以避免manifest 3的eval

"content_security_policy": {
   "extension_page":"script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
}

如果你正在使用任何iframe添加以下代码

"content_security_policy": {
   "extension_page":"script-src 'self' 'wasm-unsafe-eval'; object-src 'self'",
   "sandbox":"script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
}
x6yk4ghg

x6yk4ghg4#

经过试验,我发现运行一个托管html文件的web服务器(也就是运行eval的web服务器)仍然可以工作,然后将结果发送到父窗口并完成。
客户:

var elm = document.createElement('iframe');
elm.src = 'http://localhost:3000'; // I used a basic expressJS server
document.body.appendChild(elm);

服务器:

const express = require('express');
const socketIO = require('socket.io'); // Optional
const http = require('http');
const path = require('path');

const app = express();
let server = http.createServer(app);
let io = socketIO(server);

const publicPath = path.resolve(__dirname, '../public');
const port = process.env.PORT || 3000;

app.use(express.static(publicPath));

io.on('connection', (client) => {
    console.log('User connected');
});

server.listen(port, (err) => {
    if (err) throw new Error(err);
    console.log(`Server running on port ${port}`);
});

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test</title>
</head>
<body>
    <script>
        eval(alert('test'))
    </script>

    <!-- Other html.. -->
</body>
</html>

相关问题