From a24c037b322dbdc56cbaae3d3fe6f5fa1e819972 Mon Sep 17 00:00:00 2001 From: Vijay Aravamudhan Date: Sat, 7 May 2022 13:17:26 -0500 Subject: New fix for fixing URLs with '//' (#105) --- src/api/apiBase.ts | 7 ++++--- src/components/auth/ChangeServer.js | 5 ++++- src/helpers/url-helpers.ts | 14 ++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/api/apiBase.ts b/src/api/apiBase.ts index 7ed3616a8..c4acb5f68 100644 --- a/src/api/apiBase.ts +++ b/src/api/apiBase.ts @@ -9,6 +9,7 @@ import { LOCAL_SERVER, SERVER_NOT_LOADED, } from '../config'; +import { fixUrl } from '../helpers/url-helpers'; // Note: This cannot be used from the internal-server since we are not running within the context of a browser window const apiBase = (withVersion = true) => { @@ -28,13 +29,13 @@ const apiBase = (withVersion = true) => { }` : (window as any).ferdium.stores.settings.all.app.server; - return withVersion ? `${url}/${API_VERSION}` : url; + return fixUrl(withVersion ? `${url}/${API_VERSION}` : url); }; export default apiBase; export function termsBase() { - return (window as any).ferdium.stores.settings.all.app.server !== LIVE_FRANZ_API + return fixUrl((window as any).ferdium.stores.settings.all.app.server !== LIVE_FRANZ_API ? (window as any).ferdium.stores.settings.all.app.server - : DEV_API_FRANZ_WEBSITE; + : DEV_API_FRANZ_WEBSITE); } diff --git a/src/components/auth/ChangeServer.js b/src/components/auth/ChangeServer.js index 081f2d61f..aa4598928 100644 --- a/src/components/auth/ChangeServer.js +++ b/src/components/auth/ChangeServer.js @@ -121,7 +121,10 @@ class ChangeServer extends Component { {!this.defaultServers.includes(form.$('server').value) && ( this.submit(e)} + onChange={e => { + this.form.$('customServer').value = this.form.$('customServer').value.replace(/\/$/, ""); + this.submit(e) + }} field={form.$('customServer')} /> )} diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts index ef2805595..1eda325da 100644 --- a/src/helpers/url-helpers.ts +++ b/src/helpers/url-helpers.ts @@ -15,9 +15,6 @@ export function isValidExternalURL(url: string | URL) { } catch { return false; } - if (url.toString().endsWith('/')) { - return false; - } const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol); @@ -26,6 +23,10 @@ export function isValidExternalURL(url: string | URL) { return isAllowed; } +export function fixUrl(url: string | URL) { + return url.toString().replaceAll('//', '/').replace('http:/', 'http://').replace('https:/', 'https://').replace('file:/', 'file://'); +} + export function isValidFileUrl(path: string) { return path.startsWith('file') && existsSync(new URL(path)); } @@ -40,8 +41,9 @@ export function openExternalUrl( url: string | URL, skipValidityCheck: boolean = false, ) { - debug('Open url:', url, 'with skipValidityCheck:', skipValidityCheck); - if (skipValidityCheck || isValidExternalURL(url)) { - shell.openExternal(url.toString()); + const fixedUrl = fixUrl(url.toString()); + debug('Open url:', fixedUrl, 'with skipValidityCheck:', skipValidityCheck); + if (skipValidityCheck || isValidExternalURL(fixedUrl)) { + shell.openExternal(fixedUrl.toString()); } } -- cgit v1.2.3-54-g00ecf