From 2b99354db4b41419d17225f14a2159f263629d3c Mon Sep 17 00:00:00 2001 From: "codefix-patchflow[bot]" <292349666+codefix-patchflow[bot]@users.noreply.github.com> Date: Sat, 20 Jun 2026 07:59:55 +0000 Subject: [PATCH] fix: recognize ports on protocol-less hostnames --- src/lib/isURL.js | 8 +++++++- test/validators.test.js | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/isURL.js b/src/lib/isURL.js index 8ae971ed6..7c20cbffe 100644 --- a/src/lib/isURL.js +++ b/src/lib/isURL.js @@ -113,8 +113,14 @@ export default function isURL(url, options) { // 2. There's an `@` symbol before any `/` // 3. The part before `@` contains only valid auth characters (alphanumeric, -, _, ., %, :) const starts_with_slashes = after_colon.slice(0, 2) === '//'; + const is_port = /^[0-9]+(?:\/|$)/.test(after_colon); - if (!starts_with_slashes) { + if (!starts_with_slashes && is_port) { + // This is a hostname with a port, not a protocol (e.g., localhost:3000) + if (options.require_protocol) { + return false; + } + } else if (!starts_with_slashes) { const first_slash_position = after_colon.indexOf('/'); const before_slash = first_slash_position === -1 ? after_colon 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',