Commit c40853e2f62 for woocommerce

commit c40853e2f62828a7a862da80fe4f5db44cc68ab8
Author: Oleksandr Aratovskyi <79862886+oaratovskyi@users.noreply.github.com>
Date:   Thu Nov 26 13:53:31 2026 +0200

    Fix fatal errors from malformed date and customer query args (#69216)

    * Align `wc_get_orders()` behaviour between CPT and HPOS for unknown post statuses.

    * Additional tests to cover values `any` and `all`

    * Match WP_Query in sanitizing statuses before comparing with known ones

    * Sanitize keys on `explode` path too

    * Normalize statuses and unify checks flow

    * Validate against theoretical possibility of non-strings in query

    * ...but allow objects that can act as strings

    * Better check for provided argument

    Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

    * Improve changelog entry

    Co-authored-by: Francesco <frosso@users.noreply.github.com>

    * Update comments and extract status check logic and move it into `query`

    * Include not just registered but all known WooCommerce statuses

    * Return no results for malformed date and customer query args

    Arrays and non-Stringable objects in order and product date args, or in the order customer arg, reached string functions and raised a TypeError that the existing catch ( Exception ) blocks do not handle. Add the error or exception those code paths already use for invalid input.

    Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

    * Keep matching Stringable customer emails in legacy order queries

    The is_string() guard also rejected objects with __toString(), which the
    legacy store matched before. Only objects that cannot be cast to a string
    fataled.

    Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

    * Add tests for object and empty array date args

    * Document the errors check in parse_date_for_wp_query()

    * Add a legacy test for Stringable product date args

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Treat an empty date array as unset in legacy order and product queries

    Matches HPOS, which already ignores empty arrays. The check sits in the caller loops because the product loop clears other meta filters before parsing the date.

    * Accept only WC_DateTime date objects and string customer emails

    Stringable dates and emails were never documented, and HPOS already rejected Stringable customer emails, so both stores now reject them.

    * Use named Stringable test doubles in legacy data store tests

    Anonymous classes in these data providers made PHPUnit run both test classes twice.

    * Remove comment that restates the HPOS date arg guard

    * Remove malformed date test cases that only vary the date key

    The guards run before any key-specific branch, so the GMT key and sale date cases cover nothing the remaining cases don't.

    ---------

    Co-authored-by: Valery Sukhomlinov <683297+dmvrtx@users.noreply.github.com>
    Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
    Co-authored-by: Francesco <frosso@users.noreply.github.com>
    Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
    Co-authored-by: Vlad Olaru <vlad.olaru@automattic.com>

diff --git a/plugins/woocommerce/changelog/58259-fix-malformed-query-args-fatal b/plugins/woocommerce/changelog/58259-fix-malformed-query-args-fatal
new file mode 100644
index 00000000000..2e52ee56931
--- /dev/null
+++ b/plugins/woocommerce/changelog/58259-fix-malformed-query-args-fatal
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Prevent fatal errors when order or product queries get malformed dates, or order queries get a malformed customer.
diff --git a/plugins/woocommerce/includes/data-stores/class-wc-data-store-wp.php b/plugins/woocommerce/includes/data-stores/class-wc-data-store-wp.php
index 387cf81eebe..9df5049dced 100644
--- a/plugins/woocommerce/includes/data-stores/class-wc-data-store-wp.php
+++ b/plugins/woocommerce/includes/data-stores/class-wc-data-store-wp.php
@@ -333,6 +333,7 @@ class WC_Data_Store_WP {
 	 * Map a valid date query var to WP_Query arguments.
 	 * Valid date formats: YYYY-MM-DD or timestamp, possibly combined with an operator from $valid_operators.
 	 * Also accepts a WC_DateTime object.
+	 * An array, or an object other than WC_DateTime, adds an entry to $wp_query_args['errors'] instead, so callers must check 'errors' before querying.
 	 *
 	 * @since 3.2.0
 	 * @param mixed  $query_var A valid date format.
@@ -354,6 +355,11 @@ class WC_Data_Store_WP {
 		$raw_start = '';
 		$raw_end   = '';

+		if ( is_array( $query_var ) || ( is_object( $query_var ) && ! $query_var instanceof WC_DateTime ) ) {
+			$wp_query_args['errors'][] = new WP_Error( 'woocommerce_query_invalid', __( 'Invalid date query.', 'woocommerce' ) );
+			return $wp_query_args;
+		}
+
 		try {
 			// Specific time query with a WC_DateTime.
 			if ( is_a( $query_var, 'WC_DateTime' ) ) {
diff --git a/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php b/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php
index 22dfde139fb..92859a8497c 100644
--- a/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php
+++ b/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php
@@ -521,7 +521,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
 					return $query_part;
 				}
 				$meta_query[] = $query_part;
-			} elseif ( is_email( $value ) ) {
+			} elseif ( is_string( $value ) && is_email( $value ) ) {
 				$meta_query['customer_emails']['value'][] = sanitize_email( $value );
 			} elseif ( is_numeric( $value ) ) {
 				$meta_query['customer_ids']['value'][] = strval( absint( $value ) );
@@ -1052,7 +1052,7 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
 			'date_paid'      => '_date_paid',
 		);
 		foreach ( $date_queries as $query_var_key => $db_key ) {
-			if ( isset( $query_vars[ $query_var_key ] ) && '' !== $query_vars[ $query_var_key ] ) {
+			if ( isset( $query_vars[ $query_var_key ] ) && '' !== $query_vars[ $query_var_key ] && array() !== $query_vars[ $query_var_key ] ) {

 				// Remove any existing meta queries for the same keys to prevent conflicts.
 				$existing_queries = wp_list_pluck( $wp_query_args['meta_query'], 'key', true );
diff --git a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php
index b6f22875639..61f9138fd85 100644
--- a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php
+++ b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php
@@ -2624,7 +2624,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
 			'date_on_sale_to'   => '_sale_price_dates_to',
 		);
 		foreach ( $date_queries as $query_var_key => $db_key ) {
-			if ( isset( $query_vars[ $query_var_key ] ) && '' !== $query_vars[ $query_var_key ] ) {
+			if ( isset( $query_vars[ $query_var_key ] ) && '' !== $query_vars[ $query_var_key ] && array() !== $query_vars[ $query_var_key ] ) {

 				// Remove any existing meta queries for the same keys to prevent conflicts.
 				$existing_queries = wp_list_pluck( $wp_query_args['meta_query'], 'key', true );
diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php
index 857ef201559..ebdd37dc7ea 100644
--- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php
+++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php
@@ -484,9 +484,14 @@ class OrdersTableQuery {
 		foreach ( $date_keys as $date_key ) {
 			$is_local   = in_array( $date_key, $local_date_keys, true );
 			$date_value = $this->args[ $date_key ];
-			$operator   = '=';
-			$dates_raw  = array();
-			$dates      = array();
+
+			if ( is_array( $date_value ) || ( is_object( $date_value ) && ! $date_value instanceof \WC_DateTime ) ) {
+				throw new \Exception( 'Invalid date_query' );
+			}
+
+			$operator  = '=';
+			$dates_raw = array();
+			$dates     = array();

 			if ( is_string( $date_value ) && preg_match( self::REGEX_SHORTHAND_DATES, $date_value, $matches ) ) {
 				$operator = in_array( $matches[2], $valid_operators, true ) ? $matches[2] : '';
diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php
index 44e5314df51..cb6ff0f2ba3 100644
--- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php
+++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php
@@ -2125,6 +2125,81 @@ class WC_Order_Data_Store_CPT_Test extends WC_Unit_Test_Case {
 		$this->assertStringStartsWith( $expected_message, $messages[ WC_Order_Data_Store_CPT::class . '::query' ], 'The notice should use the English wording for the argument count.' );
 	}

+	/**
+	 * @testdox Malformed date and customer args return no orders instead of a fatal error.
+	 *
+	 * @dataProvider malformed_query_arg_provider
+	 * @param string $query_var The query var.
+	 * @param mixed  $value     The malformed value.
+	 */
+	public function test_malformed_query_arg_returns_no_orders( string $query_var, $value ): void {
+		$order = WC_Helper_Order::create_order( 1 );
+		$order->set_date_created( '2000-01-01T10:00:00' );
+		$order->set_date_paid( '2000-01-01T10:00:00' );
+		$order->save();
+
+		$valid_values = array(
+			'date_created' => '2000-01-01',
+			'date_paid'    => '2000-01-01',
+			'customer'     => 1,
+		);
+
+		$this->assertSame(
+			array( $order->get_id() ),
+			wc_get_orders(
+				array(
+					$query_var => $valid_values[ $query_var ],
+					'return'   => 'ids',
+				)
+			),
+			'A valid value should match the order'
+		);
+
+		$queried_orders = wc_get_orders(
+			array(
+				$query_var => $value,
+				'return'   => 'ids',
+			)
+		);
+
+		$this->assertSame( array(), $queried_orders, 'A malformed value should not match any order' );
+	}
+
+	/**
+	 * @testdox An empty date array applies no date filter, like an unset date arg.
+	 */
+	public function test_empty_date_array_applies_no_date_filter(): void {
+		$order = WC_Helper_Order::create_order();
+		$order->set_date_created( '2000-01-01T10:00:00' );
+		$order->save();
+		$other_order = WC_Helper_Order::create_order();
+
+		$queried_orders = wc_get_orders(
+			array(
+				'date_created' => array(),
+				'return'       => 'ids',
+			)
+		);
+
+		sort( $queried_orders );
+		$this->assertSame( array( $order->get_id(), $other_order->get_id() ), $queried_orders );
+	}
+
+	/**
+	 * Data provider for test_malformed_query_arg_returns_no_orders.
+	 *
+	 * @return array
+	 */
+	public function malformed_query_arg_provider(): array {
+		return array(
+			'date, array'                => array( 'date_created', array( '2000-01-01' ) ),
+			'date, object'               => array( 'date_paid', new stdClass() ),
+			'customer, object'           => array( 'customer', new stdClass() ),
+			'customer, object in array'  => array( 'customer', array( 1, new stdClass() ) ),
+			'customer, Stringable email' => array( 'customer', new WC_Order_Data_Store_CPT_Test_Stringable( 'admin@example.org' ) ),
+		);
+	}
+
 	/**
 	 * @testdox clear_recorded_coupon_usage_counts() clears a set flag once, and only while the stored status matches the order object.
 	 */
@@ -2186,3 +2261,32 @@ class WC_Order_Data_Store_CPT_Test extends WC_Unit_Test_Case {
 		return $order;
 	}
 }
+
+// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound -- Test double.
+/**
+ * A Stringable value for customer query tests.
+ */
+class WC_Order_Data_Store_CPT_Test_Stringable {
+	/**
+	 * The string value.
+	 *
+	 * @var string
+	 */
+	private $value;
+
+	/**
+	 * Constructor.
+	 *
+	 * @param string $value The string value.
+	 */
+	public function __construct( string $value ) {
+		$this->value = $value;
+	}
+
+	/**
+	 * @return string
+	 */
+	public function __toString(): string {
+		return $this->value;
+	}
+}
diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
index 173cb910172..7a00d2a22fa 100644
--- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
+++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
@@ -1,5 +1,6 @@
 <?php

+use Automattic\WooCommerce\Enums\ProductStockStatus;
 use Automattic\WooCommerce\Internal\CostOfGoodsSold\CogsAwareUnitTestSuiteTrait;

 /**
@@ -861,6 +862,73 @@ class WC_Product_Data_Store_CPT_Test extends WC_Unit_Test_Case {
 		$this->assertSame( array( 'size' => 'small' ), $product->get_default_attributes(), 'Scalar default attributes should still load' );
 	}

+	/**
+	 * @testdox Malformed date args return no products instead of a fatal error.
+	 *
+	 * @dataProvider malformed_date_arg_provider
+	 * @param string $date_key   The date query var.
+	 * @param mixed  $date_value The malformed value.
+	 */
+	public function test_malformed_date_arg_returns_no_products( string $date_key, $date_value ): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$product->set_date_created( '2000-01-01T10:00:00' );
+		$product->save();
+
+		$this->assertSame(
+			array( $product->get_id() ),
+			wc_get_products(
+				array(
+					$date_key => '2000-01-01',
+					'return'  => 'ids',
+				)
+			),
+			'A valid date should match the product'
+		);
+
+		$queried_products = wc_get_products(
+			array(
+				$date_key => $date_value,
+				'return'  => 'ids',
+			)
+		);
+
+		$this->assertSame( array(), $queried_products, 'A malformed date should not match any product' );
+	}
+
+	/**
+	 * Data provider for test_malformed_date_arg_returns_no_products.
+	 *
+	 * @return array
+	 */
+	public function malformed_date_arg_provider(): array {
+		return array(
+			'post column, array'      => array( 'date_created', array( '2000-01-01' ) ),
+			'post column, object'     => array( 'date_created', new stdClass() ),
+			'post column, Stringable' => array( 'date_created', new WC_Product_Data_Store_CPT_Test_Stringable( '2000-01-01' ) ),
+		);
+	}
+
+	/**
+	 * @testdox An empty date array applies no date filter and keeps the other filters, like an unset date arg.
+	 */
+	public function test_empty_date_array_applies_no_date_filter(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$product->set_date_created( '2000-01-01T10:00:00' );
+		$product->set_stock_status( ProductStockStatus::OUT_OF_STOCK );
+		$product->save();
+		WC_Helper_Product::create_simple_product();
+
+		$queried_products = wc_get_products(
+			array(
+				'date_created' => array(),
+				'stock_status' => ProductStockStatus::OUT_OF_STOCK,
+				'return'       => 'ids',
+			)
+		);
+
+		$this->assertSame( array( $product->get_id() ), $queried_products );
+	}
+
 	/**
 	 * @testdox Creating and updating a product keeps backslashes and quotes in its name and descriptions.
 	 */
@@ -945,3 +1013,32 @@ class WC_Product_Data_Store_CPT_Test extends WC_Unit_Test_Case {
 		$this->assertSame( 'Men\'s 1\\2 inch', get_post( $product->get_id() )->post_title );
 	}
 }
+
+// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound -- Test double.
+/**
+ * A Stringable value for date query tests.
+ */
+class WC_Product_Data_Store_CPT_Test_Stringable {
+	/**
+	 * The string value.
+	 *
+	 * @var string
+	 */
+	private $value;
+
+	/**
+	 * Constructor.
+	 *
+	 * @param string $value The string value.
+	 */
+	public function __construct( string $value ) {
+		$this->value = $value;
+	}
+
+	/**
+	 * @return string
+	 */
+	public function __toString(): string {
+		return $this->value;
+	}
+}
diff --git a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableQueryTests.php b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableQueryTests.php
index 7294d2b5a43..9984a464ed4 100644
--- a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableQueryTests.php
+++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableQueryTests.php
@@ -195,6 +195,116 @@ class OrdersTableQueryTests extends \WC_Unit_Test_Case {
 		$this->assertContains( $orders[1]->get_id(), $queried_orders );
 	}

+	/**
+	 * @testdox Malformed date args return no orders instead of a fatal error.
+	 *
+	 * @dataProvider malformed_date_arg_provider
+	 * @param string $date_key   The date query var.
+	 * @param mixed  $date_value The malformed value.
+	 */
+	public function test_malformed_date_arg_returns_no_orders( string $date_key, $date_value ): void {
+		$order = OrderHelper::create_order();
+		$order->set_date_created( '2000-01-01T10:00:00' );
+		$order->save();
+
+		$this->assertSame(
+			array( $order->get_id() ),
+			wc_get_orders(
+				array(
+					$date_key => '2000-01-01',
+					'return'  => 'ids',
+				)
+			),
+			'A valid date should match the order'
+		);
+
+		$queried_orders = wc_get_orders(
+			array(
+				$date_key => $date_value,
+				'return'  => 'ids',
+			)
+		);
+
+		$this->assertSame( array(), $queried_orders, 'A malformed date should not match any order' );
+	}
+
+	/**
+	 * Data provider for test_malformed_date_arg_returns_no_orders.
+	 *
+	 * @return array
+	 */
+	public function malformed_date_arg_provider(): array {
+		return array(
+			'local key, array'      => array( 'date_created', array( '2000-01-01' ) ),
+			'local key, object'     => array( 'date_created', new \stdClass() ),
+			'local key, Stringable' => array(
+				'date_created',
+				new class() {
+					/**
+					 * @return string
+					 */
+					public function __toString(): string {
+						return '2000-01-01';
+					}
+				},
+			),
+		);
+	}
+
+	/**
+	 * @testdox WC_DateTime date args match orders.
+	 *
+	 * @dataProvider date_time_arg_provider
+	 * @param string       $date_key   The date query var.
+	 * @param \WC_DateTime $date_value The date.
+	 */
+	public function test_date_time_arg_matches_order( string $date_key, $date_value ): void {
+		$orders = $this->create_orders_with_different_dates();
+
+		$queried_orders = wc_get_orders(
+			array(
+				$date_key => $date_value,
+				'return'  => 'ids',
+			)
+		);
+
+		$this->assertSame( array( $orders[0]->get_id() ), $queried_orders );
+	}
+
+	/**
+	 * Data provider for test_date_time_arg_matches_order.
+	 *
+	 * @return array
+	 */
+	public function date_time_arg_provider(): array {
+		$date_time = new \WC_DateTime( '2000-01-01T10:00:00', new \DateTimeZone( 'UTC' ) );
+
+		return array(
+			'local key, WC_DateTime' => array( 'date_created', $date_time ),
+			'GMT key, WC_DateTime'   => array( 'date_created_gmt', $date_time ),
+		);
+	}
+
+	/**
+	 * @testdox An empty date array applies no date filter, like an unset date arg.
+	 */
+	public function test_empty_date_array_applies_no_date_filter(): void {
+		$order_ids = array_map(
+			fn( $order ) => $order->get_id(),
+			$this->create_orders_with_different_dates()
+		);
+
+		$queried_orders = wc_get_orders(
+			array(
+				'date_created' => array(),
+				'return'       => 'ids',
+			)
+		);
+
+		sort( $queried_orders );
+		$this->assertSame( $order_ids, $queried_orders );
+	}
+
 	/**
 	 * @testDox 'suppress_filters' arg is honored in queries.
 	 */