From 543e54a7a5e4dc4680334ee933b3bf1827320584 Mon Sep 17 00:00:00 2001 From: Danh Date: Sat, 14 Oct 2023 16:19:29 +0700 Subject: [PATCH 1/7] fix: allow external link with url: /pathname --- .../Header/web3/WalletModal/index.tsx | 9 +++++---- src/components/Menu/index.tsx | 8 ++++---- src/pages/KyberDAO/KNCUtility/index.tsx | 13 ++++++------ src/theme/components.tsx | 20 +++++++++++++++---- src/utils/redirect.ts | 14 +++++++++---- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/components/Header/web3/WalletModal/index.tsx b/src/components/Header/web3/WalletModal/index.tsx index 67e7834b65..0986a50436 100644 --- a/src/components/Header/web3/WalletModal/index.tsx +++ b/src/components/Header/web3/WalletModal/index.tsx @@ -32,6 +32,7 @@ import { useWalletModalToggle, } from 'state/application/hooks' import { useIsConnectingWallet } from 'state/authen/hooks' +import { ExternalLink } from 'theme' import { isEVMWallet, isOverriddenWallet, isSolanaWallet } from 'utils' import Option from './Option' @@ -316,13 +317,13 @@ export default function WalletModal() { By connecting a wallet, you accept{' '} - e.stopPropagation()}> + e.stopPropagation()}> KyberSwap‘s Terms of Use - {' '} + {' '} and consent to its{' '} - e.stopPropagation()}> + e.stopPropagation()}> Privacy Policy - + . Last updated: {dayjs(TERM_FILES_PATH.VERSION).format('DD MMM YYYY')} diff --git a/src/components/Menu/index.tsx b/src/components/Menu/index.tsx index 87115a075a..44796e4579 100644 --- a/src/components/Menu/index.tsx +++ b/src/components/Menu/index.tsx @@ -473,7 +473,7 @@ export default function Menu() { )} - { toggle() @@ -482,10 +482,10 @@ export default function Menu() { > Terms - + - { toggle() @@ -494,7 +494,7 @@ export default function Menu() { > Privacy Policy - + These Terms and Conditions should be read in conjunction with the KyberSwap{' '} - Terms of Use, which lay out the terms and conditions - that apply to all KyberSwap activities. + Terms of Use, which lay out + the terms and conditions that apply to all KyberSwap activities. @@ -290,7 +290,7 @@ export default function KNCUtility() { By visiting KyberSwap and participating in the program, the User is deemed to have read, understood, and agreed to these Terms and Conditions and the KyberSwap{' '} - Terms of Use. + Terms of Use. @@ -308,9 +308,10 @@ export default function KNCUtility() { KyberSwap maintains the right, at its sole discretion, to take action or remove rewards against - the User who violates the KyberSwap Terms of Use{' '} - and/or violates, cheats, or exploits the program, including but not limited to, any suspicious - activities, or any attempts to circumvent these Terms and Conditions. + the User who violates the KyberSwap{' '} + Terms of Use and/or violates, + cheats, or exploits the program, including but not limited to, any suspicious activities, or any + attempts to circumvent these Terms and Conditions. diff --git a/src/theme/components.tsx b/src/theme/components.tsx index 5fb445e175..d00c4dcb9e 100644 --- a/src/theme/components.tsx +++ b/src/theme/components.tsx @@ -193,7 +193,7 @@ export function ExternalLink({ }: Omit, 'as' | 'ref'> & { href: string }) { const handleClick = useCallback( (event: React.MouseEvent) => { - onClick && onClick(event) + onClick?.(event) // don't prevent default, don't redirect if it's a new tab if (target === '_blank' || event.ctrlKey || event.metaKey) { } else { @@ -203,7 +203,13 @@ export function ExternalLink({ [target, onClick], ) return ( - + ) } @@ -221,13 +227,19 @@ export function ExternalLinkIcon({ console.debug('Fired outbound link event', href) } else { event.preventDefault() - navigateToUrl(href, false) + navigateToUrl(href, { whitelistKyberSwap: false, allowPath: true }) } }, [href, target], ) return ( - + ) diff --git a/src/utils/redirect.ts b/src/utils/redirect.ts index 4dec7f623a..f41e6c1fa4 100644 --- a/src/utils/redirect.ts +++ b/src/utils/redirect.ts @@ -6,9 +6,15 @@ import { useActiveWeb3React } from 'hooks' import { useChangeNetwork } from 'hooks/web3/useChangeNetwork' const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\.kyberengineering\.io$/] -export const validateRedirectURL = (url: string | undefined, whitelistKyberSwap = true) => { + +type Options = { whitelistKyberSwap?: boolean; allowPath?: boolean } +export const validateRedirectURL = ( + url: string | undefined, + { whitelistKyberSwap = true, allowPath = false }: Options = {}, +) => { try { if (!url) throw new Error() + if (allowPath && url.startsWith('/')) return url const newUrl = new URL(url) // valid url if ( url.endsWith('.js') || @@ -24,8 +30,8 @@ export const validateRedirectURL = (url: string | undefined, whitelistKyberSwap } } -export const navigateToUrl = (url: string | undefined, whitelistKyberSwap = true) => { - const urlFormatted = validateRedirectURL(url, whitelistKyberSwap) +export const navigateToUrl = (url: string | undefined, options?: Options) => { + const urlFormatted = validateRedirectURL(url, options) if (urlFormatted) window.location.href = urlFormatted } @@ -46,7 +52,7 @@ export const useNavigateToUrl = () => { return } const { pathname, host, search } = new URL(actionURL) - if (!validateRedirectURL(actionURL, false)) return + if (!validateRedirectURL(actionURL, { whitelistKyberSwap: false })) return if (window.location.host === host) { navigate(`${pathname}${search}`) } else { From c8772ef679d380673d782cdc68658f25538a42e3 Mon Sep 17 00:00:00 2001 From: Danh Date: Mon, 16 Oct 2023 10:23:50 +0700 Subject: [PATCH 2/7] fix: layout tab LO break --- src/pages/SwapV3/Tabs/LimitTab.tsx | 35 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/pages/SwapV3/Tabs/LimitTab.tsx b/src/pages/SwapV3/Tabs/LimitTab.tsx index c46bd4b04c..59fe4c6cce 100644 --- a/src/pages/SwapV3/Tabs/LimitTab.tsx +++ b/src/pages/SwapV3/Tabs/LimitTab.tsx @@ -1,7 +1,6 @@ import { Trans } from '@lingui/macro' import { rgba } from 'polished' import { useLocation } from 'react-router-dom' -import { Text } from 'rebass' import { useGetNumberOfInsufficientFundOrdersQuery } from 'services/limitOrder' import styled from 'styled-components' @@ -45,22 +44,24 @@ export default function LimitTab({ onClick }: Props) { } return ( - - - Limit{' '} - {numberOfInsufficientFundOrders ? ( - - You have {numberOfInsufficientFundOrders} active orders that don't have sufficient funds - - } - > - {numberOfInsufficientFundOrders} - - ) : null} - + + Limit{' '} + {!!numberOfInsufficientFundOrders && ( + You have {numberOfInsufficientFundOrders} active orders that don't have sufficient funds + } + > + {numberOfInsufficientFundOrders} + + )} ) } From 76db5494d45ca5014efb39f6167be1d2270f6763 Mon Sep 17 00:00:00 2001 From: Danh Date: Mon, 16 Oct 2023 10:49:09 +0700 Subject: [PATCH 3/7] validate js first --- src/theme/components.tsx | 6 +++--- src/utils/redirect.ts | 9 ++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/theme/components.tsx b/src/theme/components.tsx index d00c4dcb9e..963a5df9f5 100644 --- a/src/theme/components.tsx +++ b/src/theme/components.tsx @@ -206,7 +206,7 @@ export function ExternalLink({ @@ -227,7 +227,7 @@ export function ExternalLinkIcon({ console.debug('Fired outbound link event', href) } else { event.preventDefault() - navigateToUrl(href, { whitelistKyberSwap: false, allowPath: true }) + navigateToUrl(href, { whitelistKyberSwap: false, allowRelativePath: true }) } }, [href, target], @@ -236,7 +236,7 @@ export function ExternalLinkIcon({ diff --git a/src/utils/redirect.ts b/src/utils/redirect.ts index f41e6c1fa4..dbbf169fd6 100644 --- a/src/utils/redirect.ts +++ b/src/utils/redirect.ts @@ -7,17 +7,16 @@ import { useChangeNetwork } from 'hooks/web3/useChangeNetwork' const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\.kyberengineering\.io$/] -type Options = { whitelistKyberSwap?: boolean; allowPath?: boolean } +type Options = { whitelistKyberSwap?: boolean; allowRelativePath?: boolean } export const validateRedirectURL = ( url: string | undefined, - { whitelistKyberSwap = true, allowPath = false }: Options = {}, + { whitelistKyberSwap = true, allowRelativePath = false }: Options = {}, ) => { try { - if (!url) throw new Error() - if (allowPath && url.startsWith('/')) return url + if (!url || url.endsWith('.js')) throw new Error() + if (allowRelativePath && url.startsWith('/')) return url const newUrl = new URL(url) // valid url if ( - url.endsWith('.js') || newUrl.pathname.endsWith('.js') || !['https:', 'http:'].includes(newUrl.protocol) || (whitelistKyberSwap && !whiteListDomains.some(regex => newUrl.origin.match(regex))) From 86346f88b486c68d22870f9f9f1025d73dfe8ac8 Mon Sep 17 00:00:00 2001 From: Danh Date: Mon, 16 Oct 2023 11:08:51 +0700 Subject: [PATCH 4/7] update validate --- src/utils/redirect.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/redirect.ts b/src/utils/redirect.ts index dbbf169fd6..905efea415 100644 --- a/src/utils/redirect.ts +++ b/src/utils/redirect.ts @@ -14,8 +14,7 @@ export const validateRedirectURL = ( ) => { try { if (!url || url.endsWith('.js')) throw new Error() - if (allowRelativePath && url.startsWith('/')) return url - const newUrl = new URL(url) // valid url + const newUrl = allowRelativePath && url.startsWith('/') ? new URL(`${window.location.origin}${url}`) : new URL(url) if ( newUrl.pathname.endsWith('.js') || !['https:', 'http:'].includes(newUrl.protocol) || From c9532bea8e60d462ecdfd82f58240b5ab7274f5f Mon Sep 17 00:00:00 2001 From: Danh Date: Mon, 16 Oct 2023 13:36:44 +0700 Subject: [PATCH 5/7] rename var rename var --- src/theme/components.tsx | 6 +++--- src/utils/redirect.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/theme/components.tsx b/src/theme/components.tsx index 963a5df9f5..fdea5c1bd6 100644 --- a/src/theme/components.tsx +++ b/src/theme/components.tsx @@ -206,7 +206,7 @@ export function ExternalLink({ @@ -227,7 +227,7 @@ export function ExternalLinkIcon({ console.debug('Fired outbound link event', href) } else { event.preventDefault() - navigateToUrl(href, { whitelistKyberSwap: false, allowRelativePath: true }) + navigateToUrl(href, { _dangerousCheckWhitelist: false, allowRelativePath: true }) } }, [href, target], @@ -236,7 +236,7 @@ export function ExternalLinkIcon({ diff --git a/src/utils/redirect.ts b/src/utils/redirect.ts index 905efea415..f7bf6d5dab 100644 --- a/src/utils/redirect.ts +++ b/src/utils/redirect.ts @@ -7,10 +7,10 @@ import { useChangeNetwork } from 'hooks/web3/useChangeNetwork' const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\.kyberengineering\.io$/] -type Options = { whitelistKyberSwap?: boolean; allowRelativePath?: boolean } +type Options = { _dangerousCheckWhitelist?: boolean; allowRelativePath?: boolean } export const validateRedirectURL = ( url: string | undefined, - { whitelistKyberSwap = true, allowRelativePath = false }: Options = {}, + { _dangerousCheckWhitelist = true, allowRelativePath = false }: Options = {}, ) => { try { if (!url || url.endsWith('.js')) throw new Error() @@ -18,7 +18,7 @@ export const validateRedirectURL = ( if ( newUrl.pathname.endsWith('.js') || !['https:', 'http:'].includes(newUrl.protocol) || - (whitelistKyberSwap && !whiteListDomains.some(regex => newUrl.origin.match(regex))) + (_dangerousCheckWhitelist && !whiteListDomains.some(regex => newUrl.origin.match(regex))) ) { throw new Error() } @@ -50,7 +50,7 @@ export const useNavigateToUrl = () => { return } const { pathname, host, search } = new URL(actionURL) - if (!validateRedirectURL(actionURL, { whitelistKyberSwap: false })) return + if (!validateRedirectURL(actionURL, { _dangerousCheckWhitelist: false })) return if (window.location.host === host) { navigate(`${pathname}${search}`) } else { From 276bcddeb096854d67a5aa1fbe4ba645a80f8676 Mon Sep 17 00:00:00 2001 From: Danh Date: Mon, 16 Oct 2023 13:44:30 +0700 Subject: [PATCH 6/7] revert var --- src/theme/components.tsx | 6 +++--- src/utils/redirect.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/theme/components.tsx b/src/theme/components.tsx index fdea5c1bd6..7afcfc381a 100644 --- a/src/theme/components.tsx +++ b/src/theme/components.tsx @@ -206,7 +206,7 @@ export function ExternalLink({ @@ -227,7 +227,7 @@ export function ExternalLinkIcon({ console.debug('Fired outbound link event', href) } else { event.preventDefault() - navigateToUrl(href, { _dangerousCheckWhitelist: false, allowRelativePath: true }) + navigateToUrl(href, { _dangerousSkipCheckWhitelist: true, allowRelativePath: true }) } }, [href, target], @@ -236,7 +236,7 @@ export function ExternalLinkIcon({ diff --git a/src/utils/redirect.ts b/src/utils/redirect.ts index f7bf6d5dab..d0f048fe3b 100644 --- a/src/utils/redirect.ts +++ b/src/utils/redirect.ts @@ -7,10 +7,10 @@ import { useChangeNetwork } from 'hooks/web3/useChangeNetwork' const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\.kyberengineering\.io$/] -type Options = { _dangerousCheckWhitelist?: boolean; allowRelativePath?: boolean } +type Options = { _dangerousSkipCheckWhitelist?: boolean; allowRelativePath?: boolean } export const validateRedirectURL = ( url: string | undefined, - { _dangerousCheckWhitelist = true, allowRelativePath = false }: Options = {}, + { _dangerousSkipCheckWhitelist = true, allowRelativePath = false }: Options = {}, ) => { try { if (!url || url.endsWith('.js')) throw new Error() @@ -18,7 +18,7 @@ export const validateRedirectURL = ( if ( newUrl.pathname.endsWith('.js') || !['https:', 'http:'].includes(newUrl.protocol) || - (_dangerousCheckWhitelist && !whiteListDomains.some(regex => newUrl.origin.match(regex))) + (!_dangerousSkipCheckWhitelist && !whiteListDomains.some(regex => newUrl.origin.match(regex))) ) { throw new Error() } @@ -50,7 +50,7 @@ export const useNavigateToUrl = () => { return } const { pathname, host, search } = new URL(actionURL) - if (!validateRedirectURL(actionURL, { _dangerousCheckWhitelist: false })) return + if (!validateRedirectURL(actionURL, { _dangerousSkipCheckWhitelist: true })) return if (window.location.host === host) { navigate(`${pathname}${search}`) } else { From b84dec2d506ddf6e80ac5f24fb7c06007dfa6eaa Mon Sep 17 00:00:00 2001 From: Danh Date: Mon, 16 Oct 2023 13:46:34 +0700 Subject: [PATCH 7/7] default value --- src/utils/redirect.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/redirect.ts b/src/utils/redirect.ts index d0f048fe3b..d472a54b66 100644 --- a/src/utils/redirect.ts +++ b/src/utils/redirect.ts @@ -10,7 +10,7 @@ const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\. type Options = { _dangerousSkipCheckWhitelist?: boolean; allowRelativePath?: boolean } export const validateRedirectURL = ( url: string | undefined, - { _dangerousSkipCheckWhitelist = true, allowRelativePath = false }: Options = {}, + { _dangerousSkipCheckWhitelist = false, allowRelativePath = false }: Options = {}, ) => { try { if (!url || url.endsWith('.js')) throw new Error()