sanitize-html 2.12.1 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +34 -1
  2. package/index.js +6 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -40,6 +40,8 @@ If `esModuleInterop=true` is not set in your `tsconfig.json` file, you have to i
40
40
  import * as sanitizeHtml from 'sanitize-html';
41
41
  ```
42
42
 
43
+ When using TypeScript, there is a minimum supported version of >=4.5 because of a dependency on the `htmlparser2` types.
44
+
43
45
  Any questions or problems while using `@types/sanitize-html` should be directed to its maintainers as directed by that project's contribution guidelines.
44
46
 
45
47
  ## How to use
@@ -243,6 +245,8 @@ allowedAttributes: {}
243
245
 
244
246
  If you set `disallowedTagsMode` to `discard` (the default), disallowed tags are discarded. Any text content or subtags are still included, depending on whether the individual subtags are allowed.
245
247
 
248
+ If you set `disallowedTagsMode` to `completelyDiscard`, disallowed tags and any content they contain are discarded. Any subtags are still included, as long as those individual subtags are allowed.
249
+
246
250
  If you set `disallowedTagsMode` to `escape`, the disallowed tags are escaped rather than discarded. Any text or subtags are handled normally.
247
251
 
248
252
  If you set `disallowedTagsMode` to `recursiveEscape`, the disallowed tags are escaped rather than discarded, and the same treatment is applied to all subtags, whether otherwise allowed or not.
@@ -703,7 +707,36 @@ disallowedTagsMode: 'escape'
703
707
 
704
708
  This will transform `<disallowed>content</disallowed>` to `&lt;disallowed&gt;content&lt;/disallowed&gt;`
705
709
 
706
- Valid values are: `'discard'` (default), `'escape'` (escape the tag) and `'recursiveEscape'` (to escape the tag and all its content).
710
+ Valid values are: `'discard'` (default), `'completelyDiscard'` (remove disallowed tag's content), `'escape'` (escape the tag) and `'recursiveEscape'` (to escape the tag and all its content).
711
+
712
+ #### Discard disallowed but but the inner content of disallowed tags is kept.
713
+
714
+ If you set `disallowedTagsMode` to `discard`, disallowed tags are discarded but but the inner content of disallowed tags is kept.
715
+
716
+ ```js
717
+ disallowedTagsMode: 'discard'
718
+ ```
719
+ This will transform `<disallowed>content</disallowed>` to `content`
720
+
721
+ #### Discard entire content of a disallowed tag
722
+
723
+ If you set `disallowedTagsMode` to `completelyDiscard`, disallowed tags and any content they contain are discarded. Any subtags are still included, as long as those individual subtags are allowed.
724
+
725
+ ```js
726
+ disallowedTagsMode: 'completelyDiscard'
727
+ ```
728
+
729
+ This will transform `<disallowed>content <allowed>content</allowed> </disallowed>` to `<allowed>content</allowed>`
730
+
731
+ #### Escape the disallowed tag and all its children even for allowed tags.
732
+
733
+ if you set `disallowedTagsMode` to `recursiveEscape`, disallowed tag and its children will be escaped even for allowed tags
734
+
735
+ ```js
736
+ disallowedTagsMode: `recursiveEscape`
737
+ ```
738
+
739
+ This will transform `<disallowed>hello<p>world</p></disallowed>` to `&lt;disallowed&gt;hello&lt;p&gt;world&lt;/p&gt;&lt;/disallowed&gt;`
707
740
 
708
741
  ### Ignore style attribute contents
709
742
 
package/index.js CHANGED
@@ -262,7 +262,7 @@ function sanitizeHtml(html, options, _recursing) {
262
262
  if (!tagAllowed(name) || (options.disallowedTagsMode === 'recursiveEscape' && !isEmptyObject(skipMap)) || (options.nestingLimit != null && depth >= options.nestingLimit)) {
263
263
  skip = true;
264
264
  skipMap[depth] = true;
265
- if (options.disallowedTagsMode === 'discard') {
265
+ if (options.disallowedTagsMode === 'discard' || options.disallowedTagsMode === 'completelyDiscard') {
266
266
  if (nonTextTagsArray.indexOf(name) !== -1) {
267
267
  skipText = true;
268
268
  skipTextDepth = 1;
@@ -272,7 +272,7 @@ function sanitizeHtml(html, options, _recursing) {
272
272
  }
273
273
  depth++;
274
274
  if (skip) {
275
- if (options.disallowedTagsMode === 'discard') {
275
+ if (options.disallowedTagsMode === 'discard' || options.disallowedTagsMode === 'completelyDiscard') {
276
276
  // We want the contents but not this tag
277
277
  return;
278
278
  }
@@ -511,7 +511,9 @@ function sanitizeHtml(html, options, _recursing) {
511
511
  text = lastFrame.innerText !== undefined ? lastFrame.innerText : text;
512
512
  }
513
513
 
514
- if (options.disallowedTagsMode === 'discard' && ((tag === 'script') || (tag === 'style'))) {
514
+ if (options.disallowedTagsMode === 'completelyDiscard' && !tagAllowed(tag)) {
515
+ text = '';
516
+ } else if ((options.disallowedTagsMode === 'discard' || options.disallowedTagsMode === 'completelyDiscard') && ((tag === 'script') || (tag === 'style'))) {
515
517
  // htmlparser2 gives us these as-is. Escaping them ruins the content. Allowing
516
518
  // script tags is, by definition, game over for XSS protection, so if that's
517
519
  // your concern, don't allow them. The same is essentially true for style tags
@@ -559,7 +561,7 @@ function sanitizeHtml(html, options, _recursing) {
559
561
  const skip = skipMap[depth];
560
562
  if (skip) {
561
563
  delete skipMap[depth];
562
- if (options.disallowedTagsMode === 'discard') {
564
+ if (options.disallowedTagsMode === 'discard' || options.disallowedTagsMode === 'completelyDiscard') {
563
565
  frame.updateParentNodeText();
564
566
  return;
565
567
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanitize-html",
3
- "version": "2.12.1",
3
+ "version": "2.13.0",
4
4
  "description": "Clean up user-submitted HTML, preserving allowlisted elements and allowlisted attributes on a per-element basis",
5
5
  "sideEffects": false,
6
6
  "main": "index.js",