'forbidden', 'message' => __( 'Administrator capabilities are required to execute changes.', '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 propose changes.', 'actionpanel-ai' ), ), 403 ); } if ( ! $this->settings->get_allow_write_operations() ) { return new WP_REST_Response( array( 'error' => 'write_operations_disabled', 'message' => __( 'Enable write operations in ActionPanel AI settings to run this action.', 'actionpanel-ai' ), ), 403 ); } $this->normalize_request( $request ); return $this->executor->execute( $request ); } /** * Sanitize a JSON-like array request argument. * * @param mixed $value Request value. * * @return array */ public static function sanitize_array_arg( $value ): array { if ( ! is_array( $value ) ) { return array(); } return self::sanitize_payload_value( $value ); } /** * Validate the actions collection before the executor inspects each action. * * @param mixed $value Request value. */ public static function validate_actions_arg( $value ): bool { if ( ! is_array( $value ) || ! self::is_list_array( $value ) || count( $value ) < 1 || count( $value ) > self::MAX_ACTIONS ) { return false; } $item_count = 0; return self::is_payload_value_allowed( $value, 0, $item_count ); } /** * Validate the optional post-action checks collection. * * @param mixed $value Request value. */ public static function validate_checks_arg( $value ): bool { if ( ! is_array( $value ) || ! self::is_list_array( $value ) || count( $value ) > self::MAX_CHECKS ) { return false; } $item_count = 0; return self::is_payload_value_allowed( $value, 0, $item_count ); } /** * Normalize execute request arrays in the controller before delegation. * * @param WP_REST_Request $request Current REST request. */ private function normalize_request( WP_REST_Request $request ): void { $request->set_param( 'actions', self::sanitize_array_arg( $request['actions'] ?? array() ) ); $request->set_param( 'checks', self::sanitize_array_arg( $request['checks'] ?? array() ) ); } /** * Recursively sanitize JSON-like executor payload values. * * @param mixed $value Payload value. * * @return mixed */ private static function sanitize_payload_value( $value ) { if ( is_string( $value ) ) { return substr( sanitize_text_field( $value ), 0, self::MAX_PAYLOAD_STRING_BYTES ); } 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_PAYLOAD_ITEMS, true ) as $key => $item ) { $safe_key = is_int( $key ) ? $key : sanitize_key( (string) $key ); $sanitized[ $safe_key ] = self::sanitize_payload_value( $item ); } return $sanitized; } /** * Validate executor payloads have bounded depth, size, and scalar lengths. * * @param mixed $value Payload value. * @param int $depth Current recursion depth. * @param int $item_count Total nested item count. */ private static function is_payload_value_allowed( $value, int $depth, int &$item_count ): bool { if ( is_string( $value ) ) { return strlen( $value ) <= self::MAX_PAYLOAD_STRING_BYTES; } if ( is_scalar( $value ) || null === $value ) { return true; } if ( ! is_array( $value ) || $depth > self::MAX_PAYLOAD_DEPTH ) { return false; } if ( isset( $value['__actionpanel_invalid_input'] ) ) { return false; } foreach ( $value as $item ) { ++$item_count; if ( $item_count > self::MAX_PAYLOAD_ITEMS ) { return false; } if ( ! self::is_payload_value_allowed( $item, $depth + 1, $item_count ) ) { return false; } } return true; } /** * Determine whether an array uses contiguous numeric keys. * * @param array $value Candidate array. */ private static function is_list_array( array $value ): bool { return array() === $value || array_keys( $value ) === range( 0, count( $value ) - 1 ); } }