Commit c5689b7e6a8 for woocommerce
commit c5689b7e6a8248f6361ab6183b746a0c07134275
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 17:41:00 2026 +0100
Validate order item type and tax data in the Orders REST API (#71208)
(cherry picked from commit 44e9e85b327c97e042809af12c4057e3d829b5f5)
Co-authored-by: Jorge Torres <jorge.torres@automattic.com>
diff --git a/plugins/woocommerce/includes/class-wc-order-item-fee.php b/plugins/woocommerce/includes/class-wc-order-item-fee.php
index 1e5caf0c649..ec8c8d97d52 100644
--- a/plugins/woocommerce/includes/class-wc-order-item-fee.php
+++ b/plugins/woocommerce/includes/class-wc-order-item-fee.php
@@ -11,6 +11,8 @@
*/
use Automattic\WooCommerce\Enums\ProductTaxStatus;
+use Automattic\WooCommerce\Utilities\ArrayUtil;
+use Automattic\WooCommerce\Utilities\MetaDataUtil;
use Automattic\WooCommerce\Utilities\NumberUtil;
defined( 'ABSPATH' ) || exit;
@@ -197,34 +199,36 @@ class WC_Order_Item_Fee extends WC_Order_Item {
* @param array $raw_tax_data Raw tax data.
*/
public function set_taxes( $raw_tax_data ) {
- $raw_tax_data = maybe_unserialize( $raw_tax_data );
+ $raw_tax_data = MetaDataUtil::maybe_unserialize_no_classes( $raw_tax_data );
$tax_data = array(
'total' => array(),
);
- if ( ! empty( $raw_tax_data['total'] ) ) {
+ if ( is_array( $raw_tax_data ) && ! empty( $raw_tax_data['total'] ) ) {
$total = $raw_tax_data['total'];
- // Handle legacy data where total might be a float/string instead of an array.
- if ( ! is_array( $total ) ) {
- $order = $this->get_order();
- $total = $this->convert_legacy_tax_value_to_array( $total, $order );
-
- // Log legacy data format for debugging purposes.
- wc_get_logger()->warning(
- sprintf(
- /* translators: %d: order item ID */
- __( 'Order item #%d contains legacy tax data format. Tax rate ID information is unavailable.', 'woocommerce' ),
- $this->get_id()
- ),
- array(
- 'source' => 'woocommerce-order-item-fee',
- 'order_item_id' => $this->get_id(),
- 'order_id' => $order ? $order->get_id() : 0,
- )
- );
- }
+ if ( is_scalar( $total ) || ArrayUtil::is_flat_scalar_array( $total ) ) {
+ // Handle legacy data where total might be a float/string instead of an array.
+ if ( ! is_array( $total ) ) {
+ $order = $this->get_order();
+ $total = $this->convert_legacy_tax_value_to_array( $total, $order );
+
+ // Log legacy data format for debugging purposes.
+ wc_get_logger()->warning(
+ sprintf(
+ /* translators: %d: order item ID */
+ __( 'Order item #%d contains legacy tax data format. Tax rate ID information is unavailable.', 'woocommerce' ),
+ $this->get_id()
+ ),
+ array(
+ 'source' => 'woocommerce-order-item-fee',
+ 'order_item_id' => $this->get_id(),
+ 'order_id' => $order ? $order->get_id() : 0,
+ )
+ );
+ }
- $tax_data['total'] = array_map( 'wc_format_decimal', $total );
+ $tax_data['total'] = array_map( 'wc_format_decimal', $total );
+ }
}
$this->set_prop( 'taxes', $tax_data );
diff --git a/plugins/woocommerce/includes/class-wc-order-item-product.php b/plugins/woocommerce/includes/class-wc-order-item-product.php
index d0388e7a31a..deb6ee951c3 100644
--- a/plugins/woocommerce/includes/class-wc-order-item-product.php
+++ b/plugins/woocommerce/includes/class-wc-order-item-product.php
@@ -10,6 +10,8 @@
use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Enums\ProductTaxStatus;
use Automattic\WooCommerce\Enums\ProductType;
+use Automattic\WooCommerce\Utilities\ArrayUtil;
+use Automattic\WooCommerce\Utilities\MetaDataUtil;
use Automattic\WooCommerce\Utilities\NumberUtil;
defined( 'ABSPATH' ) || exit;
@@ -175,48 +177,53 @@ class WC_Order_Item_Product extends WC_Order_Item {
* but scalar values (floats/strings) are accepted for legacy compatibility.
*/
public function set_taxes( $raw_tax_data ) {
- $raw_tax_data = maybe_unserialize( $raw_tax_data );
+ $raw_tax_data = MetaDataUtil::maybe_unserialize_no_classes( $raw_tax_data );
$tax_data = array(
'total' => array(),
'subtotal' => array(),
);
- if ( ! empty( $raw_tax_data['total'] ) && ! empty( $raw_tax_data['subtotal'] ) ) {
+ if ( is_array( $raw_tax_data ) && ! empty( $raw_tax_data['total'] ) && ! empty( $raw_tax_data['subtotal'] ) ) {
$subtotal = $raw_tax_data['subtotal'];
$total = $raw_tax_data['total'];
- // Handle legacy data where total/subtotal might be floats/strings instead of arrays.
- // Convert scalar values to array format to preserve the tax amount.
- $has_legacy_data = ! is_array( $subtotal ) || ! is_array( $total );
-
- if ( $has_legacy_data ) {
- $order = $this->get_order();
- if ( ! is_array( $subtotal ) ) {
- $subtotal = $this->convert_legacy_tax_value_to_array( $subtotal, $order );
- }
- if ( ! is_array( $total ) ) {
- $total = $this->convert_legacy_tax_value_to_array( $total, $order );
+ if (
+ ( is_scalar( $total ) || ArrayUtil::is_flat_scalar_array( $total ) )
+ && ( is_scalar( $subtotal ) || ArrayUtil::is_flat_scalar_array( $subtotal ) )
+ ) {
+ // Handle legacy data where total/subtotal might be floats/strings instead of arrays.
+ // Convert scalar values to array format to preserve the tax amount.
+ $has_legacy_data = ! is_array( $subtotal ) || ! is_array( $total );
+
+ if ( $has_legacy_data ) {
+ $order = $this->get_order();
+ if ( ! is_array( $subtotal ) ) {
+ $subtotal = $this->convert_legacy_tax_value_to_array( $subtotal, $order );
+ }
+ if ( ! is_array( $total ) ) {
+ $total = $this->convert_legacy_tax_value_to_array( $total, $order );
+ }
+ // Log legacy data format for debugging purposes.
+ wc_get_logger()->warning(
+ sprintf(
+ /* translators: %d: order item ID */
+ __( 'Order item #%d contains legacy tax data format. Tax rate ID information is unavailable.', 'woocommerce' ),
+ $this->get_id()
+ ),
+ array(
+ 'source' => 'woocommerce-order-item-product',
+ 'order_item_id' => $this->get_id(),
+ 'order_id' => $order ? $order->get_id() : 0,
+ )
+ );
}
- // Log legacy data format for debugging purposes.
- wc_get_logger()->warning(
- sprintf(
- /* translators: %d: order item ID */
- __( 'Order item #%d contains legacy tax data format. Tax rate ID information is unavailable.', 'woocommerce' ),
- $this->get_id()
- ),
- array(
- 'source' => 'woocommerce-order-item-product',
- 'order_item_id' => $this->get_id(),
- 'order_id' => $order ? $order->get_id() : 0,
- )
- );
- }
- $tax_data['subtotal'] = array_map( 'wc_format_decimal', $subtotal );
- $tax_data['total'] = array_map( 'wc_format_decimal', $total );
+ $tax_data['subtotal'] = array_map( 'wc_format_decimal', $subtotal );
+ $tax_data['total'] = array_map( 'wc_format_decimal', $total );
- // Subtotal cannot be less than total!
- if ( NumberUtil::array_sum( $tax_data['subtotal'] ) < NumberUtil::array_sum( $tax_data['total'] ) ) {
- $tax_data['subtotal'] = $tax_data['total'];
+ // Subtotal cannot be less than total!
+ if ( NumberUtil::array_sum( $tax_data['subtotal'] ) < NumberUtil::array_sum( $tax_data['total'] ) ) {
+ $tax_data['subtotal'] = $tax_data['total'];
+ }
}
}
$this->set_prop( 'taxes', $tax_data );
diff --git a/plugins/woocommerce/includes/class-wc-order-item-shipping.php b/plugins/woocommerce/includes/class-wc-order-item-shipping.php
index f3ddadf2e6b..58559d8c620 100644
--- a/plugins/woocommerce/includes/class-wc-order-item-shipping.php
+++ b/plugins/woocommerce/includes/class-wc-order-item-shipping.php
@@ -8,6 +8,8 @@
*/
use Automattic\WooCommerce\Enums\ProductTaxStatus;
+use Automattic\WooCommerce\Utilities\ArrayUtil;
+use Automattic\WooCommerce\Utilities\MetaDataUtil;
use Automattic\WooCommerce\Utilities\NumberUtil;
defined( 'ABSPATH' ) || exit;
@@ -137,35 +139,37 @@ class WC_Order_Item_Shipping extends WC_Order_Item {
* @throws WC_Data_Exception May throw exception if data is invalid.
*/
public function set_taxes( $raw_tax_data ) {
- $raw_tax_data = maybe_unserialize( $raw_tax_data );
+ $raw_tax_data = MetaDataUtil::maybe_unserialize_no_classes( $raw_tax_data );
$tax_data = array(
'total' => array(),
);
- if ( isset( $raw_tax_data['total'] ) ) {
+ if ( is_array( $raw_tax_data ) && isset( $raw_tax_data['total'] ) ) {
$total = $raw_tax_data['total'];
- // Handle legacy data where total might be a float/string instead of an array.
- if ( ! is_array( $total ) ) {
- $order = $this->get_order();
- $total = $this->convert_legacy_tax_value_to_array( $total, $order );
-
- // Log legacy data format for debugging purposes.
- wc_get_logger()->warning(
- sprintf(
- /* translators: %d: order item ID */
- __( 'Order item #%d contains legacy tax data format. Tax rate ID information is unavailable.', 'woocommerce' ),
- $this->get_id()
- ),
- array(
- 'source' => 'woocommerce-order-item-shipping',
- 'order_item_id' => $this->get_id(),
- 'order_id' => $order ? $order->get_id() : 0,
- )
- );
+ if ( is_scalar( $total ) || ArrayUtil::is_flat_scalar_array( $total ) ) {
+ // Handle legacy data where total might be a float/string instead of an array.
+ if ( ! is_array( $total ) ) {
+ $order = $this->get_order();
+ $total = $this->convert_legacy_tax_value_to_array( $total, $order );
+
+ // Log legacy data format for debugging purposes.
+ wc_get_logger()->warning(
+ sprintf(
+ /* translators: %d: order item ID */
+ __( 'Order item #%d contains legacy tax data format. Tax rate ID information is unavailable.', 'woocommerce' ),
+ $this->get_id()
+ ),
+ array(
+ 'source' => 'woocommerce-order-item-shipping',
+ 'order_item_id' => $this->get_id(),
+ 'order_id' => $order ? $order->get_id() : 0,
+ )
+ );
+ }
+
+ $tax_data['total'] = array_map( 'wc_format_decimal', $total );
}
-
- $tax_data['total'] = array_map( 'wc_format_decimal', $total );
- } elseif ( ! empty( $raw_tax_data ) && is_array( $raw_tax_data ) ) {
+ } elseif ( ! empty( $raw_tax_data ) && ArrayUtil::is_flat_scalar_array( $raw_tax_data ) ) {
// Older versions just used an array.
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data );
}
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php
index 10ab129c3b4..31356c47e93 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php
@@ -30,6 +30,18 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller {
use OrderLineItemValidationTrait;
+ /**
+ * Maps each order line request argument to the order_item_type stored for it.
+ *
+ * @var array<string, string>
+ */
+ protected const STORED_ITEM_TYPES = array(
+ 'line_items' => 'line_item',
+ 'shipping_lines' => 'shipping',
+ 'fee_lines' => 'fee',
+ 'coupon_lines' => 'coupon',
+ );
+
/**
* Endpoint namespace.
*
@@ -824,7 +836,7 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller {
/**
* Wrapper method to create/update order items.
* When updating, the item ID provided is checked to ensure it is associated
- * with the order.
+ * with the order and it's of the correct type.
*
* @param WC_Order $order order
* @param string $item_type
@@ -842,15 +854,18 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller {
$method = 'prepare_' . $item_type;
- // Verify provided line item ID is associated with order.
+ // Verify provided line item ID is associated with order and matches its stored type.
if ( 'update' === $action ) {
$result = $wpdb->get_row(
- $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
- absint( $posted['id'] ),
- absint( $order->get_id() )
- ) );
+ $wpdb->prepare(
+ "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d AND order_item_type = %s",
+ absint( $posted['id'] ),
+ absint( $order->get_id() ),
+ static::STORED_ITEM_TYPES[ $item_type ] ?? $item_type
+ )
+ );
if ( is_null( $result ) ) {
- throw new WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 );
+ throw new WC_REST_Exception( 'woocommerce_rest_invalid_item_id', esc_html__( 'Order item ID provided is not associated with order, or does not match the expected item type.', 'woocommerce' ), 400 );
}
}
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 4945ba5a204..d2ad4ae2cec 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -12138,12 +12138,6 @@ parameters:
count: 1
path: includes/class-wc-order-item-fee.php
- -
- message: '#^Parameter \#1 \$data of function maybe_unserialize expects string, array given\.$#'
- identifier: argument.type
- count: 1
- path: includes/class-wc-order-item-fee.php
-
-
message: '#^Parameter \#1 \$raw_tax_data of method WC_Order_Item_Fee\:\:set_taxes\(\) expects array, false given\.$#'
identifier: argument.type
@@ -12306,12 +12300,6 @@ parameters:
count: 2
path: includes/class-wc-order-item-product.php
- -
- message: '#^Parameter \#1 \$data of function maybe_unserialize expects string, array given\.$#'
- identifier: argument.type
- count: 1
- path: includes/class-wc-order-item-product.php
-
-
message: '#^Parameter \#1 \$value of method WC_Order_Item_Product\:\:set_subtotal\(\) expects string, int\|string given\.$#'
identifier: argument.type
@@ -12390,12 +12378,6 @@ parameters:
count: 1
path: includes/class-wc-order-item-shipping.php
- -
- message: '#^Parameter \#1 \$data of function maybe_unserialize expects string, array given\.$#'
- identifier: argument.type
- count: 1
- path: includes/class-wc-order-item-shipping.php
-
-
message: '#^Parameter \#1 \$instance_id of static method WC_Shipping_Zones\:\:get_shipping_method\(\) expects int, string given\.$#'
identifier: argument.type
diff --git a/plugins/woocommerce/src/Utilities/ArrayUtil.php b/plugins/woocommerce/src/Utilities/ArrayUtil.php
index a83dcf7e141..a41952e6d13 100644
--- a/plugins/woocommerce/src/Utilities/ArrayUtil.php
+++ b/plugins/woocommerce/src/Utilities/ArrayUtil.php
@@ -368,6 +368,18 @@ class ArrayUtil {
return true;
}
+ /**
+ * Check whether a value is an array whose direct values are all scalars.
+ *
+ * @since 11.3.1
+ *
+ * @param mixed $value Value to check.
+ * @return bool
+ */
+ public static function is_flat_scalar_array( $value ): bool {
+ return is_array( $value ) && self::array_all( $value, static fn( $item ) => is_scalar( $item ) );
+ }
+
/**
* Get the unique truthy values of an array, reindexed.
*
diff --git a/plugins/woocommerce/src/Utilities/MetaDataUtil.php b/plugins/woocommerce/src/Utilities/MetaDataUtil.php
index 11685bdfe0e..26b49b6bd63 100644
--- a/plugins/woocommerce/src/Utilities/MetaDataUtil.php
+++ b/plugins/woocommerce/src/Utilities/MetaDataUtil.php
@@ -6,7 +6,7 @@ namespace Automattic\WooCommerce\Utilities;
use WC_Data;
/**
- * Utility methods for handling meta data in REST API requests.
+ * Utility methods for handling order item meta data.
*
* @since 10.8.0
*/
@@ -61,4 +61,21 @@ class MetaDataUtil {
}
return $normalized;
}
+
+ /**
+ * Unserialize a meta value, if it is serialized, without instantiating any object it may contain.
+ *
+ * @since 11.3.1
+ *
+ * @param mixed $value Raw meta value, maybe serialized.
+ * @return mixed Unserialized value.
+ */
+ public static function maybe_unserialize_no_classes( $value ) {
+ if ( ! is_serialized( $value ) ) {
+ return $value;
+ }
+
+ // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- maybe_unserialize() can't restrict classes, and @ hides the warning on bad input.
+ return @unserialize( trim( $value ), array( 'allowed_classes' => false ) );
+ }
}
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php
index 0d8543f03c8..b57d6e7ec07 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php
@@ -423,4 +423,67 @@ class WC_REST_Orders_V1_Controller_Tests extends WC_REST_Unit_Test_Case {
$this->assertSame( $parent->get_id(), $reloaded->get_product_id(), 'The line item should retain the parent product ID.' );
$this->assertSame( $parent->get_id(), $reloaded->get_product()->get_id(), 'The line item should resolve to the parent product.' );
}
+
+ /**
+ * Creates an order holding one item of each type that the v1 endpoint can update.
+ *
+ * @return array{WC_Order, array<string, int>} The order, and its item IDs keyed by the request argument they belong to.
+ */
+ private function create_order_with_an_item_of_each_type(): array {
+ $items = array(
+ 'line_items' => new WC_Order_Item_Product(),
+ 'shipping_lines' => new WC_Order_Item_Shipping(),
+ 'fee_lines' => new WC_Order_Item_Fee(),
+ 'coupon_lines' => new WC_Order_Item_Coupon(),
+ );
+ $order = new WC_Order();
+ foreach ( $items as $item ) {
+ $order->add_item( $item );
+ }
+ $order->save();
+
+ return array( $order, array_map( fn( $item ) => $item->get_id(), $items ) );
+ }
+
+ /**
+ * @testdox An order item can only be updated through the request argument that matches its type.
+ */
+ public function test_update_order_item_through_an_argument_of_another_type_is_rejected(): void {
+ list( $order, $item_ids ) = $this->create_order_with_an_item_of_each_type();
+
+ foreach ( array_keys( $item_ids ) as $argument ) {
+ foreach ( $item_ids as $item_argument => $item_id ) {
+ $request = new WP_REST_Request( 'PUT', '/wc/v1/orders/' . $order->get_id() );
+ $request->set_body_params( array( $argument => array( array( 'id' => $item_id ) ) ) );
+ $response = $this->server->dispatch( $request );
+
+ if ( $argument === $item_argument ) {
+ $this->assertSame( 200, $response->get_status(), "An item of {$item_argument} should be accepted under {$argument}." );
+ } else {
+ $this->assertSame( 400, $response->get_status(), "An item of {$item_argument} should be rejected under {$argument}." );
+ $this->assertSame( 'woocommerce_rest_invalid_item_id', $response->get_data()['code'] );
+ }
+ }
+ }
+ }
+
+ /**
+ * @testdox Custom meta of a shipping item is not read as the tax data of a product line item.
+ */
+ public function test_shipping_item_meta_is_not_read_as_line_item_tax_data(): void {
+ list( $order, $item_ids ) = $this->create_order_with_an_item_of_each_type();
+
+ // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- The meta API serializes it once more, so reading the meta gives this string back.
+ $serialized = serialize( array( 'total' => new stdClass() ) );
+ wc_add_order_item_meta( $item_ids['shipping_lines'], '_line_tax_data', $serialized );
+
+ $request = new WP_REST_Request( 'PUT', '/wc/v1/orders/' . $order->get_id() );
+ $request->set_body_params( array( 'line_items' => array( array( 'id' => $item_ids['shipping_lines'] ) ) ) );
+ $response = $this->server->dispatch( $request );
+
+ $this->assertSame( 400, $response->get_status() );
+ $this->assertSame( 'woocommerce_rest_invalid_item_id', $response->get_data()['code'] );
+ $this->assertSame( 'shipping', WC_Data_Store::load( 'order-item' )->get_order_item_type( $item_ids['shipping_lines'] ), 'The stored item type should be left untouched.' );
+ $this->assertSame( $serialized, wc_get_order_item_meta( $item_ids['shipping_lines'], '_line_tax_data' ), 'The meta should be left untouched.' );
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Utilities/ArrayUtilTest.php b/plugins/woocommerce/tests/php/src/Utilities/ArrayUtilTest.php
index 845b0c65cbb..16aa5dbe76d 100644
--- a/plugins/woocommerce/tests/php/src/Utilities/ArrayUtilTest.php
+++ b/plugins/woocommerce/tests/php/src/Utilities/ArrayUtilTest.php
@@ -484,4 +484,32 @@ class ArrayUtilTest extends \WC_Unit_Test_Case {
public function test_unique_truthy_values_returns_empty_array_when_nothing_survives( $input ) {
$this->assertEquals( array(), ArrayUtil::unique_truthy_values( $input ) );
}
+
+ /**
+ * @testdox `is_flat_scalar_array` is true only for an array whose direct values are all scalars.
+ *
+ * @testWith [[], true]
+ * [[1, "2.00", true], true]
+ * [[null], false]
+ * [[[1, 2]], false]
+ *
+ * @param array $input The input value to test.
+ * @param bool $expected The expected result.
+ */
+ public function test_is_flat_scalar_array_checks_direct_values_only( $input, bool $expected ) {
+ $this->assertSame( $expected, ArrayUtil::is_flat_scalar_array( $input ) );
+ }
+
+ /**
+ * @testdox `is_flat_scalar_array` is false for a non-array value.
+ *
+ * @testWith [null]
+ * ["a string"]
+ * [42]
+ *
+ * @param mixed $input The input value to test.
+ */
+ public function test_is_flat_scalar_array_is_false_for_non_arrays( $input ) {
+ $this->assertFalse( ArrayUtil::is_flat_scalar_array( $input ) );
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Utilities/MetaDataUtilTest.php b/plugins/woocommerce/tests/php/src/Utilities/MetaDataUtilTest.php
index a3c792b4530..e3e4ecd457d 100644
--- a/plugins/woocommerce/tests/php/src/Utilities/MetaDataUtilTest.php
+++ b/plugins/woocommerce/tests/php/src/Utilities/MetaDataUtilTest.php
@@ -220,4 +220,49 @@ class MetaDataUtilTest extends WC_Unit_Test_Case {
$this->assertSame( 'v', $updated_rows[0]->value );
$this->assertCount( 0, $rows_with_key( $order, 'original_key' ), 'The row named by default_id should have been rewritten in place.' );
}
+
+ /**
+ * @testdox `maybe_unserialize_no_classes` passes through non-serialized values, and unserializes serialized ones without instantiating any class they hold.
+ */
+ public function test_maybe_unserialize_no_classes(): void {
+ // Not serialized: returned unchanged.
+ $this->assertSame( array( 'total' => array( 1 => '24.00' ) ), MetaDataUtil::maybe_unserialize_no_classes( array( 'total' => array( 1 => '24.00' ) ) ) );
+ $this->assertSame( 'not serialized', MetaDataUtil::maybe_unserialize_no_classes( 'not serialized' ) );
+
+ // Serialized array: unserialized faithfully.
+ // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Testing a serialized value.
+ $array_result = MetaDataUtil::maybe_unserialize_no_classes( serialize( array( 'total' => array( 1 => '24.00' ) ) ) );
+ $this->assertSame( array( 'total' => array( 1 => '24.00' ) ), $array_result );
+
+ // Serialized object: unserialized as-is, without instantiating the real class.
+ // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Testing serialized data that holds an object.
+ $object_result = MetaDataUtil::maybe_unserialize_no_classes( serialize( array( 'total' => new \stdClass() ) ) );
+ $this->assertInstanceOf( \__PHP_Incomplete_Class::class, $object_result['total'] );
+
+ // Malformed input that merely looks serialized (declares 2 array elements, has only 1)
+ // and a legitimately serialized false both return unserialize()'s raw `false`.
+ $this->assertFalse( MetaDataUtil::maybe_unserialize_no_classes( 'a:2:{s:1:"a";i:1;}' ) );
+ // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Testing a serialized value.
+ $this->assertFalse( MetaDataUtil::maybe_unserialize_no_classes( serialize( false ) ) );
+ }
+
+ /**
+ * @testdox Order item types with tax data discard set_taxes() input holding an unresolved class, whether at the top level or nested under 'total'.
+ * @testWith ["WC_Order_Item_Product"]
+ * ["WC_Order_Item_Fee"]
+ * ["WC_Order_Item_Shipping"]
+ *
+ * @param string $item_class Order item class to test.
+ */
+ public function test_order_item_set_taxes_discards_unresolved_class( string $item_class ): void {
+ $item = new $item_class();
+
+ // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Testing serialized data that holds an object.
+ $item->set_taxes( serialize( new \stdClass() ) );
+ $this->assertSame( array(), $item->get_taxes()['total'], 'A bare object as the raw value should be discarded.' );
+
+ // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Testing serialized data that holds an object nested under 'total'.
+ $item->set_taxes( serialize( array( 'total' => new \stdClass() ) ) );
+ $this->assertSame( array(), $item->get_taxes()['total'], 'An object nested under "total" should be discarded.' );
+ }
}