Improve domain replacement regex

1. Let regex only match href, close #267
2. Enforce HTTPS if the instance is on HTTPS
3. Prevent redirecting domains such as blog.twitter.com and help.instagram.com
This commit is contained in:
Austin Huang 2022-01-26 12:41:18 -05:00
parent 1efe34c0ad
commit b5878ea031
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
1 changed files with 11 additions and 9 deletions

View File

@ -182,27 +182,29 @@ module.exports = function(request, fs) {
this.replaceUserDomains = (str, user_preferences) => {
let redditRegex = /([A-z.]+\.)?(reddit(\.com)|redd(\.it))/gm;
let youtubeRegex = /([A-z.]+\.)?youtu(be\.com|\.be)/gm;
let twitterRegex = /([A-z.]+\.)?twitter\.com/gm;
let instagramRegex = /([A-z.]+\.)?instagram.com/gm;
let redditRegex = /(?<=href=")(https?:\/\/)([A-z.]+\.)?(reddit(\.com)|redd(\.it))(?=.+")/gm;
let youtubeRegex = /(?<=href=")(https?:\/\/)([A-z.]+\.)?youtu(be\.com|\.be)(?=.+")/gm;
let twitterRegex = /(?<=href=")(https?:\/\/)(www\.)?twitter\.com(?=.+")/gm;
let instagramRegex = /(?<=href=")(https?:\/\/)(www+\.)?instagram.com(?=.+")/gm;
str = str.replace(redditRegex, config.domain)
let protocol = config.https_enabled || config.api_force_https ? 'https://' : 'http://'
str = str.replace(redditRegex, protocol + config.domain)
if(typeof(user_preferences) == 'undefined')
return str
if(typeof(user_preferences.domain_youtube) != 'undefined')
if(user_preferences.domain_youtube)
str = str.replace(youtubeRegex, user_preferences.domain_youtube)
if(user_preferences.domain_youtube)
str = str.replace(youtubeRegex, protocol + user_preferences.domain_youtube)
if(typeof(user_preferences.domain_twitter) != 'undefined')
if(user_preferences.domain_twitter)
str = str.replace(twitterRegex, user_preferences.domain_twitter)
str = str.replace(twitterRegex, protocol + user_preferences.domain_twitter)
if(typeof(user_preferences.domain_instagram) != 'undefined')
if(user_preferences.domain_instagram)
str = str.replace(instagramRegex, user_preferences.domain_instagram)
str = str.replace(instagramRegex, protocol + user_preferences.domain_instagram)
return str
}