2019-05-29 05:21:06 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
2019-09-25 10:29:10 -04:00
|
|
|
const { URL } = require('url')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-25 10:29:10 -04:00
|
|
|
function getSafeRedirectPath(value) {
|
|
|
|
const baseURL = Settings.siteUrl // base URL is required to construct URL from path
|
|
|
|
const url = new URL(value, baseURL)
|
|
|
|
let safePath = `${url.pathname}${url.search}${url.hash}`
|
|
|
|
if (safePath === '/') {
|
|
|
|
safePath = undefined
|
|
|
|
}
|
|
|
|
return safePath
|
|
|
|
}
|
|
|
|
|
|
|
|
const UrlHelper = {
|
|
|
|
getSafeRedirectPath,
|
2019-05-29 05:21:06 -04:00
|
|
|
wrapUrlWithProxy(url) {
|
|
|
|
// TODO: Consider what to do for Community and Enterprise edition?
|
2019-07-15 09:09:04 -04:00
|
|
|
if (!Settings.apis.linkedUrlProxy.url) {
|
2019-05-29 05:21:06 -04:00
|
|
|
throw new Error('no linked url proxy configured')
|
|
|
|
}
|
|
|
|
return `${Settings.apis.linkedUrlProxy.url}?url=${encodeURIComponent(url)}`
|
|
|
|
},
|
|
|
|
|
|
|
|
prependHttpIfNeeded(url) {
|
|
|
|
if (!url.match('://')) {
|
|
|
|
url = `http://${url}`
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 10:29:10 -04:00
|
|
|
|
|
|
|
module.exports = UrlHelper
|