← Back
Editing: class-hosting-api.php
<?php namespace WPaaS; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Client for the hosting control plane's site-signed routes. * * Every other API call this plugin makes is authenticated as the *product* — a shared * wpsecurity credential that says nothing about which site is calling. These routes are * authenticated as the *site*: a per-app, per-variant HMAC key the platform renders into * this app's own gd-config, so a signature proves the caller is this app's publish (or * staging) filesystem and nothing else. That is what lets a CDN purge be scoped to the * calling app without the caller naming an app id the control plane has to trust. * * Availability is a platform decision, not a site one: with no delivered base URL, no v2 * app id, or no delivered key, every entry point here answers as if the lane did not exist * and the caller keeps its legacy behavior. That is what makes this shippable ahead of * the control-plane half. */ final class Hosting_API { use Helpers; /** * Signature header, matching the control plane's verifier. */ const SIG_HEADER = 'X-Wp-Site-Sig'; /** * The only flush scope the control plane accepts. A closed vocabulary rather than a * pass-through: a scope the verifier does not know refuses the whole request. */ const SCOPE_ALL = 'all'; /** * The only urgency lane enabled today. Deliberately outside the signed canonical, so * adding a lane later does not invalidate a release's signatures. */ const LANE_INTERACTIVE = 'interactive'; /** * Canonical discriminators. Both canonicals are four fields joined by newlines, so * the leading prefix is the only thing stopping a signature minted for one route * from replaying on the other. */ const FLUSH_PREFIX = 'cache-flush'; const STATUS_PREFIX = 'cache-flush-status'; /** * Whether the site-signed lane is usable in this container. * * @return bool */ public static function is_available() { return '' !== self::base_url() && '' !== self::app_id() && '' !== self::signing_key(); } /** * Request a CDN purge for this app and variant. * * @param string $scope * * @return string|null Request id to poll, or null when the call did not land. */ public static function request_cache_flush( $scope = self::SCOPE_ALL ) { if ( self::SCOPE_ALL !== $scope ) { return null; } $variant = self::variant(); $body = [ 'variant' => $variant, 'scope' => $scope, 'lane' => self::LANE_INTERACTIVE, ]; $signature = self::sign( [ self::FLUSH_PREFIX, self::app_id(), $variant, $scope ] ); if ( '' === $signature ) { return null; } // wp_json_encode here and not at the canonical: the signature covers the field // values, not the serialization, so the two cannot drift on key order or escaping. $response = Plugin::remote_post( self::endpoint( 'cache-flush' ), [ 'timeout' => 10, 'headers' => [ 'Content-Type' => 'application/json', self::SIG_HEADER => $signature, ], 'body' => wp_json_encode( $body ), ] ); $decoded = self::decode( $response ); return empty( $decoded['requestId'] ) ? null : (string) $decoded['requestId']; } /** * Poll one flush request. * * Answers in the legacy CDN vocabulary (SUCCESS / FAILED / PENDING) so the admin * status route and its poller do not need to learn a second one. * * @param string $request_id * * @return array|null */ public static function cache_flush_status( $request_id ) { $request_id = (string) $request_id; if ( '' === $request_id ) { return null; } $variant = self::variant(); $signature = self::sign( [ self::STATUS_PREFIX, self::app_id(), $variant, $request_id ] ); if ( '' === $signature ) { return null; } $url = add_query_arg( 'variant', $variant, self::endpoint( 'cache-flush/' . rawurlencode( $request_id ) ) ); $response = Plugin::remote_get( $url, [ 'timeout' => 10, 'headers' => [ self::SIG_HEADER => $signature ], ] ); $decoded = self::decode( $response ); if ( null === $decoded ) { return null; } return [ 'status' => self::legacy_status( $decoded ), 'state' => isset( $decoded['state'] ) ? $decoded['state'] : '', 'requestId' => $request_id, ]; } /** * Report the actual WordPress title. Only HTTP 200 with boolean success acknowledges delivery. * The raw JSON digest binds every field to this route's signature. * * @param string $title Decoded blogname (at most 1024 UTF-8 bytes). * @return bool Whether hosting acknowledged this request. */ public static function report_site_title( $title ) { if ( ! self::is_available() || ! is_string( $title ) || strlen( $title ) > 1024 ) { return false; } $variant = self::variant(); $body = wp_json_encode( [ 'variant' => $variant, 'title' => $title ] ); if ( ! is_string( $body ) ) { return false; } $digest = rtrim( strtr( base64_encode( hash( 'sha256', $body, true ) ), '+/', '-_' ), '=' ); $signature = self::sign( [ 'site-title', self::app_id(), $variant, $digest ] ); $response = Plugin::remote_request( self::endpoint( 'site-title' ), [ 'method' => 'PUT', 'timeout' => 5, 'redirection' => 0, 'limit_response_size' => 4096, 'headers' => [ 'Content-Type' => 'application/json', self::SIG_HEADER => $signature ], 'body' => $body, ] ); if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return false; } $decoded = self::decode( $response ); return is_array( $decoded ) && isset( $decoded['success'] ) && true === $decoded['success']; } /** * Which variant's key this filesystem holds. * * Read from the delivered constant rather than Plugin::is_staging_site(), which is * filterable: a site that could talk itself into the other variant would sign a * canonical naming a variant whose key it does not have, and every call would refuse. * * @return string */ public static function variant() { return ( defined( 'GD_STAGING_SITE' ) && GD_STAGING_SITE ) ? 'staging' : 'publish'; } /** * Full URL for one site-signed endpoint. * * @param string $path * * @return string */ private static function endpoint( $path ) { return self::base_url() . 'v1/wp-site/apps/' . rawurlencode( self::app_id() ) . '/' . $path; } /** * The hosting API root delivered by the platform, if any. * * Held to the same shape bound as the SSO base — https, a hostname rather than an * address literal, no credentials, default port, no query or fragment — because an * absent constant is definable by site code, and this one decides where the site's * signed requests go. Anything else reads as not delivered. * * @return string */ private static function base_url() { $url = defined( 'GD_HOSTING_API_URL' ) ? trim( (string) GD_HOSTING_API_URL ) : ''; if ( '' === $url || ! API::is_platform_endpoint( $url ) ) { return ''; } return trailingslashit( $url ); } /** * This app's id, or empty when the site is not on v2. * * @return string */ private static function app_id() { if ( ! self::is_wpaas_v2() ) { return ''; } $env = (string) getenv( 'WPAAS_V2_SITE_ID' ); $parts = explode( ':', $env ); return isset( $parts[1] ) ? trim( $parts[1] ) : ''; } /** * The signing key for this app and variant. * * The gd-config this reads is already per variant, so the constant carries whichever * variant's key belongs to this filesystem and there is nothing here to select. * * @return string */ private static function signing_key() { return defined( 'GD_WP_SITE_SIGNING_KEY' ) ? trim( (string) GD_WP_SITE_SIGNING_KEY ) : ''; } /** * Sign a canonical. * * @param array $fields Canonical fields, in the order the verifier joins them. * * @return string Base64url signature, or empty when anything needed is missing. */ private static function sign( array $fields ) { $key = self::signing_key(); if ( '' === $key ) { return ''; } foreach ( $fields as $field ) { if ( ! is_string( $field ) || '' === $field ) { return ''; } } $raw = hash_hmac( 'sha256', implode( "\n", $fields ), $key, true ); return rtrim( strtr( base64_encode( $raw ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions } /** * Decode a successful response body. * * A refusal carries no detail worth surfacing — the control plane answers every * pre-authorization failure identically on purpose — so any non-2xx is just null. * * @param array|\WP_Error $response * * @return array|null */ private static function decode( $response ) { if ( is_wp_error( $response ) ) { return null; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code < 200 || $code > 299 ) { return null; } $decoded = json_decode( wp_remote_retrieve_body( $response ), true ); return is_array( $decoded ) ? $decoded : null; } /** * Translate a flush record into the legacy CDN status vocabulary. * * Keyed on `terminal` and not on the state name alone, so a state this release has * never heard of still resolves: terminal-and-not-succeeded reads as a failure, * anything non-terminal keeps the poller going. * * @param array $record * * @return string */ private static function legacy_status( array $record ) { $terminal = ! empty( $record['terminal'] ); $state = isset( $record['state'] ) ? (string) $record['state'] : ''; if ( ! $terminal ) { return 'PENDING'; } return ( 'succeeded' === $state ) ? 'SUCCESS' : 'FAILED'; } }
Save File
Cancel