fix: enhance IP addresses hostname validation

537f0559abf7f80e2d9a703f4a4d3a62485238bd

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

1 files changed, +2 -1Showing whitespace changes
src/endpoints/search.js+2 -1
@@ -1,5 +1,6 @@
1import fetch from 'node-fetch';1import fetch from 'node-fetch';
2import express from 'express';2import express from 'express';
3import ipRegex from 'ip-regex';
34
4import { decode } from 'html-entities';5import { decode } from 'html-entities';
5import { readSecret, SECRET_KEYS } from './secrets.js';6import { readSecret, SECRET_KEYS } from './secrets.js';
@@ -416,7 +417,7 @@ router.post('/visit', async (request, response) => {
416 }417 }
417418
418 // Reject IP addresses419 // 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)) {
420 throw new Error('Invalid hostname');421 throw new Error('Invalid hostname');
421 }422 }
422 } catch (error) {423 } catch (error) {