Skip to content

Commit

Permalink
Merge pull request #623 from KevinSJ/main
Browse files Browse the repository at this point in the history
Bug Fix: allow false in allowedClasses
  • Loading branch information
boutell committed Jun 13, 2023
2 parents 170269b + 94a79b6 commit 7df9d8b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## UNRELEASED

- Fix to allow `false` in `allowedClasses` attributes
- Upgrade mocha version
- Apply small linter fixes in tests
- Add `.idea` temp files to `.gitignore`
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ allowedClasses: {
}
```

If `allowedClasses` for a certain tag is `false`, all the classes for this tag will be allowed.

> Note: It is advised that your regular expressions always begin with `^` so that you are requiring a known prefix. A regular expression with neither `^` nor `$` just requires that something appear in the middle.
### Allowed CSS Styles
Expand Down
30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,24 @@ function sanitizeHtml(html, options, _recursing) {
allowedAttributesMap[tag].push('class');
}

allowedClassesMap[tag] = [];
allowedClassesRegexMap[tag] = [];
const globRegex = [];
classes.forEach(function(obj) {
if (typeof obj === 'string' && obj.indexOf('*') >= 0) {
globRegex.push(escapeStringRegexp(obj).replace(/\\\*/g, '.*'));
} else if (obj instanceof RegExp) {
allowedClassesRegexMap[tag].push(obj);
} else {
allowedClassesMap[tag].push(obj);
allowedClassesMap[tag] = classes;

if (Array.isArray(classes)) {
const globRegex = [];
allowedClassesMap[tag] = [];
allowedClassesRegexMap[tag] = [];
classes.forEach(function(obj) {
if (typeof obj === 'string' && obj.indexOf('*') >= 0) {
globRegex.push(escapeStringRegexp(obj).replace(/\\\*/g, '.*'));
} else if (obj instanceof RegExp) {
allowedClassesRegexMap[tag].push(obj);
} else {
allowedClassesMap[tag].push(obj);
}
});
if (globRegex.length) {
allowedClassesGlobMap[tag] = new RegExp('^(' + globRegex.join('|') + ')$');
}
});
if (globRegex.length) {
allowedClassesGlobMap[tag] = new RegExp('^(' + globRegex.join('|') + ')$');
}
});

Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,20 @@ describe('sanitizeHtml', function() {
'<p class="nifty simple dippy">whee</p>'
);
});
it('should allow all classes for a single tag if `allowedClasses` for the tag is false', function() {
assert.equal(
sanitizeHtml(
'<p class="nifty simple dippy">whee</p>',
{
allowedTags: [ 'p' ],
allowedClasses: {
p: false
}
}
),
'<p class="nifty simple dippy">whee</p>'
);
});
it('should allow only classes that matches `allowedClasses` regex', function() {
assert.equal(
sanitizeHtml(
Expand Down

0 comments on commit 7df9d8b

Please sign in to comment.