From 068400bc42efa48c7e540631d41fc8f30df87eff Mon Sep 17 00:00:00 2001 From: Martin Marosi Date: Mon, 4 Dec 2023 09:37:16 +0100 Subject: [PATCH] Ensure platform URL will always end with trailing / --- src/auth/OIDCConnector/OIDCProvider.tsx | 4 +--- src/auth/platformUrl.test.ts | 8 +++++--- src/auth/platformUrl.ts | 12 +++++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/auth/OIDCConnector/OIDCProvider.tsx b/src/auth/OIDCConnector/OIDCProvider.tsx index 3a86b77d5..218bc3def 100644 --- a/src/auth/OIDCConnector/OIDCProvider.tsx +++ b/src/auth/OIDCConnector/OIDCProvider.tsx @@ -26,9 +26,7 @@ const OIDCProvider: React.FC = ({ children }) => { config: { ssoUrl }, }, } = data; - // add trailing slash if missing - const sanitizedSSOUrl = `${ssoUrl.replace(/\/$/, '')}/`; - setState({ ssoUrl: platformUrl(DEFAULT_SSO_ROUTES, sanitizedSSOUrl), microFrontendConfig: data }); + setState({ ssoUrl: platformUrl(DEFAULT_SSO_ROUTES, ssoUrl), microFrontendConfig: data }); } catch (error) { setState({ ssoUrl: platformUrl(DEFAULT_SSO_ROUTES), microFrontendConfig: data }); } diff --git a/src/auth/platformUrl.test.ts b/src/auth/platformUrl.test.ts index d06f7d9bd..02f985989 100644 --- a/src/auth/platformUrl.test.ts +++ b/src/auth/platformUrl.test.ts @@ -17,7 +17,7 @@ describe('platformUrl', () => { it('should return dev sso url if env is set to console.dev', () => { window.location.hostname = 'console.dev.redhat.com'; const ssourl = platformUrl(DEFAULT_SSO_ROUTES); - expect(ssourl).toBe(DEFAULT_SSO_ROUTES.dev.sso); + expect(ssourl).toBe(DEFAULT_SSO_ROUTES.dev.sso + '/'); // don't forget to reset the hostname for other tests window.location.hostname = ''; }); @@ -25,7 +25,7 @@ describe('platformUrl', () => { it('should return custom sso url if provided', () => { const customSsoUrl = 'https://custom.sso.url'; const ssourl = platformUrl(DEFAULT_SSO_ROUTES, customSsoUrl); - expect(ssourl).toBe(customSsoUrl); + expect(ssourl).toBe(customSsoUrl + '/'); }); // test for all envs using the DEFAULT_SSO_ROUTES @@ -34,7 +34,9 @@ describe('platformUrl', () => { it(`should return ${env} sso url if env is set to ${url}`, () => { window.location.hostname = url; const ssourl = platformUrl(DEFAULT_SSO_ROUTES); - expect(ssourl).toBe(DEFAULT_SSO_ROUTES[env as keyof typeof DEFAULT_SSO_ROUTES].sso); + expect(ssourl).toMatch(new RegExp(DEFAULT_SSO_ROUTES[env as keyof typeof DEFAULT_SSO_ROUTES].sso)); + // Must always end with trailing slash + expect(ssourl).toMatch(/\/$/); // don't forget to reset the hostname for other tests window.location.hostname = ''; }); diff --git a/src/auth/platformUrl.ts b/src/auth/platformUrl.ts index da904b6a3..617afa177 100644 --- a/src/auth/platformUrl.ts +++ b/src/auth/platformUrl.ts @@ -2,15 +2,21 @@ import { DEFAULT_SSO_ROUTES } from '../utils/common'; import logger from './logger'; const log = logger('auth/platform.ts'); +// add trailing slash if missing +function sanitizeUrl(url: string) { + return `${url.replace(/\/$/, '')}/`; +} + // Parse through keycloak options routes export default function platformUlr(env: typeof DEFAULT_SSO_ROUTES, configSsoUrl?: string) { // we have to use hard coded value for console.dev.redhat.com // ugly hack + if (location.hostname === 'console.dev.redhat.com') { - return DEFAULT_SSO_ROUTES.dev.sso; + return sanitizeUrl(DEFAULT_SSO_ROUTES.dev.sso); } if (configSsoUrl) { - return configSsoUrl; + return sanitizeUrl(configSsoUrl); } const ssoEnv = Object.entries(env).find(([, { url }]) => url.includes(location.hostname)); @@ -18,7 +24,7 @@ export default function platformUlr(env: typeof DEFAULT_SSO_ROUTES, configSsoUrl if (ssoEnv) { log(`SSO Url: ${ssoEnv?.[1].sso}`); log(`Current env: ${ssoEnv?.[0]}`); - return ssoEnv?.[1].sso; + return sanitizeUrl(ssoEnv?.[1].sso); } else { log('SSO url: not found, defaulting to qa'); log('Current env: not found, defaulting to qa');