validate_callback is called before permission_callback. I think this is wrong
-
I am building a REST endpoint that goes like this
users/articles/{article_id}while{article_id}can be validated withvalidate_callbackon the$argsparameter level.
In my validation, I check for author of the article to match the current logged in user. If not, it returns WP_Error of 404, assuming the user is logged in.
I expect 403 permission error to come up for guest, but it does not. Rather, it throws 404 since the user is not logged in.permission_callbackis called aftervalidate_callbackwhich I think is wrong. Permissions should come first before validation in my opinion
Exampleregister_rest_route($this->namespace, $this->resource_name .'/(?P<article_id>[\d]+)', [
'methods' => WP_REST_Server::READABLE,
'permission_callback' => [$this, 'get_item_permissions_check'], // This happens later
'callback' => [$this, 'get_item'],
'validate_callback' => [self::class, 'validate_article'], // This is the validation
]);
-
If I understand correctly, permission_callback is checked here: https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/class-wp-rest-server.php#L1257 – only then is callback called.
I am therefore unsure which area you are referring to?
By the way: if you really consider this to be a bug, you can also open a ticket with the core trac so that the core developers can evaluate it: https://core.trac.wordpress.org/newticket
That is where the method is defined, but it is called at L1125 while L1113 is where the validation happens first.
I don’t see anything like that in the lines:
https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/class-wp-rest-server.php#L1113
https://github.com/WordPress/WordPress/blob/master/wp-includes/rest-api/class-wp-rest-server.php#L1125Are you looking at an older version of the source code?
No. That is the right one. The validations happen first in 1113, then the
respond_to_requestmethod is called in 1125 where permission_callback in handled. I think permission checks should come first before validations.If you see a need for improvement in the WordPress core, create a ticket with the core development team as described above.
The topic ‘validate_callback is called before permission_callback. I think this is wrong’ is closed to new replies.