Skip to content

Commit 9a316d7

Browse files
authored
fix(ip-validation): implement strict IPv4 format validation (#2429)
* fix(regex): correct IP validation to handle IPv4 edge cases and robust IPv6 * enhance(regex): improve IP extractor to prevent false positives and enforce IPv4 octet ranges Tighten IPv4 pattern to enforce 0-255 per octet, rejecting invalid addresses like 256.256.256.256 and 999.999.999.999 Replace loose IPv6 matcher with comprehensive pattern covering full, compressed, link-local with zone IDs, and IPv4-embedded forms
1 parent 0908783 commit 9a316d7

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
The regex in the file getIP4OrIPV6address.js files validates for both ip address(ipv4) or ipv6 based on the input.
1+
This snippet extracts IPv4 and IPv6 addresses from free text. For single-value validation, see `validateIPInput.js` and `Validate IPv6 Address/script.js`.
22

3-
This regex cover the folloing IPv6 examples:
4-
Full IPv6 addresses (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334).
5-
Shorthand versions (e.g., fe80::1, where :: represents omitted zeros).
6-
IPv6 with embedded IPv4 addresses (e.g., ::ffff:192.168.1.1).
7-
And also includes following IPv4 addresses.
8-
Following are the valid IP address examples:
3+
The regex in `getIP4OrIPV6address.js` finds both IPv4 and IPv6 addresses within arbitrary text content.
94

10-
192.168.1.1
5+
IPv6 coverage includes:
6+
- Full addresses like `2001:0db8:85a3:0000:0000:8a2e:0370:7334`
7+
- Compressed forms like `fe80::1` (`::` for omitted zeros)
8+
- IPv4-embedded forms like `::ffff:192.168.1.1`
119

12-
127.0.0.1
10+
IPv4 validation now strictly enforces each octet to be in the range 0–255.
1311

14-
0.0.0.0
12+
Valid IPv4 examples:
1513

16-
255.255.255.255
14+
- 192.168.1.1
15+
- 127.0.0.1
16+
- 0.0.0.0
17+
- 255.255.255.255
18+
- 1.2.3.4
1719

18-
256.256.256.256
19-
20-
999.999.999.999
21-
22-
1.2.3
23-
24-
1.2.3.4
20+
Invalid IPv4 examples (correctly rejected by the regex):
2521

22+
- 256.256.256.256
23+
- 999.999.999.999
24+
- 1.2.3
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
// Extracts IPv4 and IPv6 addresses from arbitrary text content
2+
// For single-value validation, use validateIPInput.js or Validate IPv6 Address/script.js
13
extractIPAddresses: function(text) {
2-
var ipRegex = /\b((\d{1,3}\.){3}\d{1,3})\b|\b([a-fA-F0-9:]+:+[a-fA-F0-9:]+)\b/g;
3-
var matches = text.match(ipRegex);
4+
var ipv4 = "(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}";
5+
var ipv6 = "("+
6+
"(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|"+
7+
"(?:[A-Fa-f0-9]{1,4}:){1,7}:|"+
8+
"(?:[A-Fa-f0-9]{1,4}:){1,6}:[A-Fa-f0-9]{1,4}|"+
9+
"(?:[A-Fa-f0-9]{1,4}:){1,5}(?::[A-Fa-f0-9]{1,4}){1,2}|"+
10+
"(?:[A-Fa-f0-9]{1,4}:){1,4}(?::[A-Fa-f0-9]{1,4}){1,3}|"+
11+
"(?:[A-Fa-f0-9]{1,4}:){1,3}(?::[A-Fa-f0-9]{1,4}){1,4}|"+
12+
"(?:[A-Fa-f0-9]{1,4}:){1,2}(?::[A-Fa-f0-9]{1,4}){1,5}|"+
13+
"[A-Fa-f0-9]{1,4}:(?:(?::[A-Fa-f0-9]{1,4}){1,6})|"+
14+
":(?:(?::[A-Fa-f0-9]{1,4}){1,7}|:)|"+
15+
"fe80:(?::[A-Fa-f0-9]{0,4}){0,4}%[0-9A-Za-z]{1,}|"+
16+
"::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:"+
17+
"(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"+
18+
")|"+
19+
"(?:[A-Fa-f0-9]{1,4}:){1,4}:(?:"+
20+
"(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}"+
21+
")"+
22+
")";
23+
var ipRegex = new RegExp("\\b(?:" + ipv4 + "|" + ipv6 + ")\\b","g");
24+
var matches = text.match(ipRegex);
425
return matches;
526
},

0 commit comments

Comments
 (0)