Skip to content

Commit

Permalink
Escape text from custom transformTags functions.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikesamuel committed Aug 11, 2017
1 parent fb89a71 commit 0fe551c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function sanitizeHtml(html, options, _recursing) {
} else {
result += ">";
if (frame.innerText && !hasText && !options.textFilter) {
result += frame.innerText;
result += escapeHtml(frame.innerText);
}
}
},
Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,28 @@ describe('sanitizeHtml', function() {
'<a href="/welcome">test</a>'
);
});
it('text from transformTags should not specify tags', function() {
var input = '<input value="&lt;script&gt;alert(1)&lt;/script&gt;">';
var want = '<u class="inlined-input">&lt;script&gt;alert(1)&lt;/script&gt;</u>';
// Runs the sanitizer with a policy that turns an attribute into
// text. A policy like this might be used to turn inputs into
// inline elements that look like the original but which do not
// affect form submissions.
var got = sanitizeHtml(
input,
{
allowedTags: [ 'u' ],
allowedAttributes: { '*': ['class'] },
transformTags: {
input: function (tagName, attribs) {
return {
tagName: 'u',
attribs: { class: 'inlined-input' },
text: attribs.value
};
}
}
});
assert.equal(got, want);
});
});

0 comments on commit 0fe551c

Please sign in to comment.