paths = $paths ?? new WordPressPathProvider(); } /** * @param array $preflight Validated preflight object. * @return array */ public function create( array $preflight, ServerObservationSet $observations ): array { $this->validate_preflight( $preflight ); $runtime = $observations->section( 'server.runtime_services' ); $profile = (string) $runtime['values']['profile']; $payload = array( 'schema_version' => '1.1.0', 'kind' => 'server_observation_import', 'collector_version' => '1.1.0', 'created_at' => gmdate( DATE_RFC3339 ), 'site_ref' => $preflight['site_ref'], 'challenge' => $preflight['challenge'], 'profile' => $profile, 'observations' => $observations->all(), ); $payload['checksum'] = hash( 'sha256', $this->encode( $payload ) ); return $payload; } /** * @return array */ public function read_preflight( string $path ): array { $value = $this->read_json( $path ); $this->validate_preflight( $value ); return $value; } /** * @return array{site_ref:string,challenge:string,created_at:string,profile:string,observations:ServerObservationSet} */ public function read_artifact( string $path ): array { $value = $this->read_json( $path ); $keys = array_keys( $value ); sort( $keys ); if ( self::ROOT_FIELDS !== $keys || '1.1.0' !== $value['schema_version'] || 'server_observation_import' !== $value['kind'] || '1.1.0' !== $value['collector_version'] ) { throw new InvalidArgumentException( 'invalid_server_import_contract' ); } if ( ! is_string( $value['site_ref'] ) || ! $this->uuid4( $value['site_ref'] ) || ! is_string( $value['challenge'] ) || 1 !== preg_match( '/^[a-f0-9]{64}$/D', $value['challenge'] ) ) { throw new InvalidArgumentException( 'invalid_server_import_identity' ); } if ( ! is_string( $value['created_at'] ) || ! $this->fresh( $value['created_at'], self::MAX_AGE ) || ! is_string( $value['profile'] ) || ! in_array( $value['profile'], array( 'shared', 'vps', 'container', 'unclassified' ), true ) ) { throw new InvalidArgumentException( 'stale_or_invalid_server_import' ); } if ( ! is_array( $value['observations'] ) || ! is_string( $value['checksum'] ) || 1 !== preg_match( '/^[a-f0-9]{64}$/D', $value['checksum'] ) ) { throw new InvalidArgumentException( 'invalid_server_import_payload' ); } $payload = $value; unset( $payload['checksum'] ); if ( ! hash_equals( $value['checksum'], hash( 'sha256', $this->encode( $payload ) ) ) ) { throw new InvalidArgumentException( 'server_import_checksum_mismatch' ); } $observations = new ServerObservationSet( $value['observations'] ); if ( $value['profile'] !== $observations->section( 'server.runtime_services' )['values']['profile'] ) { throw new InvalidArgumentException( 'server_import_profile_mismatch' ); } return array( 'site_ref' => $value['site_ref'], 'challenge' => $value['challenge'], 'created_at' => $value['created_at'], 'profile' => $value['profile'], 'observations' => $observations, ); } /** * @param array $preflight Preflight object. */ private function validate_preflight( array $preflight ): void { $keys = array_keys( $preflight ); sort( $keys ); if ( self::PREFLIGHT_FIELDS !== $keys || '1.0.0' !== $preflight['schema_version'] || 'server_import_preflight' !== $preflight['kind'] ) { throw new InvalidArgumentException( 'invalid_server_preflight_contract' ); } if ( ! is_string( $preflight['site_ref'] ) || ! $this->uuid4( $preflight['site_ref'] ) || ! is_string( $preflight['challenge'] ) || 1 !== preg_match( '/^[a-f0-9]{64}$/D', $preflight['challenge'] ) ) { throw new InvalidArgumentException( 'invalid_server_preflight_identity' ); } if ( ! is_string( $preflight['issued_at'] ) || ! is_string( $preflight['expires_at'] ) || ! $this->fresh( $preflight['issued_at'], self::MAX_AGE ) || ! $this->not_expired( $preflight['expires_at'] ) ) { throw new InvalidArgumentException( 'expired_server_preflight' ); } if ( array( 'normalized_server_observations' ) !== $preflight['data_classes'] ) { throw new InvalidArgumentException( 'invalid_server_preflight_classes' ); } } /** * @return array */ private function read_json( string $path ): array { if ( 1 !== preg_match( '#^(?:/|[A-Za-z]:[\\\\/])#', $path ) || is_link( $path ) ) { throw new InvalidArgumentException( 'server_artifact_path_invalid' ); } $real = realpath( $path ); if ( false === $real || str_replace( '\\', '/', $real ) !== str_replace( '\\', '/', $path ) || ! is_file( $real ) || ! is_readable( $real ) ) { throw new InvalidArgumentException( 'server_artifact_path_invalid' ); } $size = filesize( $real ); if ( false === $size || $size < 2 || $size > self::MAX_BYTES || $this->inside_wordpress( $real ) ) { throw new InvalidArgumentException( 'server_artifact_size_or_location_invalid' ); } // Explicit owner-selected local artifact outside the WordPress root. // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $json = file_get_contents( $real ); if ( false === $json ) { throw new InvalidArgumentException( 'server_artifact_unreadable' ); } try { $value = json_decode( $json, true, 64, JSON_THROW_ON_ERROR ); } catch ( JsonException $exception ) { throw new InvalidArgumentException( 'server_artifact_json_invalid' ); } if ( ! is_array( $value ) || array_values( $value ) === $value ) { throw new InvalidArgumentException( 'server_artifact_json_invalid' ); } return $value; } /** * @param array $value Canonical payload. */ private function encode( array $value ): string { try { // This contract is also used by the standalone CLI before WordPress loads. // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode return json_encode( $value, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR ); } catch ( JsonException $exception ) { throw new InvalidArgumentException( 'server_artifact_encode_failed' ); } } private function uuid4( string $value ): bool { return 1 === preg_match( '/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/Di', $value ); } private function fresh( string $value, int $max_age ): bool { try { $time = ( new DateTimeImmutable( $value ) )->getTimestamp(); } catch ( \Exception $exception ) { return false; } return abs( time() - $time ) <= $max_age; } private function not_expired( string $value ): bool { try { $time = ( new DateTimeImmutable( $value ) )->getTimestamp(); } catch ( \Exception $exception ) { return false; } return $time >= time() && $time <= time() + self::MAX_AGE + 60; } private function inside_wordpress( string $path ): bool { $roots = array(); foreach ( array( $this->paths->wordpress_root(), $this->paths->content_root() ) as $root ) { if ( '' !== $root ) { $roots[] = $root; } } $path = str_replace( '\\', '/', $path ); foreach ( array_unique( $roots ) as $candidate ) { $root = realpath( $candidate ); if ( false === $root ) { return true; } $root = rtrim( str_replace( '\\', '/', $root ), '/' ) . '/'; if ( 0 === strpos( $path, $root ) ) { return true; } } return false; } }