escape postgres regex meta characters (#604)
Co-authored-by: rleed <rleed1@pm.me>
This commit is contained in:
parent
0b872cbbe7
commit
fb724ed9ec
|
@ -552,17 +552,25 @@ export default {
|
||||||
},
|
},
|
||||||
dupes: async (parent, { url }, { me, models }) => {
|
dupes: async (parent, { url }, { me, models }) => {
|
||||||
const urlObj = new URL(ensureProtocol(url))
|
const urlObj = new URL(ensureProtocol(url))
|
||||||
let uri = urlObj.hostname + '(:[0-9]+)?' + urlObj.pathname
|
let { hostname, pathname } = urlObj
|
||||||
uri = uri.endsWith('/') ? uri.slice(0, -1) : uri
|
|
||||||
|
|
||||||
|
hostname = hostname + '(:[0-9]+)?'
|
||||||
const parseResult = parse(urlObj.hostname)
|
const parseResult = parse(urlObj.hostname)
|
||||||
if (parseResult?.subdomain?.length) {
|
if (parseResult?.subdomain?.length) {
|
||||||
const { subdomain } = parseResult
|
const { subdomain } = parseResult
|
||||||
uri = uri.replace(subdomain, '(%)?')
|
hostname = hostname.replace(subdomain, '(%)?')
|
||||||
} else {
|
} 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}/?`
|
let similar = `(http(s)?://)?${uri}/?`
|
||||||
const whitelist = ['news.ycombinator.com/item', 'bitcointalk.org/index.php']
|
const whitelist = ['news.ycombinator.com/item', 'bitcointalk.org/index.php']
|
||||||
const youtube = ['www.youtube.com', 'youtube.com', 'm.youtube.com', 'youtu.be']
|
const youtube = ['www.youtube.com', 'youtube.com', 'm.youtube.com', 'youtu.be']
|
||||||
|
|
Loading…
Reference in New Issue