Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/NodeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,13 @@ function serializeOne(kid, parent) {
s += '<!--' + commentData + '-->';
break;
case 7: //PROCESSING_INSTRUCTION_NODE
const content = escapeProcessingInstructionContent(kid.data);
let content = escapeProcessingInstructionContent(kid.data);
if (content.includes('</')) {
const fallbackTags = fallbackRawContentTags(parent);
for (const fallbackTag of fallbackTags) {
content = escapeMatchingClosingTag(content, fallbackTag);
}
}
s += '<?' + kid.target + ' ' + content + '?>';
break;
case 10: //DOCUMENT_TYPE_NODE
Expand Down
21 changes: 21 additions & 0 deletions test/xss.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,27 @@ exports.badProcessingInstruction = function () {
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
};

exports.noscriptProcessingInstructionAncestorClosingTagEscaped = function () {
const document = domino.createDocument('');
const noscript = document.createElement('noscript');
const pi = document.createProcessingInstruction('x', '</noscript ');
noscript.appendChild(pi);
const img = document.createElement('img');
img.setAttribute('src', 'x');
img.setAttribute('onerror', 'alert(1)');
noscript.appendChild(img);
document.body.appendChild(noscript);

document.body
.serialize()
.should.equal(
'<noscript><?x &lt;/noscript ?><img src="x" onerror="alert(1)"></noscript>',
);

const html = document.serialize();
return alertFired(html).should.eventually.be.false('alert fired for: ' + html);
};

exports.verifyEscapeMatchingClosingTag = function () {
const cases = [
['', 'style', ''], // no artifacts while processing an empty string
Expand Down
Loading