escape postgres regex meta characters (#604)

Co-authored-by: rleed <rleed1@pm.me>
This commit is contained in:
rleed 2023-11-08 21:25:36 -03:00 committed by GitHub
parent 0b872cbbe7
commit fb724ed9ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 4 deletions

View File

@ -552,17 +552,25 @@ export default {
},
dupes: async (parent, { url }, { me, models }) => {
const urlObj = new URL(ensureProtocol(url))
let uri = urlObj.hostname + '(:[0-9]+)?' + urlObj.pathname
uri = uri.endsWith('/') ? uri.slice(0, -1) : uri
let { hostname, pathname } = urlObj
hostname = hostname + '(:[0-9]+)?'
const parseResult = parse(urlObj.hostname)
if (parseResult?.subdomain?.length) {
const { subdomain } = parseResult
uri = uri.replace(subdomain, '(%)?')
hostname = hostname.replace(subdomain, '(%)?')
} else {
uri = `(%.)?${uri}`
hostname = `(%.)?${hostname}`
}
// escape postgres regex meta characters
pathname = pathname.replace(/\+/g, '\\+')
pathname = pathname.replace(/%/g, '\\%')
pathname = pathname.replace(/_/g, '\\_')
let uri = hostname + pathname
uri = uri.endsWith('/') ? uri.slice(0, -1) : uri
let similar = `(http(s)?://)?${uri}/?`
const whitelist = ['news.ycombinator.com/item', 'bitcointalk.org/index.php']
const youtube = ['www.youtube.com', 'youtube.com', 'm.youtube.com', 'youtu.be']