← Back
Editing: class-GD-cORe-site-auth.php
<?php if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Site identity for GDCore calls routed through the WP Security Public API * proxy at /api/v1/gdcore/{GD_ACCOUNT_UID}/…. * * The settings GET, the Lighthouse report POST and the Lighthouse report * reads use the proxy, so a report is created and read in the site's own * environment. All are signed like the WPaaS system plugin's * Plugin::sign_http_request(), with GD_SITE_TOKEN, GD_ACCOUNT_UID and * GD_TEMP_DOMAIN; a site without all three cannot make them. Never log, * echo, or pass the token or signature headers to the browser. * * @package GD_cORe * @subpackage GD_cORe/includes */ final class GD_cORe_Site_Auth { const SIGNING_KEY = 'wp1_request'; const REPORT_ID_PATTERN = '/^id:[A-Za-z0-9_-]+\z/'; const UUID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i'; const REPORT_PATH_PREFIX = 'lighthouse/reports/'; const REPORT_FORMATS = array( 'json', 'reportJson', 'reportHtml' ); /** * Credentials from the site's WPaaS constants. * * @return array|null Null unless all three are provisioned. */ public static function credentials() { return self::credentials_from( defined( 'GD_SITE_TOKEN' ) ? GD_SITE_TOKEN : null, defined( 'GD_ACCOUNT_UID' ) ? GD_ACCOUNT_UID : null, defined( 'GD_TEMP_DOMAIN' ) ? GD_TEMP_DOMAIN : null ); } /** * @return array|null {token, account_uid, origin}, or null if any is * missing or the account UID is not a UUID. */ public static function credentials_from( $token, $account_uid, $origin ) { foreach ( array( $token, $account_uid, $origin ) as $value ) { if ( ! is_string( $value ) || $value === '' ) { return null; } } // GDCore requires accountId (sent as GD_ACCOUNT_UID) to be a UUID. if ( ! preg_match( self::UUID_PATTERN, $account_uid ) ) { return null; } return array( 'token' => $token, 'account_uid' => $account_uid, 'origin' => $origin, ); } /** * accountId for Lighthouse requests: GD_ACCOUNT_UID (the account the * proxy creates reports under) when provisioned, otherwise the legacy * GD_ACCOUNT_ID_IN_CORE. */ public static function account_id() { $credentials = self::credentials(); return $credentials ? $credentials['account_uid'] : GD_ACCOUNT_ID_IN_CORE; } /** * WPaaS environment, derived like wp-paas Plugin::get_env(). */ public static function environment() { $environment = self::environment_from( getenv( 'SERVER_ENV' ), defined( 'GD_TEMP_DOMAIN' ) ? GD_TEMP_DOMAIN : null ); return (string) apply_filters( 'wpaas_get_env', $environment ); } public static function environment_from( $server_env, $temp_domain ) { if ( $server_env ) { return (string) $server_env; } if ( is_string( $temp_domain ) && strpos( $temp_domain, '.ide' ) !== false ) { return 'test'; } return 'prod'; } /** * Whether an endpoint (relative to the API base) goes through the proxy: * the settings GET, the report POST and any report read. Nothing else * does. Every report read path is a proxy route, so proxy_operation() * refuses a malformed one instead of it being sent directly to GDCore. */ public static function is_proxy_route( $method, $endpoint ) { if ( ! is_string( $endpoint ) ) { return false; } $path = explode( '?', $endpoint, 2 )[0]; return ( $method === 'GET' && $path === 'api-settings' ) || ( $method === 'POST' && $path === 'lighthouse/reports' ) || ( $method === 'GET' && strpos( $path, self::REPORT_PATH_PREFIX ) === 0 ); } public static function is_report_id( $report_id ) { return is_string( $report_id ) && strlen( $report_id ) <= 255 && preg_match( self::REPORT_ID_PATTERN, $report_id ) === 1; } /** * Public API origin for a WPaaS environment, hardcoded like the system * plugin's wp_public_api_url(): dev and test (or myh.test) have their own * hosts; anything else is prod. */ public static function proxy_base_for( $environment ) { if ( $environment === 'myh.test' ) { $environment = 'test'; } if ( in_array( $environment, array( 'dev', 'test' ), true ) ) { return "https://wp-api.wpsecurity.{$environment}-godaddy.com"; } return 'https://wp-api.wpsecurity.godaddy.com'; } public static function proxy_base() { return self::proxy_base_for( self::environment() ); } /** Local guard only; authoritative domain/redirect policy belongs upstream. */ public static function is_scan_url( $url ) { $parts = is_string( $url ) ? parse_url( $url ) : false; return is_array( $parts ) && in_array( $parts['scheme'] ?? '', array( 'http', 'https' ), true ) && ! empty( $parts['host'] ) && ! isset( $parts['user'] ) && ! isset( $parts['pass'] ) && ! in_array( strtolower( $parts['host'] ), array( 'localhost', '127.0.0.1' ), true ); } /** * Build one signed proxy operation for a settings GET, report POST or * report read. The caller's accountId is dropped and GD_ACCOUNT_UID goes * in the path, which wpsec checks against the site that verified the * signature. Only a report read keeps a query: its allowlisted format. * * @return array|WP_Error {url, headers}; an error means no network request. */ public static function proxy_operation( $method, $endpoint, $body = '' ) { $credentials = self::credentials(); if ( ! $credentials || ! is_string( $endpoint ) || ! is_string( $body ) ) { return new WP_Error( 'proxy_unavailable', 'Site service is not configured.' ); } $parts = explode( '?', $endpoint, 2 ); $query = self::query_params( $parts[1] ?? '' ); $format = $query['format'] ?? null; unset( $query['accountId'], $query['format'] ); if ( $query !== array() ) { return new WP_Error( 'proxy_invalid_operation', 'Invalid service request.' ); } $prefix = '/api/v1/gdcore/' . $credentials['account_uid']; $payload = $method === 'POST' ? json_decode( $body, true ) : null; $report_id = strpos( $parts[0], self::REPORT_PATH_PREFIX ) === 0 ? substr( $parts[0], strlen( self::REPORT_PATH_PREFIX ) ) : null; if ( $method === 'GET' && $parts[0] === 'api-settings' && $body === '' && $format === null ) { $path = $prefix . '/api-settings'; } elseif ( $method === 'POST' && $parts[0] === 'lighthouse/reports' && $format === null && strlen( $body ) <= 16384 && is_array( $payload ) && count( $payload ) === 1 && self::is_scan_url( $payload['url'] ?? null ) ) { $path = $prefix . '/lighthouse/reports'; } elseif ( $method === 'GET' && self::is_report_id( $report_id ) && $body === '' && in_array( $format, self::REPORT_FORMATS, true ) ) { // Validated IDs and allowlisted formats are URL-safe as-is. $path = $prefix . '/lighthouse/reports/' . $report_id . '?format=' . $format; } else { return new WP_Error( 'proxy_invalid_operation', 'Invalid service request.' ); } $headers = self::signature_headers( $credentials, $body, self::new_nonce() ); if ( ! $headers ) { return new WP_Error( 'proxy_invalid_operation', 'Invalid service request.' ); } return array( 'url' => self::proxy_base() . $path, 'headers' => $headers ); } /** * Strict query parsing: each pair is key=value, keys are accountId or * format, and none repeats. Anything else returns a marker key, so the * operation is refused. * * @return array Key => value. */ private static function query_params( $query ) { $params = array(); if ( $query === '' ) { return $params; } foreach ( explode( '&', $query ) as $pair ) { $pair = explode( '=', $pair, 2 ); if ( count( $pair ) !== 2 || ! in_array( $pair[0], array( 'accountId', 'format' ), true ) || array_key_exists( $pair[0], $params ) ) { return array( 'invalid' => true ); } $params[ $pair[0] ] = $pair[1]; } return $params; } /** * WPaaS x-wp-* headers, as Plugin::sign_http_request() builds them and * wpsec's V1 check verifies them: x-wp-bodyhash is sha256 of the exact * body sent, or of '[]' when there is none (a GET). x-wp-token is omitted. * * @param array $credentials From credentials(). * @param string $body Exact request body ('' for a GET). * @param string $nonce From new_nonce(). * * @return array|null Null if the request cannot be signed. */ public static function signature_headers( array $credentials, $body, $nonce ) { if ( ! is_string( $body ) || ! is_string( $nonce ) || ! preg_match( '/^[0-9a-f]{32}_[0-9]{10}$/D', $nonce ) || ! self::credentials_from( $credentials['token'] ?? null, $credentials['account_uid'] ?? null, $credentials['origin'] ?? null ) || ! preg_match( '/^[a-zA-Z0-9.-]+$/D', $credentials['origin'] ) ) { return null; } $bodyhash = hash( 'sha256', $body === '' ? '[]' : $body ); $key = hash_hmac( 'sha256', $credentials['token'], $nonce ); $key = hash_hmac( 'sha256', $key, $credentials['account_uid'] ); $key = hash_hmac( 'sha256', $key, self::SIGNING_KEY ); return array( 'x-wp-nonce' => $nonce, 'x-wp-origin' => $credentials['origin'], 'x-wp-bodyhash' => $bodyhash, 'x-wp-signature' => hash_hmac( 'sha256', $nonce . $credentials['origin'] . $bodyhash, $key ), ); } /** Fresh per call; never reuse a signature. */ public static function new_nonce() { return bin2hex( random_bytes( 16 ) ) . '_' . time(); } }
Save File
Cancel