Skip to content

Commit ba3a2f6

Browse files
authored
Merge pull request #577 from chalotrekking/main
For allowedTags, stop treating falsy values other than false, same as false.
2 parents 0573fb6 + 47fb1f7 commit ba3a2f6

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- If allowedTags is falsy but not exactly `false`, then do not assume that all tags are allowed. Rather, allow no tags in this case, to be on a safer side. This fixes [issue #176](https://github.com/apostrophecms/sanitize-html/issues/176).
6+
37
## 2.7.2 (2022-09-15)
48

59
- Closing tags must agree with opening tags. This fixes [issue #549](https://github.com/apostrophecms/sanitize-html/issues/549), in which closing tags not associated with any permitted opening tag could be passed through. No known exploit exists, but it's better not to permit this. Thanks to

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function sanitizeHtml(html, options, _recursing) {
117117
// vulnerableTags
118118
vulnerableTags.forEach(function (tag) {
119119
if (
120-
options.allowedTags && options.allowedTags.indexOf(tag) > -1 &&
120+
options.allowedTags !== false && (options.allowedTags || []).indexOf(tag) > -1 &&
121121
!options.allowVulnerableTags
122122
) {
123123
console.warn(`\n\n⚠️ Your \`allowedTags\` option includes, \`${tag}\`, which is inherently\nvulnerable to XSS attacks. Please remove it from \`allowedTags\`.\nOr, to disable this warning, add the \`allowVulnerableTags\` option\nand ensure you are accounting for this risk.\n\n`);
@@ -251,7 +251,7 @@ function sanitizeHtml(html, options, _recursing) {
251251
}
252252
}
253253

254-
if ((options.allowedTags && options.allowedTags.indexOf(name) === -1) || (options.disallowedTagsMode === 'recursiveEscape' && !isEmptyObject(skipMap)) || (options.nestingLimit != null && depth >= options.nestingLimit)) {
254+
if ((options.allowedTags !== false && (options.allowedTags || []).indexOf(name) === -1) || (options.disallowedTagsMode === 'recursiveEscape' && !isEmptyObject(skipMap)) || (options.nestingLimit != null && depth >= options.nestingLimit)) {
255255
skip = true;
256256
skipMap[depth] = true;
257257
if (options.disallowedTagsMode === 'discard') {

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ describe('sanitizeHtml', function() {
4040
allowedAttributes: false
4141
}), '<div><wiggly worms="ewww">hello</wiggly></div>');
4242
});
43+
it('should not pass through any markup if allowedTags is set to undefined (falsy but not exactly false)', function() {
44+
assert.equal(sanitizeHtml('<div><wiggly worms="ewww">hello</wiggly></div>', {
45+
allowedTags: undefined
46+
}), 'hello');
47+
});
48+
it('should not pass through any markup if allowedTags is set to 0 (falsy but not exactly false)', function() {
49+
assert.equal(sanitizeHtml('<div><wiggly worms="ewww">hello</wiggly></div>', {
50+
allowedTags: 0
51+
}), 'hello');
52+
});
53+
it('should not pass through any markup if allowedTags is set to null (falsy but not exactly false)', function() {
54+
assert.equal(sanitizeHtml('<div><wiggly worms="ewww">hello</wiggly></div>', {
55+
allowedTags: null
56+
}), 'hello');
57+
});
58+
it('should not pass through any markup if allowedTags is set to empty string (falsy but not exactly false)', function() {
59+
assert.equal(sanitizeHtml('<div><wiggly worms="ewww">hello</wiggly></div>', {
60+
allowedTags: ''
61+
}), 'hello');
62+
});
4363
it('should respect text nodes at top level', function() {
4464
assert.equal(sanitizeHtml('Blah blah blah<p>Whee!</p>'), 'Blah blah blah<p>Whee!</p>');
4565
});

0 commit comments

Comments
 (0)