Commit 6b0ab89b27e for woocommerce
commit 6b0ab89b27ed6bf512abfa861434fae3345667b6
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date: Wed Dec 2 20:08:24 2026 +0000
Abilities: Reject empty coupon and tax-rate updates (#71688)
* Abilities: refuse coupon and tax rate updates that send only the id
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Reject coupon updates with empty extension fields
A caller can send an empty extensions object or an empty namespace and receive success without supplying a coupon field. Check for field keys before saving so extension-only updates still accept zero and other empty values.
---------
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-tax-rate-update-no-change b/plugins/woocommerce/changelog/fix-coupon-tax-rate-update-no-change
new file mode 100644
index 00000000000..0d304252e5c
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-coupon-tax-rate-update-no-change
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Refuse coupon and tax rate updates through abilities that send no field to change, instead of reporting success.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
index 237745ffa5a..2afedc7807e 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
@@ -87,7 +87,21 @@ class CouponUpdate extends ActionableAbility {
);
}
- return $coupon;
+ if ( array_diff( array_keys( $input ), array( 'id', 'extensions' ) ) ) {
+ return $coupon;
+ }
+
+ foreach ( $input['extensions'] ?? array() as $fields ) {
+ if ( (array) $fields ) {
+ return $coupon;
+ }
+ }
+
+ return new \WP_Error(
+ 'woocommerce_ability_coupon_no_fields',
+ __( 'No change was sent. Send at least one coupon field to change, in addition to the id.', 'woocommerce' ),
+ array( 'status' => 400 )
+ );
}
/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRateUpdate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRateUpdate.php
index ec6d98d4235..25811afa79e 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRateUpdate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/TaxRateUpdate.php
@@ -106,7 +106,11 @@ class TaxRateUpdate extends AbstractDomainAbility implements AbilityDefinition {
}
if ( empty( $columns ) && empty( $locations ) ) {
- return array( 'rate' => $current );
+ return new \WP_Error(
+ 'woocommerce_ability_tax_rate_no_fields',
+ __( 'No change was sent. Send at least one tax rate field to change, in addition to the id.', 'woocommerce' ),
+ array( 'status' => 400 )
+ );
}
if ( ! empty( $columns ) ) {
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php
index e3be840eb80..5a168585458 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php
@@ -7,12 +7,18 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Tests\Internal\Abilities\Domain;
+use Automattic\WooCommerce\Abilities\ExtensibleAbilities;
use Automattic\WooCommerce\Internal\Abilities\Domain\CouponsQuery;
+use Automattic\WooCommerce\Internal\Abilities\Domain\CouponUpdate;
+use Automattic\WooCommerce\Tests\Internal\Abilities\ExtensibleAbilitiesTestTrait;
/**
* Tests coupon reads through the ability API.
*/
class CouponsQueryTest extends \WC_Unit_Test_Case {
+
+ use ExtensibleAbilitiesTestTrait;
+
/**
* @testdox Should find scheduled coupons by code with their email restrictions.
*/
@@ -295,6 +301,84 @@ class CouponsQueryTest extends \WC_Unit_Test_Case {
$this->assertEquals( $before, ( new \WC_Coupon( $coupon->get_id() ) )->get_data() );
}
+ /**
+ * @testdox Should refuse an update that sends only the id.
+ */
+ public function test_update_with_only_id_is_refused(): void {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ $coupon = new \WC_Coupon();
+ $coupon->set_code( 'only-id' );
+ $coupon->save();
+ $class = \Automattic\WooCommerce\Internal\Abilities\Domain\CouponUpdate::class;
+ $ability = new $class( $class::ABILITY_NAME, $class::get_registration_args() );
+ $result = $ability->execute( array( 'id' => $coupon->get_id() ) );
+ $this->assertWPError( $result );
+ $this->assertSame( 'woocommerce_ability_coupon_no_fields', $result->get_error_code() );
+ $this->assertSame( 400, $result->get_error_data()['status'] );
+ }
+
+ /**
+ * @testdox Should require a coupon field or a field inside an extension namespace.
+ */
+ public function test_update_with_extension_fields(): void {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ add_action( 'woocommerce_extensible_abilities_resources_init', static fn() => ExtensibleAbilities::register_resource( 'coupon' ) );
+ $this->enable_extensible_abilities(
+ static function () {
+ ExtensibleAbilities::register_field(
+ array(
+ 'resource' => 'coupon',
+ 'namespace' => 'test-ext',
+ 'field' => 'limit',
+ 'input_schema' => array( 'type' => 'integer' ),
+ 'output_schema' => array( 'type' => 'integer' ),
+ 'get_callback' => static fn( $coupon ) => (int) $coupon->get_meta( '_test_limit' ),
+ 'set_callback' => static function ( $coupon, $value ) {
+ $coupon->update_meta_data( '_test_limit', $value );
+ },
+ )
+ );
+ }
+ );
+ $coupon = new \WC_Coupon();
+ $coupon->set_code( 'extension-update' );
+ $id = $coupon->save();
+ $ability = new CouponUpdate( CouponUpdate::ABILITY_NAME, CouponUpdate::get_registration_args() );
+ $saves = did_action( 'woocommerce_before_coupon_object_save' );
+
+ foreach ( array( array(), array( 'test-ext' => array() ) ) as $extensions ) {
+ $result = $ability->execute(
+ array(
+ 'id' => $id,
+ 'extensions' => $extensions,
+ )
+ );
+ $this->assertWPError( $result );
+ $this->assertSame( 'woocommerce_ability_coupon_no_fields', $result->get_error_code() );
+ $this->assertSame( 400, $result->get_error_data()['status'] );
+ $this->assertSame( $saves, did_action( 'woocommerce_before_coupon_object_save' ) );
+ }
+
+ $result = $ability->execute(
+ array(
+ 'id' => $id,
+ 'extensions' => array( 'test-ext' => array( 'limit' => 0 ) ),
+ )
+ );
+ $this->assertNotWPError( $result );
+ $this->assertSame( '0', ( new \WC_Coupon( $id ) )->get_meta( '_test_limit' ) );
+
+ $result = $ability->execute(
+ array(
+ 'id' => $id,
+ 'extensions' => array(),
+ 'amount' => '2',
+ )
+ );
+ $this->assertNotWPError( $result );
+ $this->assertSame( '2', ( new \WC_Coupon( $id ) )->get_amount() );
+ }
+
/**
* @testdox Should reject duplicate draft codes and unscheduled future creation.
* @testWith [{"code":"existing-draft"}, "woocommerce_ability_coupon_code_exists"]
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php
index d2e4ee5d3c2..25a71bea80f 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/TaxRateAbilitiesTest.php
@@ -425,9 +425,9 @@ class TaxRateAbilitiesTest extends \WC_Unit_Test_Case {
}
/**
- * @testdox Should return the rate unchanged, without a write, when only the id is sent.
+ * @testdox Should refuse an update that sends only the id, without a write.
*/
- public function test_update_with_only_id_returns_the_rate(): void {
+ public function test_update_with_only_id_is_refused(): void {
$rate = $this->create_rate( array( 'rate' => '19' ) );
$updated = 0;
add_action(
@@ -439,7 +439,9 @@ class TaxRateAbilitiesTest extends \WC_Unit_Test_Case {
$result = $this->run_ability( 'woocommerce/tax-rate-update', array( 'id' => $rate['id'] ) );
- $this->assertSame( array( 'rate' => $rate ), $result );
+ $this->assertWPError( $result );
+ $this->assertSame( 'woocommerce_ability_tax_rate_no_fields', $result->get_error_code() );
+ $this->assertSame( 400, $result->get_error_data()['status'] );
$this->assertSame( 0, $updated, 'No write should happen.' );
}