interfaces
2 years ago
traits
2 years ago
match-not-regexp-rule.php
2 years ago
match-regexp-rule.php
2 years ago
must-contain-characters-rule.php
2 years ago
must-equal-rule.php
2 years ago
must-not-contain-characters-rule.php
2 years ago
rule.php
2 years ago
server-side-rule.php
1 month ago
server-side-rule.php
251 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Validation\Advanced_Rules; |
| 5 | |
| 6 | use JFB_Modules\Validation\Ssr; |
| 7 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 9 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 10 | use JFB_Modules\Validation\Handlers\Validation_Handler; |
| 11 | use Jet_Form_Builder\Request\Request_Tools; |
| 12 | |
| 13 | // If this file is called directly, abort. |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | class Server_Side_Rule extends Rule { |
| 19 | |
| 20 | use Repository_Pattern_Trait; |
| 21 | |
| 22 | /** |
| 23 | * Blacklist of dangerous functions that should never be allowed as callbacks. |
| 24 | * All values MUST be lowercase for case-insensitive comparison. |
| 25 | * |
| 26 | * @since 3.5.6.2 Security fix: expanded list and case-insensitive check |
| 27 | */ |
| 28 | const NOT_ALLOWED = array( |
| 29 | // Debug/output functions |
| 30 | 'var_dump', |
| 31 | 'var_export', |
| 32 | 'print_r', |
| 33 | 'sprintf', |
| 34 | 'printf', |
| 35 | // Command execution |
| 36 | 'shell_exec', |
| 37 | 'system', |
| 38 | 'exec', |
| 39 | 'passthru', |
| 40 | 'proc_open', |
| 41 | 'popen', |
| 42 | 'pcntl_exec', |
| 43 | 'proc_nice', |
| 44 | 'proc_terminate', |
| 45 | 'proc_close', |
| 46 | // Code execution |
| 47 | 'eval', |
| 48 | 'assert', |
| 49 | 'create_function', |
| 50 | 'call_user_func', |
| 51 | 'call_user_func_array', |
| 52 | 'preg_replace_callback', |
| 53 | 'array_map', |
| 54 | 'array_filter', |
| 55 | 'array_reduce', |
| 56 | 'usort', |
| 57 | 'uasort', |
| 58 | 'uksort', |
| 59 | 'array_walk', |
| 60 | 'array_walk_recursive', |
| 61 | // User mutations |
| 62 | 'wp_insert_user', |
| 63 | 'wp_update_user', |
| 64 | // File inclusion |
| 65 | 'include', |
| 66 | 'include_once', |
| 67 | 'require', |
| 68 | 'require_once', |
| 69 | // Serialization (object injection) |
| 70 | 'unserialize', |
| 71 | 'maybe_unserialize', |
| 72 | // File operations |
| 73 | 'file_get_contents', |
| 74 | 'file_put_contents', |
| 75 | 'fwrite', |
| 76 | 'fputs', |
| 77 | 'fopen', |
| 78 | 'readfile', |
| 79 | 'file', |
| 80 | 'fread', |
| 81 | 'fgets', |
| 82 | 'fgetc', |
| 83 | 'fgetcsv', |
| 84 | 'fpassthru', |
| 85 | 'move_uploaded_file', |
| 86 | 'copy', |
| 87 | 'rename', |
| 88 | 'unlink', |
| 89 | 'rmdir', |
| 90 | 'mkdir', |
| 91 | 'chmod', |
| 92 | 'chown', |
| 93 | 'chgrp', |
| 94 | // Network functions |
| 95 | 'curl_exec', |
| 96 | 'curl_multi_exec', |
| 97 | 'fsockopen', |
| 98 | 'pfsockopen', |
| 99 | 'stream_socket_client', |
| 100 | 'stream_socket_server', |
| 101 | // Dangerous PHP functions |
| 102 | 'parse_str', |
| 103 | 'extract', |
| 104 | 'putenv', |
| 105 | 'ini_set', |
| 106 | 'ini_alter', |
| 107 | 'dl', |
| 108 | 'mail', |
| 109 | 'header', |
| 110 | 'setcookie', |
| 111 | 'setrawcookie', |
| 112 | // POSIX functions |
| 113 | 'posix_kill', |
| 114 | 'posix_mkfifo', |
| 115 | 'posix_setpgid', |
| 116 | 'posix_setsid', |
| 117 | 'posix_setuid', |
| 118 | 'posix_setgid', |
| 119 | 'posix_seteuid', |
| 120 | 'posix_setegid', |
| 121 | // Apache functions |
| 122 | 'apache_child_terminate', |
| 123 | 'apache_setenv', |
| 124 | // Reflection/class manipulation |
| 125 | 'get_defined_functions', |
| 126 | 'get_defined_vars', |
| 127 | 'get_defined_constants', |
| 128 | 'phpinfo', |
| 129 | 'highlight_file', |
| 130 | 'show_source', |
| 131 | 'php_strip_whitespace', |
| 132 | 'get_cfg_var', |
| 133 | 'get_current_user', |
| 134 | 'getmyuid', |
| 135 | 'getmypid', |
| 136 | 'getenv', |
| 137 | ); |
| 138 | |
| 139 | public function __construct() { |
| 140 | $this->rep_install(); |
| 141 | } |
| 142 | |
| 143 | public function rep_instances(): array { |
| 144 | return apply_filters( |
| 145 | 'jet-form-builder/validation-callbacks', |
| 146 | array( |
| 147 | new Ssr\Is_User_Login_Unique(), |
| 148 | new Ssr\Is_User_Email_Unique(), |
| 149 | new Ssr\Is_Field_Value_Unique(), |
| 150 | new Ssr\Is_User_Password_Valid(), |
| 151 | ) |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | public function get_id(): string { |
| 156 | return 'ssr'; |
| 157 | } |
| 158 | |
| 159 | public function get_label(): string { |
| 160 | return __( 'Server-Side callback', 'jet-form-builder' ); |
| 161 | } |
| 162 | |
| 163 | public function validate_field( Field_Data_Parser $parser ) { |
| 164 | $function_name = $this->get_setting( 'value' ); |
| 165 | |
| 166 | if ( ! $this->validate_submission_signature( $parser ) ) { |
| 167 | $parser->collect_error( 'rule:ssr:invalid_signature', $this->get_setting( 'message' ) ); |
| 168 | |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | $is_valid = $this->validate( $parser, $function_name ); |
| 173 | |
| 174 | if ( $is_valid ) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | $parser->collect_error( 'rule:ssr:' . $function_name, $this->get_setting( 'message' ) ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @return Ssr\Base_Validation_Callback[] |
| 183 | */ |
| 184 | public function get_callbacks(): array { |
| 185 | return $this->rep_get_items(); |
| 186 | } |
| 187 | |
| 188 | protected function validate( Field_Data_Parser $parser, string $function_name ): bool { |
| 189 | try { |
| 190 | /** @var Ssr\Base_Validation_Callback $callback */ |
| 191 | $callback = $this->rep_get_item( $function_name ); |
| 192 | } catch ( Repository_Exception $exception ) { |
| 193 | return $this->validate_custom( $parser, $function_name ); |
| 194 | } |
| 195 | |
| 196 | return $callback->is_valid( $parser->get_value(), $parser->get_context() ); |
| 197 | } |
| 198 | |
| 199 | protected function validate_submission_signature( Field_Data_Parser $parser ): bool { |
| 200 | $rule_index = $this->get_setting( '_rule_index' ); |
| 201 | |
| 202 | if ( false === $rule_index ) { |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | return Validation_Handler::validate_main_signature( |
| 207 | Request_Tools::get_request(), |
| 208 | jet_fb_handler()->get_form_id(), |
| 209 | explode( '.', $parser->get_scoped_name() ), |
| 210 | absint( $rule_index ) |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | protected function validate_custom( Field_Data_Parser $parser, string $function_name ): bool { |
| 215 | $name = $this->validate_callback( $function_name ); |
| 216 | |
| 217 | if ( ! $name ) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | return (bool) call_user_func( $name, $parser->get_value(), $parser->get_context() ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Validate callback function name for security. |
| 226 | * |
| 227 | * @since 3.5.6.2 |
| 228 | * |
| 229 | * @param string $function_name The function name to validate. |
| 230 | * |
| 231 | * @return string Empty string if invalid, function name if valid. |
| 232 | */ |
| 233 | protected function validate_callback( string $function_name ): string { |
| 234 | $name = preg_replace( '/[^\w]/i', '', $function_name ); |
| 235 | |
| 236 | if ( $name !== $function_name ) { |
| 237 | return ''; |
| 238 | } |
| 239 | |
| 240 | // Case-insensitive blacklist check (PHP function names are case-insensitive) |
| 241 | $name_lower = strtolower( $name ); |
| 242 | |
| 243 | if ( in_array( $name_lower, self::NOT_ALLOWED, true ) ) { |
| 244 | return ''; |
| 245 | } |
| 246 | |
| 247 | return function_exists( $name ) ? $name : ''; |
| 248 | } |
| 249 | |
| 250 | } |
| 251 |