dompurify 3.0.1 → 3.2.7
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/LICENSE +202 -12
- package/README.md +132 -90
- package/dist/purify.cjs.d.ts +442 -0
- package/dist/purify.cjs.js +601 -1004
- package/dist/purify.cjs.js.map +1 -1
- package/dist/purify.es.d.mts +439 -0
- package/dist/purify.es.mjs +1357 -0
- package/dist/purify.es.mjs.map +1 -0
- package/dist/purify.js +601 -1004
- package/dist/purify.js.map +1 -1
- package/dist/purify.min.js +2 -2
- package/dist/purify.min.js.map +1 -1
- package/package.json +65 -22
- package/dist/purify.es.js +0 -1760
- package/dist/purify.es.js.map +0 -1
package/LICENSE
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
DOMPurify
|
|
2
|
-
Copyright
|
|
2
|
+
Copyright 2025 Dr.-Ing. Mario Heiderich, Cure53
|
|
3
3
|
|
|
4
4
|
DOMPurify is free software; you can redistribute it and/or modify it under the
|
|
5
5
|
terms of either:
|
|
@@ -9,17 +9,207 @@ b) the Mozilla Public License Version 2.0
|
|
|
9
9
|
|
|
10
10
|
-----------------------------------------------------------------------------
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
Apache License
|
|
13
|
+
Version 2.0, January 2004
|
|
14
|
+
http://www.apache.org/licenses/
|
|
15
|
+
|
|
16
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
17
|
+
|
|
18
|
+
1. Definitions.
|
|
19
|
+
|
|
20
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
21
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
22
|
+
|
|
23
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
24
|
+
the copyright owner that is granting the License.
|
|
25
|
+
|
|
26
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
27
|
+
other entities that control, are controlled by, or are under common
|
|
28
|
+
control with that entity. For the purposes of this definition,
|
|
29
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
30
|
+
direction or management of such entity, whether by contract or
|
|
31
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
32
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
33
|
+
|
|
34
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
35
|
+
exercising permissions granted by this License.
|
|
36
|
+
|
|
37
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
38
|
+
including but not limited to software source code, documentation
|
|
39
|
+
source, and configuration files.
|
|
40
|
+
|
|
41
|
+
"Object" form shall mean any form resulting from mechanical
|
|
42
|
+
transformation or translation of a Source form, including but
|
|
43
|
+
not limited to compiled object code, generated documentation,
|
|
44
|
+
and conversions to other media types.
|
|
45
|
+
|
|
46
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
47
|
+
Object form, made available under the License, as indicated by a
|
|
48
|
+
copyright notice that is included in or attached to the work
|
|
49
|
+
(an example is provided in the Appendix below).
|
|
50
|
+
|
|
51
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
52
|
+
form, that is based on (or derived from) the Work and for which the
|
|
53
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
54
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
55
|
+
of this License, Derivative Works shall not include works that remain
|
|
56
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
57
|
+
the Work and Derivative Works thereof.
|
|
58
|
+
|
|
59
|
+
"Contribution" shall mean any work of authorship, including
|
|
60
|
+
the original version of the Work and any modifications or additions
|
|
61
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
62
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
63
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
64
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
65
|
+
means any form of electronic, verbal, or written communication sent
|
|
66
|
+
to the Licensor or its representatives, including but not limited to
|
|
67
|
+
communication on electronic mailing lists, source code control systems,
|
|
68
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
69
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
70
|
+
excluding communication that is conspicuously marked or otherwise
|
|
71
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
72
|
+
|
|
73
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
74
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
75
|
+
subsequently incorporated within the Work.
|
|
76
|
+
|
|
77
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
78
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
79
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
80
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
81
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
82
|
+
Work and such Derivative Works in Source or Object form.
|
|
83
|
+
|
|
84
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
85
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
86
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
87
|
+
(except as stated in this section) patent license to make, have made,
|
|
88
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
89
|
+
where such license applies only to those patent claims licensable
|
|
90
|
+
by such Contributor that are necessarily infringed by their
|
|
91
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
92
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
93
|
+
institute patent litigation against any entity (including a
|
|
94
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
95
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
96
|
+
or contributory patent infringement, then any patent licenses
|
|
97
|
+
granted to You under this License for that Work shall terminate
|
|
98
|
+
as of the date such litigation is filed.
|
|
99
|
+
|
|
100
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
101
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
102
|
+
modifications, and in Source or Object form, provided that You
|
|
103
|
+
meet the following conditions:
|
|
104
|
+
|
|
105
|
+
(a) You must give any other recipients of the Work or
|
|
106
|
+
Derivative Works a copy of this License; and
|
|
107
|
+
|
|
108
|
+
(b) You must cause any modified files to carry prominent notices
|
|
109
|
+
stating that You changed the files; and
|
|
110
|
+
|
|
111
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
112
|
+
that You distribute, all copyright, patent, trademark, and
|
|
113
|
+
attribution notices from the Source form of the Work,
|
|
114
|
+
excluding those notices that do not pertain to any part of
|
|
115
|
+
the Derivative Works; and
|
|
116
|
+
|
|
117
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
118
|
+
distribution, then any Derivative Works that You distribute must
|
|
119
|
+
include a readable copy of the attribution notices contained
|
|
120
|
+
within such NOTICE file, excluding those notices that do not
|
|
121
|
+
pertain to any part of the Derivative Works, in at least one
|
|
122
|
+
of the following places: within a NOTICE text file distributed
|
|
123
|
+
as part of the Derivative Works; within the Source form or
|
|
124
|
+
documentation, if provided along with the Derivative Works; or,
|
|
125
|
+
within a display generated by the Derivative Works, if and
|
|
126
|
+
wherever such third-party notices normally appear. The contents
|
|
127
|
+
of the NOTICE file are for informational purposes only and
|
|
128
|
+
do not modify the License. You may add Your own attribution
|
|
129
|
+
notices within Derivative Works that You distribute, alongside
|
|
130
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
131
|
+
that such additional attribution notices cannot be construed
|
|
132
|
+
as modifying the License.
|
|
133
|
+
|
|
134
|
+
You may add Your own copyright statement to Your modifications and
|
|
135
|
+
may provide additional or different license terms and conditions
|
|
136
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
137
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
138
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
139
|
+
the conditions stated in this License.
|
|
140
|
+
|
|
141
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
142
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
143
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
144
|
+
this License, without any additional terms or conditions.
|
|
145
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
146
|
+
the terms of any separate license agreement you may have executed
|
|
147
|
+
with Licensor regarding such Contributions.
|
|
148
|
+
|
|
149
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
150
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
151
|
+
except as required for reasonable and customary use in describing the
|
|
152
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
153
|
+
|
|
154
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
155
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
156
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
157
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
158
|
+
implied, including, without limitation, any warranties or conditions
|
|
159
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
160
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
161
|
+
appropriateness of using or redistributing the Work and assume any
|
|
162
|
+
risks associated with Your exercise of permissions under this License.
|
|
163
|
+
|
|
164
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
165
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
166
|
+
unless required by applicable law (such as deliberate and grossly
|
|
167
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
168
|
+
liable to You for damages, including any direct, indirect, special,
|
|
169
|
+
incidental, or consequential damages of any character arising as a
|
|
170
|
+
result of this License or out of the use or inability to use the
|
|
171
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
172
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
173
|
+
other commercial damages or losses), even if such Contributor
|
|
174
|
+
has been advised of the possibility of such damages.
|
|
175
|
+
|
|
176
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
177
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
178
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
179
|
+
or other liability obligations and/or rights consistent with this
|
|
180
|
+
License. However, in accepting such obligations, You may act only
|
|
181
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
182
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
183
|
+
defend, and hold each Contributor harmless for any liability
|
|
184
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
185
|
+
of your accepting any such warranty or additional liability.
|
|
186
|
+
|
|
187
|
+
END OF TERMS AND CONDITIONS
|
|
188
|
+
|
|
189
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
190
|
+
|
|
191
|
+
To apply the Apache License to your work, attach the following
|
|
192
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
193
|
+
replaced with your own identifying information. (Don't include
|
|
194
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
195
|
+
comment syntax for the file format. We also recommend that a
|
|
196
|
+
file or class name and description of purpose be included on the
|
|
197
|
+
same "printed page" as the copyright notice for easier
|
|
198
|
+
identification within third-party archives.
|
|
199
|
+
|
|
200
|
+
Copyright [yyyy] [name of copyright owner]
|
|
201
|
+
|
|
202
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
203
|
+
you may not use this file except in compliance with the License.
|
|
204
|
+
You may obtain a copy of the License at
|
|
205
|
+
|
|
206
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
207
|
+
|
|
208
|
+
Unless required by applicable law or agreed to in writing, software
|
|
209
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
210
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
211
|
+
See the License for the specific language governing permissions and
|
|
212
|
+
limitations under the License.
|
|
23
213
|
|
|
24
214
|
-----------------------------------------------------------------------------
|
|
25
215
|
Mozilla Public License, version 2.0
|
package/README.md
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
# DOMPurify
|
|
2
2
|
|
|
3
|
-
[](https://nodei.co/npm/dompurify/)
|
|
3
|
+
[](http://badge.fury.io/js/dompurify)  [](https://www.npmjs.com/package/dompurify)  [](https://github.com/cure53/DOMPurify/network/dependents) [](https://cloudback.it)
|
|
6
4
|
|
|
7
5
|
DOMPurify is a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG.
|
|
8
6
|
|
|
9
|
-
It's also very simple to use and get started with. DOMPurify was [started in February 2014](https://github.com/cure53/DOMPurify/commit/a630922616927373485e0e787ab19e73e3691b2b) and, meanwhile, has reached version
|
|
7
|
+
It's also very simple to use and get started with. DOMPurify was [started in February 2014](https://github.com/cure53/DOMPurify/commit/a630922616927373485e0e787ab19e73e3691b2b) and, meanwhile, has reached version **v3.2.7**.
|
|
10
8
|
|
|
11
9
|
DOMPurify is written in JavaScript and works in all modern browsers (Safari (10+), Opera (15+), Edge, Firefox and Chrome - as well as almost anything else using Blink, Gecko or WebKit). It doesn't break on MSIE or other legacy browsers. It simply does nothing.
|
|
12
10
|
|
|
13
|
-
**Note that [DOMPurify v2.
|
|
11
|
+
**Note that [DOMPurify v2.5.8](https://github.com/cure53/DOMPurify/releases/tag/2.5.8) is the latest version supporting MSIE. For important security updates compatible with MSIE, please use the [2.x branch](https://github.com/cure53/DOMPurify/tree/2.x).**
|
|
14
12
|
|
|
15
|
-
Our automated tests cover [
|
|
13
|
+
Our automated tests cover [28 different browsers](https://github.com/cure53/DOMPurify/blob/main/test/karma.custom-launchers.config.js#L5) right now, more to come. We also cover Node.js v18.x, v19.x, v20.x, v21.x, v22.x and v23.x, running DOMPurify on [jsdom](https://github.com/jsdom/jsdom). Older Node versions are known to work as well, but hey... no guarantees.
|
|
16
14
|
|
|
17
15
|
DOMPurify is written by security people who have vast background in web attacks and XSS. Fear not. For more details please also read about our [Security Goals & Threat Model](https://github.com/cure53/DOMPurify/wiki/Security-Goals-&-Threat-Model). Please, read it. Like, really.
|
|
18
16
|
|
|
@@ -24,10 +22,10 @@ DOMPurify sanitizes HTML and prevents XSS attacks. You can feed DOMPurify with s
|
|
|
24
22
|
|
|
25
23
|
It's easy. Just include DOMPurify on your website.
|
|
26
24
|
|
|
27
|
-
### Using the unminified
|
|
25
|
+
### Using the unminified version (source-map available)
|
|
28
26
|
|
|
29
27
|
```html
|
|
30
|
-
<script type="text/javascript" src="
|
|
28
|
+
<script type="text/javascript" src="dist/purify.js"></script>
|
|
31
29
|
```
|
|
32
30
|
|
|
33
31
|
### Using the minified and tested production version (source-map available)
|
|
@@ -39,28 +37,24 @@ It's easy. Just include DOMPurify on your website.
|
|
|
39
37
|
Afterwards you can sanitize strings by executing the following code:
|
|
40
38
|
|
|
41
39
|
```js
|
|
42
|
-
|
|
40
|
+
const clean = DOMPurify.sanitize(dirty);
|
|
43
41
|
```
|
|
44
42
|
|
|
45
43
|
Or maybe this, if you love working with Angular or alike:
|
|
46
44
|
|
|
47
45
|
```js
|
|
48
|
-
import
|
|
46
|
+
import DOMPurify from 'dompurify';
|
|
49
47
|
|
|
50
|
-
|
|
48
|
+
const clean = DOMPurify.sanitize('<b>hello there</b>');
|
|
51
49
|
```
|
|
52
50
|
|
|
53
51
|
The resulting HTML can be written into a DOM element using `innerHTML` or the DOM using `document.write()`. That is fully up to you.
|
|
54
52
|
Note that by default, we permit HTML, SVG **and** MathML. If you only need HTML, which might be a very common use-case, you can easily set that up as well:
|
|
55
53
|
|
|
56
54
|
```js
|
|
57
|
-
|
|
55
|
+
const clean = DOMPurify.sanitize(dirty, { USE_PROFILES: { html: true } });
|
|
58
56
|
```
|
|
59
57
|
|
|
60
|
-
### Where are the TypeScript type definitions?
|
|
61
|
-
|
|
62
|
-
They can be found here: [@types/dompurify](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/dompurify)
|
|
63
|
-
|
|
64
58
|
### Is there any foot-gun potential?
|
|
65
59
|
|
|
66
60
|
Well, please note, if you _first_ sanitize HTML and then modify it _afterwards_, you might easily **void the effects of sanitization**. If you feed the sanitized markup to another library _after_ sanitization, please be certain that the library doesn't mess around with the HTML on its own.
|
|
@@ -77,6 +71,8 @@ Running DOMPurify on the server requires a DOM to be present, which is probably
|
|
|
77
71
|
|
|
78
72
|
Why? Because older versions of _jsdom_ are known to be buggy in ways that result in XSS _even if_ DOMPurify does everything 100% correctly. There are **known attack vectors** in, e.g. _jsdom v19.0.0_ that are fixed in _jsdom v20.0.0_ - and we really recommend to keep _jsdom_ up to date because of that.
|
|
79
73
|
|
|
74
|
+
Please also be aware that tools like [happy-dom](https://github.com/capricorn86/happy-dom) exist but **are not considered safe** at this point. Combining DOMPurify with _happy-dom_ is currently not recommended and will likely lead to XSS.
|
|
75
|
+
|
|
80
76
|
Other than that, you are fine to use DOMPurify on the server. Probably. This really depends on _jsdom_ or whatever DOM you utilize server-side. If you can live with that, this is how you get it to work:
|
|
81
77
|
|
|
82
78
|
```bash
|
|
@@ -156,76 +152,89 @@ In version 2.0.0, a config flag was added to control DOMPurify's behavior regard
|
|
|
156
152
|
|
|
157
153
|
When `DOMPurify.sanitize` is used in an environment where the Trusted Types API is available and `RETURN_TRUSTED_TYPE` is set to `true`, it tries to return a `TrustedHTML` value instead of a string (the behavior for `RETURN_DOM` and `RETURN_DOM_FRAGMENT` config options does not change).
|
|
158
154
|
|
|
155
|
+
Note that in order to create a policy in `trustedTypes` using DOMPurify, `RETURN_TRUSTED_TYPE: false` is required, as `createHTML` expects a normal string, not `TrustedHTML`. The example below shows this.
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
window.trustedTypes!.createPolicy('default', {
|
|
159
|
+
createHTML: (to_escape) =>
|
|
160
|
+
DOMPurify.sanitize(to_escape, { RETURN_TRUSTED_TYPE: false }),
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
159
164
|
## Can I configure DOMPurify?
|
|
160
165
|
|
|
161
166
|
Yes. The included default configuration values are pretty good already - but you can of course override them. Check out the [`/demos`](https://github.com/cure53/DOMPurify/tree/main/demos) folder to see a bunch of examples on how you can [customize DOMPurify](https://github.com/cure53/DOMPurify/tree/main/demos#what-is-this).
|
|
162
167
|
|
|
168
|
+
### General settings
|
|
163
169
|
```js
|
|
164
|
-
/**
|
|
165
|
-
* General settings
|
|
166
|
-
*/
|
|
167
|
-
|
|
168
170
|
// strip {{ ... }}, ${ ... } and <% ... %> to make output safe for template systems
|
|
169
171
|
// be careful please, this mode is not recommended for production usage.
|
|
170
172
|
// allowing template parsing in user-controlled HTML is not advised at all.
|
|
171
173
|
// only use this mode if there is really no alternative.
|
|
172
|
-
|
|
174
|
+
const clean = DOMPurify.sanitize(dirty, {SAFE_FOR_TEMPLATES: true});
|
|
175
|
+
|
|
173
176
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
+
// change how e.g. comments containing risky HTML characters are treated.
|
|
178
|
+
// be very careful, this setting should only be set to `false` if you really only handle
|
|
179
|
+
// HTML and nothing else, no SVG, MathML or the like.
|
|
180
|
+
// Otherwise, changing from `true` to `false` will lead to XSS in this or some other way.
|
|
181
|
+
const clean = DOMPurify.sanitize(dirty, {SAFE_FOR_XML: false});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Control our allow-lists and block-lists
|
|
185
|
+
```js
|
|
177
186
|
// allow only <b> elements, very strict
|
|
178
|
-
|
|
187
|
+
const clean = DOMPurify.sanitize(dirty, {ALLOWED_TAGS: ['b']});
|
|
179
188
|
|
|
180
189
|
// allow only <b> and <q> with style attributes
|
|
181
|
-
|
|
190
|
+
const clean = DOMPurify.sanitize(dirty, {ALLOWED_TAGS: ['b', 'q'], ALLOWED_ATTR: ['style']});
|
|
182
191
|
|
|
183
192
|
// allow all safe HTML elements but neither SVG nor MathML
|
|
184
193
|
// note that the USE_PROFILES setting will override the ALLOWED_TAGS setting
|
|
185
194
|
// so don't use them together
|
|
186
|
-
|
|
195
|
+
const clean = DOMPurify.sanitize(dirty, {USE_PROFILES: {html: true}});
|
|
187
196
|
|
|
188
197
|
// allow all safe SVG elements and SVG Filters, no HTML or MathML
|
|
189
|
-
|
|
198
|
+
const clean = DOMPurify.sanitize(dirty, {USE_PROFILES: {svg: true, svgFilters: true}});
|
|
190
199
|
|
|
191
200
|
// allow all safe MathML elements and SVG, but no SVG Filters
|
|
192
|
-
|
|
201
|
+
const clean = DOMPurify.sanitize(dirty, {USE_PROFILES: {mathMl: true, svg: true}});
|
|
193
202
|
|
|
194
203
|
// change the default namespace from HTML to something different
|
|
195
|
-
|
|
204
|
+
const clean = DOMPurify.sanitize(dirty, {NAMESPACE: 'http://www.w3.org/2000/svg'});
|
|
196
205
|
|
|
197
206
|
// leave all safe HTML as it is and add <style> elements to block-list
|
|
198
|
-
|
|
207
|
+
const clean = DOMPurify.sanitize(dirty, {FORBID_TAGS: ['style']});
|
|
199
208
|
|
|
200
209
|
// leave all safe HTML as it is and add style attributes to block-list
|
|
201
|
-
|
|
210
|
+
const clean = DOMPurify.sanitize(dirty, {FORBID_ATTR: ['style']});
|
|
202
211
|
|
|
203
212
|
// extend the existing array of allowed tags and add <my-tag> to allow-list
|
|
204
|
-
|
|
213
|
+
const clean = DOMPurify.sanitize(dirty, {ADD_TAGS: ['my-tag']});
|
|
205
214
|
|
|
206
215
|
// extend the existing array of allowed attributes and add my-attr to allow-list
|
|
207
|
-
|
|
216
|
+
const clean = DOMPurify.sanitize(dirty, {ADD_ATTR: ['my-attr']});
|
|
208
217
|
|
|
209
218
|
// prohibit ARIA attributes, leave other safe HTML as is (default is true)
|
|
210
|
-
|
|
219
|
+
const clean = DOMPurify.sanitize(dirty, {ALLOW_ARIA_ATTR: false});
|
|
211
220
|
|
|
212
221
|
// prohibit HTML5 data attributes, leave other safe HTML as is (default is true)
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Control behavior relating to Custom Elements
|
|
217
|
-
*/
|
|
222
|
+
const clean = DOMPurify.sanitize(dirty, {ALLOW_DATA_ATTR: false});
|
|
223
|
+
```
|
|
218
224
|
|
|
225
|
+
### Control behavior relating to Custom Elements
|
|
226
|
+
```js
|
|
219
227
|
// DOMPurify allows to define rules for Custom Elements. When using the CUSTOM_ELEMENT_HANDLING
|
|
220
228
|
// literal, it is possible to define exactly what elements you wish to allow (by default, none are allowed).
|
|
221
229
|
//
|
|
222
230
|
// The same goes for their attributes. By default, the built-in or configured allow.list is used.
|
|
223
231
|
//
|
|
224
232
|
// You can use a RegExp literal to specify what is allowed or a predicate, examples for both can be seen below.
|
|
233
|
+
// When using a predicate function for attributeNameCheck, it can optionally receive the tagName as a second parameter
|
|
234
|
+
// for more granular control over which attributes are allowed for specific elements.
|
|
225
235
|
// The default values are very restrictive to prevent accidental XSS bypasses. Handle with great care!
|
|
226
236
|
|
|
227
|
-
|
|
228
|
-
var clean = DOMPurify.sanitize(
|
|
237
|
+
const clean = DOMPurify.sanitize(
|
|
229
238
|
'<foo-bar baz="foobar" forbidden="true"></foo-bar><div is="foo-baz"></div>',
|
|
230
239
|
{
|
|
231
240
|
CUSTOM_ELEMENT_HANDLING: {
|
|
@@ -236,7 +245,7 @@ var clean = DOMPurify.sanitize(
|
|
|
236
245
|
}
|
|
237
246
|
); // <div is=""></div>
|
|
238
247
|
|
|
239
|
-
|
|
248
|
+
const clean = DOMPurify.sanitize(
|
|
240
249
|
'<foo-bar baz="foobar" forbidden="true"></foo-bar><div is="foo-baz"></div>',
|
|
241
250
|
{
|
|
242
251
|
CUSTOM_ELEMENT_HANDLING: {
|
|
@@ -245,9 +254,9 @@ var clean = DOMPurify.sanitize(
|
|
|
245
254
|
allowCustomizedBuiltInElements: true, // customized built-ins are allowed
|
|
246
255
|
},
|
|
247
256
|
}
|
|
248
|
-
); // <foo-bar baz="foobar"></foo-bar><div is=""></div>
|
|
257
|
+
); // <foo-bar baz="foobar"></foo-bar><div is="foo-baz"></div>
|
|
249
258
|
|
|
250
|
-
|
|
259
|
+
const clean = DOMPurify.sanitize(
|
|
251
260
|
'<foo-bar baz="foobar" forbidden="true"></foo-bar><div is="foo-baz"></div>',
|
|
252
261
|
{
|
|
253
262
|
CUSTOM_ELEMENT_HANDLING: {
|
|
@@ -258,72 +267,99 @@ var clean = DOMPurify.sanitize(
|
|
|
258
267
|
}
|
|
259
268
|
); // <foo-bar baz="foobar"></foo-bar><div is="foo-baz"></div>
|
|
260
269
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
270
|
+
// Example with attributeNameCheck receiving tagName as a second parameter
|
|
271
|
+
const clean = DOMPurify.sanitize(
|
|
272
|
+
'<element-one attribute-one="1" attribute-two="2"></element-one><element-two attribute-one="1" attribute-two="2"></element-two>',
|
|
273
|
+
{
|
|
274
|
+
CUSTOM_ELEMENT_HANDLING: {
|
|
275
|
+
tagNameCheck: (tagName) => tagName.match(/^element-(one|two)$/),
|
|
276
|
+
attributeNameCheck: (attr, tagName) => {
|
|
277
|
+
if (tagName === 'element-one') {
|
|
278
|
+
return ['attribute-one'].includes(attr);
|
|
279
|
+
} else if (tagName === 'element-two') {
|
|
280
|
+
return ['attribute-two'].includes(attr);
|
|
281
|
+
} else {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
allowCustomizedBuiltInElements: false,
|
|
286
|
+
},
|
|
287
|
+
}
|
|
288
|
+
); // <element-one attribute-one="1"></element-one><element-two attribute-two="2"></element-two>
|
|
289
|
+
```
|
|
290
|
+
### Control behavior relating to URI values
|
|
291
|
+
```js
|
|
264
292
|
// extend the existing array of elements that can use Data URIs
|
|
265
|
-
|
|
293
|
+
const clean = DOMPurify.sanitize(dirty, {ADD_DATA_URI_TAGS: ['a', 'area']});
|
|
266
294
|
|
|
267
295
|
// extend the existing array of elements that are safe for URI-like values (be careful, XSS risk)
|
|
268
|
-
|
|
296
|
+
const clean = DOMPurify.sanitize(dirty, {ADD_URI_SAFE_ATTR: ['my-attr']});
|
|
269
297
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
298
|
+
```
|
|
299
|
+
### Control permitted attribute values
|
|
300
|
+
```js
|
|
273
301
|
// allow external protocol handlers in URL attributes (default is false, be careful, XSS risk)
|
|
274
|
-
// by default only http, https, ftp, ftps, tel, mailto, callto, cid and xmpp are allowed.
|
|
275
|
-
|
|
302
|
+
// by default only http, https, ftp, ftps, tel, mailto, callto, sms, cid and xmpp are allowed.
|
|
303
|
+
const clean = DOMPurify.sanitize(dirty, {ALLOW_UNKNOWN_PROTOCOLS: true});
|
|
276
304
|
|
|
277
305
|
// allow specific protocols handlers in URL attributes via regex (default is false, be careful, XSS risk)
|
|
278
|
-
// by default only http, https, ftp, ftps, tel, mailto, callto, cid and xmpp are allowed.
|
|
279
|
-
// Default RegExp: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
|
|
280
|
-
|
|
306
|
+
// by default only http, https, ftp, ftps, tel, mailto, callto, sms, cid and xmpp are allowed.
|
|
307
|
+
// Default RegExp: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
|
|
308
|
+
const clean = DOMPurify.sanitize(dirty, {ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|xxx):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i});
|
|
281
309
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
310
|
+
```
|
|
311
|
+
### Influence the return-type
|
|
312
|
+
```js
|
|
285
313
|
// return a DOM HTMLBodyElement instead of an HTML string (default is false)
|
|
286
|
-
|
|
314
|
+
const clean = DOMPurify.sanitize(dirty, {RETURN_DOM: true});
|
|
287
315
|
|
|
288
316
|
// return a DOM DocumentFragment instead of an HTML string (default is false)
|
|
289
|
-
|
|
317
|
+
const clean = DOMPurify.sanitize(dirty, {RETURN_DOM_FRAGMENT: true});
|
|
290
318
|
|
|
291
319
|
// use the RETURN_TRUSTED_TYPE flag to turn on Trusted Types support if available
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
320
|
+
const clean = DOMPurify.sanitize(dirty, {RETURN_TRUSTED_TYPE: true}); // will return a TrustedHTML object instead of a string if possible
|
|
321
|
+
|
|
322
|
+
// use a provided Trusted Types policy
|
|
323
|
+
const clean = DOMPurify.sanitize(dirty, {
|
|
324
|
+
// supplied policy must define createHTML and createScriptURL
|
|
325
|
+
TRUSTED_TYPES_POLICY: trustedTypes.createPolicy({
|
|
326
|
+
createHTML(s) { return s},
|
|
327
|
+
createScriptURL(s) { return s},
|
|
328
|
+
})
|
|
329
|
+
});
|
|
330
|
+
```
|
|
331
|
+
### Influence how we sanitize
|
|
332
|
+
```js
|
|
297
333
|
// return entire document including <html> tags (default is false)
|
|
298
|
-
|
|
334
|
+
const clean = DOMPurify.sanitize(dirty, {WHOLE_DOCUMENT: true});
|
|
299
335
|
|
|
300
336
|
// disable DOM Clobbering protection on output (default is true, handle with care, minor XSS risks here)
|
|
301
|
-
|
|
337
|
+
const clean = DOMPurify.sanitize(dirty, {SANITIZE_DOM: false});
|
|
302
338
|
|
|
303
339
|
// enforce strict DOM Clobbering protection via namespace isolation (default is false)
|
|
304
|
-
// when enabled, isolates the namespace of named properties (i.e., `id` and `name` attributes)
|
|
305
|
-
// from JS variables by prefixing them with the string `user-content-`
|
|
306
|
-
|
|
340
|
+
// when enabled, isolates the namespace of named properties (i.e., `id` and `name` attributes)
|
|
341
|
+
// from JS variables by prefixing them with the string `user-content-`
|
|
342
|
+
const clean = DOMPurify.sanitize(dirty, {SANITIZE_NAMED_PROPS: true});
|
|
307
343
|
|
|
308
344
|
// keep an element's content when the element is removed (default is true)
|
|
309
|
-
|
|
345
|
+
const clean = DOMPurify.sanitize(dirty, {KEEP_CONTENT: false});
|
|
310
346
|
|
|
311
347
|
// glue elements like style, script or others to document.body and prevent unintuitive browser behavior in several edge-cases (default is false)
|
|
312
|
-
|
|
348
|
+
const clean = DOMPurify.sanitize(dirty, {FORCE_BODY: true});
|
|
313
349
|
|
|
314
350
|
// remove all <a> elements under <p> elements that are removed
|
|
315
|
-
|
|
351
|
+
const clean = DOMPurify.sanitize(dirty, {FORBID_CONTENTS: ['a'], FORBID_TAGS: ['p']});
|
|
316
352
|
|
|
317
353
|
// change the parser type so sanitized data is treated as XML and not as HTML, which is the default
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
*/
|
|
354
|
+
const clean = DOMPurify.sanitize(dirty, {PARSER_MEDIA_TYPE: 'application/xhtml+xml'});
|
|
355
|
+
```
|
|
356
|
+
### Influence where we sanitize
|
|
357
|
+
```js
|
|
323
358
|
// use the IN_PLACE mode to sanitize a node "in place", which is much faster depending on how you use DOMPurify
|
|
324
|
-
|
|
359
|
+
const dirty = document.createElement('a');
|
|
325
360
|
dirty.setAttribute('href', 'javascript:alert(1)');
|
|
326
|
-
|
|
361
|
+
|
|
362
|
+
const clean = DOMPurify.sanitize(dirty, {IN_PLACE: true}); // see https://github.com/cure53/DOMPurify/issues/288 for more info
|
|
327
363
|
```
|
|
328
364
|
|
|
329
365
|
There is even [more examples here](https://github.com/cure53/DOMPurify/tree/main/demos#what-is-this), showing how you can run, customize and configure DOMPurify to fit your needs.
|
|
@@ -352,20 +388,26 @@ _Example_:
|
|
|
352
388
|
|
|
353
389
|
```js
|
|
354
390
|
DOMPurify.addHook(
|
|
355
|
-
'
|
|
391
|
+
'uponSanitizeAttribute',
|
|
356
392
|
function (currentNode, hookEvent, config) {
|
|
357
|
-
// Do something with the current node
|
|
358
|
-
// You can also mutate hookEvent (i.e. set hookEvent.forceKeepAttr = true)
|
|
359
|
-
|
|
393
|
+
// Do something with the current node
|
|
394
|
+
// You can also mutate hookEvent for current node (i.e. set hookEvent.forceKeepAttr = true)
|
|
395
|
+
// For other than 'uponSanitizeAttribute' hook types hookEvent equals to null
|
|
360
396
|
}
|
|
361
397
|
);
|
|
362
398
|
```
|
|
363
399
|
|
|
400
|
+
## Removed Configuration
|
|
401
|
+
|
|
402
|
+
| Option | Since | Note |
|
|
403
|
+
|-----------------|-------|--------------------------|
|
|
404
|
+
| SAFE_FOR_JQUERY | 2.1.0 | No replacement required. |
|
|
405
|
+
|
|
364
406
|
## Continuous Integration
|
|
365
407
|
|
|
366
408
|
We are currently using Github Actions in combination with BrowserStack. This gives us the possibility to confirm for each and every commit that all is going according to plan in all supported browsers. Check out the build logs here: https://github.com/cure53/DOMPurify/actions
|
|
367
409
|
|
|
368
|
-
You can further run local tests by executing `npm test`.
|
|
410
|
+
You can further run local tests by executing `npm run test`.
|
|
369
411
|
|
|
370
412
|
All relevant commits will be signed with the key `0x24BB6BF4` for additional security (since 8th of April 2016).
|
|
371
413
|
|
|
@@ -407,10 +449,10 @@ Feature releases will not be announced to this list.
|
|
|
407
449
|
|
|
408
450
|
Many people helped and help DOMPurify become what it is and need to be acknowledged here!
|
|
409
451
|
|
|
410
|
-
[JGraph 💸](https://github.com/jgraph), [
|
|
452
|
+
[Cybozu 💛💸](https://github.com/cybozu), [hata6502 💸](https://github.com/hata6502), [intra-mart-dh 💸](https://github.com/intra-mart-dh), [hash_kitten ❤️](https://twitter.com/hash_kitten), [kevin_mizu ❤️](https://twitter.com/kevin_mizu), [icesfont ❤️](https://github.com/icesfont), [reduckted ❤️](https://github.com/reduckted), [dcramer 💸](https://github.com/dcramer), [JGraph 💸](https://github.com/jgraph), [baekilda 💸](https://github.com/baekilda), [Healthchecks 💸](https://github.com/healthchecks), [Sentry 💸](https://github.com/getsentry), [jarrodldavis 💸](https://github.com/jarrodldavis), [CynegeticIO](https://github.com/CynegeticIO), [ssi02014 ❤️](https://github.com/ssi02014), [GrantGryczan](https://github.com/GrantGryczan), [Lowdefy](https://twitter.com/lowdefy), [granlem](https://twitter.com/MaximeVeit), [oreoshake](https://github.com/oreoshake), [tdeekens ❤️](https://github.com/tdeekens), [peernohell ❤️](https://github.com/peernohell), [is2ei](https://github.com/is2ei), [SoheilKhodayari](https://github.com/SoheilKhodayari), [franktopel](https://github.com/franktopel), [NateScarlet](https://github.com/NateScarlet), [neilj](https://github.com/neilj), [fhemberger](https://github.com/fhemberger), [Joris-van-der-Wel](https://github.com/Joris-van-der-Wel), [ydaniv](https://github.com/ydaniv), [terjanq](https://twitter.com/terjanq), [filedescriptor](https://github.com/filedescriptor), [ConradIrwin](https://github.com/ConradIrwin), [gibson042](https://github.com/gibson042), [choumx](https://github.com/choumx), [0xSobky](https://github.com/0xSobky), [styfle](https://github.com/styfle), [koto](https://github.com/koto), [tlau88](https://github.com/tlau88), [strugee](https://github.com/strugee), [oparoz](https://github.com/oparoz), [mathiasbynens](https://github.com/mathiasbynens), [edg2s](https://github.com/edg2s), [dnkolegov](https://github.com/dnkolegov), [dhardtke](https://github.com/dhardtke), [wirehead](https://github.com/wirehead), [thorn0](https://github.com/thorn0), [styu](https://github.com/styu), [mozfreddyb](https://github.com/mozfreddyb), [mikesamuel](https://github.com/mikesamuel), [jorangreef](https://github.com/jorangreef), [jimmyhchan](https://github.com/jimmyhchan), [jameydeorio](https://github.com/jameydeorio), [jameskraus](https://github.com/jameskraus), [hyderali](https://github.com/hyderali), [hansottowirtz](https://github.com/hansottowirtz), [hackvertor](https://github.com/hackvertor), [freddyb](https://github.com/freddyb), [flavorjones](https://github.com/flavorjones), [djfarrelly](https://github.com/djfarrelly), [devd](https://github.com/devd), [camerondunford](https://github.com/camerondunford), [buu700](https://github.com/buu700), [buildog](https://github.com/buildog), [alabiaga](https://github.com/alabiaga), [Vector919](https://github.com/Vector919), [Robbert](https://github.com/Robbert), [GreLI](https://github.com/GreLI), [FuzzySockets](https://github.com/FuzzySockets), [ArtemBernatskyy](https://github.com/ArtemBernatskyy), [@garethheyes](https://twitter.com/garethheyes), [@shafigullin](https://twitter.com/shafigullin), [@mmrupp](https://twitter.com/mmrupp), [@irsdl](https://twitter.com/irsdl),[ShikariSenpai](https://github.com/ShikariSenpai), [ansjdnakjdnajkd](https://github.com/ansjdnakjdnajkd), [@asutherland](https://twitter.com/asutherland), [@mathias](https://twitter.com/mathias), [@cgvwzq](https://twitter.com/cgvwzq), [@robbertatwork](https://twitter.com/robbertatwork), [@giutro](https://twitter.com/giutro), [@CmdEngineer\_](https://twitter.com/CmdEngineer_), [@avr4mit](https://twitter.com/avr4mit), [davecardwell](https://github.com/davecardwell) and especially [@securitymb ❤️](https://twitter.com/securitymb) & [@masatokinugawa ❤️](https://twitter.com/masatokinugawa)
|
|
411
453
|
|
|
412
454
|
## Testing powered by
|
|
413
455
|
|
|
414
|
-
<a target="_blank" href="https://www.browserstack.com/"><img width="200" src="https://
|
|
456
|
+
<a target="_blank" href="https://www.browserstack.com/"><img width="200" src="https://github.com/cure53/DOMPurify/assets/6709482/f70be7eb-8fc4-41ea-9653-9d359235328f"></a><br>
|
|
415
457
|
|
|
416
458
|
And last but not least, thanks to [BrowserStack Open-Source Program](https://www.browserstack.com/open-source) for supporting this project with their services for free and delivering excellent, dedicated and very professional support on top of that.
|