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

Merge non validated and non sanitized errors #934

Conversation

david-binda
Copy link
Contributor

The ValidatedSanitizedInput Sniff is currently reporting two different errors for the same code, in case the global variable is not sanitized nor validated. This commit is merging the two errors into one, in caes both conditions are met. It's preserving the individual errors for cases when only one of them is true.

We have been using this improvement on the WordPress.com VIP for a while already (introduced in Automattic/VIP-Coding-Standards#12 )

cc. @grappler

The ValidatedSanitizedInput Sniff is currently reporting two different errors for the same code, in case the global variable is not sanitized nor validated. This commit is merging the two errors into one, in caes both conditions are met. It's preserving the individual errors for cases when only one of them is true.
…mit, where non-sanitized and non-validated errors, if both are present, are merged into a single error.

This commit is lowering the number of expected errors on lines 5 and 33 from 3 to 2 (which is the case due to the error merging).
@@ -122,8 +125,7 @@ public function process_token( $stackPtr ) {

// Check for validation first.
if ( ! $this->is_validated( $stackPtr, $array_key, $this->check_validation_in_scope_only ) ) {
$this->phpcsFile->addError( 'Detected usage of a non-validated input variable: %s', $stackPtr, 'InputNotValidated', $error_data );
// return; // Should we just return and not look for sanitizing functions ?
$notValidated = true;
}

if ( $this->has_whitelist_comment( 'sanitization', $stackPtr ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Because of this if condition and the one below it, if sanitization isn't needed the validation error would no longer be given either. This logic needs to be updated to fix that. (And also the unit tests, so that they'd catch this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good catch! I'll look into it, thanks!

The previous commits introduced bug which lead to loosing errors in case the sanitisation has been whitelisted via comment or in case the validation was missing for usage in comparison.
…ts - loosing errors on whitelisted sanitisation and missing validation on comparison
@@ -82,6 +82,9 @@ public function register() {
*/
public function process_token( $stackPtr ) {

$notValidated = false;
$notSanitized = false;
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering if the code would be easier to read if these variables would be reversed in meaning, i.e.

$validated = true;
$sanitized = true;

Also wondering whether the default should be true (or false as the current code stands), though changing that might mean more extensive changes to the logic of the sniff are needed.

@GaryJones
Copy link
Member

Has merge conflicts.

@jrfnl
Copy link
Member

jrfnl commented Mar 29, 2019

I've had another look at this and IMO this change will make the sniff less easy to work with for end-users and should not be merged.

The error codes, as they are, address separate problems which can currently be ignored via the ruleset or via inline comments separately using the appropriate error code.
By combining the error codes in certain cases, it makes it more fiddly for people to ignore issues.

For example, say someone has non-validated, non-sanitized code, but has in another method already validated and sanitized all superglobal keys they use. In that case, they could ignore the new InputNotValidatedNotSanitized code.

Now, in a refactor, they move the sanitization to the places where the superglobals are used. As the error code will now change, all the ignores will need to be updated, while in the original situation, this would not be necessary and the ignores (covering both original codes) would still work as expected.

With that in mind, I'm going to suggest closing this PR.

Aside from that:

  1. No response was ever received on the last feedback round.
  2. It's been in a merge conflict state for a pretty long time.
  3. This a breaking change which would have to go into a major release as it changes the error code for this sniff.

@jrfnl
Copy link
Member

jrfnl commented Oct 7, 2019

Closing for lack of response/progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants