Skip to content

Commit 913f9cc

Browse files
authored
Merge pull request #1634 from WordPress-Coding-Standards/feature/sniff-is-validated-check-variable-name
ValidatedSanitizedInput: only recognize a variable as validated if the correct variable is examined
2 parents 3666bbb + b5acadb commit 913f9cc

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

WordPress/Sniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,10 @@ protected function is_validated( $stackPtr, $array_key = null, $in_condition_onl
17721772
continue;
17731773
}
17741774

1775+
if ( $this->tokens[ $stackPtr ]['content'] !== $this->tokens[ $i ]['content'] ) {
1776+
continue;
1777+
}
1778+
17751779
// If we're checking for a specific array key (ex: 'hello' in
17761780
// $_POST['hello']), that must match too. Quote-style, however, doesn't matter.
17771781
if ( isset( $array_key )

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ echo array_map( array( $obj, 'sanitize_text_field' ), wp_unslash( $_GET['test']
8585
$foo = (int) $_POST['foo6']; // Bad.
8686

8787
// Non-assignment checks are OK.
88-
if ( 'bar' === $_POST['foo'] ) {} // Ok.
88+
if ( isset( $_POST['foo'] ) && 'bar' === $_POST['foo'] ) {} // Ok.
8989
if ( $_GET['test'] != 'a' ) {} // Ok.
9090
if ( 'bar' === do_something( wp_unslash( $_POST['foo'] ) ) ) {} // Bad.
9191

@@ -183,3 +183,8 @@ function barfoo() {
183183
if ( isset( $_POST[ 'currentid' ] ) ){
184184
$id = (int) $_POST[ "currentid" ]; // OK.
185185
}
186+
187+
// Only recognize validation if the correct superglobal is examined.
188+
if ( isset ( $_POST['thisisnotget'] ) ) {
189+
$get = (int) $_GET['thisisnotget']; // Bad.
190+
}

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function getErrorList() {
5454
138 => 1,
5555
150 => 2,
5656
160 => 2,
57+
189 => 1,
5758
);
5859
}
5960

0 commit comments

Comments
 (0)