From ab23d02f4b93c313c50f7204f7ce071f9d720f31 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Fri, 16 Oct 2020 15:44:39 +0100 Subject: [PATCH] client - sanitize urls, so we can match routes with nasty chars --- packages/client/src/render/screenRouter.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/client/src/render/screenRouter.js b/packages/client/src/render/screenRouter.js index 6af4f5491a..a925fab198 100644 --- a/packages/client/src/render/screenRouter.js +++ b/packages/client/src/render/screenRouter.js @@ -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))