$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>ajax - Should I check for privileges before hooking into `wp_ajax_$handle` or after?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ajax - Should I check for privileges before hooking into `wp_ajax_$handle` or after?

matteradmin6PV0评论

Should I heck for privileges before even hooking the wp_ajax_hook_name or in the function that I've hooked to it?

The first way:

if( current_user_can( 'manage_options' ) ) {
    add_action( 'wp_ajax_my_hook', array( $this, 'myHookFunction' ) );
}
//It won't even get to the function's code.

The second:

add_action( 'wp_ajax_my_hook', array( $this, 'myHookFunction' ) );
..
..
function myHookFunction() {
    if( current_user_can( 'manage_options' ) ) {
        return;
    }
    //All the code that would normally be executed is now skipped.
}

Here are a few considerations:

  1. The myHookFunction is a class, because it's hooked to an AJAX call, it has to be a public function, as such, anyone that's got access to my class can call the function itself.

    1.1. As such, we might encounter situations where a(nother) plugin running from a low-tier user calls my function that is supposed to actually not do anything, under any circumstance if the current calling user's privileges are not met, but remember, we only check for them in the class' __construct when we hook to wp_ajax_$.

  2. My concern is that I'm putting too much in a function, I tried my best to separate code from these functions and if I also add checks (even if minimal) for this too, the functions turn into a monster, but I won't sacrifice security over lines of code.

As such, it seems my second method is the best, security wise, but, assuming we're purists and we want clean classes, as well as clean functions, is it really the best? What are my options here?

Post a comment

comment list (0)

  1. No comments so far