From 0ad05e3e089804fa316495eec0ba00eeaca69b36 Mon Sep 17 00:00:00 2001 From: "codefix-patchflow[bot]" <292349666+codefix-patchflow[bot]@users.noreply.github.com> Date: Fri, 19 Jun 2026 11:15:40 +0000 Subject: [PATCH] fix: allow hostname ports without protocol --- src/lib/isURL.js | 21 +++++++++++++++++++-- test/validators.test.js | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lib/isURL.js b/src/lib/isURL.js index 8ae971ed6..99d0105c7 100644 --- a/src/lib/isURL.js +++ b/src/lib/isURL.js @@ -103,6 +103,23 @@ export default function isURL(url, options) { return url.substring(protocol_match[0].length); }; + const isHostWithPort = (potential_host, potential_port) => { + if (options.require_protocol || !/^[0-9]+$/.test(potential_port)) { + return false; + } + + const port_number = parseInt(potential_port, 10); + if (port_number <= 0 || port_number > 65535) { + return false; + } + + if (isIP(potential_host) || isFQDN(potential_host, options)) { + return true; + } + + return options.host_whitelist ? checkHost(potential_host, options.host_whitelist) : false; + }; + if (protocol_match) { const potential_protocol = protocol_match[1]; const after_colon = url.substring(protocol_match[0].length); @@ -141,8 +158,8 @@ export default function isURL(url, options) { return false; } } - } else { - // No @ symbol, this is definitely a protocol + } else if (!isHostWithPort(potential_protocol, before_slash)) { + // No @ symbol and not a hostname with a numeric port, so treat it as a protocol. url = cleanUpProtocol(potential_protocol); if (url === false) { diff --git a/test/validators.test.js b/test/validators.test.js index 5196c6fe9..9db1f1745 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -499,6 +499,7 @@ describe('Validators', () => { 'localhost', 'localhost:3000', 'service-name:8080', + 'my-3272-service:9000', 'https://localhost', 'http://localhost:3000', 'http://service-name:8080',