← Back
Editing: class-GD-cORe-updater.php
<?php if ( ! defined( 'ABSPATH' ) ) { exit; } class GD_cORe_Updater { const PLUGINS_CEPH_DOWNLOAD_PLUGIN = "plugins/:key/signed-url"; const PLUGINS_GET_AVAILABLE_PLUGINS = 'plugins'; const EMPTY_STRING = ''; private $slug; // plugin slug private $pluginData; // plugin data private $pluginFile; // __FILE__ of our plugin private $coreReleaseInfo; // holds data from gdcore API const OPTION_NAME = 'gd_core_update_response'; function __construct( $pluginFile ) { add_filter( "pre_set_site_transient_update_plugins", array( $this, "set_transient" ) ); add_filter( "plugins_api", array( $this, "set_plugin_info" ), 10, 3 ); add_filter( 'auto_update_plugin', array( $this, 'auto_update' ), 10, 2 ); add_filter( 'user_has_cap', array( $this, 'remove_deactivate_cap'), 10, 3 ); add_filter( 'upgrader_pre_download', array( $this, 'verify_and_download' ), 10, 4 ); $this->pluginFile = $pluginFile; $this->slug = plugin_basename( $this->pluginFile ); } /** * Auto update this plugin. * * @param boolean $update Whether or not the plugin should be auto updated. * @param object $item Plugin object. * * @return boolean True when the plugin should auto update, else false. */ public function auto_update( $update, $item ) { return $this->slug === $item->plugin ? true : $update; } /** * Intercept the WP upgrader's download step for this plugin. Fetch the * ZIP ourselves, verify its SHA-256 against the digest the GD Core API * published, and only hand WordPress a file path on match. On mismatch * or missing digest, return a WP_Error so the upgrader aborts before * anything gets extracted into wp-content/plugins. * * Fail-closed by design: no digest means we can't prove the package * matches what the API published, so we refuse to install it. * * @param bool|WP_Error $reply Short-circuit return from a prior filter. * @param string $package URL WordPress would otherwise download. * @param WP_Upgrader $upgrader * @param array $hook_extra Upgrade context; hook_extra['plugin'] is the slug. */ public function verify_and_download( $reply, $package, $upgrader, $hook_extra = array() ) { // Only intervene for our plugin. Every other plugin still follows // the default WP path. $target_plugin = isset( $hook_extra['plugin'] ) ? $hook_extra['plugin'] : null; if ( $target_plugin !== $this->slug ) { return $reply; } $expected = $this->get_expected_sha256_for_package( $package ); if ( empty( $expected ) ) { return new \WP_Error( 'gd_core_missing_digest', 'Refusing to install GD cORe update: no SHA-256 digest was published for this release. This is a fail-closed guard against unsigned auto-updates. Contact the GD cORe team if this persists.' ); } require_once ABSPATH . 'wp-admin/includes/file.php'; $temp_file = download_url( $package ); if ( is_wp_error( $temp_file ) ) { return $temp_file; } $actual = hash_file( 'sha256', $temp_file ); if ( ! hash_equals( strtolower( $expected ), strtolower( (string) $actual ) ) ) { @unlink( $temp_file ); error_log( sprintf( 'GD cORe update refused: sha256 mismatch. expected=%s actual=%s package=%s', $expected, $actual, $package ) ); return new \WP_Error( 'gd_core_digest_mismatch', 'Refusing to install GD cORe update: downloaded package does not match the expected SHA-256. The upstream artifact may have been tampered with.' ); } return $temp_file; } /** * Look up the expected SHA-256 for a package URL we handed WordPress * earlier via set_transient(). Reading it back from the same option * means both flows (WP's cached transient and our stored copy) agree. */ private function get_expected_sha256_for_package( $package ) { $stored = get_option( self::OPTION_NAME, false ); if ( is_object( $stored ) && isset( $stored->package ) && $stored->package === $package && isset( $stored->sha256 ) ) { return (string) $stored->sha256; } return self::EMPTY_STRING; } /** * Prevent User from deactivating the plugin entirely. * The only way will be to delete the plugin in the filesystem. * ref: https://developer.wordpress.org/reference/hooks/user_has_cap/ * * @param array $allcaps * @param string $cap * @param array $args * * @return array */ public function remove_deactivate_cap( array $allcaps, $cap, $args ) { if ( $args[0] === 'deactivate_plugin' && $args[2] === $this->slug ) { $allcaps[ $cap[0] ] = false; } return $allcaps; } // Get information regarding our plugin from WordPress private function initPluginData() { $this->pluginData = get_plugin_data( $this->pluginFile ); } // Get information regarding our plugin from gdcore api private function getRepoReleaseInfo() { if (!empty($this->coreReleaseInfo)) { return; } $endpoint = self::PLUGINS_GET_AVAILABLE_PLUGINS; $gdcoreResponse = $this->callGdcoreAPI($endpoint); if (!empty($gdcoreResponse) && is_object($gdcoreResponse)) { foreach ($gdcoreResponse as $key => $plugin) { if (isset($plugin->name) && $plugin->name === 'GD cORe') { $this->coreReleaseInfo = $plugin; break; } } } } /** * @param string $endpoint * @return mixed|string */ public function callGdcoreAPI(string $endpoint) { $uri = GD_CORE_API_BASE . $endpoint . '?accountId=' . GD_ACCOUNT_ID_IN_CORE; $response = wp_remote_get($uri, array( 'headers' => array( 'User-Agent' => 'GDCorePlugin/' . GD_cORe_VERSION, 'x-app-key' => 'wordpress-site', ) )); if (is_wp_error($response)) { return self::EMPTY_STRING; } $responseCode = wp_remote_retrieve_response_code($response); if ($responseCode !== 200) { return self::EMPTY_STRING; } $body = wp_remote_retrieve_body($response); return @json_decode($body); } /** * Fetch the signed download URL and its server-computed SHA-256 digest * for a plugin. Returns [url, sha256] or ['', ''] on failure. sha256 may * be empty when the API response omits it, which the caller must treat * as a fail-closed condition — installing an update without a digest to * verify against would defeat the point. */ private function retrieveCephUrlForPlugin($pluginKey) { // Replace `:key` with the actual plugin key $endpoint = str_replace(':key', urlencode($pluginKey), self::PLUGINS_CEPH_DOWNLOAD_PLUGIN); $gdcoreResponse = $this->callGdcoreAPI($endpoint); if (!empty($gdcoreResponse) && isset($gdcoreResponse->url)) { $sha256 = isset($gdcoreResponse->sha256) ? $gdcoreResponse->sha256 : self::EMPTY_STRING; return array($gdcoreResponse->url, $sha256); } return array(self::EMPTY_STRING, self::EMPTY_STRING); } // Push in plugin version information to get the update notification public function set_transient( $transient ) { $gd_core_up_resp = get_option( self::OPTION_NAME, false ); // Cache hit — but only trust it if it already carries a sha256. // Entries persisted before digest support won't have one, and we'd // rather force a fresh API call than hand WordPress a package URL // that the verify step will then reject as un-digestable. $cache_fresh = $gd_core_up_resp && 12 * HOUR_IN_SECONDS > ( time() - $gd_core_up_resp->last_checked ); $cache_has_digest = ! isset( $gd_core_up_resp->new_version ) || ! empty( $gd_core_up_resp->sha256 ); if ( $cache_fresh && $cache_has_digest ) { if ( isset( $gd_core_up_resp->new_version ) ) { unset( $gd_core_up_resp->last_checked ); $transient->response[ $this->slug ] = $gd_core_up_resp; } return $transient; } // Get plugin & GitHub release information $this->initPluginData(); $this->getRepoReleaseInfo(); $oldVersion = $transient->checked[$this->slug] ?? $this->pluginData["Version"]; // Check the versions if we need to do an update $doUpdate = version_compare( $this->coreReleaseInfo->version, $oldVersion); $obj = new \stdClass(); $obj->last_checked = time(); // Update the transient to include our updated plugin data if ( $doUpdate !== 1 ) { $transient->no_update[ $this->slug ] =$obj; update_option( self::OPTION_NAME, $obj ); return $transient; } $key = $this->coreReleaseInfo->key; list($package, $sha256) = $this->retrieveCephUrlForPlugin($key); $obj->slug = $this->slug; $obj->plugin = $this->slug; $obj->new_version = $this->coreReleaseInfo->version; $obj->url = $this->pluginData["PluginURI"]; $obj->package = $package; $obj->sha256 = $sha256; // There is a bug in how WordPress fetches transient on page load and during cron which result // in inconsistent data structure (response field often disappearing). // This will make sure we don't overload github API. update_option( self::OPTION_NAME, $obj ); unset( $obj->last_checked ); $transient->response[ $this->slug ] = $obj; return $transient; } // Push in plugin version information to display in the details lightbox public function set_plugin_info( $false, $action, $args ) { // If nothing is found, do nothing if ( empty( $args->slug ) || $args->slug != $this->slug ) { return $false; } // Get plugin & GitHub release information $this->initPluginData(); $this->getRepoReleaseInfo(); // Add our plugin information $args->slug = $this->slug; $args->plugin_name = $this->pluginData["Name"]; $args->version = $this->coreReleaseInfo->version; $args->author = $this->pluginData["AuthorName"]; $args->homepage = $this->pluginData["PluginURI"]; $id = $this->coreReleaseInfo->key; list($download_link, $_sha256) = $this->retrieveCephUrlForPlugin($id); $args->download_link = $download_link; return $args; } }
Save File
Cancel