Full Toolkit for Next-Level Domain Analysis
Add IP validation and URL encoding to getIpDetails 👮🔗 (#60)
Felix Wotschofskyeddbc6f44779
diff --git a/lib/ips.test.ts b/lib/ips.test.tsindex 237ea73..5a0fdd9 100644--- a/lib/ips.test.ts+++ b/lib/ips.test.ts@@ -1,3 +1,4 @@+import isIP from 'validator/lib/isIP';import { describe, expect, it, vi } from 'vitest';import {@@ -77,20 +78,47 @@ describe('normalizeIpEnding', () => {expect(normalizeIpEnding('10.0.0.1')).toBe('10.0.0.0');});- it('should normalize IPv6 by removing the last segment after a colon', () => {+ it('should normalize the last group of an IPv6 address to 0', () => {expect(normalizeIpEnding('fe80::250:56ff:fe97:2b82')).toBe(- 'fe80::250:56ff:fe97:',+ 'fe80::250:56ff:fe97:0',);- expect(normalizeIpEnding('2001:db8::1428:57ab')).toBe('2001:db8::1428:');+ expect(normalizeIpEnding('2001:db8::1428:57ab')).toBe('2001:db8::1428:0');});it('should not alter the IP address if there is no trailing segment to normalize', () => {expect(normalizeIpEnding('192.168.1.0')).toBe('192.168.1.0');- expect(normalizeIpEnding('2001:db8::1428:')).toBe('2001:db8::1428:');+ expect(normalizeIpEnding('2001:db8::1428:0')).toBe('2001:db8::1428:0');+ });++ it('should canonicalize compressed zero-tail IPv6 to the same key as the expanded form', () => {+ expect(normalizeIpEnding('2001:db8::')).toBe(+ normalizeIpEnding('2001:db8::0'),+ );+ expect(normalizeIpEnding('fe80::')).toBe(normalizeIpEnding('fe80::0'));+ expect(normalizeIpEnding('::')).toBe(normalizeIpEnding('::0'));});it('should still normalize based on pattern even for non-standard inputs', () => {expect(normalizeIpEnding('999.999.999.999')).toBe('999.999.999.0');- expect(normalizeIpEnding('abcd:ef01::3456:7890')).toBe('abcd:ef01::3456:');+ expect(normalizeIpEnding('abcd:ef01::3456:7890')).toBe('abcd:ef01::3456:0');+ });++ it('should produce a valid IP address for valid inputs', () => {+ const inputs = [+ '192.168.1.254',+ '10.0.0.1',+ '8.8.8.8',+ 'fe80::250:56ff:fe97:2b82',+ '2001:db8::1428:57ab',+ 'abcd:ef01::3456:7890',+ '2001:0db8:85a3:0000:0000:8a2e:0370:7334',+ '::1',+ ];++ for (const ip of inputs) {+ expect(isIP(ip), `precondition: ${ip} is a valid IP`).toBe(true);+ const normalized = normalizeIpEnding(ip);+ expect(isIP(normalized), `${ip} -> ${normalized}`).toBe(true);+ }});});diff --git a/lib/ips.ts b/lib/ips.tsindex 7523a54..092bbd0 100644--- a/lib/ips.ts+++ b/lib/ips.ts@@ -1,4 +1,5 @@import DataLoader from 'dataloader';+import isIP from 'validator/lib/isIP';type IpDetails = {country: string;@@ -17,7 +18,10 @@ type IpDetails = {};export const getIpDetails = async (ip: string) => {- const response = await fetch(`http://ip-api.com/json/${ip}`);+ if (!isIP(ip)) throw new Error('Invalid IP address');++ const url = new URL(`http://ip-api.com/json/${encodeURIComponent(ip)}`);+ const response = await fetch(url.toString());if (!response.ok)throw new Error(`Error fetching IP details: ${response.statusText}`);@@ -77,9 +81,12 @@ export const lookupReverse = async (ip: string): Promise<string[]> => {// Standardize last segment of IP address to reduce the number of requests and avoid rate limiting// 1st Regex is for IPv4-// 2nd Regex is for IPv6+// 2nd & 3rd Regexes are for IPv6 (3rd canonicalizes a trailing "::" to "::0")export const normalizeIpEnding = (ip: string) =>- ip.replace(/\.[0-9]+$/, '.0').replace(/:([0-9a-fA-F]+)$/, ':');+ ip+ .replace(/\.[0-9]+$/, '.0')+ .replace(/:([0-9a-fA-F]+)$/, ':0')+ .replace(/::$/, '::0');export const hostLookupLoader = new DataLoader(async (keys: readonly string[]) =>