Skip to content

Add true validation functions validate_var_array() and input_input_array() #2048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 42 commits into from

Conversation

yohgaki
Copy link
Contributor

@yohgaki yohgaki commented Aug 1, 2016

See RFC for more details and updates.

RFC: https://wiki.php.net/rfc/add_validate_functions_to_filter

filter_var_array()/filter_input_array() return value is hard to tell if there were validation errors because they simply filter inputs. i.e. They set FALSE or NULL for invalid elements and return filtered array. This behavior makes it difficult to use filter_array()/filter_input_array() as validation functions that reject invalid inputs.

This patch adds true validation functions

  • filter_require_var_array() - Almost the same as filter_var_array() except it raise UnexpectedValueException on validation failure(s), instead of filtered array and does not add NULL for non-existing elements by default.
  • filter_require_input_array(), validate_var() and validate_input(). The same as above.
  • filter_check_definition() - Checks definition array for validate___array() and filter___array(). Since definition errors are ignored silently. This function is mandatory for security reasons. i.e. Filter module could fallback to weakest option.

String validation filter

  • String validation filter is added.
  • UTF-8 encoding is supported only and used by default.
  • FILTER_STRING_ENCODING_PASS should be used for binary and other encodings.

Invalid validation filter

  • Internally, there is code assumes invalid filter existence, but it didn't exist.

Note on callback filter

  • No callback for validation filter is added.
  • Users must raise UnexpectedValueException when there is invalid data.

Multiple sanitize/validation filter.

  • Allows multiple filters and filter options by array of filters/options.
  • Older implementation ignored such spec silently.

e.g.

    <?php
    error_reporting(E_ALL | E_STRICT);
    );

    $args = array(
        'component'    =>
        array(
                // New filter module allows multiple filters and options as follows.
                // Array elements are evaluated in order. Non array spec is evaluated last.
                // Older implementation ignores this kind of spec silently.
                array(
                        'filter'    => FILTER_VALIDATE_INT,
                        'options'   => array('min_range' => 1, 'max_range' => 10)
                ),
                array(
                        'filter' => FILTER_VALIDATE_REGEXP,
                        'options' => array('regexp' => '/[0123456789]{2}/')
                ),
                array(
                        'filter' => FILTER_VALIDATE_FLOAT
                ),
        ),
    );

    // Throws UnexpectedValueException for invalid inputs.
    try {
        $myinputs = validate_var_array($data, $args);
        var_dump($myinputs); // NOTE: If you need returned array value, it MUST be inside try block.
    } catch (UnexpectedValueException $e) {
        var_dump($e->getMessage());
    }

zval_ptr_dtor(return_value);
RETURN_FALSE;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing /* }}} */

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed.

@staabm
Copy link
Contributor

staabm commented Aug 1, 2016

when is a array considered invalid? (and why?)

@yohgaki
Copy link
Contributor Author

yohgaki commented Aug 1, 2016

I updated the patch. It is considered invalid as soon as a validation rule fails now.

Yasuo Ohgaki added 5 commits August 2, 2016 10:37
All filters are used. Evaluation order is the same as defined. It's possible to mix sanitize and validation filters. Any validation failure result in exception.

Example
<?php
error_reporting(E_ALL | E_STRICT);
$data = array(
    'component'     => '10',
);

$args = array(
    'component'    =>
	array(
		array(
			'filter'    => FILTER_VALIDATE_INT,
			'options'   => array('min_range' => 1, 'max_range' => 10)
		),
		array(
			'filter' => FILTER_VALIDATE_REGEXP,
			'options' => array('regexp' => '/[0123456789]{2}/')
		),
		array(
			'filter' => FILTER_VALIDATE_FLOAT
		),
	),
);

$myinputs = validate_var_array($data, $args);

var_dump($myinputs);
Add validate_var() and validate_input() for consistency and testing feature.
#define FILTER_STRING_ENCODING_UTF8 1

#define FILTER_FLAG_STRING_RAW 0x0001
#define FILTER_FLAG_STRING_ALLOW_CNTRL 0x0002
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be named FILTER_FLAG_STRING_ALLOW_CTRL (without the 'n' in the end), as well as the userland constant as the constant name is already fairly long, and because CTRL is a more common abbreviation of control than CNTRL

Copy link
Contributor Author

@yohgaki yohgaki Aug 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem for me. The reason CNTRL is used is this API.

  • int iscntrl(int c);

I have mixed feeling. We have following

http://php.net/manual/en/intlchar.iscntrl.php
http://php.net/manual/en/function.ctype-cntrl.php

Any comments on using CTRL? Anyone?

Yasuo Ohgaki added 14 commits August 4, 2016 05:18
There is code assume invalid filter existence, yet it didn't exist.

Add filter_check_definition() function.

Filter definition error is silently ignored for perfomance reason.
Definition error could be fatal bug. This function provide check
feature finds typo, format error.
There is code assume invalid filter existence, yet it didn't exist.

Add filter_check_definition() function.

Filter definition error is silently ignored for perfomance reason.
Definition error could be fatal bug. This function provide check
feature finds typo, format error.
…php-src into master-rfc-validation-functions
…ers are retained because users will get invalid return value and continue execution. Added missing options and finish filter_check_definition().
This behavior is unacceptable for validation functions.

Removed trim from int/float/bool validation.

Bool validation filter converts empty strings to FALSE. This is not
prefered behavior for validation function. Reject empty string and
makede it optional.

FILTER_FLAG_BOOL_ALLOW_EMPTY is added.
Yasuo Ohgaki added 11 commits August 6, 2016 13:26
…ValidateException.

As a result, users could get more useful infomation about validation exceptions.
i.e. Message returned from getMessage() contains invalid_key, filter_name, filter_flags.

class FilterValidateException extends Exception {
   protected $invalid_key;
   protected $invalid_value;
   protected $filter_id;
   protected $filter_name;
   protected $filter_flags;
}

filter_get_invalid_key() has been removed by Exception improvement.
@smalyshev smalyshev added the RFC label Sep 5, 2016
@KalleZ
Copy link
Member

KalleZ commented Mar 2, 2019

Gonna close this due to inactivity, please open a new PR if you decide to pick up on it and post an RFC

@KalleZ KalleZ closed this Mar 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants