Skip to content

Commit 712cb68

Browse files
authored
Merge pull request jsdom#156 from mikesamuel/master
Escape text from custom transformTags functions.
2 parents d0b658b + ad0f0cf commit 712cb68

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ function sanitizeHtml(html, options, _recursing) {
390390
} else {
391391
result += '>';
392392
if (frame.innerText && !hasText && !options.textFilter) {
393-
result += frame.innerText;
393+
result += escapeHtml(frame.innerText);
394394
}
395395
}
396396
if (skip) {

test/test.js

+25
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,31 @@ describe('sanitizeHtml', function() {
703703
'<img src="fallback.jpg" srcset="foo.jpg 100w 2x, bar.jpg 200w 1x" />'
704704
);
705705
});
706+
707+
it('text from transformTags should not specify tags', function() {
708+
var input = '<input value="&lt;script&gt;alert(1)&lt;/script&gt;">';
709+
var want = '<u class="inlined-input">&lt;script&gt;alert(1)&lt;/script&gt;</u>';
710+
// Runs the sanitizer with a policy that turns an attribute into
711+
// text. A policy like this might be used to turn inputs into
712+
// inline elements that look like the original but which do not
713+
// affect form submissions.
714+
var got = sanitizeHtml(
715+
input,
716+
{
717+
allowedTags: [ 'u' ],
718+
allowedAttributes: { '*': ['class'] },
719+
transformTags: {
720+
input: function (tagName, attribs) {
721+
return {
722+
tagName: 'u',
723+
attribs: { class: 'inlined-input' },
724+
text: attribs.value
725+
};
726+
}
727+
}
728+
});
729+
assert.equal(got, want);
730+
});
706731
it('drop attribute names with meta-characters', function() {
707732
assert.equal(
708733
sanitizeHtml('<span data-<script>alert(1)//>', {

0 commit comments

Comments
 (0)