php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #47435
Patch IPv6_FILTERS_WITH_TESTS revision 2010-04-07 18:58 UTC by zippy1981 at gmail dot com

Patch IPv6_FILTERS_WITH_TESTS for Filter related Bug #47435

Patch version 2010-04-07 18:58 UTC

Return to Bug #47435 | Download this patch
Patch Revisions:

Developer: zippy1981@gmail.com

Index: ext/filter/tests/bug47435.phpt
===================================================================
--- ext/filter/tests/bug47435.phpt	(revision 0)
+++ ext/filter/tests/bug47435.phpt	(revision 0)
@@ -0,0 +1,34 @@
+--TEST--
+Bug #47435 (FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE don't work with ipv6)
+--FILE--
+<?php
+var_dump(filter_var("FC00::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("FC00::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE));
+var_dump(filter_var("::", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("::", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
+var_dump(filter_var("::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
+var_dump(filter_var("fe8:5:6::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("fe8:5:6::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
+var_dump(filter_var("2001:0db8::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("2001:0db8::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
+var_dump(filter_var("5f::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("5f::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
+var_dump(filter_var("3ff3::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("3ff3::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
+?>
+--EXPECT--	
+string(7) "FC00::1"
+bool(false)
+string(2) "::"
+bool(false)
+string(3) "::1"
+bool(false)
+string(10) "fe8:5:6::1"
+bool(false)
+string(12) "2001:0db8::1"
+bool(false)
+string(5) "5f::1"
+bool(false)
+string(7) "3ff3::1"
+bool(false)
Index: ext/filter/tests/018.phpt
===================================================================
--- ext/filter/tests/018.phpt	(revision 297627)
+++ ext/filter/tests/018.phpt	(working copy)
@@ -24,6 +24,7 @@
 var_dump(filter_var("::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
 var_dump(filter_var("127.0.0.1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
 var_dump(filter_var("::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
+var_dump(filter_var("::1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE));
 var_dump(filter_var("127.0.0.1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
 echo "Done\n";
 ?>
@@ -48,5 +49,6 @@
 bool(false)
 bool(false)
 string(3) "::1"
+bool(false)
 string(9) "127.0.0.1"
 Done
Index: ext/filter/logical_filters.c
===================================================================
--- ext/filter/logical_filters.c	(revision 297627)
+++ ext/filter/logical_filters.c	(working copy)
@@ -652,6 +652,7 @@
 
 	int            ip[4];
 	int            mode;
+	int            value_strlen;
 
 	if (memchr(Z_STRVAL_P(value), ':', Z_STRLEN_P(value))) {
 		mode = FORMAT_IPV6;
@@ -707,11 +708,50 @@
 				}
 				/* Check flags */
 				if (flags & FILTER_FLAG_NO_PRIV_RANGE) {
-					if (Z_STRLEN_P(value) >=2 && (!strncasecmp("FC", Z_STRVAL_P(value), 2) || !strncasecmp("FD", Z_STRVAL_P(value), 2))) {
+					// rfc4193 defines the subnet FC00::/7 as a private range.
+					if (
+							Z_STRLEN_P(value) >= 2 && 
+							(
+								!strncasecmp("FC", Z_STRVAL_P(value), 2) || 
+								!strncasecmp("FD", Z_STRVAL_P(value), 2)
+							)
+						) {
 						RETURN_VALIDATION_FAILED
 					}
 				}
+
+				if (flags & FILTER_FLAG_NO_RES_RANGE) {
+				if (
+					// unspecified address (rfc4291)
+					(Z_STRLEN_P(value) == 2 &&  !strncasecmp("::", Z_STRVAL_P(value), 2)) ||
+					//(Z_STRLEN_P(value) == 6 &&  !strncasecmp("::/128", Z_STRVAL_P(value), 6)) || */
+					// loopback address (rfc4291)
+					(Z_STRLEN_P(value) == 3 &&  !strncasecmp("::1", Z_STRVAL_P(value), 3)) ||
+					//(Z_STRLEN_P(value) == 7 &&  !strncasecmp("::1/128", Z_STRVAL_P(value), 7)) ||
+					// fe80::/10 link local unicast (rfc4291)
+					(
+						Z_STRLEN_P(value) >= 5 && (
+							!strncasecmp("fe8", Z_STRVAL_P(value), 3) ||
+							!strncasecmp("fe9", Z_STRVAL_P(value), 3) ||
+							!strncasecmp("fea", Z_STRVAL_P(value), 3) ||
+							!strncasecmp("feb", Z_STRVAL_P(value), 3)
+						)
+					) ||
+					// documentation addresses (rfc3849)
+					(Z_STRLEN_P(value) >= 9 &&  !strncasecmp("2001:0db8", Z_STRVAL_P(value), 9)) ||
+					// 6Bone
+					(Z_STRLEN_P(value) >= 2 &&  !strncasecmp("5f", Z_STRVAL_P(value), 2)) ||
+					(Z_STRLEN_P(value) >= 4 &&  !strncasecmp("3ff3", Z_STRVAL_P(value), 4)) ||
+					//TODO IPv4 Mapping ::ffff:0:0/96
+					// ORCHID addresses (rfc4843)
+					(Z_STRLEN_P(value) >= 8 &&  !strncasecmp("2001:001", Z_STRVAL_P(value), 8)) /* ||
+					// default unicast route address
+					(Z_STRLEN_P(value) == 4 &&  !strncasecmp("::/0", Z_STRVAL_P(value), 4)) */
+				) {
+					RETURN_VALIDATION_FAILED
+				}
 			}
+			}
 			break;
 	}
 }
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 04:01:55 2014 UTC