From b2f4041e098b27fcfcf4996fe24667bdfaa1acdb Mon Sep 17 00:00:00 2001 From: Stavros Date: Mon, 10 Feb 2025 22:12:30 +0200 Subject: [PATCH] refactor: handle url queries null values better --- site/src/pages/continue-page.tsx | 26 ++++++++++++++++++++------ site/src/pages/login-page.tsx | 5 +++-- site/src/pages/unauthorized-page.tsx | 8 ++++---- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/site/src/pages/continue-page.tsx b/site/src/pages/continue-page.tsx index a9b491e..69e23d3 100644 --- a/site/src/pages/continue-page.tsx +++ b/site/src/pages/continue-page.tsx @@ -8,7 +8,7 @@ import { ReactNode } from "react"; export const ContinuePage = () => { const queryString = window.location.search; const params = new URLSearchParams(queryString); - const redirectUri = params.get("redirect_uri"); + const redirectUri = params.get("redirect_uri") ?? ""; const { isLoggedIn, disableContinue } = useUserContext(); @@ -16,7 +16,7 @@ export const ContinuePage = () => { return ; } - if (redirectUri === "null") { + if (redirectUri === "null" || redirectUri === "") { return ; } @@ -27,15 +27,29 @@ export const ContinuePage = () => { color: "blue", }); setTimeout(() => { - window.location.href = redirectUri!; + window.location.href = redirectUri; }, 500); }; - const urlParsed = URL.parse(redirectUri!); + const urlParsed = URL.parse(redirectUri); + + if (urlParsed === null) { + return ( + + + Invalid Redirect + + + The redirect URL is invalid, please contact the app owner to fix the + issue. + + + ); + } if ( window.location.protocol === "https:" && - urlParsed!.protocol === "http:" + urlParsed.protocol === "http:" ) { return ( @@ -54,7 +68,7 @@ export const ContinuePage = () => { } if (disableContinue) { - window.location.href = redirectUri!; + window.location.href = redirectUri; return ( diff --git a/site/src/pages/login-page.tsx b/site/src/pages/login-page.tsx index 1f4b509..ebfbb2d 100644 --- a/site/src/pages/login-page.tsx +++ b/site/src/pages/login-page.tsx @@ -24,9 +24,10 @@ import { TailscaleIcon } from "../icons/tailscale"; export const LoginPage = () => { const queryString = window.location.search; const params = new URLSearchParams(queryString); - const redirectUri = params.get("redirect_uri"); + const redirectUri = params.get("redirect_uri") ?? ""; const { isLoggedIn, configuredProviders } = useUserContext(); + const oauthProviders = configuredProviders.filter( (value) => value !== "username", ); @@ -69,7 +70,7 @@ export const LoginPage = () => { color: "green", }); setTimeout(() => { - if (redirectUri === "null") { + if (redirectUri === "null" || redirectUri === "") { window.location.replace("/"); } else { window.location.replace(`/continue?redirect_uri=${redirectUri}`); diff --git a/site/src/pages/unauthorized-page.tsx b/site/src/pages/unauthorized-page.tsx index 7c3b8a8..c5b0f81 100644 --- a/site/src/pages/unauthorized-page.tsx +++ b/site/src/pages/unauthorized-page.tsx @@ -5,10 +5,10 @@ import { Navigate } from "react-router"; export const UnauthorizedPage = () => { const queryString = window.location.search; const params = new URLSearchParams(queryString); - const username = params.get("username"); - const resource = params.get("resource"); + const username = params.get("username") ?? ""; + const resource = params.get("resource") ?? ""; - if (username === "null") { + if (username === "null" || username === "") { return ; } @@ -20,7 +20,7 @@ export const UnauthorizedPage = () => { The user with username {username} is not authorized to{" "} - {resource !== "null" ? ( + {resource !== "null" && resource !== "" ? ( access the {resource} resource.