-
Notifications
You must be signed in to change notification settings - Fork 16
/
common.js
188 lines (173 loc) · 4.13 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict'
const clones = require('clones')
const hasWindow = (typeof window !== 'undefined')
exports.hasWindow = hasWindow
const hasGlobal = (typeof global !== 'undefined')
exports.hasGlobal = hasGlobal
const FN_NOOP = 'function () {}'
const NON_IDENTIFIER = /^\d|-|^(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with|class|const|enum|export|extends|import|super|implements|interface|let|package|private|protected|public|static|yield|null|true|false)$/
const isIdentifier = key => !NON_IDENTIFIER.test(key)
exports.isIdentifier = isIdentifier
/**
* create a fresh context where nearly nothing is allowed
* @private
*/
exports.createContext = function () {
// protection might not be complete!
const context = {
// disallowed
global: undefined,
process: undefined,
module: undefined,
require: undefined,
document: undefined,
window: undefined,
Window: undefined,
// no evil...
eval: undefined,
Function: undefined
}
const fillContext = (root) => {
Object.keys(root).forEach(key => {
if (isIdentifier(key)) {
context[key] = undefined
}
})
}
// locally define all potential global vars
if (hasGlobal) {
fillContext(global)
cloneFunctions(context)
context.Buffer = _protect('Buffer')
context.console = clones(console, console) // console needs special treatment
context.console.constructor.constructor = FN_NOOP
}
if (hasWindow) {
fillContext(window)
cloneFunctions(context)
protectBuiltInObjects(context)
context.console = clones(console, console) // console needs special treatment
try {
context.Object.constructor.constructor = FN_NOOP
} catch (e) {
}
}
return context
}
/**
* Apply allowed context properties
* @private
*/
exports.allow = function (context, newContext) {
Object.keys(context || {}).forEach(key => {
if (isIdentifier(key)) {
newContext[key] = context[key] // this is harmful - objects can be overwritten
}
})
}
/**
* clone global functions
* @private
*/
function cloneFunctions (context) {
;[
'clearImmediate',
'clearInterval',
'clearTimeout'
].forEach((str) => {
try {
const fn = new Function(`return ${str}`)() // eslint-disable-line no-new-func
context[str] = fn
? function () {
return fn.apply(null, [].slice.call(arguments))
}
: undefined
} catch (e) {}
})
;[
'setImmediate',
'setInterval',
'setTimeout'
].forEach((str) => {
try {
const fn = new Function(`return ${str}`)() // eslint-disable-line no-new-func
context[str] = fn
? function (f) {
if (typeof f === 'function') {
return fn.apply(null, [].slice.call(arguments))
} else {
throw new Error(str + ' requires function as argument')
}
}
: undefined
} catch (e) {}
})
}
/**
* wraps up build-in objects using a cloned copy
* protect object against overwriting
* @private
*/
function protectBuiltInObjects (context) {
;[
'Object',
'Boolean',
'Symbol',
'Error',
'EvalError',
'InternalError',
'RangeError',
'ReferenceError',
'SyntaxError',
'TypeError',
'URIError',
'Number',
'Math',
'Date',
'String',
'RegExp',
'Array',
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'Map',
'Set',
'WeakMap',
'WeakSet',
'ArrayBuffer',
'SharedArrayBuffer',
'Atomics',
'DataView',
'JSON',
'Promise',
'Generator',
'GeneratorFunction',
'Reflect',
'Proxy',
'Intl',
'Buffer'
].forEach((str) => {
try {
context[str] = _protect(str)
new context[str]() // eslint-disable-line no-new
} catch (e) {
}
})
}
/**
* @private
*/
function _protect (str) {
try {
const type = new Function(`return ${str}`)() // eslint-disable-line no-new-func
return type
? clones.classes(type)
: undefined
} catch (e) {}
}