diff --git a/app/util/browser.js b/app/util/browser.js index eef950409c2..861d736e7f3 100644 --- a/app/util/browser.js +++ b/app/util/browser.js @@ -1,4 +1,5 @@ import URL from 'url-parse'; +import isUrl from 'is-url'; /** * Returns a sanitized url, which could be a search engine url if @@ -11,8 +12,8 @@ import URL from 'url-parse'; */ export default function onUrlSubmit(input, searchEngine = 'Google', defaultProtocol = 'https://') { //Check if it's a url or a keyword - const res = input.match(/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!&',;=.+]+$/g); - if (res === null) { + const regEx = new RegExp(/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!&',;=.+]+$/g); + if (!isUrl(input) && !regEx.test(input)) { // Add exception for localhost if (!input.startsWith('http://localhost') && !input.startsWith('localhost')) { // In case of keywords we default to google search @@ -23,7 +24,7 @@ export default function onUrlSubmit(input, searchEngine = 'Google', defaultProto return searchUrl; } } - const hasProtocol = input.match(/^[a-z]*:\/\//); + const hasProtocol = /^[a-z]*:\/\//.test(input); const sanitizedURL = hasProtocol ? input : `${defaultProtocol}${input}`; return sanitizedURL; } diff --git a/package.json b/package.json index b996eaee948..e5e57aa4133 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,7 @@ "events": "3.0.0", "fuse.js": "3.4.4", "https-browserify": "0.0.1", + "is-url": "^1.2.4", "json-rpc-engine": "5.1.5", "json-rpc-middleware-stream": "2.1.1", "lottie-react-native": "git+https://github.com/MetaMask/lottie-react-native.git#7ce6a78ac4ac7b9891bc513cb3f12f8b9c9d9106", @@ -128,7 +129,7 @@ "react-native": "0.62.2", "react-native-actionsheet": "beefe/react-native-actionsheet#107/head", "react-native-aes-crypto": "^1.3.9", - "react-native-aes-crypto-forked": "git+https://github.com/MetaMask/react-native-aes-crypto-forked.git#397d5db5250e8e7408294807965b5b9fd4ca6a25", + "react-native-aes-crypto-forked": "git+https://github.com/MetaMask/react-native-aes-crypto-forked.git#397d5db5250e8e7408294807965b5b9fd4ca6a25", "react-native-animated-fox": "git+https://github.com/MetaMask/react-native-animated-fox.git#16e38d54d829709e497f196e31fa8ff00cdf2aa9", "react-native-background-timer": "2.1.1", "react-native-branch": "5.0.0-beta.1", diff --git a/yarn.lock b/yarn.lock index 8cb80320fe3..b801f34978b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6961,6 +6961,11 @@ is-typedarray@1.0.0, is-typedarray@^1.0.0, is-typedarray@~1.0.0: resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a" integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo= +is-url@^1.2.4: + version "1.2.4" + resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52" + integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww== + is-weakmap@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/is-weakmap/-/is-weakmap-2.0.1.tgz#5008b59bdc43b698201d18f62b37b2ca243e8cf2"