8989#define FORMAT_IPV4 4
9090#define FORMAT_IPV6 6
9191
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 ]);
9393
9494static int php_filter_parse_int (const char * str , size_t str_len , zend_long * ret ) { /* {{{ */
9595 zend_long ctx_value ;
@@ -580,6 +580,14 @@ static int is_userinfo_valid(zend_string *str)
580580 return 1 ;
581581}
582582
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+
583591void php_filter_validate_url (PHP_INPUT_FILTER_PARAM_DECL ) /* {{{ */
584592{
585593 php_url * url ;
@@ -600,7 +608,7 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
600608
601609 if (url -> scheme != NULL &&
602610 (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 ;
604612 size_t l ;
605613
606614 if (url -> host == NULL ) {
@@ -609,17 +617,14 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
609617
610618 s = ZSTR_VAL (url -> host );
611619 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- }
620620
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+ ) {
623628 php_url_free (url );
624629 RETURN_VALIDATION_FAILED
625630 }
@@ -753,15 +758,15 @@ static int _php_filter_validate_ipv4(char *str, size_t str_len, int *ip) /* {{{
753758}
754759/* }}} */
755760
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 ]) /* {{{ */
757762{
758763 int compressed_pos = -1 ;
759764 int blocks = 0 ;
760765 int num , n , i ;
761766 char * ipv4 ;
762- char * end ;
767+ const char * end ;
763768 int ip4elm [4 ];
764- char * s = str ;
769+ const char * s = str ;
765770
766771 if (!memchr (str , ':' , str_len )) {
767772 return 0 ;
0 commit comments