Skip to content

Full Toolkit for Next-Level Domain Analysis

Fix IP regexes 🔍🕸️

Felix Wotschofsky93ccff1f08cd
diff --git a/lib/utils.test.ts b/lib/utils.test.ts
index a07cf3b..1689374 100644
--- a/lib/utils.test.ts
+++ b/lib/utils.test.ts
@@ -11,6 +11,8 @@ import {
import {
deduplicate,
getBaseDomain,
+ IPV4_REGEX,
+ IPV6_REGEX,
isAppleDevice,
isValidDomain,
isWildcardDomain,
@@ -175,6 +177,58 @@ describe('isWildcardDomain', () => {
});
});
+describe('IPV4_REGEX', () => {
+ it('matches IPv4 addresses in SPF records', () => {
+ const spf = 'v=spf1 ip4:166.84.6.31 ip4:166.84.7.238 ~all';
+ const matches = spf.match(IPV4_REGEX);
+ expect(matches).toEqual(['166.84.6.31', '166.84.7.238']);
+ });
+
+ it('does not match partial IPv4 from ip4: prefix in SPF records', () => {
+ const spf = 'v=spf1 ip4:166.84.6.31 ip4:166.84.7.238 ~all';
+ const matches = spf.match(IPV4_REGEX);
+ expect(matches).toEqual(['166.84.6.31', '166.84.7.238']);
+ expect(matches).not.toContain('4.166.84.6');
+ });
+
+ it('matches standard IPv4 addresses', () => {
+ expect('192.168.1.1'.match(IPV4_REGEX)).toEqual(['192.168.1.1']);
+ expect('255.255.255.255'.match(IPV4_REGEX)).toEqual(['255.255.255.255']);
+ expect('0.0.0.0'.match(IPV4_REGEX)).toEqual(['0.0.0.0']);
+ });
+});
+
+describe('IPV6_REGEX', () => {
+ it('does not match partial IPv6 from ip6: prefix in SPF records', () => {
+ const spf =
+ 'v=spf1 ip4:166.84.6.31 ip6:2602:f977:800::e276:63ff:fe66:3400 ~all';
+ const matches = spf.match(IPV6_REGEX);
+ expect(matches).toEqual(['2602:f977:800::e276:63ff:fe66:3400']);
+ });
+
+ it('matches multiple IPv6 addresses in SPF records', () => {
+ const spf =
+ 'v=spf1 ip6:2602:f977:800:f7f6::/64 ip6:2602:f977:800::e276:63ff:fe66:3400 ~all';
+ const matches = spf.match(IPV6_REGEX);
+ expect(matches).toEqual([
+ '2602:f977:800:f7f6::',
+ '2602:f977:800::e276:63ff:fe66:3400',
+ ]);
+ });
+
+ it('matches standard IPv6 addresses', () => {
+ expect('2001:db8::1'.match(IPV6_REGEX)).toEqual(['2001:db8::1']);
+ expect('::1'.match(IPV6_REGEX)).toEqual(['::1']);
+ expect('fe80::1%eth0'.match(IPV6_REGEX)).toEqual(['fe80::1%eth0']);
+ });
+
+ it('matches full IPv6 addresses', () => {
+ expect('2001:0db8:85a3:0000:0000:8a2e:0370:7334'.match(IPV6_REGEX)).toEqual(
+ ['2001:0db8:85a3:0000:0000:8a2e:0370:7334'],
+ );
+ });
+});
+
// User agent strings from https://gist.github.com/pzb/b4b6f57144aea7827ae4
describe('isAppleDevice', () => {
it('detects Apple devices', () => {
diff --git a/lib/utils.ts b/lib/utils.ts
index 21224bc..bc0ec16 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -6,9 +6,9 @@ export const DOMAIN_REGEX = /([a-zA-Z0-9-_]+\.)+[a-z]+\.?/gi;
const DOMAIN_LABEL_REGEX =
/^(?:xn--|_)?[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
export const IPV4_REGEX =
- /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/g;
+ /(?<!\w)(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/g;
export const IPV6_REGEX =
- /((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){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