Skip to content

Commit 05aac54

Browse files
committed
refactor: improve syntax
1 parent c22e4cd commit 05aac54

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

js/src/scrollspy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ class ScrollSpy extends BaseComponent {
211211
continue
212212
}
213213

214-
const observableSection = SelectorEngine.findOne(anchor.hash, this._element)
214+
const observableSection = SelectorEngine.findOne(decodeURI(anchor.hash), this._element)
215215

216216
// ensure that the observableSection exists & is visible
217217
if (isVisible(observableSection)) {
218-
this._targetLinks.set(anchor.hash, anchor)
218+
this._targetLinks.set(decodeURI(anchor.hash), anchor)
219219
this._observableSections.set(anchor.hash, observableSection)
220220
}
221221
}

js/src/util/sanitizer.js

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,6 @@
88
* --------------------------------------------------------------------------
99
*/
1010

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-
5211
// js-docs-start allow-list
5312
const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
5413

@@ -87,6 +46,42 @@ export const DefaultAllowlist = {
8746
}
8847
// js-docs-end allow-list
8948

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+
9085
export function sanitizeHtml(unsafeHtml, allowList, sanitizeFunction) {
9186
if (!unsafeHtml.length) {
9287
return unsafeHtml

0 commit comments

Comments
 (0)