Commit 0455d77585b for woocommerce
commit 0455d77585b1181c35cabbcc2fc3b279a2e71838
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 17:35:21 2026 +0100
Require order capabilities on action and receipt REST endpoints (#71244)
Require order capabilities on the order action and receipt REST endpoints
(cherry picked from commit 46d6f8380d01a3ce0a312b78328d89ce0d6b8026)
Co-authored-by: Raluca Stan <ralucastn@gmail.com>
diff --git a/plugins/woocommerce/src/Internal/Orders/OrderActionsRestController.php b/plugins/woocommerce/src/Internal/Orders/OrderActionsRestController.php
index fbcf527f878..784c42fc915 100644
--- a/plugins/woocommerce/src/Internal/Orders/OrderActionsRestController.php
+++ b/plugins/woocommerce/src/Internal/Orders/OrderActionsRestController.php
@@ -70,7 +70,7 @@ class OrderActionsRestController extends RestApiControllerBase {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => fn( $request ) => $this->run( $request, 'get_email_templates' ),
- 'permission_callback' => fn( $request ) => $this->check_permissions( $request ),
+ 'permission_callback' => fn( $request ) => $this->check_permissions( $request, 'read' ),
'args' => array(),
),
'schema' => array( $this, 'get_schema_for_email_templates' ),
@@ -90,7 +90,7 @@ class OrderActionsRestController extends RestApiControllerBase {
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => fn( $request ) => $this->run( $request, 'send_email' ),
- 'permission_callback' => fn( $request ) => $this->check_permissions( $request ),
+ 'permission_callback' => fn( $request ) => $this->check_permissions( $request, 'edit' ),
'args' => $this->get_args_for_order_actions( 'send_email', WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_schema_for_order_actions' ),
@@ -110,7 +110,7 @@ class OrderActionsRestController extends RestApiControllerBase {
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => fn( $request ) => $this->run( $request, 'send_order_details' ),
- 'permission_callback' => fn( $request ) => $this->check_permissions( $request ),
+ 'permission_callback' => fn( $request ) => $this->check_permissions( $request, 'edit' ),
'args' => $this->get_args_for_order_actions( 'send_order_details', WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_schema_for_order_actions' ),
@@ -155,19 +155,20 @@ class OrderActionsRestController extends RestApiControllerBase {
}
/**
- * Permission check for REST API endpoint.
+ * Permission check for an order action endpoint.
*
* @param WP_REST_Request $request The request for which the permission is checked.
- * @return bool|WP_Error True if the current user has the capability, otherwise a WP_Error object.
+ * @param string $context The request context ('read' or 'edit'), as accepted by wc_rest_check_post_permissions().
+ * @return bool|WP_Error True if the current user has the required permission, otherwise a WP_Error object.
*/
- private function check_permissions( WP_REST_Request $request ) {
+ private function check_permissions( WP_REST_Request $request, string $context ) {
$order_id = $this->validate_order_id( $request );
if ( is_wp_error( $order_id ) ) {
return $order_id;
}
- return $this->check_permission( $request, 'read_shop_order', $order_id );
+ return $this->check_post_type_permission( $request, 'shop_order', $context, (int) $order_id );
}
/**
diff --git a/plugins/woocommerce/src/Internal/ReceiptRendering/ReceiptRenderingRestController.php b/plugins/woocommerce/src/Internal/ReceiptRendering/ReceiptRenderingRestController.php
index c1248066b38..f7e0202372a 100644
--- a/plugins/woocommerce/src/Internal/ReceiptRendering/ReceiptRenderingRestController.php
+++ b/plugins/woocommerce/src/Internal/ReceiptRendering/ReceiptRenderingRestController.php
@@ -10,7 +10,7 @@ use Automattic\WooCommerce\Internal\RestApiControllerBase;
/**
* Controller for the REST endpoints associated to the receipt rendering engine.
- * The endpoints require the read_shop_order capability for the order at hand.
+ * The endpoints require the same permission as reading an order through the REST API.
*/
class ReceiptRenderingRestController extends RestApiControllerBase {
@@ -34,7 +34,7 @@ class ReceiptRenderingRestController extends RestApiControllerBase {
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => fn( $request ) => $this->run( $request, 'create_order_receipt' ),
- 'permission_callback' => fn( $request ) => $this->check_permission( $request, 'read_shop_order', $request->get_param( 'id' ) ),
+ 'permission_callback' => fn( $request ) => $this->check_post_type_permission( $request, 'shop_order', 'read', (int) $request->get_param( 'id' ) ),
'args' => $this->get_args_for_create_order_receipt(),
'schema' => $this->get_schema_for_get_and_post_order_receipt(),
),
@@ -48,7 +48,7 @@ class ReceiptRenderingRestController extends RestApiControllerBase {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => fn( $request ) => $this->run( $request, 'get_order_receipt' ),
- 'permission_callback' => fn( $request ) => $this->check_permission( $request, 'read_shop_order', $request->get_param( 'id' ) ),
+ 'permission_callback' => fn( $request ) => $this->check_post_type_permission( $request, 'shop_order', 'read', (int) $request->get_param( 'id' ) ),
'args' => $this->get_args_for_get_order_receipt(),
'schema' => $this->get_schema_for_get_and_post_order_receipt(),
),
diff --git a/plugins/woocommerce/src/Internal/RestApiControllerBase.php b/plugins/woocommerce/src/Internal/RestApiControllerBase.php
index 6a105004fd5..a8514a7053d 100644
--- a/plugins/woocommerce/src/Internal/RestApiControllerBase.php
+++ b/plugins/woocommerce/src/Internal/RestApiControllerBase.php
@@ -198,6 +198,40 @@ abstract class RestApiControllerBase implements RegisterHooksInterface {
return true;
}
+ return $this->get_permission_denied_error( $request );
+ }
+
+ /**
+ * Permission check for REST API endpoints that act on a post type object.
+ *
+ * Resolves the capability through wc_rest_check_post_permissions(), so the endpoint
+ * uses the same rules and filters as the post type's own REST API controller.
+ *
+ * @since 11.3.1
+ *
+ * @param WP_REST_Request $request The request for which the permission is checked.
+ * @param string $post_type The post type the endpoint acts on.
+ * @param string $context The request context, one of the contexts accepted by wc_rest_check_post_permissions().
+ * @param int $object_id The ID of the object the endpoint acts on.
+ * @phpstan-param WP_REST_Request<array<string, mixed>> $request
+ * @return bool|WP_Error True if the current user has the required permission, otherwise a WP_Error object.
+ */
+ protected function check_post_type_permission( WP_REST_Request $request, string $post_type, string $context, int $object_id = 0 ) {
+ if ( wc_rest_check_post_permissions( $post_type, $context, $object_id ) ) {
+ return true;
+ }
+
+ return $this->get_permission_denied_error( $request );
+ }
+
+ /**
+ * Build the error returned when a permission check fails.
+ *
+ * @param WP_REST_Request $request The request that failed the permission check.
+ * @phpstan-param WP_REST_Request<array<string, mixed>> $request
+ * @return bool|WP_Error A WP_Error object, or false if the request method has no registered error message.
+ */
+ private function get_permission_denied_error( WP_REST_Request $request ) {
$error_information = $this->get_authentication_error_by_method( $request->get_method() );
if ( is_null( $error_information ) ) {
return false;
diff --git a/plugins/woocommerce/tests/php/src/Internal/Orders/OrderActionsRestControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/Orders/OrderActionsRestControllerTest.php
index 02959b51809..9544f1e8245 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Orders/OrderActionsRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Orders/OrderActionsRestControllerTest.php
@@ -3,6 +3,7 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Tests\Internal\Orders;
+use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Internal\Orders\OrderActionsRestController;
use WC_Helper_Order;
use WC_Unit_Test_Case;
@@ -805,6 +806,45 @@ class OrderActionsRestControllerTest extends WC_Unit_Test_Case {
$this->assertEquals( 403, $response->get_status() );
}
+ /**
+ * A customer who authored the order's post must still be refused on every action route.
+ */
+ public function test_order_author_customer_is_denied_on_all_actions() {
+ $order = WC_Helper_Order::create_order();
+ $order->set_status( OrderStatus::PROCESSING );
+ $order->save();
+ $this->set_order_post_author( $order->get_id(), $this->user['customer'] );
+
+ wp_set_current_user( $this->user['customer'] );
+ $this->assertTrue(
+ current_user_can( 'read_shop_order', $order->get_id() ), // phpcs:ignore WordPress.WP.Capabilities.Unknown -- shop_order meta cap, not in the sniff's core list.
+ 'Fixture must model an order the author can read.'
+ );
+
+ $routes = array(
+ array( 'GET', '/actions/email_templates' ),
+ array( 'POST', '/actions/send_email' ),
+ array( 'POST', '/actions/send_order_details' ),
+ );
+
+ foreach ( $routes as list( $method, $suffix ) ) {
+ $response = $this->server->dispatch( new WP_REST_Request( $method, '/wc/v3/orders/' . $order->get_id() . $suffix ) );
+ $this->assertEquals( 403, $response->get_status(), "$method $suffix should be forbidden for the order author." );
+ }
+ }
+
+ /**
+ * Record a user as the author of an order's underlying post.
+ *
+ * @param int $order_id The order ID.
+ * @param int $user_id The user to record as the post author.
+ */
+ private function set_order_post_author( int $order_id, int $user_id ): void {
+ global $wpdb;
+ $wpdb->update( $wpdb->posts, array( 'post_author' => $user_id ), array( 'ID' => $order_id ) );
+ clean_post_cache( $order_id );
+ }
+
/**
* Test sending order details email with a custom email, but the order already has a billing email.
*/
diff --git a/plugins/woocommerce/tests/php/src/Internal/ReceiptRendering/ReceiptRenderingRestControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/ReceiptRendering/ReceiptRenderingRestControllerTest.php
new file mode 100644
index 00000000000..c00f1b0909d
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Internal/ReceiptRendering/ReceiptRenderingRestControllerTest.php
@@ -0,0 +1,139 @@
+<?php
+declare( strict_types=1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\ReceiptRendering;
+
+use Automattic\WooCommerce\Internal\ReceiptRendering\ReceiptRenderingEngine;
+use Automattic\WooCommerce\Internal\ReceiptRendering\ReceiptRenderingRestController;
+use Automattic\WooCommerce\Internal\TransientFiles\TransientFilesEngine;
+use WC_Helper_Order;
+use WC_Unit_Test_Case;
+use WP_REST_Request;
+use WP_REST_Server;
+use WP_UnitTest_Factory;
+
+/**
+ * ReceiptRenderingRestController API controller test.
+ *
+ * @class ReceiptRenderingRestController
+ */
+class ReceiptRenderingRestControllerTest extends WC_Unit_Test_Case {
+
+ /**
+ * REST server used to dispatch receipt requests.
+ *
+ * @var WP_REST_Server
+ */
+ private $server;
+
+ /**
+ * User fixture IDs keyed by role.
+ *
+ * @var int[]
+ */
+ private static $fixture_user = array();
+
+ /**
+ * @var int[] Associative array of user IDs.
+ */
+ private $user = array();
+
+ /**
+ * Name of the receipt file a test created on disk, deleted in tearDown since the database rollback doesn't cover files.
+ *
+ * @var string|null
+ */
+ private $receipt_filename = null;
+
+ /**
+ * Create immutable users shared by the test class.
+ *
+ * @param WP_UnitTest_Factory $factory WordPress unit test factory.
+ */
+ public static function wpSetUpBeforeClass( $factory ): void {
+ self::$fixture_user['shop_manager'] = $factory->user->create( array( 'role' => 'shop_manager' ) );
+ self::$fixture_user['customer'] = $factory->user->create( array( 'role' => 'customer' ) );
+ }
+
+ /**
+ * Set up test.
+ */
+ public function setUp(): void {
+ parent::setUp();
+
+ $controller = wc_get_container()->get( ReceiptRenderingRestController::class );
+ $this->server = $this->create_rest_server_with_routes(
+ array( array( $controller, 'register_routes' ) ),
+ true
+ );
+
+ $this->user = self::$fixture_user;
+ }
+
+ /**
+ * Tear down test.
+ */
+ public function tearDown(): void {
+ $this->clear_rest_server();
+ unset( $this->server );
+
+ if ( $this->receipt_filename ) {
+ wc_get_container()->get( TransientFilesEngine::class )->delete_transient_file( $this->receipt_filename );
+ $this->receipt_filename = null;
+ }
+
+ parent::tearDown();
+ }
+
+ /**
+ * Record a user as the author of an order's underlying post.
+ *
+ * @param int $order_id The order ID.
+ * @param int $user_id The user to record as the post author.
+ */
+ private function set_order_post_author( int $order_id, int $user_id ): void {
+ global $wpdb;
+ $wpdb->update( $wpdb->posts, array( 'post_author' => $user_id ), array( 'ID' => $order_id ) );
+ clean_post_cache( $order_id );
+ }
+
+ /**
+ * A customer who authored the order's post must be refused on both receipt routes.
+ */
+ public function test_order_author_customer_is_denied_on_receipt_routes() {
+ $order = WC_Helper_Order::create_order();
+ $this->set_order_post_author( $order->get_id(), $this->user['customer'] );
+
+ wp_set_current_user( $this->user['customer'] );
+ $this->assertTrue(
+ current_user_can( 'read_shop_order', $order->get_id() ), // phpcs:ignore WordPress.WP.Capabilities.Unknown -- shop_order meta cap, not in the sniff's core list.
+ 'Fixture must model an order the author can read.'
+ );
+
+ foreach ( array( 'GET', 'POST' ) as $method ) {
+ $response = $this->server->dispatch( new WP_REST_Request( $method, '/wc/v3/orders/' . $order->get_id() . '/receipt' ) );
+ $this->assertEquals( 403, $response->get_status(), "$method receipt should be forbidden for the order author." );
+ }
+ }
+
+ /**
+ * A shop manager passes the receipt permission check on both routes.
+ *
+ * GET on an order with no receipt yet returns 404 (past the gate, not 403); POST returns the created receipt.
+ */
+ public function test_shop_manager_passes_receipt_permission() {
+ $order = WC_Helper_Order::create_order();
+
+ wp_set_current_user( $this->user['shop_manager'] );
+
+ $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() . '/receipt' ) );
+ $this->assertEquals( 404, $response->get_status() );
+
+ $response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/orders/' . $order->get_id() . '/receipt' ) );
+ $this->assertEquals( 200, $response->get_status() );
+ $this->assertArrayHasKey( 'receipt_url', $response->get_data() );
+
+ $this->receipt_filename = wc_get_order( $order->get_id() )->get_meta( ReceiptRenderingEngine::RECEIPT_FILE_NAME_META_KEY );
+ $this->assertNotEmpty( $this->receipt_filename );
+ }
+}