Skip to content

Commit 5922db6

Browse files
committed
Merge branch 'main' of git@github.com:cure53/DOMPurify.git into main
2 parents 7f6dfe2 + 4743c0b commit 5922db6

4 files changed

Lines changed: 283 additions & 155 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"eslint-plugin-prettier": "^3.1.3",
7070
"he": "^1.2.0",
7171
"jquery": "^3.5.0",
72-
"jsdom": "8.x.x",
72+
"jsdom": "16.x.x",
7373
"karma": "^5.1.0",
7474
"karma-browserstack-launcher": "^1.5.1",
7575
"karma-chrome-launcher": "^2.2.0",

test/bootstrap-test-suite.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
module.exports = function (jsdom) {
1+
const fs = require('fs');
2+
3+
module.exports = function (JSDOM) {
24
class StringWrapper {
35
constructor(s) {
46
this.s = s;
@@ -9,40 +11,39 @@ module.exports = function (jsdom) {
911
}
1012
}
1113

12-
function loadDOMPurify(assert, head, setup, onload) {
14+
function loadDOMPurify(assert, addScriptAttribute, setup, onload) {
1315
const testDone = assert.async();
14-
jsdom.env({
15-
html: '<head>' + head + '</head>',
16-
features: {
17-
FetchExternalResources: ['script'],
18-
ProcessExternalResources: ['script'],
19-
},
20-
created(err, window) {
21-
if (setup) {
22-
setup(window);
23-
}
24-
},
25-
done(err, window) {
26-
assert.ok(window.DOMPurify.sanitize);
27-
// Sanity check
28-
assert.equal(
29-
window.DOMPurify.sanitize('<img src=x onerror=alert(1)>'),
30-
'<img src="x">'
31-
);
32-
if (onload) {
33-
onload(window);
34-
}
35-
testDone();
36-
},
37-
});
16+
const { window } = new JSDOM('<head></head>', { runScripts: "dangerously" });
17+
require('jquery')(window);
18+
if (setup) {
19+
setup(window);
20+
}
21+
22+
const myLibrary = fs.readFileSync('dist/purify.js', { encoding: "utf-8" });
23+
const scriptEl = window.document.createElement("script");
24+
if (addScriptAttribute) scriptEl.setAttribute('data-tt-policy-suffix', 'suffix');
25+
26+
scriptEl.textContent = myLibrary;
27+
window.document.body.appendChild(scriptEl);
28+
29+
assert.ok(window.DOMPurify.sanitize);
30+
// Sanity check
31+
assert.equal(
32+
window.DOMPurify.sanitize('<img src=x onerror=alert(1)>'),
33+
'<img src="x">'
34+
);
35+
if (onload) {
36+
onload(window);
37+
}
38+
testDone();
3839
}
3940

4041
QUnit.test('works in a non-Trusted Type environment', function (assert) {
4142
let policyCreated;
4243

4344
loadDOMPurify(
4445
assert,
45-
'<script src="dist/purify.js"></script>',
46+
false,
4647
function setup(window) {
4748
delete window.trustedTypes;
4849
},
@@ -58,7 +59,7 @@ module.exports = function (jsdom) {
5859

5960
loadDOMPurify(
6061
assert,
61-
'<script src="dist/purify.js"></script>',
62+
false,
6263
function setup(window) {
6364
window.trustedTypes = {
6465
createPolicy(name, rules) {
@@ -89,7 +90,7 @@ module.exports = function (jsdom) {
8990

9091
loadDOMPurify(
9192
assert,
92-
'<script data-tt-policy-suffix="suffix" src="dist/purify.js"></script>',
93+
true,
9394
function setup(window) {
9495
window.trustedTypes = {
9596
createPolicy(name, rules) {

test/jsdom-node.js

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
// Test DOMPurify + jsdom using Node.js (version 8 and up)
66
const createDOMPurify = require('../dist/purify.cjs');
77
const jsdom = require('jsdom');
8+
const { JSDOM, VirtualConsole } = jsdom;
9+
const virtualConsole = new VirtualConsole();
10+
const { window } = new JSDOM(`<html><head></head><body><div id="qunit-fixture"></div></body></html>`, { runScripts: "dangerously", virtualConsole });
11+
require('jquery')(window);
12+
813
const sanitizeTestSuite = require('./test-suite');
914
const bootstrapTestSuite = require('./bootstrap-test-suite');
1015
const tests = require('./fixtures/expect');
@@ -19,44 +24,27 @@ QUnit.assert.contains = function (needle, haystack, message) {
1924

2025
QUnit.config.autostart = false;
2126

22-
QUnit.module('DOMPurify - bootstrap', bootstrapTestSuite(jsdom));
23-
24-
jsdom.env({
25-
html: `<html><head></head><body><div id="qunit-fixture"></div></body></html>`,
26-
scripts: ['node_modules/jquery/dist/jquery.js'],
27-
features: {
28-
ProcessExternalResources: ['script'], // needed for firing the onload event for about:blank iframes
29-
},
30-
done(err, window) {
31-
QUnit.module('DOMPurify in jsdom');
32-
if (err) {
33-
console.error(
34-
'Unexpected error returned by jsdom.env():',
35-
err,
36-
err.stack
37-
);
38-
process.exit(1);
39-
}
40-
41-
if (!window.jQuery) {
42-
console.warn('Unable to load jQuery');
43-
}
44-
45-
const DOMPurify = createDOMPurify(window);
46-
if (!DOMPurify.isSupported) {
47-
console.error(
48-
'Unexpected error returned by jsdom.env():',
49-
err,
50-
err.stack
51-
);
52-
process.exit(1);
53-
}
54-
55-
window.alert = () => {
56-
window.xssed = true;
57-
};
58-
59-
sanitizeTestSuite(DOMPurify, window, tests, xssTests);
60-
QUnit.start();
61-
},
62-
});
27+
QUnit.module('DOMPurify - bootstrap', bootstrapTestSuite(JSDOM));
28+
29+
QUnit.module('DOMPurify in jsdom');
30+
31+
if (!window.jQuery) {
32+
console.warn('Unable to load jQuery');
33+
}
34+
35+
const DOMPurify = createDOMPurify(window);
36+
if (!DOMPurify.isSupported) {
37+
console.error(
38+
'Unexpected error returned by jsdom.env():',
39+
err,
40+
err.stack
41+
);
42+
process.exit(1);
43+
}
44+
45+
window.alert = () => {
46+
window.xssed = true;
47+
};
48+
49+
sanitizeTestSuite(DOMPurify, window, tests, xssTests);
50+
QUnit.start();

0 commit comments

Comments
 (0)