-
Notifications
You must be signed in to change notification settings - Fork 74
/
javascript-eval.service.ts
234 lines (172 loc) · 8.44 KB
/
javascript-eval.service.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import {Injectable, NgZone} from "@angular/core";
import {ExpressionEvaluator} from "cwlts/models";
import {Guid} from "../guid.service";
import {fromEvent} from "rxjs/observable/fromEvent";
import {filter} from "rxjs/operators";
@Injectable()
export class JavascriptEvalService {
private iFrame = null;
private pendingRequestsForEvaluation = {};
constructor(private zone: NgZone) {
this.zone.runOutsideAngular(() => {
fromEvent(window, "message").pipe(
filter((msg: MessageEvent) => msg.data.jsEvaluator)
).subscribe((msg: MessageEvent) => {
const promise = this.pendingRequestsForEvaluation[msg.data.id];
if (promise) {
if (msg.data.error) {
promise.rej(new Error(msg.data.data));
} else {
promise.res(msg.data.data);
}
delete this.pendingRequestsForEvaluation[msg.data.id]
}
});
});
// FIXME Maybe there is a better way to include iframe
this.iFrame = document.createElement("iframe");
const perm = ["allow-same-origin", "allow-scripts"];
this.iFrame.style.display = "none";
this.iFrame.sandbox = perm.join(" ");
this.iFrame.onload = () => {
this.iFrame.contentWindow.document.open("text/html", "replace");
this.iFrame.contentWindow.document.write(this.iframeContent());
this.iFrame.contentWindow.document.close();
};
document.body.appendChild(this.iFrame);
// FIXME We have to improve this
ExpressionEvaluator.evaluateExpression = this.evaluateCode.bind(this);
}
private iframeContent() {
return `
<!DOCTYPE html>
<script type="text/javascript">
// Blob code for worker
const blobCode = \`
self.addEventListener("message", function(m) {
self.postMessage(runHidden(m.data.script, m.data.context));
});
// Blacklisted properties
var runHidden = function(code, context) {
var self = null;
this.indexedDB = null;
this.location = null;
this.navigator = null;
this.onerror = null;
this.onmessage = null;
this.performance = null;
this.webkitIndexedDB = null;
this.close = null;
this.openDatabase = null;
this.openDatabaseSync = null;
this.webkitRequestFileSystem = null;
this.webkitRequestFileSystemSync = null;
this.webkitResolveLocalFileSystemSyncURL = null;
this.webkitResolveLocalFileSystemURL = null;
this.addEventListener = null;
this.dispatchEvent = null;
this.removeEventListener = null;
this.dump = null;
this.onoffline = null;
this.ononline = null;
this.importScripts = null;
this.console = null;
this.application = null;
this.XMLHttpRequest = null;
// Make local variables in order to be able to use context ($job, self...)
for(var item in context) {
// Using eval and not this[item] because of keywords (for an example if we use "self" and its
// readonly, we would not be able to use "self" when executing)
eval("var " + item + " = " + JSON.stringify(context[item]));
}
return eval(code);
} \`;
const blobUrl = window.URL.createObjectURL(
new Blob([blobCode])
);
// Queue of pending requests
const pendingRequestsQueue = [];
// Worker reference
let worker = null;
// Flag that determines whether worker is running or not
let workerIsRunning = false;
// Currently executed request
let currentlyExecutedRequest = null;
// Timeout that stops worker if running too long
let timeout = null;
// Post message to a parent window
function postMessageToParentWindow(data, id, error) {
window.parent.postMessage({
jsEvaluator: true,
data: data,
id: id,
error
}, '*');
}
// Put request in a queue and try to process the request (if its possible)
function putRequestInQueue(e) {
pendingRequestsQueue.push(e.data);
// Try to process next pending request
// This line here is because of scenario when requests comes and queue is empty
processNextRequest();
}
// Listening to the messages (requests for js evaluation) that will come from a parent window
window.addEventListener("message", putRequestInQueue, false);
// Process next pending request if possible (if worker is not running)
function processNextRequest() {
if (!workerIsRunning) {
// Take next pending request from the queue
const requestToExecute = pendingRequestsQueue.shift();
if (requestToExecute) {
workerIsRunning = true;
if (!worker) {
worker = new Worker(blobUrl);
const messageCallback = function (data, error) {
window.clearTimeout(timeout);
workerIsRunning = false;
postMessageToParentWindow(data, currentlyExecutedRequest.id, error);
currentlyExecutedRequest = null;
processNextRequest();
};
worker.onmessage = function (m) {
messageCallback(m.data);
};
worker.onerror = function (e) {
e.preventDefault();
messageCallback(e.message + " (at line " + e.lineno + ")", true);
}
}
currentlyExecutedRequest = requestToExecute;
worker.postMessage(currentlyExecutedRequest);
// Set timeout in order to stop the execution if it is taking too long
timeout = window.setTimeout(function () {
workerIsRunning = false;
worker.terminate();
worker = null;
postMessageToParentWindow("Error: Takes too long to execute, loops possible", currentlyExecutedRequest.id, true);
currentlyExecutedRequest = null;
processNextRequest();
}, requestToExecute.timeout);
}
}
}
<\/script>
</html>`;
}
evaluateCode(expression?: string, context?: any, timeout: Number = 300) {
const generatedId = Guid.generate();
this.zone.runOutsideAngular(() => {
this.iFrame.contentWindow.postMessage({
script: expression,
context: context,
id: generatedId,
timeout: timeout
}, "*");
});
return new Promise((res, rej) => {
this.pendingRequestsForEvaluation[generatedId] = {
res: res, rej: rej
};
});
}
}