From eed0934c0a4b9684621b940cff7e4eed1249313f Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sat, 27 Nov 2021 17:41:00 +0100 Subject: [PATCH] Safe Link: Fix opening links with no protocol (#1556) --- src/components/common/SafeLink.tsx | 9 +-------- src/components/main/SafeLinkModal.tsx | 3 ++- src/components/middle/composer/TextFormatter.tsx | 3 ++- src/util/ensureProtocol.ts | 9 +++++++++ 4 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 src/util/ensureProtocol.ts diff --git a/src/components/common/SafeLink.tsx b/src/components/common/SafeLink.tsx index dcf587ee..8cd36bcc 100644 --- a/src/components/common/SafeLink.tsx +++ b/src/components/common/SafeLink.tsx @@ -6,6 +6,7 @@ import { DEBUG, RE_TG_LINK, RE_TME_LINK, } from '../../config'; import buildClassName from '../../util/buildClassName'; +import { ensureProtocol } from '../../util/ensureProtocol'; type OwnProps = { url?: string; @@ -72,14 +73,6 @@ const SafeLink: FC = ({ ); }; -function ensureProtocol(url?: string) { - if (!url) { - return undefined; - } - - return url.includes('://') ? url : `https://${url}`; -} - function getDomain(url?: string) { if (!url) { return undefined; diff --git a/src/components/main/SafeLinkModal.tsx b/src/components/main/SafeLinkModal.tsx index d9b27029..0e7094f9 100644 --- a/src/components/main/SafeLinkModal.tsx +++ b/src/components/main/SafeLinkModal.tsx @@ -4,6 +4,7 @@ import { withGlobal } from '../../lib/teact/teactn'; import { GlobalActions } from '../../global/types'; import { pick } from '../../util/iteratees'; +import { ensureProtocol } from '../../util/ensureProtocol'; import renderText from '../common/helpers/renderText'; import useLang from '../../hooks/useLang'; import useCurrentOrPrev from '../../hooks/useCurrentOrPrev'; @@ -20,7 +21,7 @@ const SafeLinkModal: FC = ({ url, toggleSafeLinkModal const lang = useLang(); const handleOpen = useCallback(() => { - window.open(url); + window.open(ensureProtocol(url)); toggleSafeLinkModal({ url: undefined }); }, [toggleSafeLinkModal, url]); diff --git a/src/components/middle/composer/TextFormatter.tsx b/src/components/middle/composer/TextFormatter.tsx index 6a99efe1..5366ef04 100644 --- a/src/components/middle/composer/TextFormatter.tsx +++ b/src/components/middle/composer/TextFormatter.tsx @@ -6,6 +6,7 @@ import { IAnchorPosition } from '../../../types'; import { EDITABLE_INPUT_ID } from '../../../config'; import buildClassName from '../../../util/buildClassName'; +import { ensureProtocol } from '../../../util/ensureProtocol'; import captureEscKeyListener from '../../../util/captureEscKeyListener'; import useShowTransition from '../../../hooks/useShowTransition'; import useVirtualBackdrop from '../../../hooks/useVirtualBackdrop'; @@ -280,7 +281,7 @@ const TextFormatter: FC = ({ ]); function handleLinkUrlConfirm() { - const formattedLinkUrl = encodeURI(linkUrl.includes('://') ? linkUrl : `http://${linkUrl}`); + const formattedLinkUrl = encodeURI(ensureProtocol(linkUrl) || ''); if (isEditingLink) { const element = getSelectedElement(); diff --git a/src/util/ensureProtocol.ts b/src/util/ensureProtocol.ts new file mode 100644 index 00000000..412ad9fc --- /dev/null +++ b/src/util/ensureProtocol.ts @@ -0,0 +1,9 @@ +export function ensureProtocol(url?: string) { + if (!url) { + return undefined; + } + + // HTTP was chosen by default as a fix for https://bugs.telegram.org/c/10712. + // It is also the default protocol in the official TDesktop client. + return url.includes('://') ? url : `http://${url}`; +}