'forbidden', 'message' => __( 'Administrator capabilities are required to read site data.', 'actionpanel-ai' ), ), 403 ); } if ( ! $this->settings->get_enable_assistant() ) { return new WP_REST_Response( array( 'error' => 'assistant_disabled', 'message' => __( 'Enable ActionPanel AI in settings to read site data.', 'actionpanel-ai' ), ), 403 ); } $this->normalize_request( $request ); return $this->reader->handle( $request ); } /** * Sanitize the read-site resource key before dispatch. * * @param mixed $value Request value. */ public static function sanitize_resource_arg( $value ): string { return sanitize_key( (string) $value ); } /** * Validate the requested diagnostics resource against the public contract. * * @param mixed $value Request value. */ public static function validate_resource_arg( $value ): bool { return is_scalar( $value ) && in_array( self::sanitize_resource_arg( $value ), self::ALLOWED_RESOURCES, true ); } /** * Sanitize a read-site filter while preserving its JSON-like shape. * * @param mixed $value Request value. * * @return array */ public static function sanitize_filter_arg( $value ): array { if ( ! is_array( $value ) ) { return array(); } return self::sanitize_filter_value( $value ); } /** * Validate filter complexity before the reader uses resource-specific keys. * * @param mixed $value Request value. */ public static function validate_filter_arg( $value ): bool { $item_count = 0; return self::is_filter_value_allowed( $value, 0, $item_count ); } /** * Normalize the optional row limit for controller-side bounds enforcement. * * @param mixed $value Request value. */ public static function sanitize_limit_arg( $value ): int { return absint( $value ); } /** * Validate that limit is integer-like. SiteReader applies resource bounds. * * @param mixed $value Request value. */ public static function validate_limit_arg( $value ): bool { return is_int( $value ) || ( is_string( $value ) && preg_match( '/^\d+$/', $value ) === 1 ); } /** * Sanitize cursors as compact pagination tokens. * * @param mixed $value Request value. */ public static function sanitize_cursor_arg( $value ): string { $cursor = sanitize_text_field( (string) $value ); $cursor = preg_replace( '#[^A-Za-z0-9+/=_-]#', '', $cursor ); return is_string( $cursor ) ? substr( $cursor, 0, self::MAX_CURSOR_LENGTH ) : ''; } /** * Validate cursor token length and character set. * * @param mixed $value Request value. */ public static function validate_cursor_arg( $value ): bool { if ( ! is_scalar( $value ) ) { return false; } $cursor = (string) $value; return strlen( $cursor ) <= self::MAX_CURSOR_LENGTH && preg_match( '#^[A-Za-z0-9+/=_-]*$#', $cursor ) === 1; } /** * Apply the same normalization in the controller as a defense-in-depth step. * * Route schemas usually sanitize REST parameters before the callback runs. * Keeping a second normalization point here makes direct controller tests and * future internal calls obey the same bounded input contract. * * @param WP_REST_Request $request Current REST request. */ private function normalize_request( WP_REST_Request $request ): void { $request->set_param( 'resource', self::sanitize_resource_arg( $request['resource'] ) ); $request->set_param( 'filter', self::sanitize_filter_arg( $request['filter'] ?? array() ) ); $request->set_param( 'limit', self::sanitize_limit_arg( $request['limit'] ?? 50 ) ); $request->set_param( 'cursor', self::sanitize_cursor_arg( $request['cursor'] ?? '' ) ); } /** * Recursively sanitize JSON-like filter values without flattening arrays. * * @param mixed $value Filter value. * * @return mixed */ private static function sanitize_filter_value( $value ) { if ( is_string( $value ) ) { return substr( sanitize_text_field( $value ), 0, self::MAX_FILTER_STRING_LENGTH ); } if ( is_scalar( $value ) || null === $value ) { return $value; } if ( ! is_array( $value ) ) { return array( '__actionpanel_invalid_input' => 'object' ); } $sanitized = array(); foreach ( array_slice( $value, 0, self::MAX_FILTER_ITEMS, true ) as $key => $item ) { $safe_key = is_int( $key ) ? $key : sanitize_key( (string) $key ); $sanitized[ $safe_key ] = self::sanitize_filter_value( $item ); } return $sanitized; } /** * Validate JSON-like filter values have bounded depth, size, and scalars. * * @param mixed $value Filter value. * @param int $depth Current recursion depth. * @param int $item_count Total nested item count. */ private static function is_filter_value_allowed( $value, int $depth, int &$item_count ): bool { if ( is_string( $value ) ) { return strlen( $value ) <= self::MAX_FILTER_STRING_LENGTH; } if ( is_scalar( $value ) || null === $value ) { return true; } if ( ! is_array( $value ) || $depth > self::MAX_FILTER_DEPTH ) { return false; } if ( isset( $value['__actionpanel_invalid_input'] ) ) { return false; } foreach ( $value as $item ) { ++$item_count; if ( $item_count > self::MAX_FILTER_ITEMS ) { return false; } if ( ! self::is_filter_value_allowed( $item, $depth + 1, $item_count ) ) { return false; } } return true; } }