Skip to content

Commit

Permalink
Merge pull request #675 from anak-dev/anak-dev-main
Browse files Browse the repository at this point in the history
fix: `allowedClasses` whitelist ignored if tag is wildcard
  • Loading branch information
boutell authored Sep 23, 2024
2 parents f47281e + 5588fd8 commit c0a7bbd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fix to allow regex in `allowedClasses` wildcard whitelist.

## 2.13.0 (2024-03-20)

- Documentation update regarding minimum supported TypeScript version.
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,13 @@ function sanitizeHtml(html, options, _recursing) {
const allowedWildcardClasses = allowedClassesMap['*'];
const allowedSpecificClassesGlob = allowedClassesGlobMap[name];
const allowedSpecificClassesRegex = allowedClassesRegexMap[name];
const allowedWildcardClassesRegex = allowedClassesRegexMap['*'];
const allowedWildcardClassesGlob = allowedClassesGlobMap['*'];
const allowedClassesGlobs = [
allowedSpecificClassesGlob,
allowedWildcardClassesGlob
]
.concat(allowedSpecificClassesRegex)
.concat(allowedSpecificClassesRegex, allowedWildcardClassesRegex)
.filter(function (t) {
return t;
});
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,19 @@ describe('sanitizeHtml', function() {
'<p class="nifty33 dippy">whee</p>'
);
});
it('should allow classes that match `allowedClasses` regex for all tags', function() {
assert.equal(
sanitizeHtml(
'<p class="nifty33 nifty2 dippy">whee</p>',
{
allowedClasses: {
'*': [ /^nifty\d{2}$/, /^d\w{4}$/ ]
}
}
),
'<p class="nifty33 dippy">whee</p>'
);
});
it('should allow defining schemes on a per-tag basis', function() {
assert.equal(
sanitizeHtml(
Expand Down

0 comments on commit c0a7bbd

Please sign in to comment.