Skip to content

Commit afdb1db

Browse files
committed
Security/ValidatedSanitizedInput: add tests for namespaced names
1 parent feaeb7c commit afdb1db

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.1.inc

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,106 @@ function test_in_match_condition_is_regarded_as_comparison() {
500500
};
501501
}
502502
}
503+
504+
/*
505+
* Safeguard correct handling of qualified and relative namespaced calls to array key exists functions.
506+
* Non-namespaced and fully qualified calls are already covered above.
507+
*/
508+
function test_namespaced_array_key_exists() {
509+
if ( MyNamespace\array_key_exists( 'key_exists1', $_POST ) ) {
510+
$id = (int) $_POST['key_exists1']; // Bad.
511+
}
512+
if ( namespace\array_key_exists( 'key_exists2', $_POST ) ) {
513+
$id = (int) $_POST['key_exists2']; // Bad. Note: This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
514+
}
515+
}
516+
517+
/*
518+
* Safeguard correct handling of all types of namespaced calls to type test functions.
519+
*/
520+
function test_namespaced_type_test_functions() {
521+
if ( isset( $_POST['type_test1'] ) && \is_int( $_POST['type_test1'] ) ) {} // OK.
522+
if ( isset( $_POST['type_test2'] ) && MyNamespace\is_int( $_POST['type_test2'] ) ) {} // Bad.
523+
if ( isset( $_POST['type_test3'] ) && \MyNamespace\is_int( $_POST['type_test3'] ) ) {} // Bad.
524+
if ( isset( $_POST['type_test4'] ) && namespace\is_int( $_POST['type_test4'] ) ) {} // Bad. Note: This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
525+
}
526+
527+
/*
528+
* Safeguard correct handling of all types of namespaced calls to array comparison functions.
529+
*/
530+
function test_namespaced_array_comparison_functions() {
531+
if ( isset( $_POST['array_cmp1'] ) && \in_array( $_POST['array_cmp1'], $my_array, true ) ) {} // OK.
532+
if ( isset( $_POST['array_cmp2'] ) && MyNamespace\in_array( $_POST['array_cmp2'], $my_array, true ) ) {} // Bad.
533+
if ( isset( $_POST['array_cmp3'] ) && \MyNamespace\in_array( $_POST['array_cmp3'], $my_array, true ) ) {} // Bad.
534+
if ( isset( $_POST['array_cmp4'] ) && namespace\in_array( $_POST['array_cmp4'], $my_array, true ) ) {} // Bad. Note: This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
535+
}
536+
537+
/*
538+
* Safeguard correct handling of all types of namespaced calls to unslashing functions.
539+
*/
540+
function test_namespaced_unslashing_functions() {
541+
if ( isset( $_POST['unslash1'] ) ) {
542+
$text = sanitize_text_field( \wp_unslash( $_POST['unslash1'] ) ); // OK.
543+
}
544+
if ( isset( $_POST['unslash2'] ) ) {
545+
$text = sanitize_text_field( MyNamespace\wp_unslash( $_POST['unslash2'] ) ); // Bad.
546+
}
547+
if ( isset( $_POST['unslash3'] ) ) {
548+
$text = sanitize_text_field( \MyNamespace\wp_unslash( $_POST['unslash3'] ) ); // Bad.
549+
}
550+
if ( isset( $_POST['unslash4'] ) ) {
551+
$text = sanitize_text_field( namespace\wp_unslash( $_POST['unslash4'] ) ); // Bad. Note: This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
552+
}
553+
}
554+
555+
/*
556+
* Safeguard correct handling of all types of namespaced calls to array walking functions.
557+
*/
558+
function test_namespaced_array_walking_functions() {
559+
if ( isset( $_POST['array_walk1'] ) ) {
560+
$text = \array_map( 'sanitize_text_field', \wp_unslash( $_POST['array_walk1'] ) ); // OK.
561+
}
562+
if ( isset( $_POST['array_walk2'] ) ) {
563+
$text = MyNamespace\array_map( 'sanitize_text_field', wp_unslash( $_POST['array_walk2'] ) ); // Bad.
564+
}
565+
if ( isset( $_POST['array_walk3'] ) ) {
566+
$text = \MyNamespace\array_map( 'sanitize_text_field', \wp_unslash( $_POST['array_walk3'] ) ); // Bad.
567+
}
568+
if ( isset( $_POST['array_walk4'] ) ) {
569+
$text = namespace\array_map( 'sanitize_text_field', wp_unslash( $_POST['array_walk4'] ) ); // Bad. Note: This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
570+
}
571+
}
572+
573+
/*
574+
* Safeguard correct handling of fully qualified and relative namespaced calls to sanitizing functions.
575+
* Qualified calls are already covered above.
576+
*/
577+
function test_namespaced_sanitizing_functions() {
578+
if ( isset( $_POST['sanitize1'] ) ) {
579+
$text = \sanitize_text_field( wp_unslash( $_POST['sanitize1'] ) ); // OK.
580+
}
581+
if ( isset( $_POST['sanitize2'] ) ) {
582+
$text = \MyNamespace\sanitize_text_field( wp_unslash( $_POST['sanitize2'] ) ); // Bad.
583+
}
584+
if ( isset( $_POST['sanitize3'] ) ) {
585+
$text = namespace\sanitize_text_field( wp_unslash( $_POST['sanitize3'] ) ); // Bad. Note: This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
586+
}
587+
}
588+
589+
/*
590+
* Safeguard correct handling of all types of namespaced calls to unslashing + sanitizing functions.
591+
*/
592+
function test_namespaced_unslashing_sanitizing_functions() {
593+
if ( isset( $_POST['unslash_sanitize1'] ) ) {
594+
$id = \absint( $_POST['unslash_sanitize1'] ); // OK.
595+
}
596+
if ( isset( $_POST['unslash_sanitize2'] ) ) {
597+
$id = MyNamespace\absint( $_POST['unslash_sanitize2'] ); // Bad.
598+
}
599+
if ( isset( $_POST['unslash_sanitize3'] ) ) {
600+
$id = \MyNamespace\absint( $_POST['unslash_sanitize3'] ); // Bad.
601+
}
602+
if ( isset( $_POST['unslash_sanitize4'] ) ) {
603+
$id = namespace\absint( $_POST['unslash_sanitize4'] ); // Bad. Note: This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
604+
}
605+
}

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ public function getErrorList( $testFile = '' ) {
114114
497 => 1,
115115
498 => 1,
116116
499 => 3,
117+
510 => 1,
118+
513 => 1,
119+
522 => 2,
120+
523 => 2,
121+
524 => 2,
122+
532 => 2,
123+
533 => 2,
124+
534 => 2,
125+
545 => 1,
126+
548 => 1,
127+
551 => 1,
128+
563 => 1,
129+
566 => 1,
130+
569 => 1,
131+
582 => 1,
132+
585 => 1,
133+
597 => 2,
134+
600 => 2,
135+
603 => 2,
117136
);
118137

119138
case 'ValidatedSanitizedInputUnitTest.2.inc':

0 commit comments

Comments
 (0)