Skip to content

Commit 1a79eae

Browse files
committed
ValidatedSanitizedInput: add unit tests for null coalesce (equals) operators
1 parent 0ef9e85 commit 1a79eae

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ some string {$_POST[some_var]} {$_GET['evil']}
161161
EOD
162162
); // Bad x2.
163163

164-
if ( ( $_POST['foo'] ?? 'post' ) === 'post' ) {} // OK.
164+
if ( ( $_POST['foo'] ?? 'post' ) === 'post' ) {} // Bad x2 - unslash, sanitize - more complex compares are not handled.
165165
if ( ( $_POST['foo'] <=> 'post' ) === 0 ) {} // OK.
166166

167167
// Test whitespace independent isset/empty detection.
@@ -289,3 +289,36 @@ function test_recognize_array_comparison_functions_as_such() {
289289
if ( array_keys( $_POST['form_fields'], 'my_form_hidden_field_value', true ) ) {} // OK.
290290
if ( array_keys( $_POST['form_fields'] ) ) {} // Bad x2.
291291
}
292+
293+
/*
294+
* Test recognition of validation via null coalesce, while still checking the var for sanitization.
295+
*/
296+
function test_null_coalesce_1() {
297+
$var = sanitize_text_field( wp_unslash( $_POST['foo'] ?? '' ) ); // OK.
298+
$var = sanitize_text_field( wp_unslash( $_POST['fool'] ?? $_POST['secondary'] ?? '' ) ); // OK.
299+
$var = sanitize_text_field( wp_unslash( $_POST['bar']['sub'] ?? '' ) ); // OK.
300+
$var = sanitize_text_field( $_POST['foobar'] ?? '' ); // Bad x1 - unslash.
301+
}
302+
303+
// The below two sets should give the same errors.
304+
function test_null_coalesce_2() {
305+
$var = $_POST['foo'] ?? ''; // Bad x2 - sanitize + unslash.
306+
$var = $_POST['bar']['sub'] ?? ''; // Bad x2 - sanitize + unslash.
307+
$var = ( $_POST['foobar']['sub'] ?? '' ); // Bad x2 - sanitize + unslash.
308+
309+
$var = isset( $_POST['_foo'] ) ? $_POST['_foo'] : ''; // Bad x2 - sanitize + unslash.
310+
$var = isset( $_POST['_bar']['_sub'] ) ? $_POST['_bar']['_sub'] : ''; // Bad x2 - sanitize + unslash.
311+
$var = ( isset( $_POST['_foobar']['_sub'] ) ? $_POST['_foobar']['_sub'] : '' ); // Bad x2 - sanitize + unslash.
312+
}
313+
314+
function test_null_coalesce_validation() {
315+
$_POST['key'] = $_POST['key'] ?? 'default'; // OK, assignment & Bad x2 - unslash, sanitize.
316+
$key = sanitize_text_field( wp_unslash( $_POST['key'] ) ); // OK, validated via null coalesce.
317+
$another_key = sanitize_text_field( wp_unslash( $_POST['another_key'] ) ); // Bad, not validated, different key.
318+
}
319+
320+
function test_null_coalesce_equals_validation() {
321+
$_POST['key'] ??= 'default'; // OK, assignment.
322+
$key = sanitize_text_field( wp_unslash( $_POST['key'] ) ); // OK, validated via null coalesce equals.
323+
$another_key = sanitize_text_field( wp_unslash( $_POST['another_key'] ) ); // Bad, not validated, different key.
324+
}

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.php

Lines changed: 11 additions & 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+
164 => 2,
5758
189 => 1,
5859
202 => 1,
5960
206 => 1,
@@ -68,6 +69,16 @@ public function getErrorList() {
6869
266 => 1,
6970
277 => 1,
7071
290 => 2,
72+
300 => 1,
73+
305 => 2,
74+
306 => 2,
75+
307 => 2,
76+
309 => 2,
77+
310 => 2,
78+
311 => 2,
79+
315 => 2,
80+
317 => 1,
81+
323 => 1,
7182
);
7283
}
7384

0 commit comments

Comments
 (0)