Skip to content

Commit

Permalink
validate some function
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh committed Oct 3, 2023
1 parent f51aeec commit e266379
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/components/Announcement/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TIMES_IN_SECS } from 'constants/index'
import { useActiveWeb3React } from 'hooks'
import { useChangeNetwork } from 'hooks/web3/useChangeNetwork'
import { useAppDispatch } from 'state/hooks'
import { isValidRedirectURL } from 'utils/redirect'

const LsKey = 'ack-announcements'
export const getAnnouncementsAckMap = () => JSON.parse(localStorage[LsKey] || '{}')
Expand Down Expand Up @@ -63,6 +64,7 @@ export const useNavigateToUrl = () => {
return
}
const { pathname, host, search } = new URL(actionURL)
if (!isValidRedirectURL(actionURL, false)) return
if (window.location.host === host) {
navigate(`${pathname}${search}`)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Oauth/AuthForm/ButtonEth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Loader from 'components/Loader'
import { useActiveWeb3React } from 'hooks'
import useAutoSignIn from 'pages/Oauth/AuthForm/useAutoSignIn'
import { FlowStatus } from 'pages/Oauth/Login'
import { isValidRedirectURL } from 'pages/Oauth/helpers'
import { useWalletModalToggle } from 'state/application/hooks'
import { ExternalLink } from 'theme'
import { isValidRedirectURL } from 'utils/redirect'

const ButtonEth = ({
loading,
Expand Down
18 changes: 0 additions & 18 deletions src/pages/Oauth/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@ export const getSupportLoginMethods = (loginFlow: LoginFlow | undefined) => {
return loginFlow?.oauth_client?.metadata?.allowed_login_methods ?? []
}

const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\.kyberengineering\.io$/]
export const isValidRedirectURL = (url: string | undefined, checkWhitelist = true) => {
try {
if (!url) return false
const newUrl = new URL(url) // valid url
if (
url.endsWith('.js') ||
newUrl.pathname.endsWith('.js') ||
(checkWhitelist && !whiteListDomains.some(regex => newUrl.origin.match(regex)))
) {
return false
}
return newUrl.protocol === 'http:' || newUrl.protocol === 'https:'
} catch (error) {
return false
}
}

type MessageParams = {
domain: string
uri: string
Expand Down
2 changes: 1 addition & 1 deletion src/theme/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ArrowLeft, ExternalLink as LinkIconFeather, X } from 'react-feather'
import { Link } from 'react-router-dom'
import styled, { css, keyframes } from 'styled-components'

import { isValidRedirectURL } from 'pages/Oauth/helpers'
import { isValidRedirectURL } from 'utils/redirect'

export const ButtonText = styled.button<{ color?: string; gap?: string }>`
outline: none;
Expand Down
17 changes: 17 additions & 0 deletions src/utils/redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const whiteListDomains = [/https:\/\/(.+?\.)?kyberswap\.com$/, /https:\/\/(.+)\.kyberengineering\.io$/]
export const isValidRedirectURL = (url: string | undefined, checkWhitelist = true) => {
try {
if (!url) return false
const newUrl = new URL(url) // valid url
if (
url.endsWith('.js') ||
newUrl.pathname.endsWith('.js') ||
(checkWhitelist && !whiteListDomains.some(regex => newUrl.origin.match(regex)))
) {
return false
}
return newUrl.protocol === 'http:' || newUrl.protocol === 'https:'
} catch (error) {
return false
}
}

0 comments on commit e266379

Please sign in to comment.