Skip to content

Commit 5224ff2

Browse files
committed
Security/ValidatedSanitizedInput: add tests for namespaced names
1 parent e0ebae2 commit 5224ff2

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.1.inc

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,110 @@ 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+
* Note: The "Bad" test cases below are false negatives. They should trigger 2 errors (MissingUnslash +
541+
* InputNotSanitized), not 1 (MissingUnslash). This problem only affects PHPCS 3.x and does not happen in PHPCS 4.x. It
542+
* will be addressed in https://github.com/WordPress/WordPress-Coding-Standards/issues/2665.
543+
*/
544+
function test_namespaced_unslashing_functions() {
545+
if ( isset( $_POST['unslash1'] ) ) {
546+
$text = sanitize_text_field( \wp_unslash( $_POST['unslash1'] ) ); // OK.
547+
}
548+
if ( isset( $_POST['unslash2'] ) ) {
549+
$text = sanitize_text_field( MyNamespace\wp_unslash( $_POST['unslash2'] ) ); // Bad.
550+
}
551+
if ( isset( $_POST['unslash3'] ) ) {
552+
$text = sanitize_text_field( \MyNamespace\wp_unslash( $_POST['unslash3'] ) ); // Bad.
553+
}
554+
if ( isset( $_POST['unslash4'] ) ) {
555+
$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.
556+
}
557+
}
558+
559+
/*
560+
* Safeguard correct handling of all types of namespaced calls to array walking functions.
561+
*/
562+
function test_namespaced_array_walking_functions() {
563+
if ( isset( $_POST['array_walk1'] ) ) {
564+
$text = \array_map( 'sanitize_text_field', \wp_unslash( $_POST['array_walk1'] ) ); // OK.
565+
}
566+
if ( isset( $_POST['array_walk2'] ) ) {
567+
$text = MyNamespace\array_map( 'sanitize_text_field', wp_unslash( $_POST['array_walk2'] ) ); // Bad.
568+
}
569+
if ( isset( $_POST['array_walk3'] ) ) {
570+
$text = \MyNamespace\array_map( 'sanitize_text_field', \wp_unslash( $_POST['array_walk3'] ) ); // Bad.
571+
}
572+
if ( isset( $_POST['array_walk4'] ) ) {
573+
$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.
574+
}
575+
}
576+
577+
/*
578+
* Safeguard correct handling of fully qualified and relative namespaced calls to sanitizing functions.
579+
* Qualified calls are already covered above.
580+
*/
581+
function test_namespaced_sanitizing_functions() {
582+
if ( isset( $_POST['sanitize1'] ) ) {
583+
$text = \sanitize_text_field( wp_unslash( $_POST['sanitize1'] ) ); // OK.
584+
}
585+
if ( isset( $_POST['sanitize2'] ) ) {
586+
$text = \MyNamespace\sanitize_text_field( wp_unslash( $_POST['sanitize2'] ) ); // Bad.
587+
}
588+
if ( isset( $_POST['sanitize3'] ) ) {
589+
$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.
590+
}
591+
}
592+
593+
/*
594+
* Safeguard correct handling of all types of namespaced calls to unslashing + sanitizing functions.
595+
*/
596+
function test_namespaced_unslashing_sanitizing_functions() {
597+
if ( isset( $_POST['unslash_sanitize1'] ) ) {
598+
$id = \absint( $_POST['unslash_sanitize1'] ); // OK.
599+
}
600+
if ( isset( $_POST['unslash_sanitize2'] ) ) {
601+
$id = MyNamespace\absint( $_POST['unslash_sanitize2'] ); // Bad.
602+
}
603+
if ( isset( $_POST['unslash_sanitize3'] ) ) {
604+
$id = \MyNamespace\absint( $_POST['unslash_sanitize3'] ); // Bad.
605+
}
606+
if ( isset( $_POST['unslash_sanitize4'] ) ) {
607+
$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.
608+
}
609+
}

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+
549 => 1,
126+
552 => 1,
127+
555 => 1,
128+
567 => 1,
129+
570 => 1,
130+
573 => 1,
131+
586 => 1,
132+
589 => 1,
133+
601 => 2,
134+
604 => 2,
135+
607 => 2,
117136
);
118137

119138
case 'ValidatedSanitizedInputUnitTest.2.inc':

0 commit comments

Comments
 (0)