@@ -161,7 +161,7 @@ some string {$_POST[some_var]} {$_GET['evil']}
161161EOD
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 .
165165if ( ( $ _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+ }
0 commit comments