Files
Verso/services/web/app/src/Features/Helpers/UrlHelper.js
T
Simon Detheridgeandsharelatex 6c005da303 Merge pull request #2201 from overleaf/hb-relative-path-fix
Handle double slash case of safe redirect paths

GitOrigin-RevId: fb6e8ea9d736a65df61f07d563235262b8aaf0f3
2019-10-04 14:21:52 +00:00

33 lines
872 B
JavaScript

const Settings = require('settings-sharelatex')
const { URL } = require('url')
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}`.replace(/^\/+/, '/')
if (safePath === '/') {
safePath = undefined
}
return safePath
}
const UrlHelper = {
getSafeRedirectPath,
wrapUrlWithProxy(url) {
// TODO: Consider what to do for Community and Enterprise edition?
if (!Settings.apis.linkedUrlProxy.url) {
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
}
}
module.exports = UrlHelper