From 534856e6736ce0d6c284e6097d230233dfa4bedc Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Sat, 18 Mar 2023 12:31:04 -0300 Subject: [PATCH] [FIX] Links do not work if protocol is not set in url prefix (#4899) * replace double slashs * minor tweak * add unit test to addProtocol * minor tweak * ensureSecureProcotol --------- Co-authored-by: gitstart Co-authored-by: Debojyoti Singha <20729878+debojyoti452@users.noreply.github.com> Co-authored-by: Gleidson Daniel Silva --- .../helpers/ensureSecureProtocol.test.ts | 20 +++++++++++++++++++ .../methods/helpers/ensureSecureProtocol.ts | 10 ++++++++++ app/lib/methods/helpers/openLink.ts | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/lib/methods/helpers/ensureSecureProtocol.test.ts create mode 100644 app/lib/methods/helpers/ensureSecureProtocol.ts diff --git a/app/lib/methods/helpers/ensureSecureProtocol.test.ts b/app/lib/methods/helpers/ensureSecureProtocol.test.ts new file mode 100644 index 000000000..6650221eb --- /dev/null +++ b/app/lib/methods/helpers/ensureSecureProtocol.test.ts @@ -0,0 +1,20 @@ +import ensureSecureProtocol from './ensureSecureProtocol'; + +describe('Add the protocol https at the begin of the URL', () => { + it('return the link as original when sent with https at the begin', () => { + const linkHttps = 'https://www.google.com'; + expect(ensureSecureProtocol(linkHttps)).toBe(linkHttps); + }); + it('return the link as original when sent with http at the begin', () => { + const linkHttp = 'http://www.google.com'; + expect(ensureSecureProtocol(linkHttp)).toBe(linkHttp); + }); + it("return a new link with protocol at the begin when there isn't the protocol at the begin", () => { + const linkWithoutProtocol = 'www.google.com'; + expect(ensureSecureProtocol(linkWithoutProtocol)).toBe('https://www.google.com'); + }); + it('return the link correctly when the original starts with double slash, because the server is returning that', () => { + const linkWithDoubleSlashAtBegin = '//www.google.com'; + expect(ensureSecureProtocol(linkWithDoubleSlashAtBegin)).toBe('https://www.google.com'); + }); +}); diff --git a/app/lib/methods/helpers/ensureSecureProtocol.ts b/app/lib/methods/helpers/ensureSecureProtocol.ts new file mode 100644 index 000000000..1acfef6dd --- /dev/null +++ b/app/lib/methods/helpers/ensureSecureProtocol.ts @@ -0,0 +1,10 @@ +// If the link does not have the protocol at the beginning, we are inserting https as the default, +// since by convention the most used is the secure protocol, with the same behavior as the web. +const ensureSecureProtocol = (url: string): string => { + if (!url.toLowerCase().startsWith('http')) { + return `https://${url.replace('//', '')}`; + } + return url; +}; + +export default ensureSecureProtocol; diff --git a/app/lib/methods/helpers/openLink.ts b/app/lib/methods/helpers/openLink.ts index e7fc9a561..45e3df5d6 100644 --- a/app/lib/methods/helpers/openLink.ts +++ b/app/lib/methods/helpers/openLink.ts @@ -5,6 +5,7 @@ import parse from 'url-parse'; import { themes } from '../../constants'; import { TSupportedThemes } from '../../../theme'; import UserPreferences from '../userPreferences'; +import ensureSecureProtocol from './ensureSecureProtocol'; export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY'; @@ -37,9 +38,9 @@ const appSchemeURL = (url: string, browser: string): string => { }; const openLink = async (url: string, theme: TSupportedThemes = 'light'): Promise => { + url = ensureSecureProtocol(url); try { const browser = UserPreferences.getString(DEFAULT_BROWSER_KEY); - if (browser === 'inApp') { await WebBrowser.openBrowserAsync(url, { toolbarColor: themes[theme].headerBackground,