From 6403a4f717875dfebc21a28b413854eea23a3876 Mon Sep 17 00:00:00 2001 From: Nazar Kovtun Date: Thu, 22 May 2025 13:12:28 +0300 Subject: [PATCH] HCK-11251: improved escaping logic to prevent last ip portion being confused with port --- .../helpers/escapeV6IPForURL.js | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/reverse_engineering/helpers/escapeV6IPForURL.js b/reverse_engineering/helpers/escapeV6IPForURL.js index 6ee75ef..fae017d 100644 --- a/reverse_engineering/helpers/escapeV6IPForURL.js +++ b/reverse_engineering/helpers/escapeV6IPForURL.js @@ -11,9 +11,9 @@ const ip = require('ip'); * When the URL also contains a port number the notation is: https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/ * * @param {{ -* host: string -* }} param -* @returns {string} + * host: string + * }} param + * @returns {string} */ function escapeV6IpForURL({ host }) { /** @@ -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 escapedHost = host.replace(unescapedIpWithPort, escapedIpWithPort); + if (isValidURL(escapedHost)) { + return escapedHost; + } + + return host.replace(unescapedIpWithPort, escapeHost(unescapedIpWithPort)); +} + +/** + * @param {string} host + * @returns {string} + */ +function escapeHost(host) { + return `[${host}]`; } /**