-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
options-run-scripts.js
528 lines (412 loc) · 20.1 KB
/
options-run-scripts.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
"use strict";
const { assert } = require("chai");
const { describe, it } = require("mocha-sugar-free");
const { delay } = require("../util.js");
const { JSDOM, VirtualConsole } = require("../..");
const jsGlobals = Object.keys(require("../../lib/jsdom/browser/js-globals.json"));
// Node 10 has a bug with the vm module that causes some global-related tests to fail.
const hasNode10 = process.versions.node && Number(process.versions.node.split(".")[0]) >= 10;
describe("API: runScripts constructor option", () => {
describe("<script>s and eval()", () => {
it("should not execute any scripts by default", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`);
assert.strictEqual(dom.window.document.body.children.length, 1);
});
it("should not execute any scripts, even in iframes, by default (GH-1821)", () => {
const dom = new JSDOM(`<iframe></iframe>`);
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;
frameWindow.document.open();
frameWindow.document.write(`<script>parent.prop = "i was executed";</script>`);
frameWindow.document.close();
assert.strictEqual(dom.window.prop, undefined);
});
it("should execute <script>s and eval when set to \"dangerously\"", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: "dangerously" });
dom.window.eval(`document.body.appendChild(document.createElement("p"));`);
assert.strictEqual(dom.window.document.body.children.length, 3);
});
// In the browser, vm-shim uses Function() on the code to be evaluated, which inserts an extra first line. So we are
// always off by one there. See https://github.com/tmpvar/jsdom/issues/2004.
it("should execute <script>s with correct location when set to \"dangerously\" and " +
"includeNodeLocations", { skipIfBrowser: true }, () => {
const virtualConsole = new VirtualConsole();
const promise = new Promise((resolve, reject) => {
virtualConsole.on("jsdomError", err => {
try {
assert.strictEqual(err.type, "unhandled exception");
assert.isTrue(err.detail.stack.includes("at about:blank:2"));
resolve();
} catch (actualErr) {
reject(actualErr);
}
});
});
// eslint-disable-next-line no-new
new JSDOM(`<body>
<script>throw new Error();</script>
</body>`, { runScripts: "dangerously", includeNodeLocations: true, virtualConsole });
return promise;
});
it("should only run eval when set to \"outside-only\"", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: "outside-only" });
dom.window.eval(`document.body.appendChild(document.createElement("p"));`);
assert.strictEqual(dom.window.document.body.children.length, 2);
});
it("should ensure eval exists on iframes when set to \"outside-only\"", () => {
const dom = new JSDOM(`<iframe></iframe>`, { runScripts: "outside-only" });
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;
frameWindow.eval(`document.body.appendChild(document.createElement("p"));`);
assert.strictEqual(frameWindow.document.body.children.length, 1);
});
it("should execute <script>s in iframes when set to \"dangerously\"", () => {
const dom = new JSDOM(`<iframe></iframe>`, { runScripts: "dangerously" });
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;
frameWindow.document.open();
frameWindow.document.write(`<script>parent.prop = "i was executed";</script>`);
frameWindow.document.close();
assert.strictEqual(dom.window.prop, "i was executed");
});
});
const jsSpecGlobalsDescribe = hasNode10 ? describe.skip : describe;
jsSpecGlobalsDescribe("JS spec globals", () => {
it("should include aliased globals by default", () => {
// Sanity check that our global-generation process hasn't broken.
assert.include(jsGlobals, "TypeError");
assert.include(jsGlobals, "Math");
assert.include(jsGlobals, "Function");
const dom = new JSDOM();
for (const globalName of jsGlobals) {
assertAliasedGlobal(dom.window, globalName);
}
});
for (const optionValue of ["outside-only", "dangerously"]) {
it(`should include fresh globals when set to "${optionValue}"`, () => {
const dom = new JSDOM(undefined, { runScripts: optionValue });
for (const globalName of jsGlobals) {
assertFreshGlobal(dom.window, globalName);
}
});
}
});
describe("event handlers", () => {
for (const optionValue of [undefined, "outside-only"]) {
describe(`when set to ${formatOptionValue(optionValue)}`, () => {
function createJSDOM() {
return new JSDOM(`<div>`, { runScripts: optionValue });
}
function createJSDOMWithParsedHandlers() {
return new JSDOM(
`<body onload="document.body.onloadRan = true;" onhashchange="document.body.onhashchangeRan = true;">
<div onclick="document.body.onclickRan = true;"></div>`,
{ runScripts: optionValue, url: "https://example.com/" } // url set for hashchange tests
);
}
describe("body onload handler set during parsing", () => {
it("should not evaluate the handler (GH-1848)", () => {
const dom = createJSDOMWithParsedHandlers();
assert.isUndefined(dom.window.document.body.onloadRan);
return delay().then(() => {
assert.isUndefined(dom.window.document.body.onloadRan);
});
});
it("should not generate the body or Window property", () => {
const dom = createJSDOMWithParsedHandlers();
assert.isNull(dom.window.document.body.onload);
assert.isNull(dom.window.onload);
});
it("should still parse the handler as an attribute", () => {
const { body } = createJSDOMWithParsedHandlers().window.document;
assert.strictEqual(body.getAttribute("onload"), "document.body.onloadRan = true;");
});
});
describe("body onhashchange handler set during parsing", () => {
it("should not evaluate the handler", () => {
const dom = createJSDOMWithParsedHandlers();
dom.window.location.href = "#foo";
return delay().then(() => {
assert.isUndefined(dom.window.document.body.onhashchangeRan);
});
});
it("should not generate the body or Window property", () => {
const dom = createJSDOMWithParsedHandlers();
assert.isNull(dom.window.document.body.onhashchange);
assert.isNull(dom.window.onhashchange);
});
it("should still parse the handler as an attribute", () => {
const { body } = createJSDOMWithParsedHandlers().window.document;
assert.strictEqual(body.getAttribute("onhashchange"), "document.body.onhashchangeRan = true;");
});
});
describe("body onhashchange handler set via setAttribute", () => {
it("should not evaluate the handler", () => {
const dom = createJSDOM();
dom.window.document.body.setAttribute("onhashchange", "document.body.onhashchangeRan = true;");
dom.window.location.href = "#foo";
return delay().then(() => {
assert.isUndefined(dom.window.document.body.onhashchangeRan);
});
});
it("should not generate the body or Window property", () => {
const dom = createJSDOM();
dom.window.document.body.setAttribute("onhashchange", "document.body.onhashchangeRan = true;");
assert.isNull(dom.window.document.body.onhashchange);
assert.isNull(dom.window.onhashchange);
});
it("should still parse the handler as an attribute", () => {
const { body } = createJSDOM().window.document;
body.setAttribute("onhashchange", "document.body.onhashchangeRan = true;");
assert.strictEqual(body.getAttribute("onhashchange"), "document.body.onhashchangeRan = true;");
});
});
describe("div onclick handler set during parsing", () => {
it("should not evaluate the handler", () => {
const dom = createJSDOMWithParsedHandlers();
dom.window.document.querySelector("div").click();
return delay().then(() => {
assert.isUndefined(dom.window.document.body.onclickRan);
});
});
it("should not generate the property", () => {
const dom = createJSDOMWithParsedHandlers();
assert.isNull(dom.window.document.querySelector("div").onclick);
});
it("should still parse the handler as an attribute", () => {
const dom = createJSDOMWithParsedHandlers();
const div = dom.window.document.querySelector("div");
assert.strictEqual(div.getAttribute("onclick"), "document.body.onclickRan = true;");
});
});
describe("div onclick handler set via setAttribute", () => {
it("should not evaluate the handler", () => {
const dom = createJSDOM();
const div = dom.window.document.body.querySelector("div");
div.setAttribute("onclick", "document.body.onclickRan = true;");
div.click();
assert.isUndefined(dom.window.document.body.onclickRan);
});
it("should not generate the property", () => {
const dom = createJSDOM();
const div = dom.window.document.body.querySelector("div");
div.setAttribute("onclick", "document.body.onclickRan = true;");
assert.isNull(div.onclick);
});
it("should still parse the handler as an attribute", () => {
const dom = createJSDOM();
const div = dom.window.document.body.querySelector("div");
div.setAttribute("onclick", "document.body.onclickRan = true;");
assert.strictEqual(div.getAttribute("onclick"), "document.body.onclickRan = true;");
});
});
testEventHandlersFromTheOutside(optionValue);
});
}
describe("when set to \"dangerously\"", () => {
function createJSDOM() {
return new JSDOM(`<div>`, { runScripts: "dangerously" });
}
function createJSDOMWithParsedHandlers() {
return new JSDOM(
`<body onload="document.body.onloadRan = true;" onhashchange="document.body.onhashchangeRan = true;">
<div onclick="document.body.onclickRan = true;"></div>`,
{ runScripts: "dangerously", url: "https://example.com/" } // url set for hashchange tests
);
}
describe("body onload handler set during parsing", () => {
it("should evaluate the handler", () => {
const dom = createJSDOMWithParsedHandlers();
return delay().then(() => {
assert.isTrue(dom.window.document.body.onloadRan);
});
});
it("should generate the body and Window property", () => {
const dom = createJSDOMWithParsedHandlers();
assert.isFunction(dom.window.document.body.onload);
assert.isFunction(dom.window.onload);
assert.strictEqual(dom.window.document.body.onload, dom.window.onload);
});
it("should parse the handler as an attribute", () => {
const { body } = createJSDOMWithParsedHandlers().window.document;
assert.strictEqual(body.getAttribute("onload"), "document.body.onloadRan = true;");
});
});
describe("body onhashchange handler set during parsing", () => {
it("should not evaluate the handler", () => {
const dom = createJSDOMWithParsedHandlers();
dom.window.location.href = "#foo";
return delay().then(() => {
assert.isTrue(dom.window.document.body.onhashchangeRan);
});
});
it("should not generate the body or Window property", () => {
const dom = createJSDOMWithParsedHandlers();
assert.isFunction(dom.window.document.body.onhashchange);
assert.isFunction(dom.window.onhashchange);
assert.strictEqual(dom.window.document.body.onhashchange, dom.window.onhashchange);
});
it("should parse the handler as an attribute", () => {
const { body } = createJSDOMWithParsedHandlers().window.document;
assert.strictEqual(body.getAttribute("onhashchange"), "document.body.onhashchangeRan = true;");
});
});
describe("body onhashchange handler set via setAttribute", () => {
it("should evaluate the handler", () => {
const dom = createJSDOM();
dom.window.document.body.setAttribute("onhashchange", "document.body.onhashchangeRan = true;");
dom.window.location.href = "#foo";
return delay().then(() => {
assert.isTrue(dom.window.document.body.onhashchangeRan);
});
});
it("should generate the body and Window property", () => {
const dom = createJSDOM();
dom.window.document.body.setAttribute("onhashchange", "document.body.onhashchangeRan = true;");
assert.isFunction(dom.window.document.body.onhashchange);
assert.isFunction(dom.window.onhashchange);
assert.strictEqual(dom.window.document.body.onhashchange, dom.window.onhashchange);
});
it("should parse the handler as an attribute", () => {
const { body } = createJSDOM().window.document;
body.setAttribute("onhashchange", "document.body.onhashchangeRan = true;");
assert.strictEqual(body.getAttribute("onhashchange"), "document.body.onhashchangeRan = true;");
});
});
describe("div onclick handler set during parsing", () => {
it("should not evaluate the handler", () => {
const dom = createJSDOMWithParsedHandlers();
dom.window.document.querySelector("div").click();
assert.isTrue(dom.window.document.body.onclickRan);
});
it("should generate the property", () => {
const dom = createJSDOMWithParsedHandlers();
assert.isFunction(dom.window.document.querySelector("div").onclick);
});
it("should parse the handler as an attribute", () => {
const dom = createJSDOMWithParsedHandlers();
const div = dom.window.document.querySelector("div");
assert.strictEqual(div.getAttribute("onclick"), "document.body.onclickRan = true;");
});
});
describe("div onclick handler set via setAttribute", () => {
it("should evaluate the handler", () => {
const dom = createJSDOM();
const div = dom.window.document.body.querySelector("div");
div.setAttribute("onclick", "document.body.onclickRan = true;");
div.click();
assert.isTrue(dom.window.document.body.onclickRan);
});
it("should generate the property", () => {
const dom = createJSDOM();
const div = dom.window.document.body.querySelector("div");
div.setAttribute("onclick", "document.body.onclickRan = true;");
assert.isFunction(div.onclick);
});
it("should parse the handler as an attribute", () => {
const dom = createJSDOM();
const div = dom.window.document.body.querySelector("div");
div.setAttribute("onclick", "document.body.onclickRan = true;");
assert.strictEqual(div.getAttribute("onclick"), "document.body.onclickRan = true;");
});
});
testEventHandlersFromTheOutside("dangerously");
});
});
it("should disallow other values", () => {
assert.throws(() => new JSDOM(``, { runScripts: null }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: "asdf" }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: true }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: false }), RangeError);
});
});
function testEventHandlersFromTheOutside(runScriptsOptionValue) {
describe("body onhashchange handler set from the outside", () => {
it("should evaluate the handler", () => {
const dom = new JSDOM(``, { runScripts: runScriptsOptionValue });
let ran = false;
dom.window.document.body.onhashchange = () => {
ran = true;
};
dom.window.location.href = "#foo";
return delay().then(() => {
assert.isTrue(ran);
});
});
it("should return the handler from both Window and the body", () => {
const dom = new JSDOM(``, { runScripts: runScriptsOptionValue });
function handler() {}
dom.window.document.body.onhashchange = handler;
assert.strictEqual(dom.window.document.body.onhashchange, handler);
assert.strictEqual(dom.window.onhashchange, handler);
});
});
describe("Window onhashchange handler set from the outside", () => {
it("should evaluate the handler", () => {
const dom = new JSDOM(``, { runScripts: runScriptsOptionValue });
let ran = false;
dom.window.onhashchange = () => {
ran = true;
};
dom.window.location.href = "#foo";
return delay().then(() => {
assert.isTrue(ran);
});
});
it("should return the handler from both Window and the body", () => {
const dom = new JSDOM(``, { runScripts: runScriptsOptionValue });
function handler() {}
dom.window.onhashchange = handler;
assert.strictEqual(dom.window.onhashchange, handler);
assert.strictEqual(dom.window.document.body.onhashchange, handler);
});
});
describe("div onclick handler set from the outside", () => {
it("should evaluate the handler", () => {
const dom = new JSDOM(`<div>`, { runScripts: runScriptsOptionValue });
const div = dom.window.document.querySelector("div");
let ran = false;
div.onclick = () => {
ran = true;
};
div.click();
assert.isTrue(ran);
});
it("should return the handler that was set", () => {
const dom = new JSDOM(`<div>`, { runScripts: runScriptsOptionValue });
const div = dom.window.document.querySelector("div");
function handler() {}
div.onclick = handler;
assert.strictEqual(div.onclick, handler);
});
});
}
function formatOptionValue(optionValue) {
return typeof optionValue === "string" ? `"${optionValue}"` : optionValue;
}
function isObject(value) {
return typeof value === "function" || (typeof value === "object" && value !== null);
}
function assertAliasedGlobal(window, globalName) {
const windowPropDesc = Object.getOwnPropertyDescriptor(window);
const globalPropDesc = Object.getOwnPropertyDescriptor(global);
assert.strictEqual(Object.is(windowPropDesc.value, globalPropDesc.value), true, `${globalName} value`);
assert.strictEqual(windowPropDesc.configurable, globalPropDesc.configurable, `${globalName} configurable`);
assert.strictEqual(windowPropDesc.enumerable, globalPropDesc.enumerable, `${globalName} enumerable`);
assert.strictEqual(windowPropDesc.writable, globalPropDesc.writable, `${globalName} writable`);
}
function assertFreshGlobal(window, globalName) {
const windowPropDesc = Object.getOwnPropertyDescriptor(window);
const globalPropDesc = Object.getOwnPropertyDescriptor(global);
if (isObject(globalPropDesc.value)) {
assert.strictEqual(Object.is(windowPropDesc.value, globalPropDesc.value), false, `${globalName} value inequality`);
} else {
assert.strictEqual(Object.is(windowPropDesc.value, globalPropDesc.value), true, `${globalName} value equality`);
}
assert.strictEqual(windowPropDesc.configurable, globalPropDesc.configurable, `${globalName} configurable`);
assert.strictEqual(windowPropDesc.enumerable, globalPropDesc.enumerable, `${globalName} enumerable`);
assert.strictEqual(windowPropDesc.writable, globalPropDesc.writable, `${globalName} writable`);
}