89
89
#define FORMAT_IPV4 4
90
90
#define FORMAT_IPV6 6
91
91
92
- static int _php_filter_validate_ipv6 (char * str , size_t str_len , int ip [8 ]);
92
+ static int _php_filter_validate_ipv6 (const char * str , size_t str_len , int ip [8 ]);
93
93
94
94
static int php_filter_parse_int (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
95
95
zend_long ctx_value ;
@@ -572,6 +572,14 @@ static int is_userinfo_valid(zend_string *str)
572
572
return 1 ;
573
573
}
574
574
575
+ static bool php_filter_is_valid_ipv6_hostname (const char * s , size_t l )
576
+ {
577
+ const char * e = s + l ;
578
+ const char * t = e - 1 ;
579
+
580
+ return * s == '[' && * t == ']' && _php_filter_validate_ipv6 (s + 1 , l - 2 , NULL );
581
+ }
582
+
575
583
void php_filter_validate_url (PHP_INPUT_FILTER_PARAM_DECL ) /* {{{ */
576
584
{
577
585
php_url * url ;
@@ -592,7 +600,7 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
592
600
593
601
if (url -> scheme != NULL &&
594
602
(zend_string_equals_literal_ci (url -> scheme , "http" ) || zend_string_equals_literal_ci (url -> scheme , "https" ))) {
595
- char * e , * s , * t ;
603
+ const char * s ;
596
604
size_t l ;
597
605
598
606
if (url -> host == NULL ) {
@@ -601,17 +609,14 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
601
609
602
610
s = ZSTR_VAL (url -> host );
603
611
l = ZSTR_LEN (url -> host );
604
- e = s + l ;
605
- t = e - 1 ;
606
-
607
- /* An IPv6 enclosed by square brackets is a valid hostname */
608
- if (* s == '[' && * t == ']' && _php_filter_validate_ipv6 ((s + 1 ), l - 2 , NULL )) {
609
- php_url_free (url );
610
- return ;
611
- }
612
612
613
- // Validate domain
614
- if (!_php_filter_validate_domain (ZSTR_VAL (url -> host ), l , FILTER_FLAG_HOSTNAME )) {
613
+ if (
614
+ /* An IPv6 enclosed by square brackets is a valid hostname.*/
615
+ !php_filter_is_valid_ipv6_hostname (s , l ) &&
616
+ /* Validate domain.
617
+ * This includes a loose check for an IPv4 address. */
618
+ !_php_filter_validate_domain (ZSTR_VAL (url -> host ), l , FILTER_FLAG_HOSTNAME )
619
+ ) {
615
620
php_url_free (url );
616
621
RETURN_VALIDATION_FAILED
617
622
}
@@ -745,15 +750,15 @@ static int _php_filter_validate_ipv4(char *str, size_t str_len, int *ip) /* {{{
745
750
}
746
751
/* }}} */
747
752
748
- static int _php_filter_validate_ipv6 (char * str , size_t str_len , int ip [8 ]) /* {{{ */
753
+ static int _php_filter_validate_ipv6 (const char * str , size_t str_len , int ip [8 ]) /* {{{ */
749
754
{
750
755
int compressed_pos = -1 ;
751
756
int blocks = 0 ;
752
757
int num , n , i ;
753
758
char * ipv4 ;
754
- char * end ;
759
+ const char * end ;
755
760
int ip4elm [4 ];
756
- char * s = str ;
761
+ const char * s = str ;
757
762
758
763
if (!memchr (str , ':' , str_len )) {
759
764
return 0 ;
0 commit comments