[java-opensaml COMMIT] in /trunk/opensaml-util/src: main/java/org/opensaml/util/net/IPRange.java test/java/org/opensa...

noreply at shibboleth.net noreply at shibboleth.net
Mon Sep 19 18:08:58 BST 2011


Author: iay
Date: Mon Sep 19 18:08:58 2011
New Revision: 2891

URL: http://svn.shibboleth.net/view/java-opensaml?rev=2891&view=rev
Log:
Make IPRange less tolerant of invalid CIDR block notation.
As agreed with Chad, only accept 4-component IPv4 addresses and the "preferred" 8-component IPv6 addresses.
Included unit tests.

Added:
    trunk/opensaml-util/src/test/java/org/opensaml/util/net/IPRangeTest.java   (with props)
Modified:
    trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java

Modified: trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java?rev=2891&r1=2890&r2=2891&view=diff
==============================================================================
--- trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java (original)
+++ trunk/opensaml-util/src/main/java/org/opensaml/util/net/IPRange.java Mon Sep 19 18:08:58 2011
@@ -63,6 +63,62 @@
     }
 
     /**
+     * Validate an IPv4 address for use as the base of a CIDR block.
+     * 
+     * Throws IllegalArgumentException if validation fails.
+     * 
+     * @param address the address to validate
+     */
+    private static void validateV4Address(final String address) {
+        String[] components = address.split("\\.");
+        if (components.length != 4) {
+            throw new IllegalArgumentException("IPv4 address should have four components");
+        }
+        for (String component : components) {
+            int value = Integer.parseInt(component, 10);
+            if (value < 0 || (value > 255)) {
+                throw new IllegalArgumentException("IPv4 component range error: " + component);
+            }
+        }
+    }
+    
+    /**
+     * Validate an IPv6 address for use as the base of a CIDR block.
+     * 
+     * Throws IllegalArgumentException if validation fails.
+     * 
+     * @param address the address to validate
+     */
+    private static void validateV6Address(final String address) {
+        String[] components = address.split(":");
+        if (components.length != 8) {
+            throw new IllegalArgumentException("IPv6 address should have eight components");
+        }
+        for (String component : components) {
+            int value = Integer.parseInt(component, 16);
+            if (value < 0 || (value > 0xFFFF)) {
+                throw new IllegalArgumentException("IPv6 component range error: " + component);
+            }
+        }
+    }
+    
+    /**
+     * Validate an IP address for use as the base of a CIDR block.
+     * 
+     * Throws IllegalArgumentException if validation fails.
+     * 
+     * @param address the address to validate
+     */
+    private static void validateIPAddress(final String address) {
+        // any colons mean a V6 address, otherwise V4
+        if (address.indexOf(':') >= 0) {
+            validateV6Address(address);
+        } else {
+            validateV4Address(address);
+        }
+    }
+    
+    /**
      * Parses a CIDR block definition in to an IP range.
      * 
      * @param cidrBlock the CIDR block definition
@@ -77,6 +133,7 @@
 
         String[] blockParts = block.split("/");
         try {
+            validateIPAddress(blockParts[0]);
             InetAddress networkAddress = InetAddress.getByName(blockParts[0]);
             int maskSize = Integer.parseInt(blockParts[1]);
             return new IPRange(networkAddress, maskSize);



More information about the commits mailing list