admin管理员组文章数量:1130349
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:
The
myHookFunctionis 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'
__constructwhen we hook towp_ajax_$.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?
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:
The
myHookFunctionis 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'
__constructwhen we hook towp_ajax_$.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?
本文标签: ajaxShould I check for privileges before hooking into wpajaxhandle or after
版权声明:本文标题:ajax - Should I check for privileges before hooking into `wp_ajax_$handle` or after? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749176516a2327887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论