Skip to content

Commit

Permalink
fix: disable unsafe-eval in production
Browse files Browse the repository at this point in the history
  • Loading branch information
ineo6 committed Mar 12, 2021
1 parent 59a3dae commit 01ed739
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const devMatches = ['https://github.com/*', 'https://gitlab.com/*', 'https://try

const prodMatches = ['http://*/*', 'https://*/*'];

let csp = "script-src 'self' https://ssl.google-analytics.com 'unsafe-eval'; object-src 'self'";

if (process.env.NODE_ENV === 'production') {
csp = "script-src 'self' https://ssl.google-analytics.com; object-src 'self'";
}

const manifestInput = {
manifest_version: 2,
name: '__MSG_name__',
Expand All @@ -26,7 +32,7 @@ const manifestInput = {

web_accessible_resources: ['*.woff2', '*.png', '*.gif', 'inject.js'],

content_security_policy: "script-src 'self' https://ssl.google-analytics.com 'unsafe-eval'; object-src 'self'",
content_security_policy: csp,

'__chrome|firefox__author': 'neo',
__opera__developer: {
Expand Down
17 changes: 1 addition & 16 deletions views/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ function executeHandle(type, handle) {
}
}

// 通过postMessage调用content-script
function invokeContentScript(code) {
window.postMessage(
{
cmd: 'invoke',
code: code,
from: gmPageId,
},
'*'
);
}

// 发送普通消息到content-script
function sendMessageToContentScriptByPostMessage(data) {
window.postMessage(
Expand All @@ -51,10 +39,7 @@ window.addEventListener(
return;
}

if (e.data && e.data.cmd === 'invoke') {
// eslint-disable-next-line no-eval
eval('(' + e.data.code + ')');
} else if (e.data && e.data.cmd === 'message') {
if (e.data && e.data.cmd === 'message') {
const data = e.data.data;

if (data.type && data.handle) {
Expand Down

0 comments on commit 01ed739

Please sign in to comment.