From ba7d2a72279822c28c9895107227ef053dd4a30f Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Thu, 22 May 2025 13:54:31 +0300 Subject: [PATCH] HCK-11251: improved escaping logic to prevent last ip portion being confused with port --- reverse_engineering/helpers/escapeV6IPForURL.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/reverse_engineering/helpers/escapeV6IPForURL.js b/reverse_engineering/helpers/escapeV6IPForURL.js index 6ee75ef..91fc498 100644 --- a/reverse_engineering/helpers/escapeV6IPForURL.js +++ b/reverse_engineering/helpers/escapeV6IPForURL.js @@ -22,7 +22,7 @@ function escapeV6IpForURL({ host }) { * !ip.isV4Format(host) check required because isV6Format returns true for ipv4 address because of backward compatibility */ if (ip.isV6Format(host) && !ip.isV4Format(host)) { - return `[${host}]`; + return escapeHost(host); } const isUrlValid = isValidURL(host); @@ -42,7 +42,20 @@ function escapeV6IpForURL({ host }) { const port = separatedIpPortionsAndPort.at(-1); const escapedIpWithPort = `[${ipPortions.join(':')}]:${port}`; - return host.replace(unescapedIpWithPort, escapedIpWithPort); + const hostWithEscapedAllPortionsButTheLastOne = host.replace(unescapedIpWithPort, escapedIpWithPort); + if (isValidURL(hostWithEscapedAllPortionsButTheLastOne)) { + return hostWithEscapedAllPortionsButTheLastOne; + } + + return host.replace(unescapedIpWithPort, escapeHost(unescapedIpWithPort)); +} + +/** + * @param {string} host + * @returns {string} + */ +function escapeHost(host) { + return `[${host}]`; } /**