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

Null coalescing operator #840

Closed

Conversation

aaronjorbin
Copy link
Member

@aaronjorbin aaronjorbin commented Feb 10, 2017

Fixes #837

jrfnl and others added 2 commits August 29, 2016 22:04
Null coalescing operators were added to PHP in PHP7. They are "syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand."

WordPress_Sniff::is_in_isset_or_empty should assume that null coalescing operators satisfy being inside a empty or isset test.

Fixes WordPress#837
Copy link
Contributor

@JDGrimes JDGrimes left a comment

Choose a reason for hiding this comment

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

Nice. What about also adding tests for this in https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/blob/develop/WordPress/Tests/VIP/ValidatedSanitizedInputUnitTest.inc ? The example from the original ticket, for example: $test = $_GET['test'] ?? '';. Not sure how that will affect running against different PHP versions though.

Looks like there are also a few codestyle issues pointed out by Travis.

@jrfnl
Copy link
Member

jrfnl commented Feb 10, 2017

Not sure how that will affect running against different PHP versions though.

As the code is tokenized, not parsed, it should be fine as long as the tokens are accounted for in the various PHP versions.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

I second @JDGrimes request for tests. I've ran some tests and find that the current code will break on a number of test cases. Will post the test code and results in a separate comment.

I also found that when the token is correctly detected, the unslash error seems to go missing.

Also: please rebase the PR against the current develop branch.

@jrfnl
Copy link
Member

jrfnl commented Mar 2, 2017

Summary of the outcome of my tests (based on a rebased copy of the PR):

  • Third test case breaks because of the brackets
  • Fourth test case should give an error for unslash, but doesn't
  • Testcase 5 to 7 throw a non validated error, but shouldn't
  • Testcase 8 should throw two errors, but throws none.

Here's the test code I've used:

/*
 * Test recognition of the T_COALESCE token.
 */
$var = sanitize_text_field( wp_unslash( $_POST['foobar'] ?? '' ) ); // OK.
$var = sanitize_text_field( wp_unslash( $_POST['foobar']['sub'] ?? '' ) ); // OK.
$var = sanitize_text_field( wp_unslash( ( $_POST['foobar']['sub'] ) ?? '' ) ); // OK (extra brackets).
$var = sanitize_text_field( $_POST['foobar'] ?? '' ); // Bad x1 - unslash.

// The below two sets should give the same errors.
$var = $_POST['foobar'] ?? ''; // Bad x2 - sanitize + unslash.
$var = $_POST['foobar']['sub'] ?? ''; // Bad x2 - sanitize + unslash.
$var = ( $_POST['foobar']['sub'] ) ?? ''; // Bad x2 - sanitize + unslash.
$var = ( $_POST['foobar']['sub'] ?? '' ); // Bad x2 - sanitize + unslash.

$var = isset( $_POST['foobar'] ) ? $_POST['foobar'] : ''; // Bad x2 - sanitize + unslash.
$var = isset( $_POST['foobar']['sub'] ) ? $_POST['foobar']['sub'] : ''; // Bad x2 - sanitize + unslash.
$var = ( isset( $_POST['foobar']['sub'] ) ) ? $_POST['foobar']['sub'] : ''; // Bad x2 - sanitize + unslash.
$var = ( isset( $_POST['foobar']['sub'] ) ? $_POST['foobar']['sub'] : '' ); // Bad x2 - sanitize + unslash.

And these are the results:

----------------------------------------------------------------------
FOUND 20 ERRORS AFFECTING 8 LINES
----------------------------------------------------------------------
  8 | ERROR | Detected usage of a non-validated input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotValidated)
  8 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
  8 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
 12 | ERROR | Detected usage of a non-validated input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotValidated)
 12 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
 12 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
 13 | ERROR | Detected usage of a non-validated input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotValidated)
 13 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
 13 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
 14 | ERROR | Detected usage of a non-validated input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotValidated)
 14 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
 14 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
 17 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
 17 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
 18 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
 18 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
 19 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
 19 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
 20 | ERROR | Missing wp_unslash() before sanitization.
    |       | (WordPress.VIP.ValidatedSanitizedInput.MissingUnslash)
 20 | ERROR | Detected usage of a non-sanitized input variable:
    |       | $_POST
    |       | (WordPress.VIP.ValidatedSanitizedInput.InputNotSanitized)
----------------------------------------------------------------------

@jrfnl
Copy link
Member

jrfnl commented Mar 1, 2018

@aaronjorbin Just checking: Do you have any intention of updating this PR based on the above feedback or should we close the PR ?

@jrfnl
Copy link
Member

jrfnl commented Jun 11, 2018

@aaronjorbin Hi Aaron, just checking in again to see if you are still interested in finishing this. If you haven't got time or lost interest, please let us know and we'll see if we can find someone to take over.

@aaronjorbin
Copy link
Member Author

I'm not sure when I'll have time for this again, so if someone else is able to work on it they should absolutely feel free to.

@jrfnl
Copy link
Member

jrfnl commented Jun 11, 2018

@aaronjorbin Thanks for the quick reply. I'll see what I can do. WCEU contributors day coming up, so who knows ;-)

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
3 participants