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 ;
@@ -580,6 +580,14 @@ static int is_userinfo_valid(zend_string *str)
580
580
return 1 ;
581
581
}
582
582
583
+ static bool php_filter_is_valid_ipv6_hostname (const char * s , size_t l )
584
+ {
585
+ const char * e = s + l ;
586
+ const char * t = e - 1 ;
587
+
588
+ return * s == '[' && * t == ']' && _php_filter_validate_ipv6 (s + 1 , l - 2 , NULL );
589
+ }
590
+
583
591
void php_filter_validate_url (PHP_INPUT_FILTER_PARAM_DECL ) /* {{{ */
584
592
{
585
593
php_url * url ;
@@ -600,7 +608,7 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
600
608
601
609
if (url -> scheme != NULL &&
602
610
(zend_string_equals_literal_ci (url -> scheme , "http" ) || zend_string_equals_literal_ci (url -> scheme , "https" ))) {
603
- char * e , * s , * t ;
611
+ const char * s ;
604
612
size_t l ;
605
613
606
614
if (url -> host == NULL ) {
@@ -609,17 +617,14 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
609
617
610
618
s = ZSTR_VAL (url -> host );
611
619
l = ZSTR_LEN (url -> host );
612
- e = s + l ;
613
- t = e - 1 ;
614
-
615
- /* An IPv6 enclosed by square brackets is a valid hostname */
616
- if (* s == '[' && * t == ']' && _php_filter_validate_ipv6 ((s + 1 ), l - 2 , NULL )) {
617
- php_url_free (url );
618
- return ;
619
- }
620
620
621
- // Validate domain
622
- if (!_php_filter_validate_domain (ZSTR_VAL (url -> host ), l , FILTER_FLAG_HOSTNAME )) {
621
+ if (
622
+ /* An IPv6 enclosed by square brackets is a valid hostname.*/
623
+ !php_filter_is_valid_ipv6_hostname (s , l ) &&
624
+ /* Validate domain.
625
+ * This includes a loose check for an IPv4 address. */
626
+ !_php_filter_validate_domain (ZSTR_VAL (url -> host ), l , FILTER_FLAG_HOSTNAME )
627
+ ) {
623
628
php_url_free (url );
624
629
RETURN_VALIDATION_FAILED
625
630
}
@@ -753,15 +758,15 @@ static int _php_filter_validate_ipv4(char *str, size_t str_len, int *ip) /* {{{
753
758
}
754
759
/* }}} */
755
760
756
- static int _php_filter_validate_ipv6 (char * str , size_t str_len , int ip [8 ]) /* {{{ */
761
+ static int _php_filter_validate_ipv6 (const char * str , size_t str_len , int ip [8 ]) /* {{{ */
757
762
{
758
763
int compressed_pos = -1 ;
759
764
int blocks = 0 ;
760
765
int num , n , i ;
761
766
char * ipv4 ;
762
- char * end ;
767
+ const char * end ;
763
768
int ip4elm [4 ];
764
- char * s = str ;
769
+ const char * s = str ;
765
770
766
771
if (!memchr (str , ':' , str_len )) {
767
772
return 0 ;
0 commit comments