From 2b6d952333f9f8c9c3195bba90fba48d6cabc9d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Tron=C3=AD=C4=8Dek?= Date: Wed, 2 Oct 2024 17:46:17 +0200 Subject: [PATCH] Fix login URL host parsing regression (#20265) * Fix login URL host parsing regression * fallback with prefixes --- components/dashboard/src/Login.tsx | 37 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/components/dashboard/src/Login.tsx b/components/dashboard/src/Login.tsx index 511587c4027044..f1a4dc5a9bc7e1 100644 --- a/components/dashboard/src/Login.tsx +++ b/components/dashboard/src/Login.tsx @@ -37,6 +37,26 @@ export function hasLoggedInBefore() { return GitpodCookie.isPresent(document.cookie); } +const SEGMENT_SEPARATOR = "/"; +const getContextUrlFromHash = (input: string): URL | undefined => { + if (typeof URL.canParse !== "function") { + return undefined; + } + if (URL.canParse(input)) { + return new URL(input); + } + + const chunks = input.split(SEGMENT_SEPARATOR); + for (const chunk of chunks) { + input = input.replace(`${chunk}${SEGMENT_SEPARATOR}`, ""); + if (URL.canParse(input)) { + return new URL(input); + } + } + + return undefined; +}; + type LoginProps = { onLoggedIn?: () => void; }; @@ -49,10 +69,19 @@ export const Login: FC = ({ onLoggedIn }) => { const enterprise = !!authProviders.data && authProviders.data.length === 0; useEffect(() => { - if (urlHash.length > 0) { - const url = new URL(urlHash); - setHostFromContext(url.host); - setRepoPathname(url.pathname); + try { + if (urlHash.length > 0) { + const url = new URL(urlHash); + setHostFromContext(url.host); + setRepoPathname(url.pathname); + } + } catch (error) { + // hash is not a valid URL, try to extract the context URL when there are parts like env vars or other prefixes + const contextUrl = getContextUrlFromHash(urlHash); + if (contextUrl) { + setHostFromContext(contextUrl.host); + setRepoPathname(contextUrl.pathname); + } } }, [urlHash]);