2024-01-11 21:29:00 +01:00
|
|
|
const VALID_PROTOCOLS = ["https:", "http:"];
|
2024-01-19 19:49:40 +01:00
|
|
|
const INVALID_OCTETS = [192, 172, 10, 127];
|
|
|
|
|
|
|
|
function isInvalidIp({ hostname }) {
|
|
|
|
const IPRegex = new RegExp(
|
|
|
|
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi
|
|
|
|
);
|
|
|
|
if (!IPRegex.test(hostname)) return false;
|
|
|
|
const [octetOne, ..._rest] = hostname.split(".");
|
|
|
|
|
|
|
|
// If fails to validate to number - abort and return as invalid.
|
|
|
|
if (isNaN(Number(octetOne))) return true;
|
|
|
|
return INVALID_OCTETS.includes(Number(octetOne));
|
|
|
|
}
|
2024-01-11 21:29:00 +01:00
|
|
|
|
2023-12-15 00:14:56 +01:00
|
|
|
function validURL(url) {
|
|
|
|
try {
|
2024-01-11 21:29:00 +01:00
|
|
|
const destination = new URL(url);
|
|
|
|
if (!VALID_PROTOCOLS.includes(destination.protocol)) return false;
|
2024-01-19 19:49:40 +01:00
|
|
|
if (isInvalidIp(destination)) return false;
|
2023-12-15 00:14:56 +01:00
|
|
|
return true;
|
|
|
|
} catch {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
validURL,
|
|
|
|
};
|