consumer_secret, $consumer_secret ) ) { return $user_id; } // WC keys need at least "read" permission for GET, "write" for POST/DELETE. if ( ! self::check_wc_key_permissions( $key_data->permissions ) ) { return $user_id; } // Update last access timestamp. self::update_last_access( $key_data->key_id ); return $key_data->user_id; } /** * Check if the current request targets a activity-log-for-mcp/ REST endpoint. * * @return bool */ private static function is_alfmcp_request() { $route = self::get_requested_rest_route(); if ( null === $route ) { return false; } return 0 === strpos( ltrim( $route, '/' ), 'activity-log-for-mcp/' ); } /** * Resolve the REST route the current request targets. * * This must never be derived from a substring search over the raw request * URI. REQUEST_URI includes the query string, so a bare `strpos()` lets an * attacker append the namespace as a throwaway parameter to any URL and * have this plugin's authentication fire on it. * * Two sources are used, in order of trustworthiness: * * 1. `$wp->query_vars['rest_route']` — the route WordPress actually parsed. * Authoritative, but only populated once `parse_request` has run. * 2. The path component of REQUEST_URI, anchored to the site's REST base. * `determine_current_user` frequently resolves before `parse_request` * (this is why WooCommerce core reads REQUEST_URI too), so a fallback is * required or key authentication would never fire. Unlike a substring * search, the prefix must appear at the *start* of the path, and the * query string is parsed rather than scanned. * * @return string|null Route relative to the REST base, or null if this is * not a REST request. */ private static function get_requested_rest_route() { // Authoritative once WordPress has parsed the request. if ( ! empty( $GLOBALS['wp']->query_vars['rest_route'] ) && is_string( $GLOBALS['wp']->query_vars['rest_route'] ) ) { return $GLOBALS['wp']->query_vars['rest_route']; } if ( empty( $_SERVER['REQUEST_URI'] ) ) { return null; } $request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); // Plain permalinks put the route in a query argument: /?rest_route=/ns/v1/x. // Parse it as a query string; never substring-search it. $query = wp_parse_url( $request_uri, PHP_URL_QUERY ); if ( is_string( $query ) && '' !== $query ) { $params = array(); wp_parse_str( $query, $params ); if ( ! empty( $params['rest_route'] ) && is_string( $params['rest_route'] ) ) { return $params['rest_route']; } } // Pretty permalinks: //. $path = wp_parse_url( $request_uri, PHP_URL_PATH ); if ( ! is_string( $path ) || '' === $path ) { return null; } $path = '/' . ltrim( $path, '/' ); // Strip the site's subdirectory, if WordPress is installed in one. $home_path = wp_parse_url( home_url( '/' ), PHP_URL_PATH ); $home_path = trailingslashit( is_string( $home_path ) && '' !== $home_path ? $home_path : '/' ); if ( '/' !== $home_path && 0 === strpos( $path, $home_path ) ) { $path = '/' . substr( $path, strlen( $home_path ) ); } // Some installs route REST through index.php: /index.php/wp-json/... if ( 0 === strpos( $path, '/index.php/' ) ) { $path = substr( $path, strlen( '/index.php' ) ); } $prefix = '/' . trailingslashit( rest_get_url_prefix() ); // Anchored at the start of the path — not searched anywhere within it. if ( 0 !== strpos( $path, $prefix ) ) { return null; } return substr( $path, strlen( $prefix ) ); } /** * Look up a WooCommerce API key by consumer_key. * * @param string $consumer_key The consumer key (unhashed). * @return object|null Key row or null. */ private static function get_wc_key_data( $consumer_key ) { global $wpdb; $hashed = wc_api_hash( $consumer_key ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Auth lookup must reflect current key state on every request; caching risks accepting revoked keys. return $wpdb->get_row( $wpdb->prepare( "SELECT key_id, user_id, permissions, consumer_key, consumer_secret FROM {$wpdb->prefix}woocommerce_api_keys WHERE consumer_key = %s", $hashed ) ); } /** * Check WC key permissions against the current HTTP method. * * @param string $permissions WC key permission level (read, write, read_write). * @return bool */ private static function check_wc_key_permissions( $permissions ) { $method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : 'GET'; if ( 'read_write' === $permissions ) { return true; } if ( 'read' === $permissions && 'GET' === $method ) { return true; } if ( 'write' === $permissions && in_array( $method, array( 'POST', 'PUT', 'PATCH', 'DELETE' ), true ) ) { return true; } return false; } /** * Update the last_access timestamp for a WC API key. * * @param int $key_id WC API key ID. */ private static function update_last_access( $key_id ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Updating WooCommerce-owned table; mirrors WC core's own update_last_access pattern. Caching N/A for write. $wpdb->update( $wpdb->prefix . 'woocommerce_api_keys', array( 'last_access' => current_time( 'mysql' ) ), array( 'key_id' => $key_id ), array( '%s' ), array( '%d' ) ); } }