From 6c999ad14844b4f9ec74dc225895db6a12e4a781 Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 26 Mar 2023 19:18:47 +0000 Subject: [PATCH] fix: ensure same protocol for configured api url (#3303) Resolves https://github.com/go-vikunja/frontend/issues/109 Vikunja would save the api url with `http` instead of `https` when the frontend was accessed via https. This was fine in most cases when the server would redirect all requests made to http to the secure https variant. However, in newer Firefox versions (and soon, Chrome probably as well) the browser would not follow that redirect anymore. Hence, we need to make sure to only make api requests to the same protocol. Doing API requests from an https hosted fronted to an http hosted api would probably fail already anyway. Co-authored-by: kolaente Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/3303 Reviewed-by: Dominik Pschenitschni Co-authored-by: konrad Co-committed-by: konrad --- src/helpers/checkAndSetApiUrl.ts | 45 +++++--------------------------- 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/src/helpers/checkAndSetApiUrl.ts b/src/helpers/checkAndSetApiUrl.ts index 0dc4595b6..f60de7fe7 100644 --- a/src/helpers/checkAndSetApiUrl.ts +++ b/src/helpers/checkAndSetApiUrl.ts @@ -6,16 +6,16 @@ export const ERROR_NO_API_URL = 'noApiUrlProvided' export const checkAndSetApiUrl = (url: string): Promise => { - if(url.startsWith('/')) { + if (url.startsWith('/')) { url = window.location.host + url } - - // Check if the url has an http prefix + + // Check if the url has a http prefix if ( !url.startsWith('http://') && !url.startsWith('https://') ) { - url = `http://${url}` + url = `${window.location.protocol}//${url}` } const urlToCheck: URL = new URL(url) @@ -41,15 +41,6 @@ export const checkAndSetApiUrl = (url: string): Promise => { } throw e }) - .catch(e => { - // Check if it has a port and if not check if it is reachable at https - if (urlToCheck.protocol === 'http:') { - urlToCheck.protocol = 'https:' - window.API_URL = urlToCheck.toString() - return updateConfig() - } - throw e - }) .catch(e => { // Check if it is reachable at /api/v1 and https urlToCheck.pathname = origUrlToCheck.pathname @@ -66,7 +57,6 @@ export const checkAndSetApiUrl = (url: string): Promise => { .catch(e => { // Check if it is reachable at port API_DEFAULT_PORT and https if (urlToCheck.port !== API_DEFAULT_PORT) { - urlToCheck.protocol = 'https:' urlToCheck.port = API_DEFAULT_PORT window.API_URL = urlToCheck.toString() return updateConfig() @@ -74,30 +64,7 @@ export const checkAndSetApiUrl = (url: string): Promise => { throw e }) .catch(e => { - // Check if it is reachable at :API_DEFAULT_PORT and /api/v1 and https - urlToCheck.pathname = origUrlToCheck.pathname - if ( - !urlToCheck.pathname.endsWith('/api/v1') && - !urlToCheck.pathname.endsWith('/api/v1/') - ) { - urlToCheck.pathname = `${urlToCheck.pathname}api/v1` - window.API_URL = urlToCheck.toString() - return updateConfig() - } - throw e - }) - .catch(e => { - // Check if it is reachable at port API_DEFAULT_PORT and http - if (urlToCheck.port !== API_DEFAULT_PORT) { - urlToCheck.protocol = 'http:' - urlToCheck.port = API_DEFAULT_PORT - window.API_URL = urlToCheck.toString() - return updateConfig() - } - throw e - }) - .catch(e => { - // Check if it is reachable at :API_DEFAULT_PORT and /api/v1 and http + // Check if it is reachable at :API_DEFAULT_PORT and /api/v1 urlToCheck.pathname = origUrlToCheck.pathname if ( !urlToCheck.pathname.endsWith('/api/v1') && @@ -118,7 +85,7 @@ export const checkAndSetApiUrl = (url: string): Promise => { localStorage.setItem('API_URL', window.API_URL) return window.API_URL } - + throw new Error(ERROR_NO_API_URL) }) }