client - sanitize urls, so we can match routes with nasty chars

This commit is contained in:
Michael Shanks 2020-10-16 15:44:39 +01:00
parent 0343a86671
commit ab23d02f4b
1 changed files with 18 additions and 3 deletions

View File

@ -3,6 +3,18 @@ import appStore from "../state/store"
import { parseAppIdFromCookie } from "./getAppId"
export const screenRouter = ({ screens, onScreenSelected, window }) => {
function sanitize(url) {
return url
.split("/")
.map(part => {
// if parameter, then use as is
if (part.startsWith(":")) return part
return encodeURIComponent(part)
})
.join("/")
.toLowerCase()
}
const makeRootedPath = url => {
const hostname = window.location && window.location.hostname
if (hostname) {
@ -13,13 +25,16 @@ export const screenRouter = ({ screens, onScreenSelected, window }) => {
) {
const appId = parseAppIdFromCookie(window.document.cookie)
if (url) {
if (url.startsWith(appId)) return url
return `/${appId}${url.startsWith("/") ? "" : "/"}${url}`
const sanitizedUrl = sanitize(url)
if (sanitizedUrl.startsWith(appId)) return sanitizedUrl
return `/${appId}${
sanitizedUrl.startsWith("/") ? "" : "/"
}${sanitizedUrl}`
}
return appId
}
}
return url
return sanitize(url)
}
const routes = screens.map(s => makeRootedPath(s.route))