[interchange] Improvements to IPv4/6 validation.

Peter Ajamian interchange-cvs at icdevgroup.org
Fri Jun 9 00:27:53 UTC 2017


commit 699db86aa16208afa0598e65d4fbe591eae3dd7c
Author: Peter Ajamian <peter at pajamian.dhs.org>
Date:   Fri Jun 9 12:20:17 2017 +1200

    Improvements to IPv4/6 validation.
    
    * Do not allow stray . at the end of IPv4 address.
    * Do not allow stray : at the end of IPv6 address, but do allow :: at start or
      end.
    * Get rid of warning about undefined $segs[-1] when :: is passed.
    * Fix non-working is_ipv6 (split ':' -> split /:+/).

 lib/Vend/Util.pm |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
---
diff --git a/lib/Vend/Util.pm b/lib/Vend/Util.pm
index 866ef3b..be01779 100644
--- a/lib/Vend/Util.pm
+++ b/lib/Vend/Util.pm
@@ -890,7 +890,7 @@ sub is_hash {
 # Verify that passed string is a valid IPv4 address.
 sub is_ipv4 {
     my $addr = shift or return;
-    my @segs = split /\./, $addr;
+    my @segs = split /\./, $addr, -1;
     return unless @segs == 4;
     foreach (@segs) {
 		return unless /^\d{1,3}$/ && !/^0\d/;
@@ -901,12 +901,14 @@ sub is_ipv4 {
 
 # Verify that passed string is a valid IPv6 address.
 sub is_ipv6 {
-    my $addr = shift or return;
-    my @segs = split ':', $addr;
+    my $tosplit = my $addr = shift or return;
+    $tosplit =~ s/^:://;
+    $tosplit =~ s/::$//;
+    my @segs = split /:+/, $tosplit, -1;
 
     my $quads = 8;
     # Check for IPv4 style ending
-    if ($segs[-1] =~ /\./) {
+    if (@segs && $segs[-1] =~ /\./) {
 	return unless is_ipv4(pop @segs);
 	$quads = 6;
     }



More information about the interchange-cvs mailing list