sanitize-html 1.27.1 → 1.27.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/CHANGELOG.md +13 -2
- package/README.md +78 -69
- package/dist/sanitize-html-es2015.js +879 -689
- package/dist/sanitize-html.js +247 -107
- package/dist/sanitize-html.min.js +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
## Changelog
|
|
2
2
|
|
|
3
|
+
1.27.5 (2020-09-23):
|
|
4
|
+
- Updates README to include ES modules syntax.
|
|
5
|
+
|
|
6
|
+
1.27.4 (2020-08-26):
|
|
7
|
+
- Fixes an IE11 regression from using `Array.prototype.includes`, replacing it with `Array.prototype.indexOf`.
|
|
8
|
+
|
|
9
|
+
1.27.3 (2020-08-12):
|
|
10
|
+
- Fixes a bug when using `transformTags` with out `textFilter`. Thanks to [Andrzej Porebski](https://github.com/andpor) for the help with a failing test.
|
|
11
|
+
|
|
12
|
+
1.27.2 (2020-07-29):
|
|
13
|
+
- Fixes CHANGELOG links. Thanks to [Alex Mayer](https://github.com/amayer5125) for the contribution.
|
|
14
|
+
- Replaces `srcset` with `parse-srcset`. Thanks to [Massimiliano Mirra](https://github.com/bard) for the contribution.
|
|
15
|
+
|
|
3
16
|
1.27.1 (2020-07-15):
|
|
4
17
|
- Removes the unused chalk dependency.
|
|
5
18
|
- Adds configuration for a Github stale bot.
|
|
@@ -88,9 +101,7 @@ There is currently a commented-out test which verifies one example of the proble
|
|
|
88
101
|
1.18.0:
|
|
89
102
|
|
|
90
103
|
* The new `allowedSchemesAppliedToAttributes` option. This determines which attributes are validated as URLs, replacing the old hardcoded list of `src` and `href` only. The default list now includes `cite`. Thanks to ml-dublin for this contribution.
|
|
91
|
-
|
|
92
104
|
* It is now easy to configure a specific list of allowed values for an attribute. When configuring `allowedAttributes`, rather than listing an attribute name, simply list an object with an attribute `name` property and an allowed `values` array property. You can also add `multiple: true` to allow multiple space-separated allowed values in the attribute, otherwise the attribute must match one and only one of the allowed values. Thanks again to ml-dublin for this contribution.
|
|
93
|
-
|
|
94
105
|
* Fixed a bug in the npm test procedure.
|
|
95
106
|
|
|
96
107
|
1.17.0: the new `allowedIframeHostnames` option. If present, this must be an array, and only iframe `src` URLs hostnames (complete hostnames; domain name matches are not enough) that appear on this list are allowed. You must also configure `hostname` as an allowed attribute for `iframe`. Thanks to Ryan Verys for this contribution.
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# sanitize-html
|
|
2
2
|
|
|
3
|
-
[](https://circleci.com/gh/apostrophecms/sanitize-html/tree/main)
|
|
4
4
|
|
|
5
|
-
<a href="https://apostrophecms.com/"><img src="https://raw.
|
|
5
|
+
<a href="https://apostrophecms.com/"><img src="https://raw.githubusercontent.com/apostrophecms/sanitize-html/main/logos/logo-box-madefor.png" align="right" /></a>
|
|
6
6
|
|
|
7
7
|
`sanitize-html` provides a simple HTML sanitizer with a clear API.
|
|
8
8
|
|
|
@@ -71,13 +71,21 @@ Install module from console:
|
|
|
71
71
|
npm install sanitize-html
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
Import the module:
|
|
75
75
|
|
|
76
|
-
```
|
|
77
|
-
|
|
76
|
+
```bash
|
|
77
|
+
// In ESM
|
|
78
|
+
import sanitizeHtml from 'sanitize-html';
|
|
79
|
+
|
|
80
|
+
// Or in CJS
|
|
81
|
+
const sanitizeHtml = require('sanitize-html');
|
|
82
|
+
```
|
|
78
83
|
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
Use it in your JavaScript app:
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
const dirty = 'some really tacky HTML';
|
|
88
|
+
const clean = sanitizeHtml(dirty);
|
|
81
89
|
```
|
|
82
90
|
|
|
83
91
|
That will allow our default list of allowed tags and attributes through. It's a nice set, but probably not quite what you want. So:
|
|
@@ -161,14 +169,15 @@ If you set `disallowedTagsMode` to `recursiveEscape`, the disallowed tags are es
|
|
|
161
169
|
When configuring the attribute in `allowedAttributes` simply use an object with attribute `name` and an allowed `values` array. In the following example `sandbox="allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-scripts"` would become `sandbox="allow-popups allow-scripts"`:
|
|
162
170
|
|
|
163
171
|
```js
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
+
allowedAttributes: {
|
|
173
|
+
iframe: [
|
|
174
|
+
{
|
|
175
|
+
name: 'sandbox',
|
|
176
|
+
multiple: true,
|
|
177
|
+
values: ['allow-popups', 'allow-same-origin', 'allow-scripts']
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
}
|
|
172
181
|
```
|
|
173
182
|
|
|
174
183
|
With `multiple: true`, several allowed values may appear in the same attribute, separated by spaces. Otherwise the attribute must exactly match one and only one of the allowed values.
|
|
@@ -177,7 +186,7 @@ With `multiple: true`, several allowed values may appear in the same attribute,
|
|
|
177
186
|
|
|
178
187
|
You can use the `*` wildcard to allow all attributes with a certain prefix:
|
|
179
188
|
|
|
180
|
-
```
|
|
189
|
+
```js
|
|
181
190
|
allowedAttributes: {
|
|
182
191
|
a: [ 'href', 'data-*' ]
|
|
183
192
|
}
|
|
@@ -185,7 +194,7 @@ allowedAttributes: {
|
|
|
185
194
|
|
|
186
195
|
Also you can use the `*` as name for a tag, to allow listed attributes to be valid for any tag:
|
|
187
196
|
|
|
188
|
-
```
|
|
197
|
+
```js
|
|
189
198
|
allowedAttributes: {
|
|
190
199
|
'*': [ 'href', 'align', 'alt', 'center', 'bgcolor' ]
|
|
191
200
|
}
|
|
@@ -196,7 +205,7 @@ Some text editing applications generate HTML to allow copying over to a web appl
|
|
|
196
205
|
|
|
197
206
|
Setting this option to true will instruct sanitize-html to discard all characters outside of `html` tag boundaries -- before `<html>` and after `</html>` tags.
|
|
198
207
|
|
|
199
|
-
```
|
|
208
|
+
```js
|
|
200
209
|
enforceHtmlBoundary: true
|
|
201
210
|
```
|
|
202
211
|
|
|
@@ -204,7 +213,7 @@ enforceHtmlBoundary: true
|
|
|
204
213
|
|
|
205
214
|
`santizeHtml` is built on `htmlparser2`. By default the only option passed down is `decodeEntities: true` You can set the options to pass by using the parser option.
|
|
206
215
|
|
|
207
|
-
```
|
|
216
|
+
```js
|
|
208
217
|
clean = sanitizeHtml(dirty, {
|
|
209
218
|
allowedTags: ['a'],
|
|
210
219
|
parser: {
|
|
@@ -234,14 +243,14 @@ The most advanced usage:
|
|
|
234
243
|
clean = sanitizeHtml(dirty, {
|
|
235
244
|
transformTags: {
|
|
236
245
|
'ol': function(tagName, attribs) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
246
|
+
// My own custom magic goes here
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
tagName: 'ul',
|
|
250
|
+
attribs: {
|
|
251
|
+
class: 'foo'
|
|
252
|
+
}
|
|
253
|
+
};
|
|
245
254
|
}
|
|
246
255
|
}
|
|
247
256
|
});
|
|
@@ -273,10 +282,10 @@ You can also add or modify the text contents of a tag:
|
|
|
273
282
|
clean = sanitizeHtml(dirty, {
|
|
274
283
|
transformTags: {
|
|
275
284
|
'a': function(tagName, attribs) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
285
|
+
return {
|
|
286
|
+
tagName: 'a',
|
|
287
|
+
text: 'Some text'
|
|
288
|
+
};
|
|
280
289
|
}
|
|
281
290
|
}
|
|
282
291
|
});
|
|
@@ -300,12 +309,12 @@ You can provide a filter function to remove unwanted tags. Let's suppose we need
|
|
|
300
309
|
|
|
301
310
|
We can do that with the following filter:
|
|
302
311
|
|
|
303
|
-
```
|
|
312
|
+
```js
|
|
304
313
|
sanitizeHtml(
|
|
305
314
|
'<p>This is <a href="http://www.linux.org"></a><br/>Linux</p>',
|
|
306
315
|
{
|
|
307
316
|
exclusiveFilter: function(frame) {
|
|
308
|
-
|
|
317
|
+
return frame.tag === 'a' && !frame.text.trim();
|
|
309
318
|
}
|
|
310
319
|
}
|
|
311
320
|
);
|
|
@@ -327,7 +336,7 @@ You can also process all text content with a provided filter function. Let's say
|
|
|
327
336
|
|
|
328
337
|
We can do that with the following filter:
|
|
329
338
|
|
|
330
|
-
```
|
|
339
|
+
```js
|
|
331
340
|
sanitizeHtml(
|
|
332
341
|
'<p>some text...</p>',
|
|
333
342
|
{
|
|
@@ -350,15 +359,15 @@ These arrays will be checked against the html that is passed to the function and
|
|
|
350
359
|
|
|
351
360
|
Make sure to pass a valid hostname along with the domain you wish to allow, i.e.:
|
|
352
361
|
|
|
353
|
-
```
|
|
354
|
-
|
|
355
|
-
|
|
362
|
+
```js
|
|
363
|
+
allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com'],
|
|
364
|
+
allowedIframeDomains: ['zoom.us']
|
|
356
365
|
```
|
|
357
366
|
|
|
358
367
|
You may also specify whether or not to allow relative URLs as iframe sources.
|
|
359
368
|
|
|
360
|
-
```
|
|
361
|
-
|
|
369
|
+
```js
|
|
370
|
+
allowIframeRelativeUrls: true
|
|
362
371
|
```
|
|
363
372
|
|
|
364
373
|
Note that if unspecified, relative URLs will be allowed by default if no hostname or domain filter is provided but removed by default if a hostname or domain filter is provided.
|
|
@@ -367,7 +376,7 @@ Note that if unspecified, relative URLs will be allowed by default if no hostnam
|
|
|
367
376
|
|
|
368
377
|
For example:
|
|
369
378
|
|
|
370
|
-
```
|
|
379
|
+
```js
|
|
371
380
|
clean = sanitizeHtml('<p><iframe src="https://www.youtube.com/embed/nykIhs12345"></iframe><p>', {
|
|
372
381
|
allowedTags: [ 'p', 'em', 'strong', 'iframe' ],
|
|
373
382
|
allowedClasses: {
|
|
@@ -382,7 +391,7 @@ clean = sanitizeHtml('<p><iframe src="https://www.youtube.com/embed/nykIhs12345"
|
|
|
382
391
|
|
|
383
392
|
will pass through as safe whereas:
|
|
384
393
|
|
|
385
|
-
```
|
|
394
|
+
```js
|
|
386
395
|
clean = sanitizeHtml('<p><iframe src="https://www.youtube.net/embed/nykIhs12345"></iframe><p>', {
|
|
387
396
|
allowedTags: [ 'p', 'em', 'strong', 'iframe' ],
|
|
388
397
|
allowedClasses: {
|
|
@@ -397,7 +406,7 @@ clean = sanitizeHtml('<p><iframe src="https://www.youtube.net/embed/nykIhs12345"
|
|
|
397
406
|
|
|
398
407
|
or
|
|
399
408
|
|
|
400
|
-
```
|
|
409
|
+
```js
|
|
401
410
|
clean = sanitizeHtml('<p><iframe src="https://www.vimeo/video/12345"></iframe><p>', {
|
|
402
411
|
allowedTags: [ 'p', 'em', 'strong', 'iframe' ],
|
|
403
412
|
allowedClasses: {
|
|
@@ -414,7 +423,7 @@ will return an empty iframe tag.
|
|
|
414
423
|
|
|
415
424
|
If you want to allow any subdomain of any level you can provide the domain in `allowedIframeDomains`
|
|
416
425
|
|
|
417
|
-
```
|
|
426
|
+
```js
|
|
418
427
|
clean = sanitizeHtml('<p><iframe src="https://us02web.zoom.us/embed/12345"></iframe><p>', {
|
|
419
428
|
allowedTags: [ 'p', 'em', 'strong', 'iframe' ],
|
|
420
429
|
allowedClasses: {
|
|
@@ -436,7 +445,7 @@ If you wish to allow specific CSS classes on a particular element, you can do so
|
|
|
436
445
|
|
|
437
446
|
This implies that the `class` attribute is allowed on that element.
|
|
438
447
|
|
|
439
|
-
```
|
|
448
|
+
```js
|
|
440
449
|
// Allow only a restricted set of CSS classes and only on the p tag
|
|
441
450
|
clean = sanitizeHtml(dirty, {
|
|
442
451
|
allowedTags: [ 'p', 'em', 'strong' ],
|
|
@@ -456,25 +465,25 @@ If you wish to allow specific CSS _styles_ on a particular element, you can do t
|
|
|
456
465
|
|
|
457
466
|
**URLs in inline styles are NOT filtered by any mechanism other than your regular expression.**
|
|
458
467
|
|
|
459
|
-
```
|
|
468
|
+
```js
|
|
460
469
|
clean = sanitizeHtml(dirty, {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
470
|
+
allowedTags: ['p'],
|
|
471
|
+
allowedAttributes: {
|
|
472
|
+
'p': ["style"],
|
|
473
|
+
},
|
|
474
|
+
allowedStyles: {
|
|
475
|
+
'*': {
|
|
476
|
+
// Match HEX and RGB
|
|
477
|
+
'color': [/^#(0x)?[0-9a-f]+$/i, /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/],
|
|
478
|
+
'text-align': [/^left$/, /^right$/, /^center$/],
|
|
479
|
+
// Match any number with px, em, or %
|
|
480
|
+
'font-size': [/^\d+(?:px|em|%)$/]
|
|
481
|
+
},
|
|
482
|
+
'p': {
|
|
483
|
+
'font-size': [/^\d+rem$/]
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
});
|
|
478
487
|
```
|
|
479
488
|
|
|
480
489
|
### Allowed URL schemes
|
|
@@ -487,7 +496,7 @@ By default we allow the following URL schemes in cases where `href`, `src`, etc.
|
|
|
487
496
|
|
|
488
497
|
You can override this if you want to:
|
|
489
498
|
|
|
490
|
-
```
|
|
499
|
+
```js
|
|
491
500
|
sanitizeHtml(
|
|
492
501
|
// teeny-tiny valid transparent GIF in a data URL
|
|
493
502
|
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />',
|
|
@@ -500,7 +509,7 @@ sanitizeHtml(
|
|
|
500
509
|
|
|
501
510
|
You can also allow a scheme for a particular tag only:
|
|
502
511
|
|
|
503
|
-
```
|
|
512
|
+
```js
|
|
504
513
|
allowedSchemes: [ 'http', 'https' ],
|
|
505
514
|
allowedSchemesByTag: {
|
|
506
515
|
img: [ 'data' ]
|
|
@@ -509,7 +518,7 @@ allowedSchemesByTag: {
|
|
|
509
518
|
|
|
510
519
|
And you can forbid the use of protocol-relative URLs (starting with `//`) to access another site using the current protocol, which is allowed by default:
|
|
511
520
|
|
|
512
|
-
```
|
|
521
|
+
```js
|
|
513
522
|
allowProtocolRelative: false
|
|
514
523
|
```
|
|
515
524
|
|
|
@@ -524,7 +533,7 @@ The exceptions are:
|
|
|
524
533
|
If you wish to replace this list, for instance to discard whatever is found
|
|
525
534
|
inside a `noscript` tag, use the `nonTextTags` option:
|
|
526
535
|
|
|
527
|
-
```
|
|
536
|
+
```js
|
|
528
537
|
nonTextTags: [ 'style', 'script', 'textarea', 'option', 'noscript' ]
|
|
529
538
|
```
|
|
530
539
|
|
|
@@ -540,7 +549,7 @@ disabled with the `allowVulnerableTags: true` option.
|
|
|
540
549
|
|
|
541
550
|
Instead of discarding, or keeping text only, you may enable escaping of the entire content:
|
|
542
551
|
|
|
543
|
-
```
|
|
552
|
+
```js
|
|
544
553
|
disallowedTagsMode: 'escape'
|
|
545
554
|
```
|
|
546
555
|
|
|
@@ -554,10 +563,10 @@ Valid values are: `'discard'` (default), `'escape'` (escape the tag) and `'recur
|
|
|
554
563
|
|
|
555
564
|
## Changelog
|
|
556
565
|
|
|
557
|
-
[The changelog is now in a separate file for readability.](https://github.com/apostrophecms/sanitize-html/blob/
|
|
566
|
+
[The changelog is now in a separate file for readability.](https://github.com/apostrophecms/sanitize-html/blob/main/CHANGELOG.md)
|
|
558
567
|
|
|
559
568
|
## Support
|
|
560
569
|
|
|
561
570
|
Feel free to open issues on [github](http://github.com/apostrophecms/sanitize-html).
|
|
562
571
|
|
|
563
|
-
<a href="http://apostrophecms.com/"><img src="https://raw.
|
|
572
|
+
<a href="http://apostrophecms.com/"><img src="https://raw.githubusercontent.com/apostrophecms/sanitize-html/main/logos/logo-box-builtby.png" /></a>
|