Skip to content

Commit 44e5857

Browse files
committed
ValidatedSanitizedInput: add tests validating variables with array_key_exists()
1 parent 913f9cc commit 44e5857

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,24 @@ if ( isset( $_POST[ 'currentid' ] ) ){
188188
if ( isset ( $_POST['thisisnotget'] ) ) {
189189
$get = (int) $_GET['thisisnotget']; // Bad.
190190
}
191+
192+
// Recognize PHP native array_key_exists() as validation function.
193+
if ( array_key_exists( 'my_field1', $_POST ) ) {
194+
$id = (int) $_POST['my_field1']; // OK.
195+
}
196+
197+
if ( \array_key_exists( 'my_field2', $_POST ) ) {
198+
$id = (int) $_POST['my_field2']; // OK.
199+
}
200+
201+
if ( \Some\ClassName\array_key_exists( 'my_field3', $_POST ) ) {
202+
$id = (int) $_POST['my_field3']; // Bad.
203+
}
204+
205+
if ( $obj->array_key_exists( 'my_field4', $_POST ) ) {
206+
$id = (int) $_POST['my_field4']; // Bad.
207+
}
208+
209+
if ( ClassName::array_key_exists( 'my_field5', $_POST ) ) {
210+
$id = (int) $_POST['my_field5']; // Bad.
211+
}

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function getErrorList() {
5555
150 => 2,
5656
160 => 2,
5757
189 => 1,
58+
202 => 1,
59+
206 => 1,
60+
210 => 1,
5861
);
5962
}
6063

0 commit comments

Comments
 (0)