|
8 | 8 | * --------------------------------------------------------------------------
|
9 | 9 | */
|
10 | 10 |
|
11 |
| -const uriAttributes = new Set([ |
12 |
| - 'background', |
13 |
| - 'cite', |
14 |
| - 'href', |
15 |
| - 'itemtype', |
16 |
| - 'longdesc', |
17 |
| - 'poster', |
18 |
| - 'src', |
19 |
| - 'xlink:href' |
20 |
| -]) |
21 |
| - |
22 |
| -/** |
23 |
| - * A pattern that recognizes a commonly useful subset of URLs that are safe. |
24 |
| - * |
25 |
| - * Shout-out to Angular https://github.com/angular/angular/blob/12.2.x/packages/core/src/sanitization/url_sanitizer.ts |
26 |
| - */ |
27 |
| -const SAFE_URL_PATTERN = /^(?:(?:https?|mailto|ftp|tel|file|sms):|[^#&/:?]*(?:[#/?]|$))/i |
28 |
| - |
29 |
| -/** |
30 |
| - * A pattern that matches safe data URLs. Only matches image, video and audio types. |
31 |
| - * |
32 |
| - * Shout-out to Angular https://github.com/angular/angular/blob/12.2.x/packages/core/src/sanitization/url_sanitizer.ts |
33 |
| - */ |
34 |
| -const DATA_URL_PATTERN = /^data:(?:image\/(?:bmp|gif|jpeg|jpg|png|tiff|webp)|video\/(?:mpeg|mp4|ogg|webm)|audio\/(?:mp3|oga|ogg|opus));base64,[\d+/a-z]+=*$/i |
35 |
| - |
36 |
| -const allowedAttribute = (attribute, allowedAttributeList) => { |
37 |
| - const attributeName = attribute.nodeName.toLowerCase() |
38 |
| - |
39 |
| - if (allowedAttributeList.includes(attributeName)) { |
40 |
| - if (uriAttributes.has(attributeName)) { |
41 |
| - return Boolean(SAFE_URL_PATTERN.test(attribute.nodeValue) || DATA_URL_PATTERN.test(attribute.nodeValue)) |
42 |
| - } |
43 |
| - |
44 |
| - return true |
45 |
| - } |
46 |
| - |
47 |
| - // Check if a regular expression validates the attribute. |
48 |
| - return allowedAttributeList.filter(attributeRegex => attributeRegex instanceof RegExp) |
49 |
| - .some(regex => regex.test(attributeName)) |
50 |
| -} |
51 |
| - |
52 | 11 | // js-docs-start allow-list
|
53 | 12 | const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
|
54 | 13 |
|
@@ -87,6 +46,42 @@ export const DefaultAllowlist = {
|
87 | 46 | }
|
88 | 47 | // js-docs-end allow-list
|
89 | 48 |
|
| 49 | +const uriAttributes = new Set([ |
| 50 | + 'background', |
| 51 | + 'cite', |
| 52 | + 'href', |
| 53 | + 'itemtype', |
| 54 | + 'longdesc', |
| 55 | + 'poster', |
| 56 | + 'src', |
| 57 | + 'xlink:href' |
| 58 | +]) |
| 59 | + |
| 60 | +/** |
| 61 | + * A pattern that recognizes URLs that are safe wrt. XSS in URL navigation |
| 62 | + * contexts. |
| 63 | + * |
| 64 | + * Shout-out to Angular https://github.com/angular/angular/blob/15.2.8/packages/core/src/sanitization/url_sanitizer.ts#L38 |
| 65 | + */ |
| 66 | +// eslint-disable-next-line unicorn/better-regex |
| 67 | +const SAFE_URL_PATTERN = /^(?!javascript:)(?:[a-z0-9+.-]+:|[^&:/?#]*(?:[/?#]|$))/i |
| 68 | + |
| 69 | +const allowedAttribute = (attribute, allowedAttributeList) => { |
| 70 | + const attributeName = attribute.nodeName.toLowerCase() |
| 71 | + |
| 72 | + if (allowedAttributeList.includes(attributeName)) { |
| 73 | + if (uriAttributes.has(attributeName)) { |
| 74 | + return Boolean(SAFE_URL_PATTERN.test(attribute.nodeValue)) |
| 75 | + } |
| 76 | + |
| 77 | + return true |
| 78 | + } |
| 79 | + |
| 80 | + // Check if a regular expression validates the attribute. |
| 81 | + return allowedAttributeList.filter(attributeRegex => attributeRegex instanceof RegExp) |
| 82 | + .some(regex => regex.test(attributeName)) |
| 83 | +} |
| 84 | + |
90 | 85 | export function sanitizeHtml(unsafeHtml, allowList, sanitizeFunction) {
|
91 | 86 | if (!unsafeHtml.length) {
|
92 | 87 | return unsafeHtml
|
|
0 commit comments