sanitize-html 1.13.0 → 1.14.3
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/README.md +13 -1
- package/index.js +9 -3
- package/package.json +2 -2
- package/test/test.js +30 -0
- package/.npmignore +0 -6
package/README.md
CHANGED
|
@@ -114,7 +114,8 @@ allowedAttributes: {
|
|
|
114
114
|
selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
|
|
115
115
|
// URL schemes we permit
|
|
116
116
|
allowedSchemes: [ 'http', 'https', 'ftp', 'mailto' ],
|
|
117
|
-
allowedSchemesByTag: {}
|
|
117
|
+
allowedSchemesByTag: {},
|
|
118
|
+
allowProtocolRelative: true
|
|
118
119
|
```
|
|
119
120
|
|
|
120
121
|
#### "What if I want to allow all tags or all attributes?"
|
|
@@ -339,6 +340,12 @@ allowedSchemesByTag: {
|
|
|
339
340
|
}
|
|
340
341
|
```
|
|
341
342
|
|
|
343
|
+
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:
|
|
344
|
+
|
|
345
|
+
```javascript
|
|
346
|
+
allowProtocolRelative: false
|
|
347
|
+
```
|
|
348
|
+
|
|
342
349
|
### Discarding the entire contents of a disallowed tag
|
|
343
350
|
|
|
344
351
|
Normally, with a few exceptions, if a tag is not allowed, all of the text within it is preserved, and so are any allowed tags within it.
|
|
@@ -359,6 +366,11 @@ The content still gets escaped properly, with the exception of the `script` and
|
|
|
359
366
|
|
|
360
367
|
## Changelog
|
|
361
368
|
|
|
369
|
+
1.14.3: inadvertent removal of lodash regexp quote dependency in 1.14.2 has been corrected.
|
|
370
|
+
1.14.2: protocol-relative URL detection must spot URLs starting with `\\` rather than `//` due to ages-old tolerance features of web browsers, intended for sleepy Windows developers. Thanks to Martin Bajanik.
|
|
371
|
+
1.14.1: documented `allowProtocolRelative` option. No code changes from 1.14.0, released a few moments ago.
|
|
372
|
+
1.14.0: the new `allowProtocolRelative` option, which is set to `true` by default, allows you to decline to accept URLs that start with `//` and thus point to a different host using the current protocol. If you do **not** want to permit this, set this option to `false`. This is fully backwards compatible because the default behavior is to allow them. Thanks to Luke Bernard.
|
|
373
|
+
|
|
362
374
|
1.13.0: `transformTags` can now add text to an element that initially had none. Thanks to Dushyant Singh.
|
|
363
375
|
|
|
364
376
|
1.12.0: option to build for browser-side use. Thanks to Michael Blum.
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var htmlparser = require('htmlparser2');
|
|
2
2
|
var extend = require('xtend');
|
|
3
|
-
var quoteRegexp = require('
|
|
3
|
+
var quoteRegexp = require('lodash.escaperegexp');
|
|
4
4
|
|
|
5
5
|
function each(obj, cb) {
|
|
6
6
|
if (obj) Object.keys(obj).forEach(function (key) {
|
|
@@ -296,7 +296,12 @@ function sanitizeHtml(html, options, _recursing) {
|
|
|
296
296
|
// Case insensitive so we don't get faked out by JAVASCRIPT #1
|
|
297
297
|
var matches = href.match(/^([a-zA-Z]+)\:/);
|
|
298
298
|
if (!matches) {
|
|
299
|
-
//
|
|
299
|
+
// Protocol-relative URL starting with any combination of '/' and '\'
|
|
300
|
+
if (href.match(/^[\/\\]{2}/)) {
|
|
301
|
+
return !options.allowProtocolRelative;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// No scheme
|
|
300
305
|
return false;
|
|
301
306
|
}
|
|
302
307
|
var scheme = matches[1].toLowerCase();
|
|
@@ -340,7 +345,8 @@ sanitizeHtml.defaults = {
|
|
|
340
345
|
selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
|
|
341
346
|
// URL schemes we permit
|
|
342
347
|
allowedSchemes: [ 'http', 'https', 'ftp', 'mailto' ],
|
|
343
|
-
allowedSchemesByTag: {}
|
|
348
|
+
allowedSchemesByTag: {},
|
|
349
|
+
allowProtocolRelative: true
|
|
344
350
|
};
|
|
345
351
|
|
|
346
352
|
sanitizeHtml.simpleTransform = function(newTagName, newAttribs, merge) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sanitize-html",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.3",
|
|
4
4
|
"description": "Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"htmlparser2": "^3.9.0",
|
|
28
|
-
"
|
|
28
|
+
"lodash.escaperegexp": "^4.1.2",
|
|
29
29
|
"xtend": "^4.0.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
package/test/test.js
CHANGED
|
@@ -514,4 +514,34 @@ describe('sanitizeHtml', function() {
|
|
|
514
514
|
), '!<textarea></textarea><svg/onload=prompt`xs`></textarea>!'
|
|
515
515
|
);
|
|
516
516
|
});
|
|
517
|
+
it('should allow protocol relative links by default', function() {
|
|
518
|
+
assert.equal(
|
|
519
|
+
sanitizeHtml('<a href="//cnn.com/example">test</a>'),
|
|
520
|
+
'<a href="//cnn.com/example">test</a>'
|
|
521
|
+
);
|
|
522
|
+
});
|
|
523
|
+
it('should not allow protocol relative links when allowProtocolRelative is false', function() {
|
|
524
|
+
assert.equal(
|
|
525
|
+
sanitizeHtml('<a href="//cnn.com/example">test</a>', { allowProtocolRelative: false }),
|
|
526
|
+
'<a>test</a>'
|
|
527
|
+
);
|
|
528
|
+
assert.equal(
|
|
529
|
+
sanitizeHtml('<a href="/\\cnn.com/example">test</a>', { allowProtocolRelative: false }),
|
|
530
|
+
'<a>test</a>'
|
|
531
|
+
);
|
|
532
|
+
assert.equal(
|
|
533
|
+
sanitizeHtml('<a href="\\\\cnn.com/example">test</a>', { allowProtocolRelative: false }),
|
|
534
|
+
'<a>test</a>'
|
|
535
|
+
);
|
|
536
|
+
assert.equal(
|
|
537
|
+
sanitizeHtml('<a href="\\/cnn.com/example">test</a>', { allowProtocolRelative: false }),
|
|
538
|
+
'<a>test</a>'
|
|
539
|
+
);
|
|
540
|
+
});
|
|
541
|
+
it('should still allow regular relative URLs when allowProtocolRelative is false', function() {
|
|
542
|
+
assert.equal(
|
|
543
|
+
sanitizeHtml('<a href="/welcome">test</a>', { allowProtocolRelative: false }),
|
|
544
|
+
'<a href="/welcome">test</a>'
|
|
545
|
+
);
|
|
546
|
+
});
|
|
517
547
|
});
|