mirror of
https://github.com/miniflux/v2.git
synced 2025-09-30 19:22:11 +00:00
refactor(sanitizer): simplify hasValidURIScheme
and isBlockedResource
functions
- use an array instead of a map for the schemes, as the overwhelming majority of them will be either http or https, which we can place in front of the array. This is faster than using a map. - Simplify hasValidURIScheme by using strings.HasPrefix instead of doing strings.IndexByte - Simplify isBlockedResource by using a simple for loop, instead of a weird slices.ContainsFunc+strings.Contains construct. On my noisy system: ``` goos: linux goarch: arm64 pkg: miniflux.app/v2/internal/reader/sanitizer │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ Sanitize-8 22.19m ± 4% 21.97m ± 4% ~ (p=0.948 n=50) ```
This commit is contained in:
parent
e279b955c4
commit
5a97bf8b5e
1 changed files with 55 additions and 51 deletions
|
@ -138,46 +138,51 @@ var (
|
||||||
"linkedin.com/shareArticle",
|
"linkedin.com/shareArticle",
|
||||||
}
|
}
|
||||||
|
|
||||||
validURISchemes = map[string]struct{}{
|
// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
||||||
"apt": {},
|
validURISchemes = []string{
|
||||||
"bitcoin": {},
|
// Most commong schemes on top.
|
||||||
"callto": {},
|
"https:",
|
||||||
"dav": {},
|
"http:",
|
||||||
"davs": {},
|
|
||||||
"ed2k": {},
|
// Then the rest.
|
||||||
"facetime": {},
|
"apt:",
|
||||||
"feed": {},
|
"bitcoin:",
|
||||||
"ftp": {},
|
"callto:",
|
||||||
"geo": {},
|
"dav:",
|
||||||
"git": {},
|
"davs:",
|
||||||
"gopher": {},
|
"ed2k:",
|
||||||
"http": {},
|
"facetime:",
|
||||||
"https": {},
|
"feed:",
|
||||||
"irc": {},
|
"ftp:",
|
||||||
"irc6": {},
|
"geo:",
|
||||||
"ircs": {},
|
"git:",
|
||||||
"itms-apps": {},
|
"gopher:",
|
||||||
"itms": {},
|
"irc:",
|
||||||
"magnet": {},
|
"irc6:",
|
||||||
"mailto": {},
|
"ircs:",
|
||||||
"news": {},
|
"itms-apps:",
|
||||||
"nntp": {},
|
"itms:",
|
||||||
"rtmp": {},
|
"magnet:",
|
||||||
"sftp": {},
|
"mailto:",
|
||||||
"sip": {},
|
"news:",
|
||||||
"sips": {},
|
"nntp:",
|
||||||
"skype": {},
|
"rtmp:",
|
||||||
"spotify": {},
|
"sftp:",
|
||||||
"ssh": {},
|
"sip:",
|
||||||
"steam": {},
|
"sips:",
|
||||||
"svn": {},
|
"skype:",
|
||||||
"svn+ssh": {},
|
"spotify:",
|
||||||
"tel": {},
|
"ssh:",
|
||||||
"webcal": {},
|
"steam:",
|
||||||
"xmpp": {},
|
"svn:",
|
||||||
|
"svn+ssh:",
|
||||||
|
"tel:",
|
||||||
|
"webcal:",
|
||||||
|
"xmpp:",
|
||||||
|
|
||||||
// iOS Apps
|
// iOS Apps
|
||||||
"opener": {}, // https://www.opener.link
|
"opener:", // https://www.opener.link
|
||||||
"hack": {}, // https://apps.apple.com/it/app/hack-for-hacker-news-reader/id1464477788?l=en-GB
|
"hack:", // https://apps.apple.com/it/app/hack-for-hacker-news-reader/id1464477788?l=en-GB
|
||||||
}
|
}
|
||||||
|
|
||||||
dataAttributeAllowedPrefixes = []string{
|
dataAttributeAllowedPrefixes = []string{
|
||||||
|
@ -467,23 +472,22 @@ func hasRequiredAttributes(tagName string, attributes []string) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
|
||||||
func hasValidURIScheme(absoluteURL string) bool {
|
func hasValidURIScheme(absoluteURL string) bool {
|
||||||
colonIndex := strings.IndexByte(absoluteURL, ':')
|
for _, scheme := range validURISchemes {
|
||||||
// Scheme must exist (colonIndex > 0). An empty scheme (e.g. ":foo") is not allowed.
|
if strings.HasPrefix(absoluteURL, scheme) {
|
||||||
if colonIndex <= 0 {
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
scheme := absoluteURL[:colonIndex]
|
|
||||||
_, ok := validURISchemes[strings.ToLower(scheme)]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func isBlockedResource(absoluteURL string) bool {
|
func isBlockedResource(absoluteURL string) bool {
|
||||||
return slices.ContainsFunc(blockedResourceURLSubstrings, func(element string) bool {
|
for _, blockedURL := range blockedResourceURLSubstrings {
|
||||||
return strings.Contains(absoluteURL, element)
|
if strings.Contains(absoluteURL, blockedURL) {
|
||||||
})
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isValidIframeSource(iframeSourceURL string) bool {
|
func isValidIframeSource(iframeSourceURL string) bool {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue