Skip to content

Commit 5d9222c

Browse files
committed
scripts: translate remove-evals.sh to remove-evals.js
1 parent ea3bdd7 commit 5d9222c

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

template/scripts/remove-evals.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
const path = require('path');
4+
const fs = require('fs');
5+
6+
const BUNDLE_DIR = path.join(__dirname, '../dist');
7+
const bundles = [
8+
'background.js',
9+
'popup/popup.js'
10+
];
11+
12+
const evalRegexForProduction = /;([a-z])=function\(\){return this}\(\);try{\1=\1\|\|Function\("return this"\)\(\)\|\|\(0,eval\)\("this"\)}catch\(t\){"object"==typeof window&&\(\1=window\)}/g;
13+
const evalRegexForDevelopment = /;\s*\/\/ This works in non-strict mode\s*([a-z])\s*=\s*\(\s*function\(\)\s*\{\s*return this;\s*}\)\(\);\s*try\s*{\s*\/\/\s*This works if eval is allowed(?:\s*|.+){1,14}/g;
14+
15+
const removeEvals = (file) => {
16+
console.info(`Removing eval() from ${file}`);
17+
18+
return new Promise((resolve, reject) => {
19+
fs.readFile(file, 'utf8', (err, data) => {
20+
if(err) {
21+
reject(err);
22+
return;
23+
}
24+
25+
const regex = process.env.NODE_ENV === 'production' ? evalRegexForProduction : evalRegexForDevelopment;
26+
27+
if(!regex.test(data)) {
28+
reject(`No CSP specific code found in ${file}.`);
29+
return;
30+
}
31+
32+
data = data.replace(regex, '=window;');
33+
34+
fs.writeFile(file, data, (err) => {
35+
if(err) {
36+
reject(err);
37+
return;
38+
}
39+
40+
resolve();
41+
});
42+
});
43+
});
44+
};
45+
46+
const main = () => {
47+
bundles.forEach(bundle => {
48+
removeEvals(path.join(BUNDLE_DIR, bundle))
49+
.then(() => console.info(`Bundle ${bundle}: OK`))
50+
.catch(console.error);
51+
});
52+
};
53+
54+
main();

template/scripts/remove-evals.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)