Skip to content

Commit f122be0

Browse files
committed
Avoid unsafe-eval CSP errors in Electron. [closes standard-things#601]
1 parent 02b3a95 commit f122be0

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ module.exports = {
99
extends: ["eslint:recommended", "plugin:import/errors"],
1010
globals: {
1111
__external__: false,
12+
__global__: false,
13+
__jest__: false,
1214
__non_webpack_module__: false,
1315
__non_webpack_require__: false,
14-
__jest__: false,
1516
__shared__: false,
1617
jest: false,
1718
WebAssembly: false

esm.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint strict: off, node/no-unsupported-features: ["error", { version: 6 }] */
2-
"use strict"
32

43
const {
54
apply,
@@ -104,6 +103,7 @@ function compileESM() {
104103
}
105104

106105
const script = new Script(
106+
"const __global__ = this;" +
107107
"(function (require, module, __jest__, __shared__) { " +
108108
content +
109109
"\n});",
@@ -123,7 +123,9 @@ function compileESM() {
123123
} else {
124124
result = apply(runInNewContext, script, [{
125125
__proto__: null,
126-
global: Function("return this")()
126+
global: (function () {
127+
return this
128+
})()
127129
}, options])
128130
}
129131

src/shared.js

+34-11
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function init() {
9696
pendingScripts: { __proto__: null },
9797
pendingWrites: { __proto__: null },
9898
reloaded: false,
99-
safeGlobal: Function("return this")(),
99+
safeGlobal: __global__,
100100
support,
101101
symbol,
102102
unsafeGlobal: global,
@@ -115,7 +115,8 @@ function init() {
115115
})
116116

117117
setDeferred(shared, "customInspectKey", () => {
118-
const { customInspectSymbol } = shared.module.safeUtil
118+
const { safeUtil } = shared.module
119+
const { customInspectSymbol } = safeUtil
119120

120121
return typeof customInspectSymbol === "symbol"
121122
? customInspectSymbol
@@ -129,7 +130,10 @@ function init() {
129130
})
130131

131132
setDeferred(shared, "originalConsole", () => {
132-
const { safeInspector, safeVM } = shared.module
133+
const {
134+
safeInspector,
135+
safeVM
136+
} = shared.module
133137

134138
return (safeInspector && safeInspector.console) ||
135139
new safeVM.Script("console").runInNewContext()
@@ -146,37 +150,48 @@ function init() {
146150
})
147151

148152
setDeferred(shared, "runtimeName", () => {
153+
const { safeCrypto } = shared.module
154+
149155
return encodeId(
150156
"_" +
151-
shared.module.safeCrypto.createHash("md5")
157+
safeCrypto.createHash("md5")
152158
.update(Date.now().toString())
153159
.digest("hex")
154160
.slice(0, 3)
155161
)
156162
})
157163

158164
setDeferred(shared, "unsafeContext", () => {
159-
const { safeVM, utilPrepareContext } = shared.module
165+
const {
166+
safeVM,
167+
utilPrepareContext
168+
} = shared.module
160169

161170
return utilPrepareContext(safeVM.createContext(shared.unsafeGlobal))
162171
})
163172

164173
setDeferred(support, "await", () => {
174+
const { safeVM } = shared.module
175+
165176
try {
166-
Function("async()=>await 1")()
177+
new safeVM.Script("async()=>await 1").runInThisContext()
167178
return true
168179
} catch {}
169180

170181
return false
171182
})
172183

173184
setDeferred(support, "createCachedData", () => {
174-
return typeof shared.module.safeVM.Script.prototype.createCachedData === "function"
185+
const { safeVM } = shared.module
186+
187+
return typeof safeVM.Script.prototype.createCachedData === "function"
175188
})
176189

177190
setDeferred(support, "inspectProxies", () => {
191+
const { safeUtil } = shared.module
192+
178193
// Node < 6.1.0 does not support inspecting proxies.
179-
const inspected = shared.module.safeUtil.inspect(dummyProxy, {
194+
const inspected = safeUtil.inspect(dummyProxy, {
180195
depth: 1,
181196
showProxy: true
182197
})
@@ -203,10 +218,12 @@ function init() {
203218
})
204219

205220
setDeferred(support, "nativeProxyReceiver", () => {
221+
const { SafeBuffer } = shared.module
222+
206223
// Detect support for invoking native functions with a proxy receiver.
207224
// https://bugs.chromium.org/p/v8/issues/detail?id=5773
208225
try {
209-
const proxy = new Proxy(shared.module.SafeBuffer.alloc(0), {
226+
const proxy = new Proxy(SafeBuffer.alloc(0), {
210227
get: (target, name) => target[name]
211228
})
212229

@@ -220,7 +237,10 @@ function init() {
220237
})
221238

222239
setDeferred(support, "replShowProxy", () => {
223-
const { safeProcess, utilSatisfies } = shared.module
240+
const {
241+
safeProcess,
242+
utilSatisfies
243+
} = shared.module
224244

225245
return utilSatisfies(safeProcess.version, ">=10")
226246
})
@@ -238,7 +258,10 @@ function init() {
238258
})
239259

240260
setDeferred(utilBinding, "hiddenKeyType", () => {
241-
const { safeProcess, utilSatisfies } = shared.module
261+
const {
262+
safeProcess,
263+
utilSatisfies
264+
} = shared.module
242265

243266
return utilSatisfies(safeProcess.version, "<7")
244267
? "string"

src/shim/function-prototype-to-string.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ function init() {
99
const Shim = {
1010
enable(context) {
1111
const cache = shared.memoize.shimFunctionPrototypeToString
12-
13-
// Avoid a silent fail accessing `context.Function` in Electron 1.
14-
const funcCtor = Function("c", "return c.Function")(context)
15-
const funcProto = funcCtor.prototype
12+
const funcProto = context.Function.prototype
1613

1714
if (check(funcProto, cache)) {
1815
return context

0 commit comments

Comments
 (0)