Details
Step 3. Guard the POST creation endpoint with a command-bus middleware
The POST /api/v2/shop/orders/{tokenValue}/payment-requests operation is a messenger: input operation: it dispatches a Sylius\Bundle\ApiBundle\Command\Payment\AddPaymentRequest command whose orderTokenValue comes straight from the URL, so no query extension or state provider runs. Add a middleware on the Sylius command bus that loads the order, applies the same ownership rule, and aborts with 404 before the handler runs.
Create file src/Messenger/Middleware/PaymentRequestOwnershipMiddleware.php:
<?php
declare(strict_types=1);
namespace App\Messenger\Middleware;
use Sylius\Bundle\ApiBundle\Command\Payment\AddPaymentRequest;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
final readonly class PaymentRequestOwnershipMiddleware implements MiddlewareInterface
{
/** @param OrderRepositoryInterface<OrderInterface> $orderRepository */
public function __construct(
private OrderRepositoryInterface $orderRepository,
private UserContextInterface $userContext,
) {
}
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$command = $envelope->getMessage();
if ($command instanceof AddPaymentRequest && !$this->isOrderAccessible($command->orderTokenValue)) {
throw new NotFoundHttpException('Not Found');
}
return $stack->next()->handle($envelope, $stack);
}
private function isOrderAccessible(string $orderTokenValue): bool
{
/** @var OrderInterface|null $order */
$order = $this->orderRepository->findOneByTokenValue($orderTokenValue);
if (null === $order) {
// Unknown token — let the handler return its own 404 (PaymentNotFoundException).
return true;
}
$user = $this->userContext->getUser();
if ($user instanceof ShopUserInterface) {
$customer = $user->getCustomer();
return $customer instanceof CustomerInterface && $order->getCustomer() === $customer;
}
$customer = $order->getCustomer();
return null === $customer
|| null === $customer->getUser()
|| $order->isCreatedByGuest();
}
}
Step 4. Wire the services
Append to config/services.yaml:
services:
App\ApiPlatform\QueryExtension\PaymentRequestOwnershipExtension:
arguments:
- '@sylius.section_resolver.uri_based'
- '@sylius_api.context.user.token_based'
tags:
- { name: api_platform.doctrine.orm.query_extension.item }
App\ApiPlatform\StateProvider\PaymentRequestOwnershipProvider:
decorates: sylius_api.state_provider.shop.payment.payment_request.item
arguments:
$inner: '@.inner'
$userContext: '@sylius_api.context.user.token_based'
App\Messenger\Middleware\PaymentRequestOwnershipMiddleware:
arguments:
- '@sylius.repository.order'
- '@sylius_api.context.user.token_based'
With the default Sylius-Standard services.yaml (autowire: true, autoconfigure: true) the two classes are already autoloaded, the block above only adds the tag and the decoration, which cannot be derived from the constructor signatures.
Step 5. Register the middleware on the Sylius command bus
Add to config/packages/messenger.yaml:
framework:
messenger:
buses:
sylius.command_bus:
middleware:
- 'App\Messenger\Middleware\PaymentRequestOwnershipMiddleware'
- 'validation'
- 'doctrine_transaction'
Step 6. Clear the cache
bin/console cache:clear
Reporters
We would like to extend our gratitude to the following individuals for their detailed reporting and responsible disclosure of this vulnerability:
- Fase Rais Baradika (@baradika)
- Anshu Chimala (@achimala)
For more information
If you have any questions or comments about this advisory:
- Open an issue in Sylius issues
- Email us at [[email protected]](mailto:[email protected])