Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/lib/isURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading