A Node.js utility to check URL validity, availability, and potential risks.
- Validates URL format
- Checks URL availability (HTTP response)
- Detects potential security risks:
- Suspicious protocols (javascript:, data:, file:)
- IP addresses instead of domain names
- Phishing indicators
- Suspicious TLDs
- Supports both single URL and array of URLs
- Returns Boolean for single URL
- Returns Object/Dictionary for array of URLs
npm installCheck a single URL and get a boolean result:
const { checkUrl } = require('./index');
// Check a single URL (with availability check)
const result = await checkUrl('https://www.google.com');
console.log(result); // true if safe and available, false otherwise
// Check a single URL (without availability check)
const result2 = await checkUrl('https://www.google.com', { checkAvailability: false });
console.log(result2); // true if valid and safe, false otherwiseCheck multiple URLs and get an object with results:
const { checkUrls } = require('./index');
// Check multiple URLs
const urls = [
'https://www.google.com',
'https://www.github.com',
'invalid-url',
'javascript:alert(1)'
];
// With availability check (default)
const results = await checkUrls(urls);
// Without availability check
const results2 = await checkUrls(urls, { checkAvailability: false });
console.log(results);
// {
// 'https://www.google.com': true,
// 'https://www.github.com': true,
// 'invalid-url': false,
// 'javascript:alert(1)': false
// }Checks a single URL for validity, availability, and potential risks.
Parameters:
urlString(string): The URL to checkoptions(Object, optional): Configuration optionscheckAvailability(boolean): Whether to check URL availability via HTTP request (default: true)
Returns:
Promise<boolean>: Returnstrueif the URL is safe and available,falseotherwise
Checks multiple URLs for validity, availability, and potential risks.
Parameters:
urls(string[]): Array of URLs to checkoptions(Object, optional): Configuration optionscheckAvailability(boolean): Whether to check URL availability via HTTP request (default: true)
Returns:
Promise<Object>: Returns an object where keys are URLs and values are booleans indicating safety
Run the tests:
npm testThe URL checker performs the following checks:
- Validity Check: Validates the URL format and ensures it uses http or https protocol
- Risk Detection: Checks for suspicious patterns including:
- Dangerous protocols (javascript:, data:, file:)
- IP addresses instead of domain names
- Phishing indicators (@ symbol)
- Suspicious keywords (exec, eval)
- Suspicious TLDs (.tk, .ml, .ga, .cf, .gq, .zip, .mov)
- Availability Check: Sends an HTTP HEAD request to verify the URL responds
If all checks pass, the URL is considered safe and returns true. If any check fails, it returns false.
const { checkUrl, checkUrls } = require('./index');
async function main() {
// Check single URL
const isSafe = await checkUrl('https://www.example.com');
console.log(`Is https://www.example.com safe? ${isSafe}`);
// Check multiple URLs
const results = await checkUrls([
'https://www.google.com',
'https://malicious-site.tk',
'javascript:void(0)'
]);
for (const [url, isSafe] of Object.entries(results)) {
console.log(`${url}: ${isSafe ? 'Safe ✓' : 'Unsafe ✗'}`);
}
}
main();ISC