-
Notifications
You must be signed in to change notification settings - Fork 3
/
nice-script.js
135 lines (130 loc) · 5.87 KB
/
nice-script.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
var NiceScript = function(){
function run(code) {
try {
if(checkSyntax(code)) {
var handler = {
get(obj, prop) {
return Reflect.has(obj, prop.toString() + '__$') ?
obj[prop.toString() + '__$'] : undefined;
},
set(obj, prop, value) {
Reflect.set(obj, prop+'__$', value);
},
has(obj,prop) {
return obj && Reflect.has(obj, prop.toString()+'__$');
}
};
var catchAllHandler = {
get(obj, prop){
return Reflect.get(obj,prop);
},
set() { },
has() {
return true
}
};
const _console = {};
(function createMockConsole(_console){
const allowConsoleProps = ["debug", "error", "info", "log", "warn",
"dir", "dirxml", "table", "trace", "group", "groupCollapsed",
"groupEnd", "clear", "count", "countReset", "assert", "profile",
"profileEnd", "time", "timeLog", "timeEnd", "timeStamp"];
for(const property of allowConsoleProps){
_console[property] = function(){
if (arguments.length > 1 && typeof arguments[0] === 'string') {
arguments[0] = arguments[0].replace(/%/g, '%%')
}
return console[property](...arguments);
}
}
})(_console);
var allowList = {
__proto__: null,
console__$:_console,
alert__$: function(){
alert("Sandboxed alert:"+arguments[0]);
},
String__$: String,
Number__$: Number,
Array__$: Array,
Symbol__$: Symbol,
Math__$: Math,
RegExp__$: RegExp,
Object__$: Object,
eval__$: function(code){
return NiceScript.run("return "+code);
}
};
if(!Object.isFrozen(String.prototype)) {
Function.prototype.constructor = null;
Object.freeze(console);
Object.freeze(_console);
Object.freeze(Object);
Object.freeze(String);
Object.freeze(Number);
Object.freeze(Array);
Object.freeze(Symbol);
Object.freeze(Math);
Object.freeze(Function);
Object.freeze(RegExp);
Object.freeze(BigInt);
Object.freeze(Promise);
Object.freeze(console);
Object.freeze(BigInt.prototype);
Object.freeze(Object.prototype);
Object.freeze(String.prototype);
Object.freeze(Number.prototype);
Object.freeze(Array.prototype);
Object.freeze(Symbol.prototype);
Object.freeze(Math.prototype);
Object.freeze(Function.prototype);
Object.freeze(RegExp.prototype);
Object.freeze(Promise.prototype);
Object.defineProperty((async function () {}).constructor.prototype, 'constructor', {
value: null,
configurable: false,
writable: false
});
Object.defineProperty((async function* () {}).constructor.prototype, 'constructor', {
value: null,
configurable: false,
writable: false
});
Object.defineProperty((function* () {}).constructor.prototype, 'constructor', {
value: null,
configurable: false,
writable: false
});
Object.freeze((async function () {}).__proto__);
Object.freeze((async function* () {}).__proto__);
Object.freeze((function* () {}).__proto__);
Object.freeze((function* () {}).__proto__.prototype);
Object.freeze((async function* () {}).__proto__.prototype);
}
var proxy = new Proxy(allowList, handler);
var catchAllProxy = new Proxy({__proto__:null, proxy:proxy, globalThis:new Proxy(allowList, handler), window:new Proxy(allowList, handler)}, catchAllHandler);
var output = Function('proxy', 'catchAllProxy', `
with(catchAllProxy) {
with(proxy) {
return (function(){
"use strict";
${code};
})();
}
}
`)(proxy, catchAllProxy);
return output;
}
} catch(e) {
throw e;
}
}
function checkSyntax(code) {
Function(code);
if(/\bimport\s*(?:[(]|\/[*]|\/\/|<!--|-->)/.test(code)) {
throw new Error("Dynamic imports are blocked");
}
return true;
}
return {run: run};
}();