hash_key = $hash_key; $this->same_site_origin = $this->origin( $same_site_origin ); } /** * @param array $input Browser observation. * @return array */ public function sanitize( array $input ): array { $encoded = wp_json_encode( $input, JSON_UNESCAPED_SLASHES ); if ( false === $encoded || strlen( $encoded ) > self::MAX_BYTES ) { throw new InvalidArgumentException( 'rendered_observation_too_large' ); } $this->exact_keys( $input, array( 'capability', 'duration_ms', 'limitations', 'page', 'schema_version' ) ); if ( self::SCHEMA_VERSION !== ( $input['schema_version'] ?? null ) ) { throw new InvalidArgumentException( 'rendered_schema_invalid' ); } $capability = $this->capability( $input['capability'] ?? null ); $duration = $this->integer( $input['duration_ms'] ?? null, 0, 25000 ); $limits = $this->limitations( $input['limitations'] ?? null ); $page = null; if ( 'available' === $capability['state'] ) { if ( ! is_array( $input['page'] ) ) { throw new InvalidArgumentException( 'rendered_page_missing' ); } $page = $this->page( $input['page'] ); } elseif ( null !== $input['page'] ) { throw new InvalidArgumentException( 'rendered_blocked_page_present' ); } return array( 'schema_version' => self::SCHEMA_VERSION, 'capability' => $capability, 'duration_ms' => $duration, 'page' => $page, 'limitations' => $limits, ); } /** * @param mixed $value Capability payload. * @return array{state:string,reason:string,features:array{dom:bool,styles:bool,performance:bool}} */ private function capability( $value ): array { if ( ! is_array( $value ) ) { throw new InvalidArgumentException( 'rendered_capability_invalid' ); } $this->exact_keys( $value, array( 'features', 'reason', 'state' ) ); $state = $value['state'] ?? null; $reason = $value['reason'] ?? null; if ( ! is_string( $state ) || ! in_array( $state, array( 'available', 'blocked', 'timeout', 'unsupported' ), true ) ) { throw new InvalidArgumentException( 'rendered_capability_invalid' ); } if ( ! is_string( $reason ) || ! in_array( $reason, array( 'available', 'dom_unavailable', 'frame_blocked', 'http_unavailable', 'response_too_large', 'same_origin_required', 'timeout', 'unsupported_browser' ), true ) ) { throw new InvalidArgumentException( 'rendered_capability_invalid' ); } if ( ! is_array( $value['features'] ?? null ) ) { throw new InvalidArgumentException( 'rendered_capability_invalid' ); } $this->exact_keys( $value['features'], array( 'dom', 'performance', 'styles' ) ); $features = array(); foreach ( array( 'dom', 'styles', 'performance' ) as $feature ) { if ( ! is_bool( $value['features'][ $feature ] ?? null ) ) { throw new InvalidArgumentException( 'rendered_capability_invalid' ); } $features[ $feature ] = $value['features'][ $feature ]; } if ( 'available' === $state && ( 'available' !== $reason || in_array( false, $features, true ) ) ) { throw new InvalidArgumentException( 'rendered_capability_invalid' ); } $unavailable_reasons = array( 'blocked' => array( 'dom_unavailable', 'frame_blocked', 'http_unavailable', 'response_too_large' ), 'timeout' => array( 'timeout' ), 'unsupported' => array( 'same_origin_required', 'unsupported_browser' ), ); if ( 'available' !== $state && ( ! in_array( $reason, $unavailable_reasons[ $state ], true ) || in_array( true, $features, true ) ) ) { throw new InvalidArgumentException( 'rendered_capability_invalid' ); } return array( 'state' => $state, 'reason' => $reason, 'features' => $features, ); } /** * @param array $page Raw page aggregates. * @return array */ private function page( array $page ): array { $this->exact_keys( $page, array( 'alternatives', 'aria', 'contrast', 'dom', 'forms', 'interactions', 'media_tables', 'privacy', 'resources', 'runtime', 'viewports' ) ); return array( 'viewports' => $this->viewports( $page['viewports'] ?? null ), 'dom' => $this->integer_map( $page['dom'] ?? null, array( 'node_count', 'max_depth', 'duplicate_id_count', 'empty_id_count', 'heading_level_skips', 'main_count', 'nav_count', 'header_count', 'footer_count' ), array( 'title_present', 'lang_present', 'truncated' ), array( 'heading_counts' => 6 ) ), 'resources' => $this->resources( $page['resources'] ?? null ), 'forms' => $this->forms( $page['forms'] ?? null ), 'alternatives' => $this->integer_map( $page['alternatives'] ?? null, array( 'image_count', 'missing_alt_count', 'decorative_image_count', 'svg_image_count', 'unnamed_svg_image_count' ) ), 'aria' => $this->integer_map( $page['aria'] ?? null, array( 'aria_node_count', 'invalid_reference_count' ) ), 'contrast' => $this->integer_map( $page['contrast'] ?? null, array( 'eligible_count', 'observed_count', 'failure_count' ) ), 'interactions' => $this->integer_map( $page['interactions'] ?? null, array( 'focusable_count', 'positive_tabindex_count', 'native_negative_tabindex_count' ) ), 'media_tables' => $this->integer_map( $page['media_tables'] ?? null, array( 'video_count', 'audio_count', 'missing_caption_count', 'table_count', 'headerless_table_count' ) ), 'privacy' => $this->integer_map( $page['privacy'] ?? null, array( 'notice_signal_count' ) ), 'runtime' => $this->integer_map( $page['runtime'] ?? null, array( 'late_shift_count', 'animated_element_count' ) ), ); } /** * @param mixed $value Viewport list. * @return list> */ private function viewports( $value ): array { if ( ! is_array( $value ) || array() === $value || count( $value ) > self::MAX_VIEWPORTS ) { throw new InvalidArgumentException( 'rendered_viewports_invalid' ); } $result = array(); $seen = array(); foreach ( $value as $viewport ) { if ( ! is_array( $viewport ) ) { throw new InvalidArgumentException( 'rendered_viewports_invalid' ); } $this->exact_keys( $viewport, array( 'clipped_fixed_count', 'document_height', 'document_width', 'height', 'overflow_element_count', 'width' ) ); $width = $this->integer( $viewport['width'] ?? null, 1, 4096 ); if ( ! in_array( $width, self::VIEWPORT_WIDTHS, true ) || isset( $seen[ $width ] ) ) { throw new InvalidArgumentException( 'rendered_viewports_invalid' ); } $seen[ $width ] = true; $result[] = array( 'width' => $width, 'height' => $this->integer( $viewport['height'] ?? null, 320, 2160 ), 'document_width' => $this->integer( $viewport['document_width'] ?? null, 0, 1000000 ), 'document_height' => $this->integer( $viewport['document_height'] ?? null, 0, 10000000 ), 'overflow_element_count' => $this->integer( $viewport['overflow_element_count'] ?? null, 0, 1000000 ), 'clipped_fixed_count' => $this->integer( $viewport['clipped_fixed_count'] ?? null, 0, 1000000 ), ); } return $result; } /** * @param mixed $value Resource aggregates. * @return array */ private function resources( $value ): array { if ( ! is_array( $value ) ) { throw new InvalidArgumentException( 'rendered_resources_invalid' ); } $this->exact_keys( $value, array( 'external_origins', 'failed_image_count', 'font_count', 'image_count', 'request_count', 'script_count', 'stylesheet_count', 'transfer_bytes', 'transfer_observed_count', 'truncated' ) ); $result = $this->integer_map( array_diff_key( $value, array( 'external_origins' => true, 'truncated' => true, ) ), array( 'request_count', 'script_count', 'stylesheet_count', 'image_count', 'font_count', 'failed_image_count', 'transfer_bytes', 'transfer_observed_count' ), array(), array(), 1000000000 ); if ( ! is_bool( $value['truncated'] ?? null ) ) { throw new InvalidArgumentException( 'rendered_resources_invalid' ); } if ( $result['request_count'] > 16 || $result['transfer_bytes'] > 3 * 1024 * 1024 || $result['transfer_observed_count'] < 1 || $result['transfer_observed_count'] > $result['request_count'] + 1 || 0 !== $result['font_count'] || 0 !== $result['failed_image_count'] ) { throw new InvalidArgumentException( 'rendered_resources_invalid' ); } $result['external_origin_refs'] = $this->origins( $value['external_origins'] ?? null ); $result['truncated'] = $value['truncated']; return $result; } /** * @param mixed $value Form aggregates. * @return array */ private function forms( $value ): array { if ( ! is_array( $value ) ) { throw new InvalidArgumentException( 'rendered_forms_invalid' ); } $this->exact_keys( $value, array( 'control_count', 'external_action_count', 'external_form_origins', 'form_count', 'get_sensitive_form_count', 'truncated', 'unlabeled_control_count' ) ); $result = $this->integer_map( array_diff_key( $value, array( 'external_form_origins' => true ) ), array( 'form_count', 'control_count', 'unlabeled_control_count', 'external_action_count', 'get_sensitive_form_count' ), array( 'truncated' ) ); $result['external_origin_refs'] = $this->origins( $value['external_form_origins'] ?? null ); return $result; } /** * @param mixed $value Aggregate map. * @param list $integer_keys Integer fields. * @param list $boolean_keys Boolean fields. * @param array $integer_lists Fixed integer lists. * @return array */ private function integer_map( $value, array $integer_keys, array $boolean_keys = array(), array $integer_lists = array(), int $maximum = 1000000 ): array { if ( ! is_array( $value ) ) { throw new InvalidArgumentException( 'rendered_aggregate_invalid' ); } $this->exact_keys( $value, array_merge( $integer_keys, $boolean_keys, array_keys( $integer_lists ) ) ); $result = array(); foreach ( $integer_keys as $key ) { $result[ $key ] = $this->integer( $value[ $key ] ?? null, 0, $maximum ); } foreach ( $boolean_keys as $key ) { if ( ! is_bool( $value[ $key ] ?? null ) ) { throw new InvalidArgumentException( 'rendered_aggregate_invalid' ); } $result[ $key ] = $value[ $key ]; } foreach ( $integer_lists as $key => $length ) { $list = $value[ $key ] ?? null; if ( ! is_array( $list ) || count( $list ) !== $length ) { throw new InvalidArgumentException( 'rendered_aggregate_invalid' ); } $result[ $key ] = array_map( fn ( $item ): int => $this->integer( $item, 0, $maximum ), array_values( $list ) ); } return $result; } /** * @param mixed $value Origin list. * @return list */ private function origins( $value ): array { if ( ! is_array( $value ) || count( $value ) > self::MAX_ORIGINS ) { throw new InvalidArgumentException( 'rendered_origins_invalid' ); } $origins = array(); foreach ( $value as $candidate ) { if ( ! is_string( $candidate ) || strlen( $candidate ) > 300 ) { throw new InvalidArgumentException( 'rendered_origins_invalid' ); } $origin = $this->origin( $candidate ); if ( $origin === $this->same_site_origin ) { continue; } $origins[] = 'origin:' . substr( hash_hmac( 'sha256', $origin, $this->hash_key ), 0, 24 ); } return array_values( array_unique( $origins ) ); } private function origin( string $candidate ): string { $parts = wp_parse_url( $candidate ); if ( ! is_array( $parts ) || ! isset( $parts['scheme'], $parts['host'] ) || ! in_array( strtolower( (string) $parts['scheme'] ), array( 'http', 'https' ), true ) || isset( $parts['user'] ) || isset( $parts['pass'] ) || isset( $parts['query'] ) || isset( $parts['fragment'] ) || ( isset( $parts['path'] ) && ! in_array( $parts['path'], array( '', '/' ), true ) ) ) { throw new InvalidArgumentException( 'rendered_origin_invalid' ); } $scheme = strtolower( (string) $parts['scheme'] ); $host = strtolower( rtrim( (string) $parts['host'], '.' ) ); if ( '' === $host || strlen( $host ) > 253 ) { throw new InvalidArgumentException( 'rendered_origin_invalid' ); } $port = isset( $parts['port'] ) ? (int) $parts['port'] : ( 'https' === $scheme ? 443 : 80 ); $default = ( 'https' === $scheme && 443 === $port ) || ( 'http' === $scheme && 80 === $port ); return $scheme . '://' . $host . ( $default ? '' : ':' . $port ); } /** * @param mixed $value Limitation list. * @return list */ private function limitations( $value ): array { if ( ! is_array( $value ) || count( $value ) !== count( self::LIMITATIONS ) ) { throw new InvalidArgumentException( 'rendered_limitations_invalid' ); } $result = array(); foreach ( $value as $limitation ) { if ( ! is_string( $limitation ) || ! in_array( $limitation, self::LIMITATIONS, true ) ) { throw new InvalidArgumentException( 'rendered_limitations_invalid' ); } $result[] = $limitation; } $result = array_values( array_unique( $result ) ); sort( $result ); $expected = self::LIMITATIONS; sort( $expected ); if ( $result !== $expected ) { throw new InvalidArgumentException( 'rendered_limitations_invalid' ); } return self::LIMITATIONS; } /** * @param array $value Map. * @param list $expected Expected keys. */ private function exact_keys( array $value, array $expected ): void { $actual = array_keys( $value ); sort( $actual ); sort( $expected ); if ( $actual !== $expected ) { throw new InvalidArgumentException( 'rendered_fields_invalid' ); } } /** @param mixed $value Integer. */ private function integer( $value, int $minimum, int $maximum ): int { if ( ! is_int( $value ) || $value < $minimum || $value > $maximum ) { throw new InvalidArgumentException( 'rendered_integer_invalid' ); } return $value; } }