sanitize-html 2.8.0 → 2.9.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 +20 -1
  2. package/index.js +18 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -139,7 +139,8 @@ allowedSchemes: [ 'http', 'https', 'ftp', 'mailto', 'tel' ],
139
139
  allowedSchemesByTag: {},
140
140
  allowedSchemesAppliedToAttributes: [ 'href', 'src', 'cite' ],
141
141
  allowProtocolRelative: true,
142
- enforceHtmlBoundary: false
142
+ enforceHtmlBoundary: false,
143
+ parseStyleAttributes: true
143
144
  ```
144
145
 
145
146
  ### Common use cases
@@ -624,6 +625,24 @@ This will transform `<disallowed>content</disallowed>` to `&lt;disallowed&gt;con
624
625
 
625
626
  Valid values are: `'discard'` (default), `'escape'` (escape the tag) and `'recursiveEscape'` (to escape the tag and all its content).
626
627
 
628
+ ### Ignore style attribute contents
629
+
630
+ Instead of discarding faulty style attributes, you can allow them by disabling the parsing of style attributes:
631
+
632
+ ```js
633
+ parseStyleAttributes: false
634
+ ```
635
+
636
+ This will transform `<div style="invalid-prop: non-existing-value">content</div>` to `<div style="invalid-prop: non-existing-value">content</div>` instead of stripping it: `<div>content</div>`
637
+
638
+ By default the parseStyleAttributes option is true.
639
+
640
+ When you disable parsing of the style attribute (`parseStyleAttributes: false`) and you pass in options for the allowedStyles property, an error will be thrown. This combination is not permitted.
641
+
642
+ we recommend sanitizing content server-side in a Node.js environment, as you cannot trust a browser to sanitize things anyway. Consider what a malicious user could do via the network panel,
643
+ the browser console, or just by writing scripts that submit content similar to what your JavaScript submits. But if you really need to run it on the client in the browser,
644
+ you may find you need to disable parseStyleAttributes. This is subject to change as it is [an upstream issue with postcss](https://github.com/postcss/postcss/issues/1727), not sanitize-html itself.
645
+
627
646
  ### Restricting deep nesting
628
647
 
629
648
  You can limit the depth of HTML tags in the document with the `nestingLimit` option:
package/index.js CHANGED
@@ -83,6 +83,9 @@ function sanitizeHtml(html, options, _recursing) {
83
83
  if (html == null) {
84
84
  return '';
85
85
  }
86
+ if (typeof html === 'number') {
87
+ html = html.toString();
88
+ }
86
89
 
87
90
  let result = '';
88
91
  // Used for hot swapping the result variable with an empty string in order to "capture" the text written to it.
@@ -435,19 +438,24 @@ function sanitizeHtml(html, options, _recursing) {
435
438
  }
436
439
  }
437
440
  if (a === 'style') {
438
- try {
439
- const abstractSyntaxTree = postcssParse(name + ' {' + value + '}');
440
- const filteredAST = filterCss(abstractSyntaxTree, options.allowedStyles);
441
+ if (options.parseStyleAttributes) {
442
+ try {
443
+ const abstractSyntaxTree = postcssParse(name + ' {' + value + '}');
444
+ const filteredAST = filterCss(abstractSyntaxTree, options.allowedStyles);
441
445
 
442
- value = stringifyStyleAttributes(filteredAST);
446
+ value = stringifyStyleAttributes(filteredAST);
443
447
 
444
- if (value.length === 0) {
448
+ if (value.length === 0) {
449
+ delete frame.attribs[a];
450
+ return;
451
+ }
452
+ } catch (e) {
453
+ console.warn('Failed to parse "' + name + ' {' + value + '}' + '", If you\'re running this in a browser, we recommend to disable style parsing: options.parseStyleAttributes: false, since this only works in a node environment due to a postcss dependency, More info: https://github.com/apostrophecms/sanitize-html/issues/547');
445
454
  delete frame.attribs[a];
446
455
  return;
447
456
  }
448
- } catch (e) {
449
- delete frame.attribs[a];
450
- return;
457
+ } else if (options.allowedStyles) {
458
+ throw new Error('allowedStyles option cannot be used together with parseStyleAttributes: false.');
451
459
  }
452
460
  }
453
461
  result += ' ' + a;
@@ -815,7 +823,8 @@ sanitizeHtml.defaults = {
815
823
  allowedSchemesByTag: {},
816
824
  allowedSchemesAppliedToAttributes: [ 'href', 'src', 'cite' ],
817
825
  allowProtocolRelative: true,
818
- enforceHtmlBoundary: false
826
+ enforceHtmlBoundary: false,
827
+ parseStyleAttributes: true
819
828
  };
820
829
 
821
830
  sanitizeHtml.simpleTransform = function(newTagName, newAttribs, merge) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanitize-html",
3
- "version": "2.8.0",
3
+ "version": "2.9.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",