Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/woo-bought-together/includes/class-helper.php
+++ b/woo-bought-together/includes/class-helper.php
@@ -64,6 +64,128 @@
return $arr;
}
+ public static function sanitize_ids( $ids ) {
+ if ( ! is_scalar( $ids ) ) {
+ return '';
+ }
+
+ $ids = wp_strip_all_tags( (string) $ids );
+ $ids = preg_replace( '/[rnt]+/', '', $ids );
+
+ return trim( $ids );
+ }
+
+ public static function sanitize_variation( $variation, $variation_product = null ) {
+ if ( ! ( $variation_product instanceof WC_Product_Variation ) ) {
+ return [];
+ }
+
+ $variation_attributes = $variation_product->get_variation_attributes();
+
+ if ( empty( $variation_attributes ) || ! is_array( $variation_attributes ) ) {
+ return [];
+ }
+
+ if ( is_object( $variation ) ) {
+ $variation = (array) $variation;
+ }
+
+ $submitted = [];
+
+ if ( is_array( $variation ) ) {
+ foreach ( $variation as $key => $value ) {
+ if ( ! is_string( $key ) || ! is_scalar( $value ) || strpos( $key, 'attribute_' ) !== 0 ) {
+ continue;
+ }
+
+ $clean_value = trim( preg_replace( '/[rnt]+/', ' ', wp_strip_all_tags( (string) $value ) ) );
+ $submitted[ $key ] = $clean_value;
+ $normalized_key = 'attribute_' . sanitize_title( substr( $key, 10 ) );
+
+ if ( ! isset( $submitted[ $normalized_key ] ) ) {
+ $submitted[ $normalized_key ] = $clean_value;
+ }
+ }
+ }
+
+ $parent_product = wc_get_product( $variation_product->get_parent_id() );
+ $parent_attributes = $parent_product ? $parent_product->get_attributes() : [];
+ $sanitized = [];
+
+ foreach ( $variation_attributes as $attr_key => $attr_value ) {
+ if ( ! is_string( $attr_key ) || strpos( $attr_key, 'attribute_' ) !== 0 ) {
+ continue;
+ }
+
+ if ( '' !== $attr_value ) {
+ $sanitized[ $attr_key ] = (string) $attr_value;
+ continue;
+ }
+
+ $attr_slug = substr( $attr_key, 10 );
+ $normalized_key = 'attribute_' . sanitize_title( $attr_slug );
+ $raw_value = $submitted[ $attr_key ] ?? ( $submitted[ $normalized_key ] ?? '' );
+
+ if ( '' === $raw_value ) {
+ continue;
+ }
+
+ $matched_attr = null;
+
+ if ( isset( $parent_attributes[ $attr_slug ] ) ) {
+ $matched_attr = $parent_attributes[ $attr_slug ];
+ } else {
+ foreach ( $parent_attributes as $parent_attr ) {
+ if ( ( $parent_attr instanceof WC_Product_Attribute ) && sanitize_title( $parent_attr->get_name() ) === sanitize_title( $attr_slug ) ) {
+ $matched_attr = $parent_attr;
+ break;
+ }
+ }
+ }
+
+ if ( $matched_attr instanceof WC_Product_Attribute ) {
+ if ( $matched_attr->is_taxonomy() ) {
+ $valid_slugs = $matched_attr->get_slugs();
+
+ if ( empty( $valid_slugs ) && $parent_product && function_exists( 'wc_get_product_terms' ) ) {
+ $valid_slugs = wc_get_product_terms( $parent_product->get_id(), $matched_attr->get_name(), [ 'fields' => 'slugs' ] );
+ }
+
+ if ( in_array( $raw_value, $valid_slugs, true ) ) {
+ $sanitized[ $attr_key ] = $raw_value;
+ } else {
+ $slug_candidate = sanitize_title( $raw_value );
+
+ if ( in_array( $slug_candidate, $valid_slugs, true ) ) {
+ $sanitized[ $attr_key ] = $slug_candidate;
+ } else {
+ $decoded_candidate = sanitize_title( rawurldecode( $raw_value ) );
+
+ if ( in_array( $decoded_candidate, $valid_slugs, true ) ) {
+ $sanitized[ $attr_key ] = $decoded_candidate;
+ }
+ }
+ }
+ } else {
+ $valid_options = $matched_attr->get_options();
+
+ foreach ( $valid_options as $option ) {
+ $clean_option = trim( preg_replace( '/[rnt]+/', ' ', wp_strip_all_tags( (string) $option ) ) );
+
+ if ( $raw_value === $clean_option ) {
+ $sanitized[ $attr_key ] = $clean_option;
+ break;
+ }
+ }
+ }
+ } else {
+ $sanitized[ $attr_key ] = sanitize_text_field( $raw_value );
+ }
+ }
+
+ return $sanitized;
+ }
+
public static function generate_key() {
$key = '';
$key_str = apply_filters( 'woobt_key_characters', 'abcdefghijklmnopqrstuvwxyz0123456789' );
--- a/woo-bought-together/includes/class-woobt.php
+++ b/woo-bought-together/includes/class-woobt.php
@@ -327,9 +327,9 @@
$custom_request = apply_filters( 'woobt_custom_request_data', 'data' );
if ( isset( $_REQUEST['woobt_ids'] ) ) {
- $ids = wp_unslash( $_REQUEST['woobt_ids'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
+ $ids = WPCleverWoobt_Helper()->sanitize_ids( wp_unslash( $_REQUEST['woobt_ids'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
} elseif ( isset( $_REQUEST[ $custom_request ]['woobt_ids'] ) ) {
- $ids = wp_unslash( $_REQUEST[ $custom_request ]['woobt_ids'] ?? '' );
+ $ids = WPCleverWoobt_Helper()->sanitize_ids( wp_unslash( $_REQUEST[ $custom_request ]['woobt_ids'] ?? '' ) );
} else {
$ids = '';
}
@@ -452,10 +452,10 @@
$custom_request = apply_filters( 'woobt_custom_request_data', 'data' );
if ( isset( $_REQUEST['woobt_ids'] ) ) {
- $ids = wp_unslash( $_REQUEST['woobt_ids'] ?? '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
+ $ids = WPCleverWoobt_Helper()->sanitize_ids( wp_unslash( $_REQUEST['woobt_ids'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
unset( $_REQUEST['woobt_ids'] );
} elseif ( isset( $_REQUEST[ $custom_request ]['woobt_ids'] ) ) {
- $ids = wp_unslash( $_REQUEST[ $custom_request ]['woobt_ids'] ?? '' );
+ $ids = WPCleverWoobt_Helper()->sanitize_ids( wp_unslash( $_REQUEST[ $custom_request ]['woobt_ids'] ?? '' ) );
unset( $_REQUEST[ $custom_request ]['woobt_ids'] );
} else {
$ids = '';
@@ -532,13 +532,15 @@
$item_qty = apply_filters( 'woobt_item_qty', $item['qty'], $item, $product_id );
$item_price = apply_filters( 'woobt_item_price', $item['price'], $item, $product_id );
- $item_variation = apply_filters( 'woobt_item_attrs', $item['attrs'], $item, $product_id );
+ $item_variation = [];
$item_variation_id = 0;
if ( $item_product instanceof WC_Product_Variation ) {
// ensure we don't add a variation to the cart directly by variation ID
$item_variation_id = $item_id;
$item_id = $item_product->get_parent_id();
+ $item_variation = apply_filters( 'woobt_item_attrs', $item['attrs'], $item, $product_id );
+ $item_variation = WPCleverWoobt_Helper()->sanitize_variation( $item_variation, $item_product );
if ( empty( $item_variation ) ) {
$item_variation = $item_product->get_variation_attributes();
@@ -602,26 +604,41 @@
return;
}
- $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( wp_unslash( $_POST['product_id'] ?? '' ) ) );
- $product = wc_get_product( $product_id );
- $product_status = get_post_status( $product_id );
- $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_POST['quantity'] ?? '' ) );
- $variation_id = absint( wp_unslash( $_POST['variation_id'] ?? 0 ) );
- $variation = (array) ( wp_unslash( $_POST['variation'] ?? [] ) );
-
- if ( $product && 'variation' === $product->get_type() ) {
- $variation_id = $product_id;
- $product_id = $product->get_parent_id();
+ $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( wp_unslash( $_POST['product_id'] ?? '' ) ) );
+ $product = wc_get_product( $product_id );
+ $product_status = get_post_status( $product_id );
+ $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_POST['quantity'] ?? '' ) );
+ $variation_id = absint( wp_unslash( $_POST['variation_id'] ?? 0 ) );
+ $raw_variation = isset( $_POST['variation'] ) && is_array( $_POST['variation'] ) ? wp_unslash( $_POST['variation'] ) : [];
+ $variation_product = null;
+
+ if ( $product instanceof WC_Product_Variation ) {
+ $variation_product = $product;
+ $variation_id = $product_id;
+ $product_id = $variation_product->get_parent_id();
+ $product = wc_get_product( $product_id );
+ $product_status = ( 'publish' === $variation_product->get_status() && 'publish' === get_post_status( $product_id ) ) ? 'publish' : '';
+ } elseif ( $product && $variation_id > 0 ) {
+ $maybe_variation = wc_get_product( $variation_id );
- if ( empty( $variation ) ) {
- $variation = $product->get_variation_attributes();
+ if ( ( $maybe_variation instanceof WC_Product_Variation ) && ( (int) $maybe_variation->get_parent_id() === (int) $product_id ) && ( 'publish' === $maybe_variation->get_status() ) ) {
+ $variation_product = $maybe_variation;
+ } else {
+ $variation_id = 0;
}
}
+ if ( $variation_product instanceof WC_Product_Variation ) {
+ $variation = WPCleverWoobt_Helper()->sanitize_variation( $raw_variation, $variation_product );
+ } else {
+ $variation_id = 0;
+ $variation = [];
+ }
+
$cart_item_data = apply_filters( 'woobt_add_to_cart_data', [] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variation );
- if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data ) && 'publish' === $product_status ) {
+ if ( $product && 'publish' === $product_status && $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data ) ) {
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
@@ -2162,7 +2179,7 @@
continue;
}
- $_item_key = $_item_data[1] ?? WPCleverWoobt_Helper()->generate_key();
+ $_item_key = sanitize_text_field( $_item_data[1] ?? WPCleverWoobt_Helper()->generate_key() );
$_item_price = WPCleverWoobt_Helper()->get_setting( 'default_price', '100%' );
if ( str_contains( $_item_key, '-' ) ) {
@@ -2184,11 +2201,21 @@
}
}
+ $_item_attrs = [];
+
+ if ( ( $_item_product instanceof WC_Product_Variation ) && isset( $_item_data[3] ) && ( '' !== $_item_data[3] ) ) {
+ $_decoded_attrs = json_decode( rawurldecode( $_item_data[3] ), true );
+
+ if ( is_array( $_decoded_attrs ) ) {
+ $_item_attrs = WPCleverWoobt_Helper()->sanitize_variation( $_decoded_attrs, $_item_product );
+ }
+ }
+
$items[ $_item_key ] = [
'id' => $_item_id,
'price' => WPCleverWoobt_Helper()->format_price( $_item_price ),
'qty' => (float) ( $_item_data[2] ?? 1 ),
- 'attrs' => isset( $_item_data[3] ) ? (array) json_decode( rawurldecode( $_item_data[3] ) ) : []
+ 'attrs' => $_item_attrs
];
}
}
--- a/woo-bought-together/wpc-frequently-bought-together.php
+++ b/woo-bought-together/wpc-frequently-bought-together.php
@@ -8,17 +8,17 @@
Text Domain: woo-bought-together
Domain Path: /languages/
Requires Plugins: woocommerce
-Version: 8.0.2
+Version: 8.0.3
Requires at least: 5.9
WC requires at least: 3.0
-WC tested up to: 11.2
+WC tested up to: 11.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'ABSPATH' ) || exit;
-! defined( 'WOOBT_VERSION' ) && define( 'WOOBT_VERSION', '8.0.2' );
+! defined( 'WOOBT_VERSION' ) && define( 'WOOBT_VERSION', '8.0.3' );
! defined( 'WOOBT_LITE' ) && define( 'WOOBT_LITE', __FILE__ );
! defined( 'WOOBT_FILE' ) && define( 'WOOBT_FILE', __FILE__ );
! defined( 'WOOBT_URI' ) && define( 'WOOBT_URI', plugin_dir_url( __FILE__ ) );