fix: enhance IP addresses hostname validation

537f0559abf7f80e2d9a703f4a4d3a62485238bd

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +2 -1Ignore whitespace
src/endpoints/search.js+2 -1
@@ -1,5 +1,6 @@
11import fetch from 'node-fetch';
22import express from 'express';
3+import ipRegex from 'ip-regex';
34
45import { decode } from 'html-entities';
56import { readSecret, SECRET_KEYS } from './secrets.js';
@@ -416,7 +417,7 @@ router.post('/visit', async (request, response) => {
416417 }
417418
418419 // Reject IP addresses
419- if (urlObj.hostname.match(/^\d+\.\d+\.\d+\.\d+$/)) {
420+ if (ipRegex.v4({ exact: true }).test(urlObj.hostname) || ipRegex.v6({ exact: true }).test(urlObj.hostname)) {
420421 throw new Error('Invalid hostname');
421422 }
422423 } catch (error) {