url_preview tests

Signed-off-by: Aleksandr Dubovikov <d.lexand@gmail.com>
This commit is contained in:
Aleksandr Dubovikov 2024-10-09 20:35:38 +02:00
parent 4232fa3e67
commit 0224d94d9a
3 changed files with 245 additions and 7 deletions

View file

@ -88,11 +88,9 @@ func makeUrlPreviewHandler(
}
// Check if the url is in the blacklist
for _, pattern := range urlBlackList {
if pattern.MatchString(pUrl) {
logger.WithField("pattern", pattern.String()).Warn("the url is blacklisted")
return util.ErrorResponse(ErrorBlackListed)
}
if checkURLBlacklisted(urlBlackList, pUrl) {
logger.Debug("The url is in the blacklist")
return util.ErrorResponse(ErrorBlackListed)
}
urlParsed, perr := url.Parse(pUrl)
@ -668,5 +666,14 @@ func createUrlBlackList(cfg *config.MediaAPI) []*regexp.Regexp {
blackList[i] = regexp.MustCompile(pattern)
}
return blackList
}
func checkURLBlacklisted(blacklist []*regexp.Regexp, url string) bool {
// Check if the url is in the blacklist
for _, pattern := range blacklist {
if pattern.MatchString(url) {
return true
}
}
return false
}