rest_cookie_check_errors( WP_Error|mixed $result ): WP_Error|mixed|bool
- Since
- 4.4.0
- Source
wp-includes/rest-api.php:1130
Description
WordPress' built-in cookie authentication is always active for logged in users. However, the API has to check nonces for each request to ensure users are not vulnerable to CSRF.
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$resultWP_Error|mixed- Error from another authentication handler, null if we should handle it, or another value if not.
Return value
WP_Error|mixed|bool- WP_Error if the cookie is invalid, the $result, otherwise true.
Performance profile
How much work a call to rest_cookie_check_errors() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 4–40
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 59.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below rest_cookie_check_errors()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_cookie_check_errors() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($result) | 4 | none |
empty($result) && $wp_rest_auth_cookie is null, false, long, double, string, array, object, resource && is_user_logged_in() | 10 | is_user_logged_in() |
empty($result) && $wp_rest_auth_cookie is not null, false, long, double, string, array, object, resource && $nonce === null | 19–21 | wp_set_current_user() |
empty($result) && $wp_rest_auth_cookie is null, false, long, double, string, array, object, resource && !is_user_logged_in() && $nonce === null | 22–24 | is_user_logged_in(), wp_set_current_user() |
empty($result) && $wp_rest_auth_cookie is not null, false, long, double, string, array, object, resource && $nonce !== null | 31–33 | wp_verify_nonce(), rest_get_server(), wp_create_nonce(), ->send_header() |
empty($result) && $wp_rest_auth_cookie is null, false, long, double, string, array, object, resource && !is_user_logged_in() && $nonce !== null | 34–36 | is_user_logged_in(), wp_verify_nonce(), rest_get_server(), wp_create_nonce(), ->send_header() |
empty($result) && $wp_rest_auth_cookie is not null, false, long, double, string, array, object, resource && $nonce !== null | 35–37 | wp_verify_nonce(), add_filter(), __() |
empty($result) && $wp_rest_auth_cookie is null, false, long, double, string, array, object, resource && !is_user_logged_in() && $nonce !== null | 38–40 | is_user_logged_in(), wp_verify_nonce(), add_filter(), __() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 59 instructions, 4–40 executed per call, 7 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 9
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- wp_set_current_user()Changes the current user by ID or name.
- wp_verify_nonce()Verifies that a correct security nonce was used with time limit.
- add_filter()Adds a callback function to a filter hook.
- __()Retrieves the translation of $text.
- rest_get_server()Retrieves the current REST server instance.
- wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.
- WP_Error::__construct()Initializes the error.
- rest_get_server()::send_header()
Source code
function rest_cookie_check_errors( $result ) { if ( ! empty( $result ) ) { return $result; } global $wp_rest_auth_cookie; /* * Is cookie authentication being used? (If we get an auth * error, but we're still logged in, another authentication * must have been used). */ if ( true !== $wp_rest_auth_cookie && is_user_logged_in() ) { return $result; } // Determine if there is a nonce. $nonce = null; if ( isset( $_REQUEST['_wpnonce'] ) ) { $nonce = $_REQUEST['_wpnonce']; } elseif ( isset( $_SERVER['HTTP_X_WP_NONCE'] ) ) { $nonce = $_SERVER['HTTP_X_WP_NONCE']; } if ( null === $nonce ) { // No nonce at all, so act as if it's an unauthenticated request. wp_set_current_user( 0 ); return true; } // Check the nonce. $result = wp_verify_nonce( $nonce, 'wp_rest' ); if ( ! $result ) { add_filter( 'rest_send_nocache_headers', '__return_true', 20 ); return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie check failed' ), array( 'status' => 403 ) ); } // Send a refreshed nonce in header. rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) ); return true;}Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.