dompurify 3.1.6 → 3.2.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.
@@ -0,0 +1,399 @@
1
+ /*! @license DOMPurify 3.2.0 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.2.0/LICENSE */
2
+
3
+ /**
4
+ * Configuration to control DOMPurify behavior.
5
+ */
6
+ interface Config {
7
+ /**
8
+ * Extend the existing array of allowed attributes.
9
+ */
10
+ ADD_ATTR?: string[] | undefined;
11
+ /**
12
+ * Extend the existing array of elements that can use Data URIs.
13
+ */
14
+ ADD_DATA_URI_TAGS?: string[] | undefined;
15
+ /**
16
+ * Extend the existing array of allowed tags.
17
+ */
18
+ ADD_TAGS?: string[] | undefined;
19
+ /**
20
+ * Extend the existing array of elements that are safe for URI-like values (be careful, XSS risk).
21
+ */
22
+ ADD_URI_SAFE_ATTR?: string[] | undefined;
23
+ /**
24
+ * Allow ARIA attributes, leave other safe HTML as is (default is true).
25
+ */
26
+ ALLOW_ARIA_ATTR?: boolean | undefined;
27
+ /**
28
+ * Allow HTML5 data attributes, leave other safe HTML as is (default is true).
29
+ */
30
+ ALLOW_DATA_ATTR?: boolean | undefined;
31
+ /**
32
+ * Allow external protocol handlers in URL attributes (default is false, be careful, XSS risk).
33
+ * By default only `http`, `https`, `ftp`, `ftps`, `tel`, `mailto`, `callto`, `sms`, `cid` and `xmpp` are allowed.
34
+ */
35
+ ALLOW_UNKNOWN_PROTOCOLS?: boolean | undefined;
36
+ /**
37
+ * Decide if self-closing tags in attributes are allowed.
38
+ * Usually removed due to a mXSS issue in jQuery 3.0.
39
+ */
40
+ ALLOW_SELF_CLOSE_IN_ATTR?: boolean | undefined;
41
+ /**
42
+ * Allow only specific attributes.
43
+ */
44
+ ALLOWED_ATTR?: string[] | undefined;
45
+ /**
46
+ * Allow only specific elements.
47
+ */
48
+ ALLOWED_TAGS?: string[] | undefined;
49
+ /**
50
+ * Allow only specific namespaces. Defaults to:
51
+ * - `http://www.w3.org/1999/xhtml`
52
+ * - `http://www.w3.org/2000/svg`
53
+ * - `http://www.w3.org/1998/Math/MathML`
54
+ */
55
+ ALLOWED_NAMESPACES?: string[] | undefined;
56
+ /**
57
+ * Allow specific protocols handlers in URL attributes via regex (be careful, XSS risk).
58
+ * Default RegExp:
59
+ * ```
60
+ * /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
61
+ * ```
62
+ */
63
+ ALLOWED_URI_REGEXP?: RegExp | undefined;
64
+ /**
65
+ * Define how custom elements are handled.
66
+ */
67
+ CUSTOM_ELEMENT_HANDLING?: {
68
+ /**
69
+ * Regular expression or function to match to allowed elements.
70
+ * Default is null (disallow any custom elements).
71
+ */
72
+ tagNameCheck?: RegExp | ((tagName: string) => boolean) | null | undefined;
73
+ /**
74
+ * Regular expression or function to match to allowed attributes.
75
+ * Default is null (disallow any attributes not on the allow list).
76
+ */
77
+ attributeNameCheck?: RegExp | ((attributeName: string) => boolean) | null | undefined;
78
+ /**
79
+ * Allow custom elements derived from built-ins if they pass `tagNameCheck`. Default is false.
80
+ */
81
+ allowCustomizedBuiltInElements?: boolean | undefined;
82
+ };
83
+ /**
84
+ * Add attributes to block-list.
85
+ */
86
+ FORBID_ATTR?: string[] | undefined;
87
+ /**
88
+ * Add child elements to be removed when their parent is removed.
89
+ */
90
+ FORBID_CONTENTS?: string[] | undefined;
91
+ /**
92
+ * Add elements to block-list.
93
+ */
94
+ FORBID_TAGS?: string[] | undefined;
95
+ /**
96
+ * Glue elements like style, script or others to `document.body` and prevent unintuitive browser behavior in several edge-cases (default is false).
97
+ */
98
+ FORCE_BODY?: boolean | undefined;
99
+ /**
100
+ * Map of non-standard HTML element names to support. Map to true to enable support. For example:
101
+ *
102
+ * ```
103
+ * HTML_INTEGRATION_POINTS: { foreignobject: true }
104
+ * ```
105
+ */
106
+ HTML_INTEGRATION_POINTS?: Record<string, boolean> | undefined;
107
+ /**
108
+ * Sanitize a node "in place", which is much faster depending on how you use DOMPurify.
109
+ */
110
+ IN_PLACE?: boolean | undefined;
111
+ /**
112
+ * Keep an element's content when the element is removed (default is true).
113
+ */
114
+ KEEP_CONTENT?: boolean | undefined;
115
+ /**
116
+ * Map of MathML element names to support. Map to true to enable support. For example:
117
+ *
118
+ * ```
119
+ * MATHML_TEXT_INTEGRATION_POINTS: { mtext: true }
120
+ * ```
121
+ */
122
+ MATHML_TEXT_INTEGRATION_POINTS?: Record<string, boolean> | undefined;
123
+ /**
124
+ * Change the default namespace from HTML to something different.
125
+ */
126
+ NAMESPACE?: string | undefined;
127
+ /**
128
+ * Change the parser type so sanitized data is treated as XML and not as HTML, which is the default.
129
+ */
130
+ PARSER_MEDIA_TYPE?: DOMParserSupportedType | undefined;
131
+ /**
132
+ * Return a DOM `DocumentFragment` instead of an HTML string (default is false).
133
+ */
134
+ RETURN_DOM_FRAGMENT?: boolean | undefined;
135
+ /**
136
+ * Return a DOM `HTMLBodyElement` instead of an HTML string (default is false).
137
+ */
138
+ RETURN_DOM?: boolean | undefined;
139
+ /**
140
+ * Return a TrustedHTML object instead of a string if possible.
141
+ */
142
+ RETURN_TRUSTED_TYPE?: boolean | undefined;
143
+ /**
144
+ * Strip `{{ ... }}`, `${ ... }` and `<% ... %>` to make output safe for template systems.
145
+ * Be careful please, this mode is not recommended for production usage.
146
+ * Allowing template parsing in user-controlled HTML is not advised at all.
147
+ * Only use this mode if there is really no alternative.
148
+ */
149
+ SAFE_FOR_TEMPLATES?: boolean | undefined;
150
+ /**
151
+ * Change how e.g. comments containing risky HTML characters are treated.
152
+ * Be very careful, this setting should only be set to `false` if you really only handle
153
+ * HTML and nothing else, no SVG, MathML or the like.
154
+ * Otherwise, changing from `true` to `false` will lead to XSS in this or some other way.
155
+ */
156
+ SAFE_FOR_XML?: boolean | undefined;
157
+ /**
158
+ * Use DOM Clobbering protection on output (default is true, handle with care, minor XSS risks here).
159
+ */
160
+ SANITIZE_DOM?: boolean | undefined;
161
+ /**
162
+ * Enforce strict DOM Clobbering protection via namespace isolation (default is false).
163
+ * When enabled, isolates the namespace of named properties (i.e., `id` and `name` attributes)
164
+ * from JS variables by prefixing them with the string `user-content-`
165
+ */
166
+ SANITIZE_NAMED_PROPS?: boolean | undefined;
167
+ /**
168
+ * Supplied policy must define `createHTML` and `createScriptURL`.
169
+ */
170
+ TRUSTED_TYPES_POLICY?: TrustedTypePolicy | undefined;
171
+ /**
172
+ * Controls categories of allowed elements.
173
+ *
174
+ * Note that the `USE_PROFILES` setting will override the `ALLOWED_TAGS` setting
175
+ * so don't use them together.
176
+ */
177
+ USE_PROFILES?: false | UseProfilesConfig | undefined;
178
+ /**
179
+ * Return entire document including <html> tags (default is false).
180
+ */
181
+ WHOLE_DOCUMENT?: boolean | undefined;
182
+ }
183
+ /**
184
+ * Defines categories of allowed elements.
185
+ */
186
+ interface UseProfilesConfig {
187
+ /**
188
+ * Allow all safe MathML elements.
189
+ */
190
+ mathMl?: boolean | undefined;
191
+ /**
192
+ * Allow all safe SVG elements.
193
+ */
194
+ svg?: boolean | undefined;
195
+ /**
196
+ * Allow all save SVG Filters.
197
+ */
198
+ svgFilters?: boolean | undefined;
199
+ /**
200
+ * Allow all safe HTML elements.
201
+ */
202
+ html?: boolean | undefined;
203
+ }
204
+
205
+ declare const _default: DOMPurify;
206
+
207
+ interface DOMPurify {
208
+ /**
209
+ * Creates a DOMPurify instance using the given window-like object.
210
+ */
211
+ (root: WindowLike): DOMPurify;
212
+ /**
213
+ * Version label, exposed for easier checks
214
+ * if DOMPurify is up to date or not
215
+ */
216
+ version: string;
217
+ /**
218
+ * Array of elements that DOMPurify removed during sanitation.
219
+ * Empty if nothing was removed.
220
+ */
221
+ removed: Array<RemovedElement | RemovedAttribute>;
222
+ /**
223
+ * Expose whether this browser supports running the full DOMPurify.
224
+ */
225
+ isSupported: boolean;
226
+ /**
227
+ * Set the configuration once.
228
+ *
229
+ * @param cfg configuration object
230
+ */
231
+ setConfig(cfg?: Config): void;
232
+ /**
233
+ * Removes the configuration.
234
+ */
235
+ clearConfig(): void;
236
+ /**
237
+ * Provides core sanitation functionality.
238
+ *
239
+ * @param dirty string or DOM node
240
+ * @param cfg object
241
+ * @returns Sanitized TrustedHTML.
242
+ */
243
+ sanitize(dirty: string | Node, cfg: Config & {
244
+ RETURN_TRUSTED_TYPE: true;
245
+ }): TrustedHTML;
246
+ /**
247
+ * Provides core sanitation functionality.
248
+ *
249
+ * @param dirty DOM node
250
+ * @param cfg object
251
+ * @returns Sanitized DOM node.
252
+ */
253
+ sanitize(dirty: Node, cfg: Config & {
254
+ IN_PLACE: true;
255
+ }): Node;
256
+ /**
257
+ * Provides core sanitation functionality.
258
+ *
259
+ * @param dirty string or DOM node
260
+ * @param cfg object
261
+ * @returns Sanitized DOM node.
262
+ */
263
+ sanitize(dirty: string | Node, cfg: Config & {
264
+ RETURN_DOM: true;
265
+ }): Node;
266
+ /**
267
+ * Provides core sanitation functionality.
268
+ *
269
+ * @param dirty string or DOM node
270
+ * @param cfg object
271
+ * @returns Sanitized document fragment.
272
+ */
273
+ sanitize(dirty: string | Node, cfg: Config & {
274
+ RETURN_DOM_FRAGMENT: true;
275
+ }): DocumentFragment;
276
+ /**
277
+ * Provides core sanitation functionality.
278
+ *
279
+ * @param dirty string or DOM node
280
+ * @param cfg object
281
+ * @returns Sanitized string.
282
+ */
283
+ sanitize(dirty: string | Node, cfg?: Config): string;
284
+ /**
285
+ * Checks if an attribute value is valid.
286
+ * Uses last set config, if any. Otherwise, uses config defaults.
287
+ *
288
+ * @param tag Tag name of containing element.
289
+ * @param attr Attribute name.
290
+ * @param value Attribute value.
291
+ * @returns Returns true if `value` is valid. Otherwise, returns false.
292
+ */
293
+ isValidAttribute(tag: string, attr: string, value: string): boolean;
294
+ /**
295
+ * Adds a DOMPurify hook.
296
+ *
297
+ * @param entryPoint entry point for the hook to add
298
+ * @param hookFunction function to execute
299
+ */
300
+ addHook(entryPoint: BasicHookName, hookFunction: Hook): void;
301
+ /**
302
+ * Adds a DOMPurify hook.
303
+ *
304
+ * @param entryPoint entry point for the hook to add
305
+ * @param hookFunction function to execute
306
+ */
307
+ addHook(entryPoint: 'uponSanitizeElement', hookFunction: UponSanitizeElementHook): void;
308
+ /**
309
+ * Adds a DOMPurify hook.
310
+ *
311
+ * @param entryPoint entry point for the hook to add
312
+ * @param hookFunction function to execute
313
+ */
314
+ addHook(entryPoint: 'uponSanitizeAttribute', hookFunction: UponSanitizeAttributeHook): void;
315
+ /**
316
+ * Remove a DOMPurify hook at a given entryPoint
317
+ * (pops it from the stack of hooks if more are present)
318
+ *
319
+ * @param entryPoint entry point for the hook to remove
320
+ * @returns removed(popped) hook
321
+ */
322
+ removeHook(entryPoint: BasicHookName): Hook | undefined;
323
+ /**
324
+ * Remove a DOMPurify hook at a given entryPoint
325
+ * (pops it from the stack of hooks if more are present)
326
+ *
327
+ * @param entryPoint entry point for the hook to remove
328
+ * @returns removed(popped) hook
329
+ */
330
+ removeHook(entryPoint: 'uponSanitizeElement'): UponSanitizeElementHook | undefined;
331
+ /**
332
+ * Remove a DOMPurify hook at a given entryPoint
333
+ * (pops it from the stack of hooks if more are present)
334
+ *
335
+ * @param entryPoint entry point for the hook to remove
336
+ * @returns removed(popped) hook
337
+ */
338
+ removeHook(entryPoint: 'uponSanitizeAttribute'): UponSanitizeAttributeHook | undefined;
339
+ /**
340
+ * Removes all DOMPurify hooks at a given entryPoint
341
+ *
342
+ * @param entryPoint entry point for the hooks to remove
343
+ */
344
+ removeHooks(entryPoint: HookName): void;
345
+ /**
346
+ * Removes all DOMPurify hooks.
347
+ */
348
+ removeAllHooks(): void;
349
+ }
350
+ /**
351
+ * An element removed by DOMPurify.
352
+ */
353
+ interface RemovedElement {
354
+ /**
355
+ * The element that was removed.
356
+ */
357
+ element: Node;
358
+ }
359
+ /**
360
+ * An element removed by DOMPurify.
361
+ */
362
+ interface RemovedAttribute {
363
+ /**
364
+ * The attribute that was removed.
365
+ */
366
+ attribute: Attr | null;
367
+ /**
368
+ * The element that the attribute was removed.
369
+ */
370
+ from: Node;
371
+ }
372
+ type BasicHookName = 'beforeSanitizeElements' | 'afterSanitizeElements' | 'beforeSanitizeAttributes' | 'afterSanitizeAttributes' | 'beforeSanitizeShadowDOM' | 'uponSanitizeShadowNode' | 'afterSanitizeShadowDOM';
373
+ type UponSanitizeElementHookName = 'uponSanitizeElement';
374
+ type UponSanitizeAttributeHookName = 'uponSanitizeAttribute';
375
+ type HookName = BasicHookName | UponSanitizeElementHookName | UponSanitizeAttributeHookName;
376
+ type Hook = (this: DOMPurify, currentNode: Node, hookEvent: null, config: Config) => void;
377
+ type UponSanitizeElementHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeElementHookEvent, config: Config) => void;
378
+ type UponSanitizeAttributeHook = (this: DOMPurify, currentNode: Node, hookEvent: UponSanitizeAttributeHookEvent, config: Config) => void;
379
+ interface UponSanitizeElementHookEvent {
380
+ tagName: string;
381
+ allowedTags: Record<string, boolean>;
382
+ }
383
+ interface UponSanitizeAttributeHookEvent {
384
+ attrName: string;
385
+ attrValue: string;
386
+ keepAttr: boolean;
387
+ allowedAttributes: Record<string, boolean>;
388
+ forceKeepAttr: boolean | undefined;
389
+ }
390
+ /**
391
+ * A `Window`-like object containing the properties and types that DOMPurify requires.
392
+ */
393
+ type WindowLike = Pick<typeof globalThis, 'DocumentFragment' | 'HTMLTemplateElement' | 'Node' | 'Element' | 'NodeFilter' | 'NamedNodeMap' | 'HTMLFormElement' | 'DOMParser'> & {
394
+ document?: Document;
395
+ MozNamedAttrMap?: typeof window.NamedNodeMap;
396
+ trustedTypes?: typeof window.trustedTypes;
397
+ };
398
+
399
+ export { type Config, type Hook, type HookName, type RemovedAttribute, type RemovedElement, type UponSanitizeAttributeHook, type UponSanitizeAttributeHookEvent, type UponSanitizeElementHook, type UponSanitizeElementHookEvent, type WindowLike, _default as default };