PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/filter - logical_filters.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 405 418 96.9 %
Date: 2014-12-13 Functions: 15 15 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Derick Rethans <derick@php.net>                             |
      16             :   |          Pierre-A. Joye <pierre@php.net>                             |
      17             :   |          Kévin Dunglas <dunglas@gmail.com>                           |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include "php_filter.h"
      24             : #include "filter_private.h"
      25             : #include "ext/standard/url.h"
      26             : #include "ext/pcre/php_pcre.h"
      27             : 
      28             : #include "zend_multiply.h"
      29             : 
      30             : #if HAVE_ARPA_INET_H
      31             : # include <arpa/inet.h>
      32             : #endif
      33             : 
      34             : #ifndef INADDR_NONE
      35             : # define INADDR_NONE ((unsigned long int) -1)
      36             : #endif
      37             : 
      38             : 
      39             : /* {{{ FETCH_LONG_OPTION(var_name, option_name) */
      40             : #define FETCH_LONG_OPTION(var_name, option_name) \
      41             :         var_name = 0; \
      42             :         var_name##_set = 0; \
      43             :         if (option_array) { \
      44             :                 if ((option_val = zend_hash_str_find(HASH_OF(option_array), option_name, sizeof(option_name) - 1)) != NULL) {   \
      45             :                         var_name = zval_get_long(option_val); \
      46             :                         var_name##_set = 1; \
      47             :                 } \
      48             :         }
      49             : /* }}} */
      50             : 
      51             : /* {{{ FETCH_STRING_OPTION(var_name, option_name) */
      52             : #define FETCH_STRING_OPTION(var_name, option_name) \
      53             :         var_name = NULL; \
      54             :         var_name##_set = 0; \
      55             :         var_name##_len = 0; \
      56             :         if (option_array) { \
      57             :                 if ((option_val = zend_hash_str_find(HASH_OF(option_array), option_name, sizeof(option_name) - 1)) != NULL) { \
      58             :                         if (Z_TYPE_P(option_val) == IS_STRING) { \
      59             :                                 var_name = Z_STRVAL_P(option_val); \
      60             :                                 var_name##_len = Z_STRLEN_P(option_val); \
      61             :                                 var_name##_set = 1; \
      62             :                         } \
      63             :                 } \
      64             :         }
      65             : /* }}} */
      66             : 
      67             : /* {{{ FETCH_STR_OPTION(var_name, option_name) */
      68             : #define FETCH_STR_OPTION(var_name, option_name) \
      69             :         var_name = NULL; \
      70             :         var_name##_set = 0; \
      71             :         if (option_array) { \
      72             :                 if ((option_val = zend_hash_str_find(HASH_OF(option_array), option_name, sizeof(option_name) - 1)) != NULL) { \
      73             :                         if (Z_TYPE_P(option_val) == IS_STRING) { \
      74             :                                 var_name = Z_STR_P(option_val); \
      75             :                                 var_name##_set = 1; \
      76             :                         } \
      77             :                 } \
      78             :         }
      79             : /* }}} */
      80             : 
      81             : #define FORMAT_IPV4    4
      82             : #define FORMAT_IPV6    6
      83             : 
      84             : static int _php_filter_validate_ipv6(char *str, size_t str_len TSRMLS_DC);
      85             : 
      86          59 : static int php_filter_parse_int(const char *str, size_t str_len, zend_long *ret TSRMLS_DC) { /* {{{ */
      87             :         zend_long ctx_value;
      88          59 :         int sign = 0, digit = 0;
      89          59 :         const char *end = str + str_len;
      90             : 
      91          59 :         switch (*str) {
      92             :                 case '-':
      93          16 :                         sign = 1;
      94             :                 case '+':
      95          17 :                         str++;
      96             :                 default:
      97             :                         break;
      98             :         }
      99             : 
     100          59 :         if (*str == '0' && str + 1 == end) {
     101             :                 /* Special cases: +0 and -0 */
     102           2 :                 return 1;
     103             :         }
     104             : 
     105             :         /* must start with 1..9*/
     106          99 :         if (str < end && *str >= '1' && *str <= '9') {
     107          42 :                 ctx_value = ((sign)?-1:1) * ((*(str++)) - '0');
     108             :         } else {
     109          15 :                 return -1;
     110             :         }
     111             : 
     112          42 :         if ((end - str > MAX_LENGTH_OF_LONG - 1) /* number too long */
     113             :          || (SIZEOF_LONG == 4 && (end - str == MAX_LENGTH_OF_LONG - 1) && *str > '2')) {
     114             :                 /* overflow */
     115           0 :                 return -1;
     116             :         }
     117             : 
     118         217 :         while (str < end) {
     119         274 :                 if (*str >= '0' && *str <= '9') {
     120         135 :                         digit = (*(str++) - '0');
     121         207 :                         if ( (!sign) && ctx_value <= (ZEND_LONG_MAX-digit)/10 ) {
     122          72 :                                 ctx_value = (ctx_value * 10) + digit;
     123         124 :                         } else if ( sign && ctx_value >= (ZEND_LONG_MIN+digit)/10) {
     124          61 :                                 ctx_value = (ctx_value * 10) - digit;
     125             :                         } else {
     126           2 :                                 return -1;
     127             :                         }
     128             :                 } else {
     129           6 :                         return -1;
     130             :                 }
     131             :         }
     132             : 
     133          34 :         *ret = ctx_value;
     134          34 :         return 1;
     135             : }
     136             : /* }}} */
     137             : 
     138          11 : static int php_filter_parse_octal(const char *str, size_t str_len, zend_long *ret TSRMLS_DC) { /* {{{ */
     139          11 :         zend_ulong ctx_value = 0;
     140          11 :         const char *end = str + str_len;
     141             : 
     142         121 :         while (str < end) {
     143         200 :                 if (*str >= '0' && *str <= '7') {
     144         100 :                         zend_ulong n = ((*(str++)) - '0');
     145             : 
     146         199 :                         if ((ctx_value > ((zend_ulong)(~(zend_long)0)) / 8) ||
     147          99 :                                 ((ctx_value = ctx_value * 8) > ((zend_ulong)(~(zend_long)0)) - n)) {
     148           1 :                                 return -1;
     149             :                         }
     150          99 :                         ctx_value += n;
     151             :                 } else {
     152           1 :                         return -1;
     153             :                 }
     154             :         }
     155             :         
     156           9 :         *ret = (zend_long)ctx_value;
     157           9 :         return 1;
     158             : }
     159             : /* }}} */
     160             : 
     161          66 : static int php_filter_parse_hex(const char *str, size_t str_len, zend_long *ret TSRMLS_DC) { /* {{{ */
     162          66 :         zend_ulong ctx_value = 0;
     163          66 :         const char *end = str + str_len;
     164             :         zend_ulong n;
     165             : 
     166         328 :         while (str < end) {
     167         320 :                 if (*str >= '0' && *str <= '9') {
     168         122 :                         n = ((*(str++)) - '0');
     169         141 :                 } else if (*str >= 'a' && *str <= 'f') {
     170          65 :                         n = ((*(str++)) - ('a' - 10));
     171          21 :                 } else if (*str >= 'A' && *str <= 'F') {
     172          10 :                         n = ((*(str++)) - ('A' - 10));
     173             :                 } else {
     174           1 :                         return -1;
     175             :                 }
     176         393 :                 if ((ctx_value > ((zend_ulong)(~(zend_long)0)) / 16) ||
     177         196 :                         ((ctx_value = ctx_value * 16) > ((zend_ulong)(~(zend_long)0)) - n)) {
     178           1 :                         return -1;
     179             :                 }
     180         196 :                 ctx_value += n;
     181             :         }
     182             : 
     183          64 :         *ret = (zend_long)ctx_value;
     184          64 :         return 1;
     185             : }
     186             : /* }}} */
     187             : 
     188          95 : void php_filter_int(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     189             : {
     190             :         zval *option_val;
     191             :         zend_long  min_range, max_range, option_flags;
     192             :         int   min_range_set, max_range_set;
     193          95 :         int   allow_octal = 0, allow_hex = 0;
     194             :         size_t    len;
     195          95 :         int error = 0;
     196             :         zend_long  ctx_value;
     197             :         char *p;
     198             : 
     199             :         /* Parse options */
     200         114 :         FETCH_LONG_OPTION(min_range,    "min_range");
     201         114 :         FETCH_LONG_OPTION(max_range,    "max_range");
     202          95 :         option_flags = flags;
     203             : 
     204          95 :         len = Z_STRLEN_P(value);
     205             : 
     206          95 :         if (len == 0) {
     207           5 :                 RETURN_VALIDATION_FAILED
     208             :         }
     209             : 
     210          90 :         if (option_flags & FILTER_FLAG_ALLOW_OCTAL) {
     211          19 :                 allow_octal = 1;
     212             :         }
     213             : 
     214          90 :         if (option_flags & FILTER_FLAG_ALLOW_HEX) {
     215          17 :                 allow_hex = 1;
     216             :         }
     217             : 
     218             :         /* Start the validating loop */
     219          90 :         p = Z_STRVAL_P(value);
     220          90 :         ctx_value = 0;
     221             : 
     222          90 :         PHP_FILTER_TRIM_DEFAULT(p, len);
     223             : 
     224          90 :         if (*p == '0') {
     225          31 :                 p++; len--;
     226          43 :                 if (allow_hex && (*p == 'x' || *p == 'X')) {
     227          12 :                         p++; len--;
     228          12 :                         if (php_filter_parse_hex(p, len, &ctx_value TSRMLS_CC) < 0) {
     229           1 :                                 error = 1;
     230             :                         }
     231          19 :                 } else if (allow_octal) {
     232          11 :                         if (php_filter_parse_octal(p, len, &ctx_value TSRMLS_CC) < 0) {
     233           2 :                                 error = 1;
     234             :                         }
     235           8 :                 } else if (len != 0) {
     236           4 :                         error = 1;
     237             :                 }
     238             :         } else {
     239          59 :                 if (php_filter_parse_int(p, len, &ctx_value TSRMLS_CC) < 0) {
     240          23 :                         error = 1;
     241             :                 }
     242             :         }
     243             : 
     244          90 :         if (error > 0 || (min_range_set && (ctx_value < min_range)) || (max_range_set && (ctx_value > max_range))) {
     245          33 :                 RETURN_VALIDATION_FAILED
     246             :         } else {
     247          57 :                 zval_ptr_dtor(value);
     248          57 :                 ZVAL_LONG(value, ctx_value);
     249          57 :                 return;
     250             :         }
     251             : }
     252             : /* }}} */
     253             : 
     254          68 : void php_filter_boolean(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     255             : {
     256          68 :         char *str = Z_STRVAL_P(value);
     257          68 :         size_t len = Z_STRLEN_P(value);
     258             :         int ret;
     259             : 
     260          68 :         PHP_FILTER_TRIM_DEFAULT_EX(str, len, 0);
     261             : 
     262             :         /* returns true for "1", "true", "on" and "yes"
     263             :          * returns false for "0", "false", "off", "no", and ""
     264             :          * null otherwise. */
     265          68 :         switch (len) {
     266             :                 case 0:
     267           8 :                         ret = 0;
     268           8 :                         break;
     269             :                 case 1:
     270          19 :                         if (*str == '1') {
     271          10 :                                 ret = 1;
     272           9 :                         } else if (*str == '0') {
     273           7 :                                 ret = 0;
     274             :                         } else {
     275           2 :                                 ret = -1;
     276             :                         }
     277          19 :                         break;
     278             :                 case 2:
     279           9 :                         if (strncasecmp(str, "on", 2) == 0) {
     280           5 :                                 ret = 1;
     281           4 :                         } else if (strncasecmp(str, "no", 2) == 0) {
     282           3 :                                 ret = 0;
     283             :                         } else {
     284           1 :                                 ret = -1;
     285             :                         }
     286           9 :                         break;
     287             :                 case 3:
     288          11 :                         if (strncasecmp(str, "yes", 3) == 0) {
     289           3 :                                 ret = 1;
     290           8 :                         } else if (strncasecmp(str, "off", 3) == 0) {
     291           5 :                                 ret = 0;
     292             :                         } else {
     293           3 :                                 ret = -1;
     294             :                         }
     295          11 :                         break;
     296             :                 case 4:
     297           6 :                         if (strncasecmp(str, "true", 4) == 0) {
     298           4 :                                 ret = 1;
     299             :                         } else {
     300           2 :                                 ret = -1;
     301             :                         }
     302           6 :                         break;
     303             :                 case 5:
     304           5 :                         if (strncasecmp(str, "false", 5) == 0) {
     305           5 :                                 ret = 0;
     306             :                         } else {
     307           0 :                                 ret = -1;
     308             :                         }
     309           5 :                         break;
     310             :                 default:
     311          10 :                         ret = -1;
     312             :         }
     313             : 
     314          68 :         if (ret == -1) {
     315          18 :                 RETURN_VALIDATION_FAILED
     316             :         } else {
     317             :                 zval_dtor(value);
     318          50 :                 ZVAL_BOOL(value, ret);
     319             :         }
     320             : }
     321             : /* }}} */
     322             : 
     323          69 : void php_filter_float(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     324             : {
     325             :         size_t len;
     326             :         char *str, *end;
     327             :         char *num, *p;
     328             :         zval *option_val;
     329             :         char *decimal;
     330             :         int decimal_set;
     331             :         size_t decimal_len;
     332          69 :         char dec_sep = '.';
     333          69 :         char tsd_sep[3] = "',.";
     334             : 
     335             :         zend_long lval;
     336             :         double dval;
     337             : 
     338             :         int first, n;
     339             : 
     340          69 :         len = Z_STRLEN_P(value);
     341          69 :         str = Z_STRVAL_P(value);
     342             : 
     343          73 :         PHP_FILTER_TRIM_DEFAULT(str, len);
     344          65 :         end = str + len;
     345             : 
     346          75 :         FETCH_STRING_OPTION(decimal, "decimal");
     347             : 
     348          65 :         if (decimal_set) {
     349           5 :                 if (decimal_len != 1) {
     350           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "decimal separator must be one char");
     351           1 :                         RETURN_VALIDATION_FAILED
     352             :                 } else {
     353           4 :                         dec_sep = *decimal;
     354             :                 }
     355             :         }
     356             : 
     357          64 :         num = p = emalloc(len+1);
     358          64 :         if (str < end && (*str == '+' || *str == '-')) {
     359           9 :                 *p++ = *str++;
     360             :         }
     361          64 :         first = 1;
     362             :         while (1) {
     363          79 :                 n = 0;
     364         310 :                 while (str < end && *str >= '0' && *str <= '9') {
     365         152 :                         ++n;
     366         152 :                         *p++ = *str++;
     367             :                 }
     368          79 :                 if (str == end || *str == dec_sep || *str == 'e' || *str == 'E') {
     369          50 :                         if (!first && n != 3) {
     370           3 :                                 goto error;
     371             :                         }
     372          47 :                         if (*str == dec_sep) {
     373          25 :                                 *p++ = '.';
     374          25 :                                 str++;
     375         126 :                                 while (str < end && *str >= '0' && *str <= '9') {
     376          76 :                                         *p++ = *str++;
     377             :                                 }
     378             :                         }
     379          47 :                         if (*str == 'e' || *str == 'E') {
     380          14 :                                 *p++ = *str++;
     381          14 :                                 if (str < end && (*str == '+' || *str == '-')) {
     382           7 :                                         *p++ = *str++;
     383             :                                 }
     384          48 :                                 while (str < end && *str >= '0' && *str <= '9') {
     385          20 :                                         *p++ = *str++;
     386             :                                 }
     387             :                         }
     388          47 :                         break;
     389             :                 }
     390          29 :                 if ((flags & FILTER_FLAG_ALLOW_THOUSAND) && (*str == tsd_sep[0] || *str == tsd_sep[1] || *str == tsd_sep[2])) {
     391          16 :                         if (first?(n < 1 || n > 3):(n != 3)) {
     392           1 :                                 goto error;
     393             :                         }
     394          15 :                         first = 0;
     395          15 :                         str++;
     396             :                 } else {
     397             :                         goto error;
     398             :                 }
     399          15 :         }
     400          47 :         if (str != end) {
     401           2 :                 goto error;
     402             :         }
     403          45 :         *p = 0;
     404             : 
     405          90 :         switch (is_numeric_string(num, p - num, &lval, &dval, 0)) {
     406             :                 case IS_LONG:
     407           9 :                         zval_ptr_dtor(value);
     408           9 :                         ZVAL_DOUBLE(value, (double)lval);
     409           9 :                         break;
     410             :                 case IS_DOUBLE:
     411          31 :                         if ((!dval && p - num > 1 && strpbrk(num, "123456789")) || !zend_finite(dval)) {
     412             :                                 goto error;
     413             :                         }
     414          29 :                         zval_ptr_dtor(value);
     415          29 :                         ZVAL_DOUBLE(value, dval);
     416          29 :                         break;
     417             :                 default:
     418             : error:
     419          26 :                         efree(num);
     420          26 :                         RETURN_VALIDATION_FAILED
     421             :         }
     422          38 :         efree(num);     
     423             : }
     424             : /* }}} */
     425             : 
     426          16 : void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     427             : {
     428             :         zval *option_val;
     429             :         zend_string *regexp;
     430             :         zend_long option_flags;
     431             :         int regexp_set, option_flags_set;
     432          16 :         pcre *re = NULL;
     433          16 :         pcre_extra *pcre_extra = NULL;
     434          16 :         int preg_options = 0;
     435             :         int ovector[3];
     436             :         int matches;
     437             : 
     438             :         /* Parse options */
     439          46 :         FETCH_STR_OPTION(regexp, "regexp");
     440          31 :         FETCH_LONG_OPTION(option_flags, "flags");
     441             : 
     442          16 :         if (!regexp_set) {
     443           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "'regexp' option missing");
     444           1 :                 RETURN_VALIDATION_FAILED
     445             :         }
     446             : 
     447          15 :         re = pcre_get_compiled_regex(regexp, &pcre_extra, &preg_options TSRMLS_CC);
     448          15 :         if (!re) {
     449           0 :                 RETURN_VALIDATION_FAILED
     450             :         }
     451          15 :         matches = pcre_exec(re, NULL, Z_STRVAL_P(value), (int)Z_STRLEN_P(value), 0, 0, ovector, 3);
     452             : 
     453             :         /* 0 means that the vector is too small to hold all the captured substring offsets */
     454          15 :         if (matches < 0) {
     455          12 :                 RETURN_VALIDATION_FAILED
     456             :         }
     457             : }
     458             : 
     459          63 : static int _php_filter_validate_domain(char * domain, int len, zend_long flags) /* {{{ */
     460             : {
     461             :         char *e, *s, *t;
     462             :         size_t l;
     463          63 :         int hostname = flags & FILTER_FLAG_HOSTNAME;
     464          63 :         unsigned char i = 1;
     465             : 
     466          63 :         s = domain;
     467          63 :         l = len;
     468          63 :         e = domain + l;
     469          63 :         t = e - 1;
     470             : 
     471             :         /* Ignore trailing dot */
     472          63 :         if (*t == '.') {
     473           3 :                 e = t;
     474           3 :                 l--;
     475             :         }
     476             : 
     477             :         /* The total length cannot exceed 253 characters (final dot not included) */
     478          63 :         if (l > 253) {
     479           2 :                 return 0;
     480             :         }
     481             : 
     482             :         /* First char must be alphanumeric */
     483          61 :         if(*s == '.' || (hostname && !isalnum((int)*(unsigned char *)s))) { 
     484           8 :                 return 0;
     485             :         }
     486             : 
     487        1384 :         while (s < e) {
     488        1294 :                 if (*s == '.') {
     489             :                         /* The first and the last character of a label must be alphanumeric */
     490          62 :                         if (*(s + 1) == '.' || (hostname && (!isalnum((int)*(unsigned char *)(s - 1)) || !isalnum((int)*(unsigned char *)(s + 1))))) {
     491           8 :                                 return 0;
     492             :                         }
     493             : 
     494             :                         /* Reset label length counter */
     495          54 :                         i = 1;
     496             :                 } else {
     497        1232 :                         if (i > 63 || (hostname && *s != '-' && !isalnum((int)*(unsigned char *)s))) {
     498           8 :                                 return 0;
     499             :                         }
     500             : 
     501        1224 :                         i++;
     502             :                 }
     503             : 
     504        1278 :                 s++;
     505             :         }
     506             :         
     507          37 :         return 1;
     508             : }
     509             : /* }}} */
     510             : 
     511          34 : void php_filter_validate_domain(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     512             : {               
     513          34 :         if (!_php_filter_validate_domain(Z_STRVAL_P(value), Z_STRLEN_P(value), flags)) {
     514          16 :                 RETURN_VALIDATION_FAILED
     515             :         }
     516             : }
     517             : /* }}} */
     518             : 
     519          65 : void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     520             : {
     521             :         php_url *url;
     522          65 :         int old_len = (int)Z_STRLEN_P(value);
     523             :         
     524          65 :         php_filter_url(value, flags, option_array, charset TSRMLS_CC);
     525             : 
     526          65 :         if (Z_TYPE_P(value) != IS_STRING || old_len != Z_STRLEN_P(value)) {
     527           2 :                 RETURN_VALIDATION_FAILED
     528             :         }
     529             : 
     530             :         /* Use parse_url - if it returns false, we return NULL */
     531          63 :         url = php_url_parse_ex(Z_STRVAL_P(value), Z_STRLEN_P(value));
     532             : 
     533          63 :         if (url == NULL) {
     534           6 :                 RETURN_VALIDATION_FAILED
     535             :         }
     536             : 
     537          57 :         if (url->scheme != NULL && (!strcasecmp(url->scheme, "http") || !strcasecmp(url->scheme, "https"))) {
     538             :                 char *e, *s, *t;
     539             :                 size_t l;
     540             : 
     541          36 :                 if (url->host == NULL) {
     542           3 :                         goto bad_url;
     543             :                 }
     544             : 
     545          33 :                 s = url->host;
     546          33 :                 l = strlen(s);
     547          33 :                 e = url->host + l;
     548          33 :                 t = e - 1;
     549             : 
     550             :                 /* An IPv6 enclosed by square brackets is a valid hostname */
     551          33 :                 if (*s == '[' && *t == ']' && _php_filter_validate_ipv6((s + 1), l - 2 TSRMLS_CC)) {
     552           4 :                         php_url_free(url);
     553           4 :                         return;
     554             :                 }
     555             : 
     556             :                 // Validate domain
     557          29 :                 if (!_php_filter_validate_domain(url->host, l, FILTER_FLAG_HOSTNAME)) {
     558          10 :                         php_url_free(url);
     559          10 :                         RETURN_VALIDATION_FAILED
     560             :                 }
     561             :         }
     562             : 
     563         125 :         if (
     564          40 :                 url->scheme == NULL || 
     565             :                 /* some schemas allow the host to be empty */
     566          34 :                 (url->host == NULL && (strcmp(url->scheme, "mailto") && strcmp(url->scheme, "news") && strcmp(url->scheme, "file"))) ||
     567          51 :                 ((flags & FILTER_FLAG_PATH_REQUIRED) && url->path == NULL) || ((flags & FILTER_FLAG_QUERY_REQUIRED) && url->query == NULL)
     568             :         ) {
     569             : bad_url:
     570          21 :                 php_url_free(url);
     571          21 :                 RETURN_VALIDATION_FAILED
     572             :         }
     573          22 :         php_url_free(url);
     574             : }
     575             : /* }}} */
     576             : 
     577          36 : void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     578             : {
     579             :         /*
     580             :          * The regex below is based on a regex by Michael Rushton.
     581             :          * However, it is not identical.  I changed it to only consider routeable
     582             :          * addresses as valid.  Michael's regex considers a@b a valid address
     583             :          * which conflicts with section 2.3.5 of RFC 5321 which states that:
     584             :          *
     585             :          *   Only resolvable, fully-qualified domain names (FQDNs) are permitted
     586             :          *   when domain names are used in SMTP.  In other words, names that can
     587             :          *   be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
     588             :          *   in Section 5) are permitted, as are CNAME RRs whose targets can be
     589             :          *   resolved, in turn, to MX or address RRs.  Local nicknames or
     590             :          *   unqualified names MUST NOT be used.
     591             :          *
     592             :          * This regex does not handle comments and folding whitespace.  While
     593             :          * this is technically valid in an email address, these parts aren't
     594             :          * actually part of the address itself.
     595             :          *
     596             :          * Michael's regex carries this copyright:
     597             :          *
     598             :          * Copyright © Michael Rushton 2009-10
     599             :          * http://squiloople.com/
     600             :          * Feel free to use and redistribute this code. But please keep this copyright notice.
     601             :          *
     602             :          */
     603          36 :         const char regexp[] = "/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD";
     604          36 :         pcre       *re = NULL;
     605          36 :         pcre_extra *pcre_extra = NULL;
     606          36 :         int preg_options = 0;
     607             :         int         ovector[150]; /* Needs to be a multiple of 3 */
     608             :         int         matches;
     609             :         zend_string *sregexp;
     610             : 
     611             : 
     612             :         /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */
     613          36 :         if (Z_STRLEN_P(value) > 320) {
     614           2 :                 RETURN_VALIDATION_FAILED
     615             :         }
     616             : 
     617          34 :         sregexp = zend_string_init(regexp, sizeof(regexp) - 1, 0);
     618          34 :         re = pcre_get_compiled_regex(sregexp, &pcre_extra, &preg_options TSRMLS_CC);
     619          34 :         if (!re) {
     620             :                 zend_string_release(sregexp);
     621           0 :                 RETURN_VALIDATION_FAILED
     622             :         }
     623             :         zend_string_release(sregexp);
     624          34 :         matches = pcre_exec(re, NULL, Z_STRVAL_P(value), (int)Z_STRLEN_P(value), 0, 0, ovector, 3);
     625             : 
     626             :         /* 0 means that the vector is too small to hold all the captured substring offsets */
     627          34 :         if (matches < 0) {
     628          22 :                 RETURN_VALIDATION_FAILED
     629             :         }
     630             : 
     631             : }
     632             : /* }}} */
     633             : 
     634          55 : static int _php_filter_validate_ipv4(char *str, size_t str_len, int *ip) /* {{{ */
     635             : {
     636          55 :         const char *end = str + str_len;
     637             :         int num, m;
     638          55 :         int n = 0;
     639             : 
     640         248 :         while (str < end) {
     641             :                 int leading_zero;
     642         193 :                 if (*str < '0' || *str > '9') {
     643           6 :                         return 0;
     644             :                 }
     645         187 :                 leading_zero = (*str == '0');
     646         187 :                 m = 1;
     647         187 :                 num = ((*(str++)) - '0');
     648         546 :                 while (str < end && (*str >= '0' && *str <= '9')) {
     649         173 :                         num = num * 10 + ((*(str++)) - '0');
     650         173 :                         if (num > 255 || ++m > 3) {
     651           1 :                                 return 0;
     652             :                         }
     653             :                 }
     654             :                 /* don't allow a leading 0; that introduces octal numbers,
     655             :                  * which we don't support */
     656         186 :                 if (leading_zero && (num != 0 || m > 1))
     657           2 :                         return 0;
     658         184 :                 ip[n++] = num;
     659         184 :                 if (n == 4) {
     660          45 :                         return str == end;
     661         139 :                 } else if (str >= end || *(str++) != '.') {
     662           1 :                         return 0;
     663             :                 }
     664             :         }
     665           0 :         return 0;               
     666             : }
     667             : /* }}} */
     668             : 
     669          72 : static int _php_filter_validate_ipv6(char *str, size_t str_len TSRMLS_DC) /* {{{ */
     670             : {
     671          72 :         int compressed = 0;
     672          72 :         int blocks = 0;
     673             :         int n;
     674             :         char *ipv4;
     675             :         char *end;
     676             :         int ip4elm[4];
     677          72 :         char *s = str;
     678             : 
     679          72 :         if (!memchr(str, ':', str_len)) {
     680           0 :                 return 0;
     681             :         }
     682             : 
     683             :         /* check for bundled IPv4 */
     684          72 :         ipv4 = memchr(str, '.', str_len);
     685          72 :         if (ipv4) {
     686         114 :                 while (ipv4 > str && *(ipv4-1) != ':') {
     687          62 :                         ipv4--;
     688             :                 }
     689             : 
     690          26 :                 if (!_php_filter_validate_ipv4(ipv4, (str_len - (ipv4 - str)), ip4elm)) {
     691           4 :                         return 0;
     692             :                 }
     693             : 
     694          22 :                 str_len = ipv4 - str; /* length excluding ipv4 */
     695          22 :                 if (str_len < 2) {
     696           0 :                         return 0;
     697             :                 }
     698             : 
     699          22 :                 if (ipv4[-2] != ':') {
     700             :                         /* don't include : before ipv4 unless it's a :: */
     701          13 :                         str_len--;
     702             :                 }
     703             : 
     704          22 :                 blocks = 2;
     705             :         }
     706             : 
     707          68 :         end = str + str_len;
     708             : 
     709         385 :         while (str < end) {
     710         277 :                 if (*str == ':') {
     711         229 :                         if (++str >= end) {
     712             :                                 /* cannot end in : without previous : */
     713           0 :                                 return 0;
     714             :                         }
     715         229 :                         if (*str == ':') {
     716          56 :                                 if (compressed) {
     717           4 :                                         return 0;
     718             :                                 }
     719          52 :                                 blocks++; /* :: means 1 or more 16-bit 0 blocks */
     720          52 :                                 compressed = 1;
     721             : 
     722          52 :                                 if (++str == end) {
     723          10 :                                         return (blocks <= 8);
     724             :                                 }
     725         173 :                         } else if ((str - 1) == s) {
     726             :                                 /* dont allow leading : without another : following */
     727           0 :                                 return 0;
     728             :                         }                               
     729             :                 }
     730         263 :                 n = 0;
     731        3087 :                 while ((str < end) &&
     732        1374 :                        ((*str >= '0' && *str <= '9') ||
     733         436 :                         (*str >= 'a' && *str <= 'f') ||
     734         280 :                         (*str >= 'A' && *str <= 'F'))) {
     735         471 :                         n++;
     736         471 :                         str++;
     737             :                 }
     738         263 :                 if (n < 1 || n > 4) {
     739           8 :                         return 0;
     740             :                 }
     741         255 :                 if (++blocks > 8)
     742           6 :                         return 0;
     743             :         }
     744          40 :         return ((compressed && blocks <= 8) || blocks == 8);
     745             : }
     746             : /* }}} */
     747             : 
     748         107 : void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     749             : {
     750             :         /* validates an ipv4 or ipv6 IP, based on the flag (4, 6, or both) add a
     751             :          * flag to throw out reserved ranges; multicast ranges... etc. If both
     752             :          * allow_ipv4 and allow_ipv6 flags flag are used, then the first dot or
     753             :          * colon determine the format */
     754             : 
     755             :         int            ip[4];
     756             :         int            mode;
     757             : 
     758         107 :         if (memchr(Z_STRVAL_P(value), ':', Z_STRLEN_P(value))) {
     759          69 :                 mode = FORMAT_IPV6;
     760          38 :         } else if (memchr(Z_STRVAL_P(value), '.', Z_STRLEN_P(value))) {
     761          30 :                 mode = FORMAT_IPV4;
     762             :         } else {
     763           8 :                 RETURN_VALIDATION_FAILED
     764             :         }
     765             : 
     766          99 :         if ((flags & FILTER_FLAG_IPV4) && (flags & FILTER_FLAG_IPV6)) {
     767             :                 /* Both formats are cool */
     768          99 :         } else if ((flags & FILTER_FLAG_IPV4) && mode == FORMAT_IPV6) {
     769           1 :                 RETURN_VALIDATION_FAILED
     770          98 :         } else if ((flags & FILTER_FLAG_IPV6) && mode == FORMAT_IPV4) {
     771           1 :                 RETURN_VALIDATION_FAILED
     772             :         }
     773             : 
     774          97 :         switch (mode) {
     775             :                 case FORMAT_IPV4:
     776          29 :                         if (!_php_filter_validate_ipv4(Z_STRVAL_P(value), Z_STRLEN_P(value), ip)) {
     777           9 :                                 RETURN_VALIDATION_FAILED
     778             :                         }
     779             : 
     780             :                         /* Check flags */
     781          20 :                         if (flags & FILTER_FLAG_NO_PRIV_RANGE) {
     782           8 :                                 if (
     783           2 :                                         (ip[0] == 10) ||
     784           2 :                                         (ip[0] == 172 && (ip[1] >= 16 && ip[1] <= 31)) ||
     785           4 :                                         (ip[0] == 192 && ip[1] == 168)
     786             :                                 ) {
     787           1 :                                         RETURN_VALIDATION_FAILED
     788             :                                 }
     789             :                         }
     790             : 
     791          19 :                         if (flags & FILTER_FLAG_NO_RES_RANGE) {
     792          51 :                                 if (
     793           8 :                                         (ip[0] == 0) ||
     794          12 :                                         (ip[0] == 100 && (ip[1] >= 64 && ip[1] <= 127)) ||
     795           6 :                                         (ip[0] == 169 && ip[1] == 254) ||
     796           8 :                                         (ip[0] == 192 && ip[1] == 0 && ip[2] == 2) ||
     797          12 :                                         (ip[0] == 127 && ip[1] == 0 && ip[2] == 0 && ip[3] == 1) ||
     798           5 :                                         (ip[0] >= 224 && ip[0] <= 255)
     799             :                                 ) {
     800           5 :                                         RETURN_VALIDATION_FAILED
     801             :                                 }
     802             :                         }
     803          14 :                         break;
     804             : 
     805             :                 case FORMAT_IPV6:
     806             :                         {
     807          68 :                                 int res = 0;
     808          68 :                                 res = _php_filter_validate_ipv6(Z_STRVAL_P(value), Z_STRLEN_P(value) TSRMLS_CC);
     809          68 :                                 if (res < 1) {
     810          27 :                                         RETURN_VALIDATION_FAILED
     811             :                                 }
     812             :                                 /* Check flags */
     813          41 :                                 if (flags & FILTER_FLAG_NO_PRIV_RANGE) {
     814           1 :                                         if (Z_STRLEN_P(value) >=2 && (!strncasecmp("FC", Z_STRVAL_P(value), 2) || !strncasecmp("FD", Z_STRVAL_P(value), 2))) {
     815           1 :                                                 RETURN_VALIDATION_FAILED
     816             :                                         }
     817             :                                 }
     818          40 :                                 if (flags & FILTER_FLAG_NO_RES_RANGE) {
     819           8 :                                         switch (Z_STRLEN_P(value)) {
     820             :                                                 case 1: case 0:
     821           0 :                                                         break;
     822             :                                                 case 2:
     823           1 :                                                         if (!strcmp("::", Z_STRVAL_P(value))) {
     824           1 :                                                                 RETURN_VALIDATION_FAILED
     825             :                                                         }
     826           0 :                                                         break;
     827             :                                                 case 3:
     828           3 :                                                         if (!strcmp("::1", Z_STRVAL_P(value)) || !strcmp("5f:", Z_STRVAL_P(value))) {
     829           3 :                                                                 RETURN_VALIDATION_FAILED
     830             :                                                         }
     831           0 :                                                         break;
     832             :                                                 default:
     833           4 :                                                         if (Z_STRLEN_P(value) >= 5) {
     834          13 :                                                                 if (
     835           4 :                                                                         !strncasecmp("fe8", Z_STRVAL_P(value), 3) ||
     836           3 :                                                                         !strncasecmp("fe9", Z_STRVAL_P(value), 3) ||
     837           3 :                                                                         !strncasecmp("fea", Z_STRVAL_P(value), 3) ||
     838           3 :                                                                         !strncasecmp("feb", Z_STRVAL_P(value), 3)
     839             :                                                                 ) {
     840           1 :                                                                         RETURN_VALIDATION_FAILED
     841             :                                                                 }
     842             :                                                         }
     843          10 :                                                         if (
     844           4 :                                                                 (Z_STRLEN_P(value) >= 9 &&  !strncasecmp("2001:0db8", Z_STRVAL_P(value), 9)) ||
     845           4 :                                                                 (Z_STRLEN_P(value) >= 2 &&  !strncasecmp("5f", Z_STRVAL_P(value), 2)) ||
     846           2 :                                                                 (Z_STRLEN_P(value) >= 4 &&  !strncasecmp("3ff3", Z_STRVAL_P(value), 4)) ||
     847           0 :                                                                 (Z_STRLEN_P(value) >= 8 &&  !strncasecmp("2001:001", Z_STRVAL_P(value), 8))
     848             :                                                         ) {
     849           3 :                                                                 RETURN_VALIDATION_FAILED
     850             :                                                         }
     851             :                                         }
     852             :                                 }
     853             :                         }
     854             :                         break;
     855             :         }
     856             : }
     857             : /* }}} */
     858             : 
     859          24 : void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
     860             : {
     861          24 :         char *input = Z_STRVAL_P(value);
     862          24 :         size_t input_len = Z_STRLEN_P(value);
     863             :         int tokens, length, i, offset, exp_separator_set;
     864             :         size_t exp_separator_len;
     865             :         char separator;
     866             :         char *exp_separator;
     867          24 :         zend_long ret = 0;
     868             :         zval *option_val;
     869             : 
     870          34 :         FETCH_STRING_OPTION(exp_separator, "separator");
     871             : 
     872          24 :         if (exp_separator_set && exp_separator_len != 1) {
     873           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Separator must be exactly one character long");
     874           2 :                 RETURN_VALIDATION_FAILED;
     875             :         }
     876             : 
     877          22 :         if (14 == input_len) {
     878             :                 /* EUI-64 format: Four hexadecimal digits separated by dots. Less
     879             :                  * commonly used but valid nonetheless.
     880             :                  */
     881           1 :                 tokens = 3;
     882           1 :                 length = 4;
     883           1 :                 separator = '.';
     884          27 :         } else if (17 == input_len && input[2] == '-') {
     885             :                 /* IEEE 802 format: Six hexadecimal digits separated by hyphens. */
     886           6 :                 tokens = 6;
     887           6 :                 length = 2;
     888           6 :                 separator = '-';
     889          21 :         } else if (17 == input_len && input[2] == ':') {
     890             :                 /* IEEE 802 format: Six hexadecimal digits separated by colons. */
     891           6 :                 tokens = 6;
     892           6 :                 length = 2;
     893           6 :                 separator = ':';
     894             :         } else {
     895           9 :                 RETURN_VALIDATION_FAILED;
     896             :         }
     897             : 
     898          13 :         if (exp_separator_set && separator != exp_separator[0]) {
     899           2 :                 RETURN_VALIDATION_FAILED;
     900             :         }
     901             : 
     902             :         /* Essentially what we now have is a set of tokens each consisting of
     903             :          * a hexadecimal number followed by a separator character. (With the
     904             :          * exception of the last token which does not have the separator.)
     905             :          */
     906          64 :         for (i = 0; i < tokens; i++) {
     907          55 :                 offset = i * (length + 1);
     908             : 
     909          55 :                 if (i < tokens - 1 && input[offset + length] != separator) {
     910             :                         /* The current token did not end with e.g. a "." */
     911           1 :                         RETURN_VALIDATION_FAILED
     912             :                 }
     913          54 :                 if (php_filter_parse_hex(input + offset, length, &ret TSRMLS_CC) < 0) {
     914             :                         /* The current token is no valid hexadecimal digit */
     915           1 :                         RETURN_VALIDATION_FAILED
     916             :                 }
     917             :         }
     918             : }
     919             : /* }}} */
     920             : 
     921             : /*
     922             :  * Local variables:
     923             :  * tab-width: 4
     924             :  * c-basic-offset: 4
     925             :  * End:
     926             :  * vim600: noet sw=4 ts=4 fdm=marker
     927             :  * vim<600: noet sw=4 ts=4
     928             :  */

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:12 +0000 (8 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.