64 ) { return false; } $hash = hash( 'sha256', $token ); foreach ( array( self::SELFCHECK_TRANSIENT, self::PROBE_TRANSIENT ) as $slot ) { $stored = get_transient( $slot ); if ( is_string( $stored ) && '' !== $stored && hash_equals( $stored, $hash ) ) { return true; } } return false; } /** * Whether the request carries the credentials of a user who can manage the site. * * @return bool */ private static function is_owner() { if ( is_user_logged_in() ) { return current_user_can( 'manage_options' ); } // No current user — which on a REST request means only that no nonce came with // the cookie, not that nobody is signed in. Ask the cookie directly. $uid = self::user_id_from_cookie(); return $uid > 0 && user_can( $uid, 'manage_options' ); } /** * The user id proven by the logged-in cookie, or 0. The cookie is verified * (`wp_validate_auth_cookie()` checks the HMAC and the session token), so a forged * one buys nothing but its own exclusion from the log. * * @return int */ private static function user_id_from_cookie() { // A crawler — and every logged-out visitor — sends no such cookie: the cheap exit, // taken on essentially every request that this log exists to record. if ( ! defined( 'LOGGED_IN_COOKIE' ) || empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) { return 0; } $cookie = wp_unslash( $_COOKIE[ LOGGED_IN_COOKIE ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- verified by wp_validate_auth_cookie() below; never stored or output. return (int) wp_validate_auth_cookie( $cookie, 'logged_in' ); } }