I am creating an Electron application, and per the Electron security tutorial I have added a CSP meta tag. When running the application, this issue appears in devtools.
Content Security Policy of your site blocks the use of 'eval' in JavaScript
The Content Security Policy (CSP) prevents the evaluation of arbitrary strings as JavaScript to make it more difficult for an attacker to inject unauthorized code on your site.
To solve this issue, avoid usingeval()
,new Function()
,setTimeout([string], ...)
andsetInterval([string], ...)
for evaluating strings.
No eval
calls or other cases of string evaluation are present in my own code. The issue does not give any clue as to what code is causing it, and my attempts to use the 'report-sample'
value had no effect on output. The issue does not appear when opening the HTML file in Chrome.
I can recreate the warning with a very basic application.
main.js
const path = require("path");
const { app, BrowserWindow } = require("electron");
const createWindow = () => {
let mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
}
});
mainWindow.loadURL(`file://${path.join(__dirname, "/index.html")}`);
};
app.on("ready", createWindow);
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">
<title>Document</title>
</head>
<body>
<h1>CSP Issue Test</h1>
</body>
</html>
I would like to understand why this issue is appearing and resolve it rather than just suppress the warning.
loadFile
instead ofloadRUL
? The error doesnt hint at this but I would still try it for some reason.mainWindow.loadFile('index.html')
– Farringtoneval()
in disguise to me). Maybe you should, instead of havingpath.join(__dirname, "/index.html")
evaluated within the string, append it tofile://
(as in"file://" + path.join(__dirname, "/index.html")
). See if the problem goes away after this modification. – Odalisque