Commit e1d67d848bf for woocommerce

commit e1d67d848bfb1b775f6b3e99fdcfaf6380810b28
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date:   Thu Nov 19 09:59:22 2026 -0300

    Return a not-found error for missing product IDs in the product abilities (#71075)

    * Answer unknown product IDs with not-found instead of permission denied

    product-update, product-delete and product-create (for a variation parent)
    checked edit_post or delete_post on the given ID. WordPress denies that cap
    for a post that does not exist, so a wrong ID came back as a permission
    error. Now an ID that is not a product falls back to the type-level check,
    as the order abilities do. Users who may edit or delete any product get
    "Product not found."; other users still get the permission error.

    The products-query id input now says that it is a lookup that errors when
    the product does not exist.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Show the product not-found error only to users who can read private products

    The fallback for an ID that is not a product now also checks the product
    read permission. A custom role that may edit or delete other users'
    products but cannot read private ones keeps the permission error, so it
    cannot tell another author's private product from a missing ID.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Keep the permission check for a variation whose parent no longer exists

    The not-found fallback ran when the resolved parent ID did not exist,
    even when the given ID was an existing variation. A user who could act
    on any product could then delete an orphan variation without the check
    on that variation. The fallback now runs only when the given ID is not
    a product or variation.

    The test for users who cannot act on any product now gives the user
    read_private_products, so it reaches the edit and delete checks.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Remove the id description from products-query

    The description is out of scope for a permission fix. The orders-query
    id has no description either, and the not-found error already tells the
    caller what happened.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    ---------

    Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/fix-wooai-1652-product-abilities-not-found b/plugins/woocommerce/changelog/fix-wooai-1652-product-abilities-not-found
new file mode 100644
index 00000000000..90467974892
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-wooai-1652-product-abilities-not-found
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Product abilities now answer an unknown product ID with "Product not found." instead of a permission error.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductCreate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductCreate.php
index 350430709ce..392bc9d91cd 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductCreate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductCreate.php
@@ -230,9 +230,7 @@ class ProductCreate extends ActionableAbility {
 			return true;
 		}

-		$parent_id = $input['parent_id'] ?? 0;
-
-		return $parent_id > 0 && wc_rest_check_post_permissions( 'product', 'edit', $parent_id );
+		return self::check_product_permission( $input['parent_id'] ?? 0, 'edit' );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php
index 9582cc2dec8..31d21312cb3 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductDelete.php
@@ -305,9 +305,7 @@ class ProductDelete extends ActionableAbility {
 	 * @return bool
 	 */
 	public static function can_delete_product( $input = array() ): bool {
-		$product_id = self::get_product_permission_id( self::get_id_from_input( $input ) );
-
-		return $product_id > 0 && wc_rest_check_post_permissions( 'product', 'delete', $product_id );
+		return self::check_product_permission( self::get_id_from_input( $input ), 'delete' );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductUpdate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductUpdate.php
index 01c55219b66..472edcbf5db 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductUpdate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductUpdate.php
@@ -247,12 +247,11 @@ class ProductUpdate extends ActionableAbility {
 	 * @since 10.9.0
 	 */
 	public static function can_update_product( $input = array() ): bool {
-		$product_id = self::get_product_permission_id( self::get_id_from_input( $input ) );
+		$product_id = self::get_id_from_input( $input );
 		$deletes    = ! empty( array_filter( array_column( $input['variations'] ?? array(), 'delete' ) ) );

-		return $product_id > 0
-			&& wc_rest_check_post_permissions( 'product', 'edit', $product_id )
-			&& ( ! $deletes || wc_rest_check_post_permissions( 'product', 'delete', $product_id ) );
+		return self::check_product_permission( $product_id, 'edit' )
+			&& ( ! $deletes || self::check_product_permission( $product_id, 'delete' ) );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
index 41d1003ad61..c2ebe234f62 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
@@ -826,6 +826,49 @@ trait ProductAbilityTrait {
 		return $product_id;
 	}

+	/**
+	 * Check an `edit` or `delete` permission on a product ID, as the REST API does.
+	 * An ID that is not a product or variation passes for users who could act on
+	 * any product and read private ones, so they get the not-found error instead
+	 * of a permission error.
+	 *
+	 * @param int    $product_id Product or variation ID.
+	 * @param string $context    `edit` or `delete`.
+	 * @return bool
+	 */
+	protected static function check_product_permission( int $product_id, string $context ): bool {
+		if ( $product_id <= 0 ) {
+			return false;
+		}
+
+		if ( in_array( get_post_type( $product_id ), array( 'product', 'product_variation' ), true ) ) {
+			return (bool) wc_rest_check_post_permissions( 'product', $context, self::get_product_permission_id( $product_id ) );
+		}
+
+		if ( ! wc_rest_check_post_permissions( 'product', 'read' ) ) {
+			return false;
+		}
+
+		if ( 'edit' === $context ) {
+			return (bool) wc_rest_check_post_permissions( 'product', 'batch' );
+		}
+
+		$type_object = get_post_type_object( 'product' );
+		$permission  = $type_object instanceof \WP_Post_Type && current_user_can( $type_object->cap->delete_others_posts );
+
+		/**
+		 * Filter a REST API permission check, as wc_rest_check_post_permissions() does.
+		 *
+		 * @since 2.6.0
+		 *
+		 * @param bool   $permission Whether the current user has the permission.
+		 * @param string $context    Permission context.
+		 * @param int    $object_id  Object ID, 0 for no object.
+		 * @param string $post_type  Post type.
+		 */
+		return (bool) apply_filters( 'woocommerce_rest_check_permissions', $permission, 'delete', 0, 'product' );
+	}
+
 	/**
 	 * The variable parent of a saved variation. The call syncs and saves it
 	 * after the variation, so it is a secondary object of the call.
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index b93862cf6da..70d13f692f3 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -4439,41 +4439,150 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * @testdox Should reject product mutation unknown IDs without leaking existence.
+	 * @testdox Should return the not-found error for an unknown product ID to users who can act on any product.
 	 *
 	 * @dataProvider provider_product_mutation_unknown_id_inputs
 	 *
 	 * @param string $ability_id Ability ID.
 	 * @param array  $input      Ability input.
+	 * @param string $error_code Expected error code.
 	 */
-	public function test_product_mutations_reject_unknown_ids_without_leaking_existence( string $ability_id, array $input ): void {
+	public function test_product_mutations_return_not_found_for_unknown_ids( string $ability_id, array $input, string $error_code ): void {
 		$result = wp_get_ability( $ability_id )->execute( $input );

+		$this->assertWPError( $result );
+		$this->assertSame( $error_code, $result->get_error_code() );
+	}
+
+	/**
+	 * @testdox Should refuse permission for an unknown product ID to users who cannot act on any product.
+	 *
+	 * @dataProvider provider_product_mutation_unknown_id_inputs
+	 *
+	 * @param string $ability_id Ability ID.
+	 * @param array  $input      Ability input.
+	 */
+	public function test_product_mutations_refuse_unknown_ids_to_users_who_cannot_act_on_any_product( string $ability_id, array $input ): void {
+		wp_set_current_user( $this->create_user_with_caps( array( 'edit_products', 'publish_products', 'delete_products', 'read_private_products' ) ) );
+
+		$result = wp_get_ability( $ability_id )->execute( $input );
+
+		$this->assertWPError( $result );
+		$this->assertSame( 'ability_invalid_permissions', $result->get_error_code() );
+	}
+
+	/**
+	 * @testdox Should refuse permission for an unknown product ID to users who cannot read private products.
+	 *
+	 * @dataProvider provider_product_mutation_unknown_id_inputs
+	 *
+	 * @param string $ability_id Ability ID.
+	 * @param array  $input      Ability input.
+	 */
+	public function test_product_mutations_refuse_unknown_ids_to_users_who_cannot_read_private_products( string $ability_id, array $input ): void {
+		wp_set_current_user( $this->create_user_with_caps( array( 'edit_products', 'edit_others_products', 'publish_products', 'delete_products', 'delete_others_products' ) ) );
+
+		$result = wp_get_ability( $ability_id )->execute( $input );
+
+		$this->assertWPError( $result );
+		$this->assertSame( 'ability_invalid_permissions', $result->get_error_code() );
+	}
+
+	/**
+	 * @testdox Should refuse permission to delete a variation whose parent no longer exists.
+	 */
+	public function test_product_delete_refuses_variation_with_missing_parent(): void {
+		global $wpdb;
+
+		$variation_id = $this->create_variation_product_id_for_test();
+		$wpdb->update( $wpdb->posts, array( 'post_parent' => 999999 ), array( 'ID' => $variation_id ) );
+		clean_post_cache( $variation_id );
+
+		$result = wp_get_ability( 'woocommerce/product-delete' )->execute( array( 'id' => $variation_id ) );
+
 		$this->assertWPError( $result );
 		$this->assertSame( 'ability_invalid_permissions', $result->get_error_code() );
+		$this->assertSame( 'publish', get_post_status( $variation_id ) );
 	}

 	/**
 	 * Provides product mutation inputs with unknown IDs.
 	 *
-	 * @return array<string, array{0: string, 1: array}>
+	 * @return array<string, array{0: string, 1: array, 2: string}>
 	 */
 	public function provider_product_mutation_unknown_id_inputs(): array {
 		return array(
-			'product-update' => array(
+			'product-update'                  => array(
 				'woocommerce/product-update',
 				array(
 					'id'   => 999999,
 					'name' => 'Unknown Product',
 				),
+				'woocommerce_product_not_found',
+			),
+			'product-update variation delete' => array(
+				'woocommerce/product-update',
+				array(
+					'id'         => 999999,
+					'variations' => array(
+						array(
+							'id'     => 999998,
+							'delete' => true,
+						),
+					),
+				),
+				'woocommerce_product_not_found',
 			),
-			'product-delete' => array(
+			'product-delete'                  => array(
 				'woocommerce/product-delete',
 				array(
 					'id' => 999999,
 				),
+				'woocommerce_product_not_found',
 			),
+			'product-create variation'        => array(
+				'woocommerce/product-create',
+				array(
+					'type'      => 'variation',
+					'parent_id' => 999999,
+				),
+				'woocommerce_product_parent_invalid',
+			),
+		);
+	}
+
+	/**
+	 * @testdox Should return the not-found error for an unknown term ID to users who can edit product terms.
+	 */
+	public function test_product_term_update_returns_not_found_for_unknown_id(): void {
+		$result = wp_get_ability( 'woocommerce/product-term-update' )->execute(
+			array(
+				'taxonomy' => 'product_cat',
+				'id'       => 999999,
+				'name'     => 'Unknown',
+			)
+		);
+
+		$this->assertWPError( $result );
+		$this->assertSame( 'woocommerce_ability_product_term_not_found', $result->get_error_code() );
+	}
+
+	/**
+	 * @testdox Should refuse permission for an unknown term ID to users who cannot edit product terms.
+	 */
+	public function test_product_term_update_refuses_unknown_id_to_users_who_cannot_edit_terms(): void {
+		wp_set_current_user( $this->create_user_with_caps( array( 'manage_product_terms' ) ) );
+
+		$result = wp_get_ability( 'woocommerce/product-term-update' )->execute(
+			array(
+				'taxonomy' => 'product_cat',
+				'id'       => 999999,
+				'name'     => 'Unknown',
+			)
 		);
+
+		$this->assertWPError( $result );
+		$this->assertSame( 'ability_invalid_permissions', $result->get_error_code() );
 	}

 	/**