Skip to content
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

ValidatedSanitizedInput: handle null coalesce (equal) correctly #1684

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Apr 1, 2019

PHP 7.0 introduced the null coalesce operator, while PHP 7.4 will introduce the null coalesce equal operator.

These operators should be accounted for in the ValidatedSanitizedInput sniff as valid ways to validate a variable, but should still allow for the sniff to also check for sanitization.

Refs:

Related to #764

Includes unit tests.

Fixes #837
Closes #840 which is superseded by this PR


As part of this PR, two methods in the Sniff class received changes:

Sniff::is_comparison(): allow to disregard null coalesce

The null coalesce operator ?? is a special comparison operator, in the sense that it doesn't compare a variable to whatever is on the other side of the comparison operator.

For this reason, it should be possible to disregard it.

Sniff::is_validated(): recognize null coalesce (equal) operator as a way to validate a variable

This adds recognition of the coalesce operator ?? (PHP 7.0) and the coalesce equals operator ??=, as will be added in PHP 7.4, to the Sniff::is_validated() method.

This prevents false positives where variables would be seen as "not validated", when the variable has in fact been validated via a coalesce equals assignment in a previous statement.

Related to #764, #840

jrfnl added 5 commits April 1, 2019 21:42
The null coalesce operator `??` is a special comparison operator, in the sense that it doesn't compare a variable to whatever is on the other side of the comparison operator.

For this reason, it should be possible to disregard it.
…way to validate a variable

This adds recognition of the coalesce operator `??` (PHP 7.0) and the coalesce equals operator `??=`, as will be added in PHP 7.4, to the `Sniff::is_validated()` method.

This prevents false positives where variables would be seen as "not validated", when the variable has in fact been validated via a coalesce equals assignment in a previous statement.

Related to 764, 840
…l coalesce equals

PHP 7.0 introduced the null coalesce operator, while PHP 7.4 will introduce the null coalesce equal operator.

These operators should be accounted for in the `ValidatedSanitizedInput` sniff as valid ways to validate a variable, but should still allow for the sniff to *also* check for sanitization.

Refs:
* https://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce
* https://wiki.php.net/rfc/isset_ternary
* https://wiki.php.net/rfc/null_coalesce_equal_operator

Related to 764

Fixes 837
Closes 840 which is superseded by this PR
@GaryJones GaryJones merged commit 363ff11 into develop Apr 2, 2019
@GaryJones GaryJones deleted the feature/837-840-validatedsanitizedinput-allow-for-null-coalesce branch April 2, 2019 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Null coalescing operator triggers Detected usage of a non-validated input variable
2 participants