Skip to content

Commit 0fe551c

Browse files
committed
Escape text from custom transformTags functions.
This makes custom tag transformations less error-prone. Prior to this patch, tag transformations which turned an attribute value into a text node could be vulnerable to code execution. The operative change prevents any Frame's innerText from specifying tag tokens.
1 parent fb89a71 commit 0fe551c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function sanitizeHtml(html, options, _recursing) {
192192
} else {
193193
result += ">";
194194
if (frame.innerText && !hasText && !options.textFilter) {
195-
result += frame.innerText;
195+
result += escapeHtml(frame.innerText);
196196
}
197197
}
198198
},

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,28 @@ describe('sanitizeHtml', function() {
532532
'<a href="/welcome">test</a>'
533533
);
534534
});
535+
it('text from transformTags should not specify tags', function() {
536+
var input = '<input value="&lt;script&gt;alert(1)&lt;/script&gt;">';
537+
var want = '<u class="inlined-input">&lt;script&gt;alert(1)&lt;/script&gt;</u>';
538+
// Runs the sanitizer with a policy that turns an attribute into
539+
// text. A policy like this might be used to turn inputs into
540+
// inline elements that look like the original but which do not
541+
// affect form submissions.
542+
var got = sanitizeHtml(
543+
input,
544+
{
545+
allowedTags: [ 'u' ],
546+
allowedAttributes: { '*': ['class'] },
547+
transformTags: {
548+
input: function (tagName, attribs) {
549+
return {
550+
tagName: 'u',
551+
attribs: { class: 'inlined-input' },
552+
text: attribs.value
553+
};
554+
}
555+
}
556+
});
557+
assert.equal(got, want);
558+
});
535559
});

0 commit comments

Comments
 (0)