Commit d8ed970828d for woocommerce

commit d8ed970828da43aa60de506c5c9a035bae9002d9
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date:   Wed Dec 2 17:36:22 2026 +0100

    Abilities: Return not-found for an unknown coupon ID in coupon-update (#71693)

    * Return not-found for an unknown coupon ID in coupon-update

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

    * Check edit access on the coupon loaded by coupon-update

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

    ---------

    Co-authored-by: aaronfc <aaron.fas@automattic.com>
    Co-authored-by: matticbot <matticbot@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/fix-coupon-update-unknown-id-not-found b/plugins/woocommerce/changelog/fix-coupon-update-unknown-id-not-found
new file mode 100644
index 00000000000..19478515244
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-coupon-update-unknown-id-not-found
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Answer an unknown coupon ID in the coupon update ability with a not-found error instead of "Permission denied".
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
index 3547706946c..237745ffa5a 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
@@ -73,7 +73,21 @@ class CouponUpdate extends ActionableAbility {
 	 * @return \WC_Coupon|\WP_Error
 	 */
 	protected function load( array $input ) {
-		return self::get_coupon_from_input( $input );
+		$coupon = self::get_coupon_from_input( $input );
+		if ( is_wp_error( $coupon ) ) {
+			return $coupon;
+		}
+
+		// The permission check may have passed for a missing ID that has since become a coupon.
+		if ( ! wc_rest_check_post_permissions( 'shop_coupon', 'edit', $coupon->get_id() ) ) {
+			return new \WP_Error(
+				'woocommerce_ability_coupon_forbidden',
+				__( 'You are not allowed to edit this coupon.', 'woocommerce' ),
+				array( 'status' => rest_authorization_required_code() )
+			);
+		}
+
+		return $coupon;
 	}

 	/**
@@ -124,7 +138,9 @@ class CouponUpdate extends ActionableAbility {
 	}

 	/**
-	 * Check coupon update access.
+	 * Check coupon update access. An ID that is not a coupon passes for users
+	 * who could edit any coupon and read private ones, so they get the
+	 * not-found error instead of a permission error.
 	 *
 	 * @param mixed $input Ability input.
 	 * @return bool
@@ -134,7 +150,15 @@ class CouponUpdate extends ActionableAbility {
 	public static function can_update_coupon( $input = array() ): bool {
 		$coupon_id = self::get_id_from_input( $input );

-		return $coupon_id > 0 && wc_rest_check_post_permissions( 'shop_coupon', 'edit', $coupon_id );
+		if ( $coupon_id <= 0 ) {
+			return false;
+		}
+
+		if ( 'shop_coupon' === get_post_type( $coupon_id ) ) {
+			return (bool) wc_rest_check_post_permissions( 'shop_coupon', 'edit', $coupon_id );
+		}
+
+		return wc_rest_check_post_permissions( 'shop_coupon', 'read' ) && wc_rest_check_post_permissions( 'shop_coupon', 'batch' );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php
index 156c68e841b..664c57a0dd7 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php
@@ -185,7 +185,8 @@ trait CouponAbilityTrait {
 		if ( 'shop_coupon' !== get_post_type( $coupon_id ) ) {
 			return new \WP_Error(
 				'woocommerce_ability_coupon_not_found',
-				__( 'Coupon not found.', 'woocommerce' ),
+				/* translators: %d: coupon ID. */
+				sprintf( __( 'Coupon %d does not exist. Use the woocommerce/coupons-query ability to find the coupon ID.', 'woocommerce' ), $coupon_id ),
 				array( 'status' => 404 )
 			);
 		}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index 5f7267eeb95..3cdff36487f 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -1560,6 +1560,87 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 		}
 	}

+	/**
+	 * @testdox Should return a not-found error naming the ID when coupon-update receives an ID that is not a coupon.
+	 */
+	public function test_coupon_update_returns_not_found_for_id_that_is_not_a_coupon(): void {
+		$product = \WC_Helper_Product::create_simple_product();
+
+		foreach ( array( 999999, $product->get_id() ) as $id ) {
+			$result = wp_get_ability( 'woocommerce/coupon-update' )->execute(
+				array(
+					'id'     => $id,
+					'amount' => '10',
+				)
+			);
+
+			$this->assertWPError( $result, "coupon-update should refuse ID {$id}." );
+			$this->assertSame( 'woocommerce_ability_coupon_not_found', $result->get_error_code(), "coupon-update should name missing coupon {$id}, not refuse permission." );
+			$this->assertStringContainsString( (string) $id, $result->get_error_message() );
+		}
+	}
+
+	/**
+	 * @testdox Should refuse to change a coupon created at an unknown ID after the permission check when the user cannot edit it.
+	 */
+	public function test_coupon_update_refuses_coupon_created_after_permission_check(): void {
+		$coupon_id = (int) self::factory()->post->create() + 1000;
+		wp_set_current_user( $this->create_user_with_caps( array( 'edit_shop_coupons', 'read_private_shop_coupons', 'edit_others_shop_coupons' ) ) );
+		add_filter(
+			'woocommerce_rest_check_permissions',
+			function ( $permission, $context, $object_id, $post_type ) use ( $coupon_id ) {
+				if ( 'batch' === $context && 'shop_coupon' === $post_type && 'shop_coupon' !== get_post_type( $coupon_id ) ) {
+					wp_insert_post(
+						array(
+							'import_id'   => $coupon_id,
+							'post_type'   => 'shop_coupon',
+							'post_status' => 'publish',
+							'post_title'  => 'raced',
+							'post_author' => self::$administrator_id,
+						)
+					);
+				}
+				return $permission;
+			},
+			10,
+			4
+		);
+
+		$result = wp_get_ability( 'woocommerce/coupon-update' )->execute(
+			array(
+				'id'     => $coupon_id,
+				'amount' => '10',
+			)
+		);
+
+		$this->assertWPError( $result );
+		$this->assertSame( 'woocommerce_ability_coupon_forbidden', $result->get_error_code() );
+		$this->assertSame( 'shop_coupon', get_post_type( $coupon_id ), 'The filter should have created the coupon.' );
+		$this->assertSame( 0.0, (float) ( new \WC_Coupon( $coupon_id ) )->get_amount(), 'The coupon should not be changed.' );
+	}
+
+	/**
+	 * @testdox Should refuse permission for an unknown coupon ID to users who cannot edit and read every coupon.
+	 *
+	 * @testWith [["edit_shop_coupons", "read_private_shop_coupons"]]
+	 *           [["edit_shop_coupons", "edit_others_shop_coupons"]]
+	 *
+	 * @param string[] $caps Only capabilities added to a subscriber.
+	 */
+	public function test_coupon_update_refuses_unknown_id_to_users_who_cannot_edit_and_read_every_coupon( array $caps ): void {
+		wp_set_current_user( $this->create_user_with_caps( $caps ) );
+
+		$result = wp_get_ability( 'woocommerce/coupon-update' )->execute(
+			array(
+				'id'     => 999999,
+				'amount' => '10',
+			)
+		);
+
+		$this->assertWPError( $result );
+		$this->assertSame( 'ability_invalid_permissions', $result->get_error_code() );
+	}
+
 	/**
 	 * @testdox Should refuse permission when an order mutation receives the ID of a refund.
 	 */