Skip to content

Fix login URL host parsing regression #20265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions components/dashboard/src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -49,10 +69,19 @@ export const Login: FC<LoginProps> = ({ 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]);

Expand Down
Loading