Skip to content

Commit

Permalink
Update jsdom to version 16.x.x
Browse files Browse the repository at this point in the history
replace call of jsdom.env by instance of new jsdom.JSDOM
create dom element to inject jquery script [took from this so answer](https://stackoverflow.com/a/56760403/958898)
  • Loading branch information
peernohell committed Sep 2, 2020
1 parent 43530e9 commit a0499ba
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 71 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"eslint-plugin-prettier": "^3.1.3",
"he": "^1.2.0",
"jquery": "^3.5.0",
"jsdom": "8.x.x",
"jsdom": "16.x.x",
"karma": "^5.1.0",
"karma-browserstack-launcher": "^1.5.1",
"karma-chrome-launcher": "^2.2.0",
Expand Down
59 changes: 30 additions & 29 deletions test/bootstrap-test-suite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = function (jsdom) {
const fs = require('fs');

module.exports = function (JSDOM) {
class StringWrapper {
constructor(s) {
this.s = s;
Expand All @@ -9,40 +11,39 @@ module.exports = function (jsdom) {
}
}

function loadDOMPurify(assert, head, setup, onload) {
function loadDOMPurify(assert, addScriptAttribute, setup, onload) {
const testDone = assert.async();
jsdom.env({
html: '<head>' + head + '</head>',
features: {
FetchExternalResources: ['script'],
ProcessExternalResources: ['script'],
},
created(err, window) {
if (setup) {
setup(window);
}
},
done(err, window) {
assert.ok(window.DOMPurify.sanitize);
// Sanity check
assert.equal(
window.DOMPurify.sanitize('<img src=x onerror=alert(1)>'),
'<img src="x">'
);
if (onload) {
onload(window);
}
testDone();
},
});
const { window } = new JSDOM('<head></head>', { runScripts: "dangerously" });
require('jquery')(window);
if (setup) {
setup(window);
}

const myLibrary = fs.readFileSync('dist/purify.js', { encoding: "utf-8" });
const scriptEl = window.document.createElement("script");
if (addScriptAttribute) scriptEl.setAttribute('data-tt-policy-suffix', 'suffix');

scriptEl.textContent = myLibrary;
window.document.body.appendChild(scriptEl);

assert.ok(window.DOMPurify.sanitize);
// Sanity check
assert.equal(
window.DOMPurify.sanitize('<img src=x onerror=alert(1)>'),
'<img src="x">'
);
if (onload) {
onload(window);
}
testDone();
}

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

loadDOMPurify(
assert,
'<script src="dist/purify.js"></script>',
false,
function setup(window) {
delete window.trustedTypes;
},
Expand All @@ -58,7 +59,7 @@ module.exports = function (jsdom) {

loadDOMPurify(
assert,
'<script src="dist/purify.js"></script>',
false,
function setup(window) {
window.trustedTypes = {
createPolicy(name, rules) {
Expand Down Expand Up @@ -89,7 +90,7 @@ module.exports = function (jsdom) {

loadDOMPurify(
assert,
'<script data-tt-policy-suffix="suffix" src="dist/purify.js"></script>',
true,
function setup(window) {
window.trustedTypes = {
createPolicy(name, rules) {
Expand Down
69 changes: 28 additions & 41 deletions test/jsdom-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// Test DOMPurify + jsdom using Node.js (version 8 and up)
const createDOMPurify = require('../dist/purify.cjs');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const { window } = new JSDOM(`<html><head></head><body><div id="qunit-fixture"></div></body></html>`, { runScripts: "dangerously" });
require('jquery')(window);

const sanitizeTestSuite = require('./test-suite');
const bootstrapTestSuite = require('./bootstrap-test-suite');
const tests = require('./fixtures/expect');
Expand All @@ -19,44 +23,27 @@ QUnit.assert.contains = function (needle, haystack, message) {

QUnit.config.autostart = false;

QUnit.module('DOMPurify - bootstrap', bootstrapTestSuite(jsdom));

jsdom.env({
html: `<html><head></head><body><div id="qunit-fixture"></div></body></html>`,
scripts: ['node_modules/jquery/dist/jquery.js'],
features: {
ProcessExternalResources: ['script'], // needed for firing the onload event for about:blank iframes
},
done(err, window) {
QUnit.module('DOMPurify in jsdom');
if (err) {
console.error(
'Unexpected error returned by jsdom.env():',
err,
err.stack
);
process.exit(1);
}

if (!window.jQuery) {
console.warn('Unable to load jQuery');
}

const DOMPurify = createDOMPurify(window);
if (!DOMPurify.isSupported) {
console.error(
'Unexpected error returned by jsdom.env():',
err,
err.stack
);
process.exit(1);
}

window.alert = () => {
window.xssed = true;
};

sanitizeTestSuite(DOMPurify, window, tests, xssTests);
QUnit.start();
},
});
QUnit.module('DOMPurify - bootstrap', bootstrapTestSuite(JSDOM));

QUnit.module('DOMPurify in jsdom');

if (!window.jQuery) {
console.warn('Unable to load jQuery');
}

const DOMPurify = createDOMPurify(window);
if (!DOMPurify.isSupported) {
console.error(
'Unexpected error returned by jsdom.env():',
err,
err.stack
);
process.exit(1);
}

window.alert = () => {
window.xssed = true;
};

sanitizeTestSuite(DOMPurify, window, tests, xssTests);
QUnit.start();

0 comments on commit a0499ba

Please sign in to comment.