sanitize-html 2.0.0-beta.2 → 2.1.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 (4) hide show
  1. package/CHANGELOG.md +169 -82
  2. package/README.md +58 -33
  3. package/index.js +32 -17
  4. package/package.json +7 -7
package/CHANGELOG.md CHANGED
@@ -1,15 +1,35 @@
1
- ## Changelog
1
+ # Changelog
2
2
 
3
- 2.0.0-beta.2:
3
+ ## 2.1.0 (2020-10-07):
4
+ - `sup` added to the default allowed tags list. Thanks to [Julian Lam](https://github.com/julianlam) for the contribution.
5
+ - Updates default `allowedTags` README documentation. Thanks to [Marco Arduini](https://github.com/nerfologist) for the contribution.
6
+
7
+ ## 2.0.0 (2020-09-23):
8
+ - `nestingLimit` option added.
9
+ - Updates ESLint config package and fixes warnings.
10
+ - Upgrade `is-plain-object` package with named export. Thanks to [Bogdan Chadkin](https://github.com/TrySound) for the contribution.
11
+ - Upgrade `postcss` package and drop Node 11 and Node 13 support (enforced by postcss).
12
+
13
+ ### Backwards compatibility breaks:
14
+ - There is no build. You should no longer directly link to a sanitize-html file directly in the browser as it is using modern Javascript that is not fully supported by all major browsers (depending on your definition). You should now include sanitize-html in your project build for this purpose if you have one.
15
+ - On the server side, Node.js 10 or higher is required.
16
+ - The default `allowedTags` array was updated significantly. This mostly added HTML tags to be more comprehensive by default. You should review your projects and consider the `allowedTags` defaults if you are not already overriding them.
17
+
18
+ ## 2.0.0-rc.2 (2020-09-09):
19
+ - Always use existing `has` function rather than duplicating it.
20
+
21
+ ## 2.0.0-rc.1 (2020-08-26):
22
+ - Upgrade `klona` package. Thanks to [Bogdan Chadkin](https://github.com/TrySound) for the contribution.
23
+
24
+ ## 2.0.0-beta.2:
4
25
  - Add `files` to `package.json` to prevent publishing unnecessary files to npm #392. Thanks to [styfle](https://github.com/styfle) for the contribution.
5
26
  - Removes `iframe` and `nl` from default allowed tags. Adds most innocuous tags to the default `allowedTags` array.
6
27
  - Fixes a bug when using `transformTags` with out `textFilter`. Thanks to [Andrzej Porebski](https://github.com/andpor) for the help with a failing test.
7
28
 
8
- 2.0.0-beta:
29
+ ## 2.0.0-beta:
9
30
  - Moves the `index.js` file to the project root and removes all build steps within the package. Going forward, it is up to the developer to include sanitize-html in their project builds as-needed. This removes major points of conflict with project code and frees this module to not worry about myriad build-related questions.
10
31
  - Replaces lodash with utility packages: klona, is-plain-object, deepmerge, escape-string-regexp.
11
- - Makes custom tag transformations less error-prone by escaping frame `innerText`. Thanks to [Mike Samuel](https://github.com/mikesamuel) for the contribution.
12
- Prior to this patch, tag transformations which turned an attribute
32
+ - Makes custom tag transformations less error-prone by escaping frame `innerText`. Thanks to [Mike Samuel](https://github.com/mikesamuel) for the contribution. Prior to this patch, tag transformations which turned an attribute
13
33
  value into a text node could be vulnerable to code execution.
14
34
  - Updates code to use modern features including `const`/`let` variable assignment.
15
35
  - ESLint clean up.
@@ -18,52 +38,72 @@ value into a text node could be vulnerable to code execution.
18
38
 
19
39
  Thanks to [Bogdan Chadkin](https://github.com/TrySound) for contributions to this major version update.
20
40
 
21
- 1.27.1 (2020-07-15):
41
+ ## 1.27.5 (2020-09-23):
42
+ - Updates README to include ES modules syntax.
43
+
44
+ ## 1.27.4 (2020-08-26):
45
+ - Fixes an IE11 regression from using `Array.prototype.includes`, replacing it with `Array.prototype.indexOf`.
46
+
47
+ ## 1.27.3 (2020-08-12):
48
+ - Fixes a bug when using `transformTags` with out `textFilter`. Thanks to [Andrzej Porebski](https://github.com/andpor) for the help with a failing test.
49
+
50
+ ## 1.27.2 (2020-07-29):
51
+ - Fixes CHANGELOG links. Thanks to [Alex Mayer](https://github.com/amayer5125) for the contribution.
52
+ - Replaces `srcset` with `parse-srcset`. Thanks to [Massimiliano Mirra](https://github.com/bard) for the contribution.
53
+
54
+ ## 1.27.1 (2020-07-15):
22
55
  - Removes the unused chalk dependency.
23
56
  - Adds configuration for a Github stale bot.
24
57
  - Replace `xtend` package with native `Object.assign`.
25
58
 
26
- 1.27.0:
59
+ ## 1.27.0:
27
60
  - Adds the `allowedIframeDomains` option. This works similar to `allowedIframeHostnames`, where you would set it to an array of web domains. It would then permit any hostname on those domains to be used in iframe `src` attributes. Thanks to [Stanislav Kravchenko](https://github.com/StanisLove) for the contribution.
28
61
 
29
- 1.26.0:
62
+ ## 1.26.0:
30
63
  - Adds the `option` element to the default `nonTextTagsArray` of tags with contents that aren't meant to be displayed visually as text. This can be overridden with the `nonTextTags` option.
31
64
 
32
- 1.25.0:
65
+ ## 1.25.0:
33
66
  - Adds `enforceHtmlBoundary` option to process code bounded by the `html` tag, discarding any code outside of those tags.
34
67
  - Migrates to the main lodash package from the per method packages since they are deprecated and cause code duplication. Thanks to [Merceyz](https://github.com/merceyz) for the contribution.
35
68
  - Adds a warning when `style` and `script` tags are allowed, as they are inherently vulnerable to being used in XSS attacks. That warning can be disabled by including the option `allowVulnerableTags: true` so this choice is knowing and explicit.
36
69
 
37
- 1.24.0:
70
+ ## 1.24.0:
38
71
  - Fixes a bug where self-closing tags resulted in deletion with `disallowedTagsMode: 'escape'` set. Thanks to [Thiago Negri](https://github.com/thiago-negri) for the contribution.
39
72
  - Adds `abbr` to the default `allowedTags` for better accessibility support. Thanks to [Will Farrell](https://github.com/willfarrell) for the contribution.
40
73
  - Adds a `mediaChildren` property to the `frame` object in custom filters. This allows you to check for links or other parent tags that contain self-contained media to prevent collapse, regardless of whether there is also text inside. Thanks to [axdg](https://github.com/axdg) for the initial implementation and [Marco Arduini](https://github.com/nerfologist) for a failing test contribution.
41
74
 
42
- 1.23.0:
75
+ ## 1.23.0:
43
76
  - Adds eslint configuration and adds eslint to test script.
44
77
  - Sets `sideEffects: false` on package.json to allow module bundlers like webpack tree-shake this module and all the dependencies from client build. Thanks to [Egor Voronov](https://github.com/egorvoronov) for the contribution.
45
78
  - Adds the `tagName` (HTML element name) as a second parameter passed to `textFilter`. Thanks to [Slava](https://github.com/slavaGanzin) for the contribution.
46
79
 
47
- 1.22.1: Increases the patch version of `lodash.mergewith` to enforce an audit fix.
80
+ ## 1.22.1:
81
+ ncreases the patch version of `lodash.mergewith` to enforce an audit fix.
48
82
 
49
- 1.22.0: bumped `htmlparser2` dependency to the 4.x series. This fixes longstanding bugs and should cause no bc breaks for this module, since the only bc breaks upstream are in regard to features we don't expose in this module.
83
+ ## 1.22.0:
84
+ bumped `htmlparser2` dependency to the 4.x series. This fixes longstanding bugs and should cause no bc breaks for this module, since the only bc breaks upstream are in regard to features we don't expose in this module.
50
85
 
51
- 1.21.1: fixed issue with bad `main` setting in package.json that broke 1.21.0.
86
+ ## 1.21.1:
87
+ fixed issue with bad `main` setting in package.json that broke 1.21.0.
52
88
 
53
- 1.21.0: new `disallowedTagsMode` option can be set to `escape` to escape disallowed tags rather than discarding them. Any subtags are handled as usual. If you want to recursively escape them too, you can set `disallowedTagsMode` to `recursiveEscape`. Thanks to Yehonatan Zecharia for this contribution.
89
+ ## 1.21.0:
90
+ new `disallowedTagsMode` option can be set to `escape` to escape disallowed tags rather than discarding them. Any subtags are handled as usual. If you want to recursively escape them too, you can set `disallowedTagsMode` to `recursiveEscape`. Thanks to Yehonatan Zecharia for this contribution.
54
91
 
55
- 1.20.1: Fix failing tests, add CircleCI config
92
+ ## 1.20.1:
93
+ Fix failing tests, add CircleCI config
56
94
 
57
- 1.20.0: reduced size of npm package via the `files` key; we only need to publish what's in `dist`. Thanks to Steven. There should be zero impact on behavior, minor version bump is precautionary.
95
+ ## 1.20.0:
96
+ reduced size of npm package via the `files` key; we only need to publish what's in `dist`. Thanks to Steven. There should be zero impact on behavior, minor version bump is precautionary.
58
97
 
59
- 1.19.3: reverted to `postcss` due to a [reported issue with `css-tree` that might or might not have XSS implications](https://github.com/punkave/sanitize-html/issues/269).
98
+ ## 1.19.3:
99
+ reverted to `postcss` due to a [reported issue with `css-tree` that might or might not have XSS implications](https://github.com/punkave/sanitize-html/issues/269).
60
100
 
61
- 1.19.2:
101
+ ## 1.19.2:
62
102
 
63
103
  * Switched out the heavy `postcss` dependency for the lightweight `css-tree` module. No API changes. Thanks to Justin Braithwaite.
64
104
  * Various doc updates. Thanks to Pulkit Aggarwal and Cody Robertson.
65
105
 
66
- 1.19.1:
106
+ ## 1.19.1:
67
107
 
68
108
  * `"` characters are now entity-escaped only when they appear in attribute values, reducing the verbosity of the resulting markup.
69
109
 
@@ -75,87 +115,105 @@ When `decodeEntities: true` is in effect (the default), this is not a problem be
75
115
 
76
116
  There is currently a commented-out test which verifies one example of the problem when `decodeEntities` is false. However a correct implementation would need to not only pass that simple example but correctly escape all invalid entities, and not escape those that are valid.
77
117
 
78
- 1.19.0:
118
+ ## 1.19.0:
79
119
 
80
120
  * New `allowIframeRelativeUrls` option. It defaults to `true` unless `allowedIframeHostnames` is present, in which case it defaults to false, for backwards compatibility with existing behavior in both cases; however you can now set the option explicitly to allow both certain hostnames and relative URLs. Thanks to Rick Martin.
81
121
 
82
- 1.18.5:
122
+ ## 1.18.5:
83
123
 
84
124
  * Stop double encoding ampersands on HTML entities. Thanks to Will Gibson.
85
125
 
86
- 1.18.4:
126
+ ## 1.18.4:
87
127
 
88
128
  * Removed incorrect `browser` key, restoring frontend build. Thanks to Felix Becker.
89
129
 
90
- 1.18.3:
130
+ ## 1.18.3:
91
131
 
92
132
  * `iframe` is an allowed tag by default, to better facilitate typical use cases and the use of the `allowedIframeHostnames` option.
93
133
  * Documentation improvements.
94
134
  * More browser packaging improvements.
95
135
  * Protocol-relative URLs are properly supported for iframe tags.
96
136
 
97
- 1.18.2:
137
+ ## 1.18.2:
98
138
 
99
139
  * Travis tests passing.
100
140
  * Fixed another case issue — and instituted Travis CI testing so this doesn't happen again. Sorry for the hassle.
101
141
 
102
- 1.18.1:
142
+ ## 1.18.1:
103
143
 
104
144
  * A file was required with incorrect case, breaking the library on case sensitive filesystems such as Linux. Fixed.
105
145
 
106
- 1.18.0:
146
+ ## 1.18.0:
107
147
 
108
148
  * 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.
109
-
110
149
  * 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.
111
-
112
150
  * Fixed a bug in the npm test procedure.
113
151
 
114
- 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.
152
+ ## 1.17.0:
153
+ 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.
115
154
 
116
- 1.16.3: don't throw away the browserified versions before publishing them. `prepare` is not a good place to `make clean`, it runs after `prepublish`.
155
+ ## 1.16.3:
156
+ Don't throw away the browserified versions before publishing them. `prepare` is not a good place to `make clean`, it runs after `prepublish`.
117
157
 
118
- 1.16.2: `sanitize-html` is now compiled with `babel`. An npm `prepublish` script takes care of this at `npm publish` time, so the latest code should always be compiled to operate all the way back to ES5 browsers and earlier versions of Node. Thanks to Ayushya Jaiswal.
158
+ ## 1.16.2:
159
+ `sanitize-html` is now compiled with `babel`. An npm `prepublish` script takes care of this at `npm publish` time, so the latest code should always be compiled to operate all the way back to ES5 browsers and earlier versions of Node. Thanks to Ayushya Jaiswal.
119
160
 
120
161
  Please note that running `sanitize-html` in the browser is usually a security hole. Are you trusting the browser? Anyone could bypass that using the network panel. Sanitization is almost always best done on servers and that is the primary use case for this module.
121
162
 
122
- 1.16.1: changelog formatting only.
163
+ ## 1.16.1:
164
+ changelog formatting only.
123
165
 
124
- 1.16.0: support for sanitizing inline CSS styles, by specifying the allowed attributes and a regular expression for each. Thanks to Cameron Will and Michael Loschiavo.
166
+ ## 1.16.0:
167
+ support for sanitizing inline CSS styles, by specifying the allowed attributes and a regular expression for each. Thanks to Cameron Will and Michael Loschiavo.
125
168
 
126
- 1.15.0: if configured as an allowed attribute (not the default), check for naughty URLs in `srcset` attributes. Thanks to Mike Samuel for the nudge to do this and to Sindre Sorhus for the `srcset` module.
169
+ ## 1.15.0:
170
+ if configured as an allowed attribute (not the default), check for naughty URLs in `srcset` attributes. Thanks to Mike Samuel for the nudge to do this and to Sindre Sorhus for the `srcset` module.
127
171
 
128
- 1.14.3: inadvertent removal of lodash regexp quote dependency in 1.14.2 has been corrected.
172
+ ## 1.14.3:
173
+ inadvertent removal of lodash regexp quote dependency in 1.14.2 has been corrected.
129
174
 
130
- 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.
175
+ ## 1.14.2:
176
+ 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.
131
177
 
132
- 1.14.1: documented `allowProtocolRelative` option. No code changes from 1.14.0, released a few moments ago.
178
+ ## 1.14.1:
179
+ documented `allowProtocolRelative` option. No code changes from 1.14.0, released a few moments ago.
133
180
 
134
- 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.
181
+ ## 1.14.0:
182
+ 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.
135
183
 
136
- 1.13.0: `transformTags` can now add text to an element that initially had none. Thanks to Dushyant Singh.
184
+ ## 1.13.0:
185
+ `transformTags` can now add text to an element that initially had none. Thanks to Dushyant Singh.
137
186
 
138
- 1.12.0: option to build for browser-side use. Thanks to Michael Blum.
187
+ ## 1.12.0:
188
+ option to build for browser-side use. Thanks to Michael Blum.
139
189
 
140
- 1.11.4: fixed crash when `__proto__` is a tag name. Now using a safe check for the existence of properties in all cases. Thanks to Andrew Krasichkov.
190
+ ## 1.11.4:
191
+ fixed crash when `__proto__` is a tag name. Now using a safe check for the existence of properties in all cases. Thanks to Andrew Krasichkov.
141
192
 
142
193
  Fixed XSS attack vector via `textarea` tags (when explicitly allowed). Decided that `script` (obviously) and `style` (due to its own XSS vectors) cannot realistically be afforded any XSS protection if allowed, unless we add a full CSS parser. Thanks again to Andrew Krasichkov.
143
194
 
144
- 1.11.3: bumped `htmlparser2` version to address crashing bug in older version. Thanks to e-jigsaw.
195
+ ## 1.11.3:
196
+ bumped `htmlparser2` version to address crashing bug in older version. Thanks to e-jigsaw.
145
197
 
146
- 1.11.2: fixed README typo that interfered with readability due to markdown issues. No code changes. Thanks to Mikael Korpela. Also improved code block highlighting in README. Thanks to Alex Siman.
198
+ ## 1.11.2:
199
+ fixed README typo that interfered with readability due to markdown issues. No code changes. Thanks to Mikael Korpela. Also improved code block highlighting in README. Thanks to Alex Siman.
147
200
 
148
- 1.11.1: fixed a regression introduced in 1.11.0 which caused the closing tag of the parent of a `textarea` tag to be lost. Thanks to Stefano Sala, who contributed the missing test.
201
+ ## 1.11.1:
202
+ fixed a regression introduced in 1.11.0 which caused the closing tag of the parent of a `textarea` tag to be lost. Thanks to Stefano Sala, who contributed the missing test.
149
203
 
150
- 1.11.0: added the `nonTextTags` option, with tests.
204
+ ## 1.11.0:
205
+ added the `nonTextTags` option, with tests.
151
206
 
152
- 1.10.1: documentation cleanup. No code changes. Thanks to Rex Schrader.
207
+ ## 1.10.1:
208
+ documentation cleanup. No code changes. Thanks to Rex Schrader.
153
209
 
154
- 1.10.0: `allowedAttributes` now allows you to allow attributes for all tags by specifying `*` as the tag name. Thanks to Zdravko Georgiev.
210
+ ## 1.10.0:
211
+ `allowedAttributes` now allows you to allow attributes for all tags by specifying `*` as the tag name. Thanks to Zdravko Georgiev.
155
212
 
156
- 1.9.0: `parser` option allows options to be passed directly to `htmlparser`. Thanks to Danny Scott.
213
+ ## 1.9.0:
214
+ `parser` option allows options to be passed directly to `htmlparser`. Thanks to Danny Scott.
157
215
 
158
- 1.8.0:
216
+ ## 1.8.0:
159
217
 
160
218
  * `transformTags` now accepts the `*` wildcard to transform all tags. Thanks to Jamy Timmermans.
161
219
 
@@ -163,70 +221,99 @@ Fixed XSS attack vector via `textarea` tags (when explicitly allowed). Decided t
163
221
 
164
222
  * Content inside `textarea` is discarded if `textarea` is not allowed. I don't know why it took me this long to see that this is just common sense. Thanks to David Frank.
165
223
 
166
- 1.7.2: removed `array-includes` dependency in favor of `indexOf`, which is a little more verbose but slightly faster and doesn't require a shim. Thanks again to Joseph Dykstra.
167
-
168
- 1.7.1: removed lodash dependency, adding lighter dependencies and polyfills in its place. Thanks to Joseph Dykstra.
224
+ ## 1.7.2:
225
+ removed `array-includes` dependency in favor of `indexOf`, which is a little more verbose but slightly faster and doesn't require a shim. Thanks again to Joseph Dykstra.
169
226
 
170
- 1.7.0: introduced `allowedSchemesByTag` option. Thanks to Cameron Will.
227
+ ## 1.7.1:
228
+ removed lodash dependency, adding lighter dependencies and polyfills in its place. Thanks to Joseph Dykstra.
171
229
 
172
- 1.6.1: the string `'undefined'` (as opposed to `undefined`) is perfectly valid text and shouldn't be expressly converted to the empty string.
230
+ ## 1.7.0:
231
+ introduced `allowedSchemesByTag` option. Thanks to Cameron Will.
173
232
 
174
- 1.6.0: added `textFilter` option. Thanks to Csaba Palfi.
233
+ ## 1.6.1:
234
+ the string `'undefined'` (as opposed to `undefined`) is perfectly valid text and shouldn't be expressly converted to the empty string.
175
235
 
176
- 1.5.3: do not escape special characters inside a script or style element, if they are allowed. This is consistent with the way browsers parse them; nothing closes them except the appropriate closing tag for the entire element. Of course, this only comes into play if you actually choose to allow those tags. Thanks to aletorrado.
236
+ ## 1.6.0:
237
+ added `textFilter` option. Thanks to Csaba Palfi.
177
238
 
178
- 1.5.2: guard checks for allowed attributes correctly to avoid an undefined property error. Thanks to Zeke.
239
+ ## 1.5.3:
240
+ do not escape special characters inside a script or style element, if they are allowed. This is consistent with the way browsers parse them; nothing closes them except the appropriate closing tag for the entire element. Of course, this only comes into play if you actually choose to allow those tags. Thanks to aletorrado.
179
241
 
180
- 1.5.1: updated to htmlparser2 1.8.x. Started using the `decodeEntities` option, which allows us to pass our filter evasion tests without the need to recursively invoke the filter.
242
+ ## 1.5.2:
243
+ guard checks for allowed attributes correctly to avoid an undefined property error. Thanks to Zeke.
181
244
 
182
- 1.5.0: support for `*` wildcards in allowedAttributes. With tests. Thanks to Calvin Montgomery.
245
+ ## 1.5.1:
246
+ updated to htmlparser2 1.8.x. Started using the `decodeEntities` option, which allows us to pass our filter evasion tests without the need to recursively invoke the filter.
183
247
 
184
- 1.4.3: invokes itself recursively until the markup stops changing to guard against [this issue](https://github.com/fb55/htmlparser2/issues/105). Bump to htmlparser2 version 3.7.x.
248
+ ## 1.5.0:
249
+ support for `*` wildcards in allowedAttributes. With tests. Thanks to Calvin Montgomery.
185
250
 
186
- 1.4.1, 1.4.2: more tests.
251
+ ## 1.4.3:
252
+ invokes itself recursively until the markup stops changing to guard against [this issue](https://github.com/fb55/htmlparser2/issues/105). Bump to htmlparser2 version 3.7.x.
187
253
 
188
- 1.4.0: ability to allow all attributes or tags through by setting `allowedAttributes` and/or `allowedTags` to false. Thanks to Anand Thakker.
254
+ ## 1.4.1, 1.4.2:
255
+ more tests.
189
256
 
190
- 1.3.0: `attribs` now available on frames passed to exclusive filter.
257
+ ## 1.4.0:
258
+ ability to allow all attributes or tags through by setting `allowedAttributes` and/or `allowedTags` to false. Thanks to Anand Thakker.
191
259
 
192
- 1.2.3: fixed another possible XSS attack vector; no definitive exploit was found but it looks possible. [See this issue.](https://github.com/punkave/sanitize-html/pull/20) Thanks to Jim O'Brien.
260
+ ## 1.3.0:
261
+ `attribs` now available on frames passed to exclusive filter.
193
262
 
194
- 1.2.2: reject `javascript:` URLs when disguised with an internal comment. This is probably not respected by browsers anyway except when inside an XML data island element, which you almost certainly are not allowing in your `allowedTags`, but we aim to be thorough. Thanks to Jim O'Brien.
263
+ ## 1.2.3:
264
+ fixed another possible XSS attack vector; no definitive exploit was found but it looks possible. [See this issue.](https://github.com/punkave/sanitize-html/pull/20) Thanks to Jim O'Brien.
195
265
 
196
- 1.2.1: fixed crashing bug when presented with bad markup. The bug was in the `exclusiveFilter` mechanism. Unit test added. Thanks to Ilya Kantor for catching it.
266
+ ## 1.2.2:
267
+ reject `javascript:` URLs when disguised with an internal comment. This is probably not respected by browsers anyway except when inside an XML data island element, which you almost certainly are not allowing in your `allowedTags`, but we aim to be thorough. Thanks to Jim O'Brien.
197
268
 
198
- 1.2.0:
269
+ ## 1.2.1:
270
+ fixed crashing bug when presented with bad markup. The bug was in the `exclusiveFilter` mechanism. Unit test added. Thanks to Ilya Kantor for catching it.
199
271
 
272
+ ## 1.2.0:
200
273
  * The `allowedClasses` option now allows you to permit CSS classes in a fine-grained way.
201
274
 
202
275
  * Text passed to your `exclusiveFilter` function now includes the text of child elements, making it more useful for identifying elements that truly lack any inner text.
203
276
 
204
- 1.1.7: use `he` for entity decoding, because it is more actively maintained.
277
+ ## 1.1.7:
278
+ use `he` for entity decoding, because it is more actively maintained.
205
279
 
206
- 1.1.6: `allowedSchemes` option for those who want to permit `data` URLs and such.
280
+ ## 1.1.6:
281
+ `allowedSchemes` option for those who want to permit `data` URLs and such.
207
282
 
208
- 1.1.5: just a packaging thing.
283
+ ## 1.1.5:
284
+ just a packaging thing.
209
285
 
210
- 1.1.4: custom exclusion filter.
286
+ ## 1.1.4:
287
+ custom exclusion filter.
211
288
 
212
- 1.1.3: moved to lodash. 1.1.2 pointed to the wrong version of lodash.
289
+ ## 1.1.3:
290
+ moved to lodash. 1.1.2 pointed to the wrong version of lodash.
213
291
 
214
- 1.1.0: the `transformTags` option was added. Thanks to [kl3ryk](https://github.com/kl3ryk).
292
+ ## 1.1.0:
293
+ the `transformTags` option was added. Thanks to [kl3ryk](https://github.com/kl3ryk).
215
294
 
216
- 1.0.3: fixed several more javascript URL attack vectors after [studying the XSS filter evasion cheat sheet](https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet) to better understand my enemy. Whitespace characters (codes from 0 to 32), which browsers ignore in URLs in certain cases allowing the "javascript" scheme to be snuck in, are now stripped out when checking for naughty URLs. Thanks again to [pinpickle](https://github.com/pinpickle).
295
+ ## 1.0.3:
296
+ fixed several more javascript URL attack vectors after [studying the XSS filter evasion cheat sheet](https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet) to better understand my enemy. Whitespace characters (codes from 0 to 32), which browsers ignore in URLs in certain cases allowing the "javascript" scheme to be snuck in, are now stripped out when checking for naughty URLs. Thanks again to [pinpickle](https://github.com/pinpickle).
217
297
 
218
- 1.0.2: fixed a javascript URL attack vector. naughtyHref must entity-decode URLs and also check for mixed-case scheme names. Thanks to [pinpickle](https://github.com/pinpickle).
298
+ ## 1.0.2:
299
+ fixed a javascript URL attack vector. naughtyHref must entity-decode URLs and also check for mixed-case scheme names. Thanks to [pinpickle](https://github.com/pinpickle).
219
300
 
220
- 1.0.1: Doc tweaks.
301
+ ## 1.0.1:
302
+ Doc tweaks.
221
303
 
222
- 1.0.0: If the style tag is disallowed, then its content should be dumped, so that it doesn't appear as text. We were already doing this for script tags, however in both cases the content is now preserved if the tag is explicitly allowed.
304
+ ## 1.0.0:
305
+ If the style tag is disallowed, then its content should be dumped, so that it doesn't appear as text. We were already doing this for script tags, however in both cases the content is now preserved if the tag is explicitly allowed.
223
306
 
224
307
  We're rocking our tests and have been working great in production for months, so: declared 1.0.0 stable.
225
308
 
226
- 0.1.3: do not double-escape entities in attributes or text. Turns out the "text" provided by htmlparser2 is already escaped.
309
+ ## 0.1.3:
310
+ do not double-escape entities in attributes or text. Turns out the "text" provided by htmlparser2 is already escaped.
227
311
 
228
- 0.1.2: packaging error meant it wouldn't install properly.
312
+ ## 0.1.2:
313
+ packaging error meant it wouldn't install properly.
229
314
 
230
- 0.1.1: discard the text of script tags.
315
+ ## 0.1.1:
316
+ discard the text of script tags.
231
317
 
232
- 0.1.0: initial release.
318
+ ## 0.1.0:
319
+ initial release.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![CircleCI](https://circleci.com/gh/apostrophecms/sanitize-html/tree/main.svg?style=svg)](https://circleci.com/gh/apostrophecms/sanitize-html/tree/main)
4
4
 
5
- <a href="https://apostrophecms.com/"><img src="https://raw.github.com/apostrophecms/sanitize-html/master/logos/logo-box-madefor.png" align="right" /></a>
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
 
@@ -63,11 +63,19 @@ Install module from console:
63
63
  npm install sanitize-html
64
64
  ```
65
65
 
66
- Use it in your node app:
66
+ Import the module:
67
67
 
68
- ```js
68
+ ```bash
69
+ // In ES modules
70
+ import sanitizeHtml from 'sanitize-html';
71
+
72
+ // Or in CommonJS
69
73
  const sanitizeHtml = require('sanitize-html');
74
+ ```
75
+
76
+ Use it in your JavaScript app:
70
77
 
78
+ ```js
71
79
  const dirty = 'some really tacky HTML';
72
80
  const clean = sanitizeHtml(dirty);
73
81
  ```
@@ -90,9 +98,15 @@ Boom!
90
98
  ### Default options
91
99
 
92
100
  ```js
93
- allowedTags: [ 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
94
- 'nl', 'li', 'b', 'i', 'strong', 'em', 'strike', 'abbr', 'code', 'hr', 'br', 'div',
95
- 'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'iframe' ],
101
+ allowedTags: [
102
+ "address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4",
103
+ "h5", "h6", "hgroup", "main", "nav", "section", "blockquote", "dd", "div",
104
+ "dl", "dt", "figcaption", "figure", "hr", "li", "main", "ol", "p", "pre",
105
+ "ul", "a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn",
106
+ "em", "i", "kbd", "mark", "q", "rb", "rp", "rt", "rtc", "ruby", "s", "samp",
107
+ "small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "caption",
108
+ "col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr"
109
+ ],
96
110
  disallowedTagsMode: 'discard',
97
111
  allowedAttributes: {
98
112
  a: [ 'href', 'name', 'target' ],
@@ -163,6 +177,7 @@ allowedAttributes: {
163
177
  values: ['allow-popups', 'allow-same-origin', 'allow-scripts']
164
178
  }
165
179
  ]
180
+ }
166
181
  ```
167
182
 
168
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.
@@ -171,7 +186,7 @@ With `multiple: true`, several allowed values may appear in the same attribute,
171
186
 
172
187
  You can use the `*` wildcard to allow all attributes with a certain prefix:
173
188
 
174
- ```javascript
189
+ ```js
175
190
  allowedAttributes: {
176
191
  a: [ 'href', 'data-*' ]
177
192
  }
@@ -179,7 +194,7 @@ allowedAttributes: {
179
194
 
180
195
  Also you can use the `*` as name for a tag, to allow listed attributes to be valid for any tag:
181
196
 
182
- ```javascript
197
+ ```js
183
198
  allowedAttributes: {
184
199
  '*': [ 'href', 'align', 'alt', 'center', 'bgcolor' ]
185
200
  }
@@ -240,7 +255,7 @@ Some text editing applications generate HTML to allow copying over to a web appl
240
255
 
241
256
  Setting this option to true will instruct sanitize-html to discard all characters outside of `html` tag boundaries -- before `<html>` and after `</html>` tags.
242
257
 
243
- ```javascript
258
+ ```js
244
259
  enforceHtmlBoundary: true
245
260
  ```
246
261
 
@@ -278,13 +293,13 @@ The most advanced usage:
278
293
  const clean = sanitizeHtml(dirty, {
279
294
  transformTags: {
280
295
  'ol': function(tagName, attribs) {
281
- // My own custom magic goes here
282
- return {
283
- tagName: 'ul',
284
- attribs: {
285
- class: 'foo'
286
- }
287
- };
296
+ // My own custom magic goes here
297
+ return {
298
+ tagName: 'ul',
299
+ attribs: {
300
+ class: 'foo'
301
+ }
302
+ };
288
303
  }
289
304
  }
290
305
  });
@@ -316,10 +331,10 @@ You can also add or modify the text contents of a tag:
316
331
  const clean = sanitizeHtml(dirty, {
317
332
  transformTags: {
318
333
  'a': function(tagName, attribs) {
319
- return {
320
- tagName: 'a',
321
- text: 'Some text'
322
- };
334
+ return {
335
+ tagName: 'a',
336
+ text: 'Some text'
337
+ };
323
338
  }
324
339
  }
325
340
  });
@@ -343,12 +358,12 @@ You can provide a filter function to remove unwanted tags. Let's suppose we need
343
358
 
344
359
  We can do that with the following filter:
345
360
 
346
- ```javascript
361
+ ```js
347
362
  sanitizeHtml(
348
363
  '<p>This is <a href="http://www.linux.org"></a><br/>Linux</p>',
349
364
  {
350
365
  exclusiveFilter: function(frame) {
351
- return frame.tag === 'a' && !frame.text.trim();
366
+ return frame.tag === 'a' && !frame.text.trim();
352
367
  }
353
368
  }
354
369
  );
@@ -370,7 +385,7 @@ You can also process all text content with a provided filter function. Let's say
370
385
 
371
386
  We can do that with the following filter:
372
387
 
373
- ```javascript
388
+ ```js
374
389
  sanitizeHtml(
375
390
  '<p>some text...</p>',
376
391
  {
@@ -393,15 +408,15 @@ These arrays will be checked against the html that is passed to the function and
393
408
 
394
409
  Make sure to pass a valid hostname along with the domain you wish to allow, i.e.:
395
410
 
396
- ```javascript
397
- allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com'],
398
- allowedIframeDomains: ['zoom.us']
411
+ ```js
412
+ allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com'],
413
+ allowedIframeDomains: ['zoom.us']
399
414
  ```
400
415
 
401
416
  You may also specify whether or not to allow relative URLs as iframe sources.
402
417
 
403
- ```javascript
404
- allowIframeRelativeUrls: true
418
+ ```js
419
+ allowIframeRelativeUrls: true
405
420
  ```
406
421
 
407
422
  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.
@@ -482,7 +497,7 @@ By default we allow the following URL schemes in cases where `href`, `src`, etc.
482
497
 
483
498
  You can override this if you want to:
484
499
 
485
- ```javascript
500
+ ```js
486
501
  sanitizeHtml(
487
502
  // teeny-tiny valid transparent GIF in a data URL
488
503
  '<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />',
@@ -495,7 +510,7 @@ sanitizeHtml(
495
510
 
496
511
  You can also allow a scheme for a particular tag only:
497
512
 
498
- ```javascript
513
+ ```js
499
514
  allowedSchemes: [ 'http', 'https' ],
500
515
  allowedSchemesByTag: {
501
516
  img: [ 'data' ]
@@ -504,7 +519,7 @@ allowedSchemesByTag: {
504
519
 
505
520
  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:
506
521
 
507
- ```javascript
522
+ ```js
508
523
  allowProtocolRelative: false
509
524
  ```
510
525
 
@@ -519,7 +534,7 @@ The exceptions are:
519
534
  If you wish to replace this list, for instance to discard whatever is found
520
535
  inside a `noscript` tag, use the `nonTextTags` option:
521
536
 
522
- ```javascript
537
+ ```js
523
538
  nonTextTags: [ 'style', 'script', 'textarea', 'option', 'noscript' ]
524
539
  ```
525
540
 
@@ -535,7 +550,7 @@ disabled with the `allowVulnerableTags: true` option.
535
550
 
536
551
  Instead of discarding, or keeping text only, you may enable escaping of the entire content:
537
552
 
538
- ```javascript
553
+ ```js
539
554
  disallowedTagsMode: 'escape'
540
555
  ```
541
556
 
@@ -543,6 +558,16 @@ This will transform `<disallowed>content</disallowed>` to `&lt;disallowed&gt;con
543
558
 
544
559
  Valid values are: `'discard'` (default), `'escape'` (escape the tag) and `'recursiveEscape'` (to escape the tag and all its content).
545
560
 
561
+ ### Restricting deep nesting
562
+
563
+ You can limit the depth of HTML tags in the document with the `nestingLimit` option:
564
+
565
+ ```javascript
566
+ nestingLimit: 6
567
+ ```
568
+
569
+ This will prevent the user from nesting tags more than 6 levels deep. Tags deeper than that are stripped out exactly as if they were disallowed. Note that this means text is preserved in the usual ways where appropriate.
570
+
546
571
  ## About ApostropheCMS
547
572
 
548
573
  `sanitize-html` was created at [P'unk Avenue](https://punkave.com) for use in [ApostropheCMS](https://apostrophecms.com), an open-source content management system built on Node.js. If you like `sanitize-html` you should definitely check out ApostropheCMS.
package/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  const htmlparser = require('htmlparser2');
2
2
  const escapeStringRegexp = require('escape-string-regexp');
3
- const klona = require('klona');
3
+ const { klona } = require('klona');
4
+ const { isPlainObject } = require('is-plain-object');
4
5
  const deepmerge = require('deepmerge');
5
- const isPlainObject = require('is-plain-object');
6
- const srcset = require('srcset');
7
- const postcss = require('postcss');
6
+ const parseSrcset = require('parse-srcset');
7
+ const { parse: postcssParse } = require('postcss');
8
8
  const url = require('url');
9
9
  // Tags that can conceivably represent stand-alone media.
10
10
  const mediaTags = [
@@ -12,7 +12,7 @@ const mediaTags = [
12
12
  'object', 'map', 'iframe', 'embed'
13
13
  ];
14
14
  // Tags that are inherently vulnerable to being used in XSS attacks.
15
- const vulnerableTags = ['script', 'style'];
15
+ const vulnerableTags = [ 'script', 'style' ];
16
16
 
17
17
  function each(obj, cb) {
18
18
  if (obj) {
@@ -47,6 +47,21 @@ function isEmptyObject(obj) {
47
47
  return true;
48
48
  }
49
49
 
50
+ function stringifySrcset(parsedSrcset) {
51
+ return parsedSrcset.map(function(part) {
52
+ if (!part.url) {
53
+ throw new Error('URL missing');
54
+ }
55
+
56
+ return (
57
+ part.url +
58
+ (part.w ? ` ${part.w}w` : '') +
59
+ (part.h ? ` ${part.h}h` : '') +
60
+ (part.d ? ` ${part.d}x` : '')
61
+ );
62
+ }).join(', ');
63
+ }
64
+
50
65
  module.exports = sanitizeHtml;
51
66
 
52
67
  // A valid attribute name.
@@ -108,7 +123,7 @@ function sanitizeHtml(html, options, _recursing) {
108
123
  // vulnerableTags
109
124
  vulnerableTags.forEach(function (tag) {
110
125
  if (
111
- options.allowedTags && options.allowedTags.includes(tag) &&
126
+ options.allowedTags && options.allowedTags.indexOf(tag) > -1 &&
112
127
  !options.allowVulnerableTags
113
128
  ) {
114
129
  console.warn(`\n\n⚠️ Your \`allowedTags\` option includes, \`${tag}\`, which is inherently\nvulnerable to XSS attacks. Please remove it from \`allowedTags\`.\nOr, to disable this warning, add the \`allowVulnerableTags\` option\nand ensure you are accounting for this risk.\n\n`);
@@ -224,7 +239,7 @@ function sanitizeHtml(html, options, _recursing) {
224
239
  }
225
240
  }
226
241
 
227
- if ((options.allowedTags && options.allowedTags.indexOf(name) === -1) || (options.disallowedTagsMode === 'recursiveEscape' && !isEmptyObject(skipMap))) {
242
+ if ((options.allowedTags && options.allowedTags.indexOf(name) === -1) || (options.disallowedTagsMode === 'recursiveEscape' && !isEmptyObject(skipMap)) || (options.nestingLimit != null && depth >= options.nestingLimit)) {
228
243
  skip = true;
229
244
  skipMap[depth] = true;
230
245
  if (options.disallowedTagsMode === 'discard') {
@@ -330,7 +345,7 @@ function sanitizeHtml(html, options, _recursing) {
330
345
  }
331
346
  if (a === 'srcset') {
332
347
  try {
333
- parsed = srcset.parse(value);
348
+ parsed = parseSrcset(value);
334
349
  parsed.forEach(function(value) {
335
350
  if (naughtyHref('srcset', value.url)) {
336
351
  value.evil = true;
@@ -343,7 +358,7 @@ function sanitizeHtml(html, options, _recursing) {
343
358
  delete frame.attribs[a];
344
359
  return;
345
360
  } else {
346
- value = srcset.stringify(filter(parsed, function(v) {
361
+ value = stringifySrcset(filter(parsed, function(v) {
347
362
  return !v.evil;
348
363
  }));
349
364
  frame.attribs[a] = value;
@@ -363,7 +378,7 @@ function sanitizeHtml(html, options, _recursing) {
363
378
  }
364
379
  if (a === 'style') {
365
380
  try {
366
- const abstractSyntaxTree = postcss.parse(name + ' {' + value + '}');
381
+ const abstractSyntaxTree = postcssParse(name + ' {' + value + '}');
367
382
  const filteredAST = filterCss(abstractSyntaxTree, options.allowedStyles);
368
383
 
369
384
  value = stringifyStyleAttributes(filteredAST);
@@ -626,7 +641,7 @@ function sanitizeHtml(html, options, _recursing) {
626
641
  function filterDeclarations(selectedRule) {
627
642
  return function (allowedDeclarationsList, attributeObject) {
628
643
  // If this property is whitelisted...
629
- if (Object.prototype.hasOwnProperty.call(selectedRule, attributeObject.prop)) {
644
+ if (has(selectedRule, attributeObject.prop)) {
630
645
  const matchesRegex = selectedRule[attributeObject.prop].some(function(regularExpression) {
631
646
  return regularExpression.test(attributeObject.value);
632
647
  });
@@ -673,25 +688,25 @@ sanitizeHtml.defaults = {
673
688
  'a', 'abbr', 'b', 'bdi', 'bdo', 'br', 'cite', 'code', 'data', 'dfn',
674
689
  'em', 'i', 'kbd', 'mark', 'q',
675
690
  'rb', 'rp', 'rt', 'rtc', 'ruby',
676
- 's', 'samp', 'small', 'span', 'strong', 'sub', 'time', 'u', 'var', 'wbr',
691
+ 's', 'samp', 'small', 'span', 'strong', 'sub', 'sup', 'time', 'u', 'var', 'wbr',
677
692
  // Table content
678
693
  'caption', 'col', 'colgroup', 'table', 'tbody', 'td', 'tfoot', 'th',
679
694
  'thead', 'tr'
680
695
  ],
681
696
  disallowedTagsMode: 'discard',
682
697
  allowedAttributes: {
683
- a: ['href', 'name', 'target'],
698
+ a: [ 'href', 'name', 'target' ],
684
699
  // We don't currently allow img itself by default, but this
685
700
  // would make sense if we did. You could add srcset here,
686
701
  // and if you do the URL is checked for safety
687
- img: ['src']
702
+ img: [ 'src' ]
688
703
  },
689
704
  // Lots of these won't come up by default because we don't allow them
690
- selfClosing: ['img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta'],
705
+ selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
691
706
  // URL schemes we permit
692
- allowedSchemes: ['http', 'https', 'ftp', 'mailto'],
707
+ allowedSchemes: [ 'http', 'https', 'ftp', 'mailto' ],
693
708
  allowedSchemesByTag: {},
694
- allowedSchemesAppliedToAttributes: ['href', 'src', 'cite'],
709
+ allowedSchemesAppliedToAttributes: [ 'href', 'src', 'cite' ],
695
710
  allowProtocolRelative: true,
696
711
  enforceHtmlBoundary: false
697
712
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanitize-html",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.1.0",
4
4
  "description": "Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis",
5
5
  "sideEffects": false,
6
6
  "main": "index.js",
@@ -25,14 +25,14 @@
25
25
  "deepmerge": "^4.2.2",
26
26
  "escape-string-regexp": "^4.0.0",
27
27
  "htmlparser2": "^4.1.0",
28
- "is-plain-object": "^4.1.1",
29
- "klona": "^1.1.2",
30
- "postcss": "^7.0.27",
31
- "srcset": "^3.0.0"
28
+ "is-plain-object": "^5.0.0",
29
+ "klona": "^2.0.3",
30
+ "parse-srcset": "^1.0.2",
31
+ "postcss": "^8.0.2"
32
32
  },
33
33
  "devDependencies": {
34
34
  "eslint": "^7.3.1",
35
- "eslint-config-apostrophe": "^3.2.0",
35
+ "eslint-config-apostrophe": "^3.4.0",
36
36
  "eslint-config-standard": "^14.1.1",
37
37
  "eslint-plugin-import": "^2.21.2",
38
38
  "eslint-plugin-node": "^11.1.0",
@@ -41,4 +41,4 @@
41
41
  "mocha": "^5.2.0",
42
42
  "sinon": "^9.0.2"
43
43
  }
44
- }
44
+ }