|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * The below should NOT be considered validated. |
| 5 | + */ |
| 6 | + |
| 7 | +function test_not_validated() { |
| 8 | + echo /* testNotValidated */ $_POST['key']; |
| 9 | +} |
| 10 | + |
| 11 | +function test_outer_function_validation_not_counted() { |
| 12 | + if ( isset( $_POST['key'] ) ) { |
| 13 | + function test_inner_function() { |
| 14 | + echo /* testOuterFunctionValidationNotCounted */ $_POST['key']; |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function test_closed_scope_validation_not_counted() { |
| 20 | + $callback = function () { |
| 21 | + if ( isset( $_POST['key'] ) ) { |
| 22 | + echo $_POST['key']; |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + $obj = new class() { |
| 27 | + public function check() { |
| 28 | + if ( isset( $_POST['key'] ) ) { |
| 29 | + echo $_POST['key']; |
| 30 | + } |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + echo /* testClosedScopeValidationNotCounted */ $_POST['key']; |
| 35 | +} |
| 36 | + |
| 37 | +function test_arrow_function_validation_not_counted() { |
| 38 | + $check = fn() => isset( $_POST['key'] ); |
| 39 | + echo /* testArrowFunctionValidationNotCounted */ $_POST['key']; |
| 40 | +} |
| 41 | + |
| 42 | +function test_construct_wrong_variable_or_key() { |
| 43 | + if ( isset( $_GET['key'] ) || isset( $_POST['other'] ) ) { |
| 44 | + echo /* testConstructWrongVariableOrKey */ $_POST['key']; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function test_construct_param_key_mismatch() { |
| 49 | + if ( isset( $_POST['key'] ) ) { |
| 50 | + echo /* testConstructParamKeyMismatch */ $_POST['key']; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function test_function_name_used_as_constant() { |
| 55 | + echo ARRAY_KEY_EXISTS; |
| 56 | + echo /* testFunctionNameUsedAsConstant */ $_POST['key']; |
| 57 | +} |
| 58 | + |
| 59 | +function test_function_call_in_attribute() { |
| 60 | + #[array_key_exists('key', $_POST)] |
| 61 | + echo /* testFunctionCallInAttribute */ $_POST['key']; |
| 62 | +} |
| 63 | + |
| 64 | +function test_function_call_non_global() { |
| 65 | + if ( |
| 66 | + $obj->array_key_exists( 'key', $_POST ) |
| 67 | + || $obj?->key_exists( 'key', $_POST ) |
| 68 | + || MyClass::array_key_exists( 'key', $_POST ) |
| 69 | + ) { |
| 70 | + echo /* testFunctionCallNonGlobal */ $_POST['key']; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function test_function_call_namespaced() { |
| 75 | + if ( |
| 76 | + MyNamespace\array_key_exists( 'key', $_POST ) |
| 77 | + || \MyNamespace\key_exists( 'key', $_POST ) |
| 78 | + || namespace\array_key_exists( 'key', $_POST ) // This should be considered validated in the future once the method is able to resolve relative namespaces. |
| 79 | + || namespace\Sub\key_exists( 'key', $_POST ) |
| 80 | + ) { |
| 81 | + echo /* testFunctionCallNamespaced */ $_POST['key']; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +function test_function_call_missing_parameters() { |
| 86 | + if ( key_exists() ) { |
| 87 | + echo /* testFunctionCallMissingParameters */ $_POST['key']; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +function test_function_call_wrong_array_param() { |
| 92 | + if ( |
| 93 | + key_exists( 'key', SOME_CONSTANT ) |
| 94 | + || array_key_exists( 'key', $_GET ) |
| 95 | + ) { |
| 96 | + echo /* testFunctionCallWrongArrayParam */ $_POST['key']; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function test_function_call_mismatched_key() { |
| 101 | + if ( array_key_exists( 'other', $_POST ) ) { |
| 102 | + echo /* testFunctionCallMismatchedKey */ $_POST['key']; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function test_coalesce_no_match() { |
| 107 | + $a = 'something' ?? 'default'; |
| 108 | + $_GET['key'] ??= 'default'; |
| 109 | + $_POST['other'] ??= 'default'; |
| 110 | + echo /* testCoalesceNoMatch */ $_POST['key']; |
| 111 | +} |
| 112 | + |
| 113 | +/* |
| 114 | + * The below should be considered validated. |
| 115 | + */ |
| 116 | + |
| 117 | +function test_validated_with_isset() { |
| 118 | + if ( isset( $_POST["key"] ) ) { |
| 119 | + echo /* testValidatedWithIsset */ $_POST['key']; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function test_validated_with_empty() { |
| 124 | + if ( empty( $_POST['key'] ) ) { |
| 125 | + echo /* testValidatedWithEmpty */ $_POST['key']; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +$closure = function() { |
| 130 | + if ( empty( $_POST['key'] ) ) { |
| 131 | + echo /* testValidatedInClosure */ $_POST['key']; |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +function test_function_call() { |
| 136 | + if ( array_key_exists( 'key', $_POST ) ) { |
| 137 | + echo /* testFunctionCall */ $_POST['key']; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +function test_function_call_mixed_case() { |
| 142 | + if ( Key_Exists( 'key', $_POST ) ) { |
| 143 | + echo /* testFunctionCallMixedCase */ $_POST['key']; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function test_function_call_fully_qualified() { |
| 148 | + if ( \array_key_exists( 'key', $_POST ) ) { |
| 149 | + echo /* testFunctionCallFullyQualified */ $_POST['key']; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +function test_function_call_fully_qualified_uppercase() { |
| 154 | + if ( \KEY_EXISTS( 'key', $_POST ) ) { |
| 155 | + echo /* testFunctionCallFullyQualifiedUppercase */ $_POST['key']; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +function test_validated_with_null_coalesce() { |
| 160 | + $_POST['key'] = $_POST['key'] ?? 'default'; |
| 161 | + echo /* testValidatedWithNullCoalesce */ $_POST['key']; |
| 162 | +} |
| 163 | + |
| 164 | +function test_validated_with_coalesce_equal() { |
| 165 | + $_POST['key'] ??= 'default'; |
| 166 | + echo /* testValidatedWithCoalesceEqual */ $_POST['key']; |
| 167 | +} |
| 168 | + |
| 169 | +/* |
| 170 | + * Test cases for `$in_condition_only` set to true. |
| 171 | + */ |
| 172 | + |
| 173 | +function test_in_condition_only_use_inside_condition() { |
| 174 | + if ( isset( $_POST['key'] ) ) { |
| 175 | + echo /* testInConditionOnlyUseInsideCondition */ $_POST['key']; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +function test_in_condition_only_use_outside_condition() { |
| 180 | + if ( empty( $_POST['key'] ) ) { |
| 181 | + return; |
| 182 | + } |
| 183 | + echo /* testInConditionOnlyUseOutsideCondition */ $_POST['key']; |
| 184 | +} |
| 185 | + |
| 186 | +/* |
| 187 | + * Test cases for multi-level array key matching across validation paths. |
| 188 | + */ |
| 189 | + |
| 190 | +function test_array_keys_construct() { |
| 191 | + if ( isset( $_POST['key']['sub'] ) ) { |
| 192 | + echo /* testArrayKeysConstruct */ $_POST['key']['sub']; |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +function test_array_keys_function_call() { |
| 197 | + if ( array_key_exists( 'sub', $_POST['key'] ) ) { |
| 198 | + echo /* testArrayKeysFunctionCall */ $_POST['key']['sub']; |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +function test_array_keys_function_call_key_param_mismatch() { |
| 203 | + if ( array_key_exists( 'wrong_key', $_POST['key'] ) ) { |
| 204 | + echo /* testArrayKeysFunctionCallKeyParamMismatch */ $_POST['key']['sub']; |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +function test_array_keys_coalesce() { |
| 209 | + $_POST['key']['sub'] = $_POST['key']['sub'] ?? 'default'; |
| 210 | + echo /* testArrayKeysCoalesce */ $_POST['key']['sub']; |
| 211 | +} |
0 commit comments