Safe Link: Fix opening links with no protocol (#1556)

This commit is contained in:
Alexander Zinchuk 2021-11-27 17:41:00 +01:00
parent 50865c78f5
commit eed0934c0a
4 changed files with 14 additions and 10 deletions

View File

@ -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<OwnProps> = ({
);
};
function ensureProtocol(url?: string) {
if (!url) {
return undefined;
}
return url.includes('://') ? url : `https://${url}`;
}
function getDomain(url?: string) {
if (!url) {
return undefined;

View File

@ -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<OwnProps & DispatchProps> = ({ url, toggleSafeLinkModal
const lang = useLang();
const handleOpen = useCallback(() => {
window.open(url);
window.open(ensureProtocol(url));
toggleSafeLinkModal({ url: undefined });
}, [toggleSafeLinkModal, url]);

View File

@ -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<OwnProps> = ({
]);
function handleLinkUrlConfirm() {
const formattedLinkUrl = encodeURI(linkUrl.includes('://') ? linkUrl : `http://${linkUrl}`);
const formattedLinkUrl = encodeURI(ensureProtocol(linkUrl) || '');
if (isEditingLink) {
const element = getSelectedElement();

View File

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