Skip to content

Commit 2b6d952

Browse files
Fix login URL host parsing regression (#20265)
* Fix login URL host parsing regression * fallback with prefixes
1 parent ca933cb commit 2b6d952

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

components/dashboard/src/Login.tsx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ export function hasLoggedInBefore() {
3737
return GitpodCookie.isPresent(document.cookie);
3838
}
3939

40+
const SEGMENT_SEPARATOR = "/";
41+
const getContextUrlFromHash = (input: string): URL | undefined => {
42+
if (typeof URL.canParse !== "function") {
43+
return undefined;
44+
}
45+
if (URL.canParse(input)) {
46+
return new URL(input);
47+
}
48+
49+
const chunks = input.split(SEGMENT_SEPARATOR);
50+
for (const chunk of chunks) {
51+
input = input.replace(`${chunk}${SEGMENT_SEPARATOR}`, "");
52+
if (URL.canParse(input)) {
53+
return new URL(input);
54+
}
55+
}
56+
57+
return undefined;
58+
};
59+
4060
type LoginProps = {
4161
onLoggedIn?: () => void;
4262
};
@@ -49,10 +69,19 @@ export const Login: FC<LoginProps> = ({ onLoggedIn }) => {
4969
const enterprise = !!authProviders.data && authProviders.data.length === 0;
5070

5171
useEffect(() => {
52-
if (urlHash.length > 0) {
53-
const url = new URL(urlHash);
54-
setHostFromContext(url.host);
55-
setRepoPathname(url.pathname);
72+
try {
73+
if (urlHash.length > 0) {
74+
const url = new URL(urlHash);
75+
setHostFromContext(url.host);
76+
setRepoPathname(url.pathname);
77+
}
78+
} catch (error) {
79+
// hash is not a valid URL, try to extract the context URL when there are parts like env vars or other prefixes
80+
const contextUrl = getContextUrlFromHash(urlHash);
81+
if (contextUrl) {
82+
setHostFromContext(contextUrl.host);
83+
setRepoPathname(contextUrl.pathname);
84+
}
5685
}
5786
}, [urlHash]);
5887

0 commit comments

Comments
 (0)