[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 <gitstart@users.noreply.github.com>
Co-authored-by: Debojyoti Singha <20729878+debojyoti452@users.noreply.github.com>
Co-authored-by: Gleidson Daniel Silva <gleidson10daniel@hotmail.com>
This commit is contained in:
Reinaldo Neto 2023-03-18 12:31:04 -03:00 committed by GitHub
parent 59a2c7c424
commit 3b41917d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View File

@ -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');
});
});

View File

@ -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;

View File

@ -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<void> => {
url = ensureSecureProtocol(url);
try {
const browser = UserPreferences.getString(DEFAULT_BROWSER_KEY);
if (browser === 'inApp') {
await WebBrowser.openBrowserAsync(url, {
toolbarColor: themes[theme].headerBackground,