Skip to content

Commit 8197843

Browse files
HCK-11251: improved escaping logic to prevent last ip portion being confused with port (#152)
1 parent 8dc2efa commit 8197843

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

reverse_engineering/helpers/escapeV6IPForURL.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function escapeV6IpForURL({ host }) {
2222
* !ip.isV4Format(host) check required because isV6Format returns true for ipv4 address because of backward compatibility
2323
*/
2424
if (ip.isV6Format(host) && !ip.isV4Format(host)) {
25-
return `[${host}]`;
25+
return escapeHost(host);
2626
}
2727

2828
const isUrlValid = isValidURL(host);
@@ -42,7 +42,20 @@ function escapeV6IpForURL({ host }) {
4242
const port = separatedIpPortionsAndPort.at(-1);
4343
const escapedIpWithPort = `[${ipPortions.join(':')}]:${port}`;
4444

45-
return host.replace(unescapedIpWithPort, escapedIpWithPort);
45+
const hostWithEscapedAllPortionsButTheLastOne = host.replace(unescapedIpWithPort, escapedIpWithPort);
46+
if (isValidURL(hostWithEscapedAllPortionsButTheLastOne)) {
47+
return hostWithEscapedAllPortionsButTheLastOne;
48+
}
49+
50+
return host.replace(unescapedIpWithPort, escapeHost(unescapedIpWithPort));
51+
}
52+
53+
/**
54+
* @param {string} host
55+
* @returns {string}
56+
*/
57+
function escapeHost(host) {
58+
return `[${host}]`;
4659
}
4760

4861
/**

0 commit comments

Comments
 (0)