From c4d85d486b244dd3338d2cf6691eadae3c12e93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Dan?= Date: Mon, 4 Sep 2023 19:43:01 +0200 Subject: [PATCH] fix: check if ip address when sanitizing urls --- src/utils.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index d62e2aa..f5219ff 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,7 +8,20 @@ export function shortenAddress( } export function sanitizeURLProtocol(protocol: 'ws' | 'http', endpoint: string) { - return location.protocol.startsWith('https') + return location.protocol.startsWith('https') && + endpoint.indexOf('localhost') === -1 && + !isStringIpAddress(endpoint) ? `${protocol}s://${endpoint}` : `${protocol}://${endpoint}` } + +function isStringIpAddress(value: string) { + if ( + /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test( + value + ) + ) { + return true + } + return false +}