sanitize-html 1.18.1 → 1.18.5

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.
package/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: node_js
2
+ node_js:
3
+ - "stable"
4
+ - "lts/*"
5
+
package/README.md CHANGED
@@ -14,7 +14,7 @@ The syntax of poorly closed `p` and `img` elements is cleaned up.
14
14
 
15
15
  `href` attributes are validated to ensure they only contain `http`, `https`, `ftp` and `mailto` URLs. Relative URLs are also allowed. Ditto for `src` attributes.
16
16
 
17
- Allowing particular urls as a `src` to an iframe tag by filtering hostnames is also supported.
17
+ Allowing particular urls as a `src` to an iframe tag by filtering hostnames is also supported.
18
18
 
19
19
  HTML comments are not preserved.
20
20
 
@@ -106,7 +106,7 @@ If you do not specify `allowedTags` or `allowedAttributes` our default list is a
106
106
  ```js
107
107
  allowedTags: [ 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
108
108
  'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div',
109
- 'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre' ],
109
+ 'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'iframe' ],
110
110
  allowedAttributes: {
111
111
  a: [ 'href', 'name', 'target' ],
112
112
  // We don't currently allow img itself by default, but this
@@ -148,7 +148,7 @@ When configuring the attribute in `allowedAttributes` simply use an object with
148
148
 
149
149
  ```js
150
150
  allowedAttributes: {
151
- iframe: [
151
+ iframe: [
152
152
  {
153
153
  name: 'sandbox',
154
154
  multiple: true,
@@ -318,11 +318,11 @@ Note that the text passed to the `textFilter` method is already escaped for safe
318
318
 
319
319
  ### Iframe Filters
320
320
 
321
- If you would like to allow iframe tags but want to control the domains that are allowed through you can provide an array of hostnames that you would like to allow as iframe sources. This hostname is a property in the options object passed as an argument to the `sanitize-html` function.
321
+ If you would like to allow iframe tags but want to control the domains that are allowed through you can provide an array of hostnames that you would like to allow as iframe sources. This hostname is a property in the options object passed as an argument to the `sanitize-html` function.
322
322
 
323
- This array will be checked against the html that is passed to the function and return only `src` urls that include the allowed hostnames in the object. The url in the html that is passed must be formatted correctly (valid hostname) as an embedded iframe otherwise the module will strip out the src from the iframe.
323
+ This array will be checked against the html that is passed to the function and return only `src` urls that include the allowed hostnames in the object. The url in the html that is passed must be formatted correctly (valid hostname) as an embedded iframe otherwise the module will strip out the src from the iframe.
324
324
 
325
- Make sure to pass a valid hostname along with the domain you wish to allow, i.e.:
325
+ Make sure to pass a valid hostname along with the domain you wish to allow, i.e.:
326
326
 
327
327
  ```javascript
328
328
  allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com']
@@ -337,6 +337,8 @@ clean = sanitizeHtml('<p><iframe src="https://www.youtube.com/embed/nykIhs12345"
337
337
  allowedTags: [ 'p', 'em', 'strong', 'iframe' ],
338
338
  allowedClasses: {
339
339
  'p': [ 'fancy', 'simple' ],
340
+ },
341
+ allowedAttributes: {
340
342
  'iframe': ['src']
341
343
  },
342
344
  allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com']
@@ -350,19 +352,23 @@ clean = sanitizeHtml('<p><iframe src="https://www.youtube.net/embed/nykIhs12345"
350
352
  allowedTags: [ 'p', 'em', 'strong', 'iframe' ],
351
353
  allowedClasses: {
352
354
  'p': [ 'fancy', 'simple' ],
355
+ },
356
+ allowedAttributes: {
353
357
  'iframe': ['src']
354
358
  },
355
359
  allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com']
356
360
  });
357
361
  ```
358
362
 
359
- or
363
+ or
360
364
 
361
365
  ```javascript
362
366
  clean = sanitizeHtml('<p><iframe src="https://www.vimeo/video/12345"></iframe><p>', {
363
367
  allowedTags: [ 'p', 'em', 'strong', 'iframe' ],
364
368
  allowedClasses: {
365
369
  'p': [ 'fancy', 'simple' ],
370
+ },
371
+ allowedAttributes: {
366
372
  'iframe': ['src']
367
373
  },
368
374
  allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com']
@@ -409,7 +415,7 @@ clean = sanitizeHtml(dirty, {
409
415
  'color': [/^\#(0x)?[0-9a-f]+$/i, /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/],
410
416
  'text-align': [/^left$/, /^right$/, /^center$/],
411
417
  // Match any number with px, em, or %
412
- 'font-size': [/^\d+$[px|em|\%]$/]
418
+ 'font-size': [/^\d+(?:px|em|%)$/]
413
419
  },
414
420
  'p': {
415
421
  'font-size': [/^\d+rem$/]
@@ -474,6 +480,26 @@ The content still gets escaped properly, with the exception of the `script` and
474
480
 
475
481
  ## Changelog
476
482
 
483
+ 1.18.5:
484
+
485
+ * Stop double encoding ampersands on HTML entities. Thanks to Will Gibson.
486
+
487
+ 1.18.4:
488
+
489
+ * Removed incorrect `browser` key, restoring frontend build. Thanks to Felix Becker.
490
+
491
+ 1.18.3:
492
+
493
+ * `iframe` is an allowed tag by default, to better facilitate typical use cases and the use of the `allowedIframeHostnames` option.
494
+ * Documentation improvements.
495
+ * More browser packaging improvements.
496
+ * Protocol-relative URLs are properly supported for iframe tags.
497
+
498
+ 1.18.2:
499
+
500
+ * Travis tests passing.
501
+ * Fixed another case issue — and instituted Travis CI testing so this doesn't happen again. Sorry for the hassle.
502
+
477
503
  1.18.1:
478
504
 
479
505
  * A file was required with incorrect case, breaking the library on case sensitive filesystems such as Linux. Fixed.
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ var quoteRegexp = require('lodash.escaperegexp');
6
6
  var cloneDeep = require('lodash.clonedeep');
7
7
  var mergeWith = require('lodash.mergewith');
8
8
  var isString = require('lodash.isstring');
9
- var isPlainObject = require('lodash.isPlainObject');
9
+ var isPlainObject = require('lodash.isplainobject');
10
10
  var srcset = require('srcset');
11
11
  var postcss = require('postcss');
12
12
  var url = require('url');
@@ -280,13 +280,10 @@ function sanitizeHtml(html, options, _recursing) {
280
280
  }
281
281
  }
282
282
  if (name === 'iframe' && a === 'src') {
283
- //Check if value contains proper hostname prefix
284
- if (value.substring(0, 2) === '//') {
285
- var prefix = 'https:';
286
- value = prefix.concat(value);
287
- }
288
283
  try {
289
- parsed = url.parse(value);
284
+ // naughtyHref is in charge of whether protocol relative URLs
285
+ // are cool. We should just accept them
286
+ parsed = url.parse(value, false, true);
290
287
  if (options.allowedIframeHostnames) {
291
288
  var whitelistedHostnames = options.allowedIframeHostnames.find(function (hostname) {
292
289
  return hostname === parsed.hostname;
@@ -454,7 +451,8 @@ function sanitizeHtml(html, options, _recursing) {
454
451
  if (typeof s !== 'string') {
455
452
  s = s + '';
456
453
  }
457
- return s.replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/\>/g, '&gt;').replace(/\"/g, '&quot;');
454
+ return s.replace(/&(?![a-zA-Z0-9#]{1,7};)/g, '&amp;') // Match ampersands not part of existing HTML entity
455
+ .replace(/</g, '&lt;').replace(/\>/g, '&gt;').replace(/\"/g, '&quot;');
458
456
  }
459
457
 
460
458
  function naughtyHref(name, href) {
@@ -7,7 +7,7 @@ var quoteRegexp = require('lodash.escaperegexp');
7
7
  var cloneDeep = require('lodash.clonedeep');
8
8
  var mergeWith = require('lodash.mergewith');
9
9
  var isString = require('lodash.isstring');
10
- var isPlainObject = require('lodash.isPlainObject');
10
+ var isPlainObject = require('lodash.isplainobject');
11
11
  var srcset = require('srcset');
12
12
  var postcss = require('postcss');
13
13
  var url = require('url');
@@ -281,13 +281,10 @@ function sanitizeHtml(html, options, _recursing) {
281
281
  }
282
282
  }
283
283
  if (name === 'iframe' && a === 'src') {
284
- //Check if value contains proper hostname prefix
285
- if (value.substring(0, 2) === '//') {
286
- var prefix = 'https:';
287
- value = prefix.concat(value);
288
- }
289
284
  try {
290
- parsed = url.parse(value);
285
+ // naughtyHref is in charge of whether protocol relative URLs
286
+ // are cool. We should just accept them
287
+ parsed = url.parse(value, false, true);
291
288
  if (options.allowedIframeHostnames) {
292
289
  var whitelistedHostnames = options.allowedIframeHostnames.find(function (hostname) {
293
290
  return hostname === parsed.hostname;
@@ -455,7 +452,8 @@ function sanitizeHtml(html, options, _recursing) {
455
452
  if (typeof s !== 'string') {
456
453
  s = s + '';
457
454
  }
458
- return s.replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/\>/g, '&gt;').replace(/\"/g, '&quot;');
455
+ return s.replace(/&(?![a-zA-Z0-9#]{1,7};)/g, '&amp;') // Match ampersands not part of existing HTML entity
456
+ .replace(/</g, '&lt;').replace(/\>/g, '&gt;').replace(/\"/g, '&quot;');
459
457
  }
460
458
 
461
459
  function naughtyHref(name, href) {
@@ -620,7 +618,7 @@ sanitizeHtml.simpleTransform = function (newTagName, newAttribs, merge) {
620
618
  };
621
619
  };
622
620
  };
623
- },{"htmlparser2":36,"lodash.clonedeep":41,"lodash.escaperegexp":42,"lodash.isPlainObject":43,"lodash.isstring":44,"lodash.mergewith":45,"postcss":60,"srcset":91,"url":109,"xtend":112}],2:[function(require,module,exports){
621
+ },{"htmlparser2":36,"lodash.clonedeep":41,"lodash.escaperegexp":42,"lodash.isplainobject":43,"lodash.isstring":44,"lodash.mergewith":45,"postcss":60,"srcset":91,"url":109,"xtend":112}],2:[function(require,module,exports){
624
622
  (function (global){
625
623
  'use strict';
626
624