最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

security - How safe is current_user_can()?

programmeradmin0浏览0评论

I'm saving sensitive data via JSON ajax request, it is not sanitized, literally anything can be passed.

I need to make sure that only admins with edit_options permission can save it.

So I do:

function received_ajax_request() {
  if ( !current_user_can('edit_options') ) {
    return;
  }

  save_to_database( $_POST['something']) );
}

My question is how safe it is? Do I need to check for anything else besides current_user_can?

For example, I saw some other plugins check also for nonce, but when rest API is used - nonces are generally not used.

I'm saving sensitive data via JSON ajax request, it is not sanitized, literally anything can be passed.

I need to make sure that only admins with edit_options permission can save it.

So I do:

function received_ajax_request() {
  if ( !current_user_can('edit_options') ) {
    return;
  }

  save_to_database( $_POST['something']) );
}

My question is how safe it is? Do I need to check for anything else besides current_user_can?

For example, I saw some other plugins check also for nonce, but when rest API is used - nonces are generally not used.

Share Improve this question asked May 18, 2019 at 7:32 Marvin3Marvin3 6631 gold badge10 silver badges20 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

current_user_can checks whether current user has a specific capability. And only that...

It won’t protect you from XSS attacks - so it would be a good idea to check some nonces too - this way you can be certain that user wants to perform given action.

Let’s say there’s a link to delete a post. Of course you will check if user can delete posts. But what if a user is logged in and I make him click the link? He doesn’t have to know - it can be a shortened link or an image.

It also won’t check if the current user is owner of given object.

Let’s day I’m a customer in shop. Of course I can add comments to orders. But only for my orders.

So you should always check full access rights and not only roles and capabilities.

And of course it won’t make the action safe. So you still have to sanitize, and escape, and so on...

发布评论

评论列表(0)

  1. 暂无评论