Permalink
Browse files

Fix validation of IPv6 on IPv4 checks.

Separate IPv4 and IPv6 validation to allow strict checking.
  • Loading branch information...
predominant committed Jan 5, 2010
1 parent 71dbd9b commit 1302fba6326f854f3a587da421a0374c64f8dca5
Showing with 84 additions and 12 deletions.
  1. +48 −5 cake/libs/validation.php
  2. +36 −7 cake/tests/cases/libs/validation.test.php
View
@@ -541,21 +541,63 @@ function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) {
}
/**
- * Validation of an IPv4 address.
+ * Validation of an IP address.
+ *
+ * Valid IP version strings for type restriction are:
+ * - both: Check both IPv4 and IPv6, return true if the supplied address matches either version
+ * - IPv4: Version 4 (Eg: 127.0.0.1, 192.168.10.123, 203.211.24.8)
+ * - IPv6: Version 6 (Eg: ::1, 2001:0db8::1428:57ab)
*
* @param string $check The string to test.
+ * @param string $type The IP Version to test against
* @return boolean Success
* @access public
*/
- function ip($check, $type = 'IPv4') {
- if (function_exists('filter_var')) {
- return (boolean) filter_var($check, FILTER_VALIDATE_IP);
+ function ip($check, $type = 'both') {
+ $_this =& Validation::getInstance();
+ $success = false;
+ if ($type === 'IPv4' || $type === 'both') {
+ $success |= $_this->_ipv4($check);
+ }
+ if ($type === 'IPv6' || $type === 'both') {
+ $success |= $_this->_ipv6($check);
}
+ return $success;
+ }
+/**
+ * Validation of IPv4 addresses.
+ *
+ * @param string $check IP Address to test
+ * @return boolean Success
+ * @access protected
+ */
+ function _ipv4($check) {
+ if (function_exists('filter_var')) {
+ return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV4)) !== false;
+ }
$_this =& Validation::getInstance();
+ $_this->__populateIp();
$_this->check = $check;
+ $_this->regex = '/^' . $_this->__pattern['IPv4'] . '$/';
+ return $_this->_check();
+ }
+
+/**
+ * Validation of IPv6 addresses.
+ *
+ * @param string $check IP Address to test
+ * @return boolean Success
+ * @access protected
+ */
+ function _ipv6($check) {
+ if (function_exists('filter_var')) {
+ return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV6)) !== false;
+ }
+ $_this =& Validation::getInstance();
$_this->__populateIp();
- $_this->regex = '/^' . $_this->__pattern[$type] . '$/';
+ $_this->check = $check;
+ $_this->regex = '/^' . $_this->__pattern['IPv6'] . '$/';
return $_this->_check();
}
@@ -954,6 +996,7 @@ function _luhn() {
* Lazily popualate the IP address patterns used for validations
*
* @return void
+ * @access private
*/
function __populateIp() {
if (!isset($this->__pattern['IPv6'])) {
@@ -1725,13 +1725,16 @@ function testEqualTo() {
* @access public
* @return void
*/
- function testIp() {
- $this->assertTrue(Validation::ip('0.0.0.0'));
- $this->assertTrue(Validation::ip('192.168.1.156'));
- $this->assertTrue(Validation::ip('255.255.255.255'));
- $this->assertFalse(Validation::ip('127.0.0'));
- $this->assertFalse(Validation::ip('127.0.0.a'));
- $this->assertFalse(Validation::ip('127.0.0.256'));
+ function testIpv4() {
+ $this->assertTrue(Validation::ip('0.0.0.0', 'IPv4'));
+ $this->assertTrue(Validation::ip('192.168.1.156', 'IPv4'));
+ $this->assertTrue(Validation::ip('255.255.255.255', 'IPv4'));
+
+ $this->assertFalse(Validation::ip('127.0.0', 'IPv4'));
+ $this->assertFalse(Validation::ip('127.0.0.a', 'IPv4'));
+ $this->assertFalse(Validation::ip('127.0.0.256', 'IPv4'));
+
+ $this->assertFalse(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'IPv4'));
}
/**
@@ -1772,6 +1775,32 @@ function testIpv6() {
$this->assertFalse(Validation::ip('1:2:3::4:5:6:7:8:9', 'IPv6'));
$this->assertFalse(Validation::ip('::ffff:2.3.4', 'IPv6'));
$this->assertFalse(Validation::ip('::ffff:257.1.2.3', 'IPv6'));
+
+ $this->assertFalse(Validation::ip('0.0.0.0', 'IPv6'));
+ }
+
+/**
+ * testIpBoth method
+ *
+ * @return void
+ * @access public
+ */
+ function testIpBoth() {
+ $this->assertTrue(Validation::ip('0.0.0.0'));
+ $this->assertTrue(Validation::ip('192.168.1.156'));
+ $this->assertTrue(Validation::ip('255.255.255.255'));
+
+ $this->assertFalse(Validation::ip('127.0.0'));
+ $this->assertFalse(Validation::ip('127.0.0.a'));
+ $this->assertFalse(Validation::ip('127.0.0.256'));
+
+ $this->assertTrue(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
+ $this->assertTrue(Validation::ip('2001:db8:85a3:0:0:8a2e:370:7334'));
+ $this->assertTrue(Validation::ip('2001:db8:85a3::8a2e:370:7334'));
+
+ $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:37023:7334'));
+ $this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:370k:7334'));
+ $this->assertFalse(Validation::ip('1:2:3:4:5:6:7:8:9'));
}
/**

0 comments on commit 1302fba

Please sign in to comment.