+ "details": "### Impact\nA Time-of-Check To Time-of-Use (TOCTOU) race condition was discovered in the promotion usage limit enforcement. The same class of vulnerability affects three independent limits:\n\n1. **Promotion usage limit** - the global `used` counter on `Promotion` entities\n2. **Coupon usage limit** - the global `used` counter on `PromotionCoupon` entities\n3. **Coupon per-customer usage limit** - the per-customer redemption count on `PromotionCoupon` entities\n\nIn all three cases, the eligibility check reads the `used` counter (or order count) from an in-memory Doctrine entity during validation, while the actual usage increment in `OrderPromotionsUsageModifier` happens later during order completion — with no database-level locking or atomic operations between the two phases.\n\nBecause Doctrine flushes an absolute value (`SET used = 1`) rather than an atomic increment (`SET used = used + 1`), and because the affected entities lack optimistic locking, concurrent requests all read the same stale usage counts and pass the eligibility checks simultaneously.\n\nAn attacker can exploit this by preparing multiple carts with the same limited-use promotion or coupon and firing simultaneous `PATCH /api/v2/shop/orders/{token}/complete` requests. All requests pass the usage limit checks and complete successfully, allowing a single-use promotion or coupon to be redeemed an arbitrary number of times. The per-customer limit can be bypassed in the same way by a single customer completing multiple orders concurrently. No authentication is required to exploit this vulnerability.\n\nThis may lead to direct financial loss through unlimited redemption of limited-use promotions and discount coupons.\n\n### Patches\nThe issue is fixed in versions: 1.9.12, 1.10.16, 1.11.17, 1.12.23, 1.13.15, 1.14.18, 2.0.16, 2.1.12, 2.2.3 and above.\n\n### Workarounds\n\nDecoration of the `OrderPromotionsUsageModifier` service to use atomic operations based on actual database-synchronized values.\n\nThe decorated service id in Sylius >=2.0 is `sylius.modifier.promotion.order_usage`, while <2.0 it's `sylius.promotion_usage_modifier`; The following instruction uses the latter, but it needs to be changed depending on the Sylius version.\n\n#### Step 1. Create the decorator service\n\n`src/Modifier/AtomicOrderPromotionsUsageModifier.php`:\n\n```php\n<?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Modifier;\n\nuse Doctrine\\DBAL\\Connection;\nuse Doctrine\\ORM\\OptimisticLockException;\nuse Sylius\\Component\\Core\\Model\\OrderInterface;\nuse Sylius\\Component\\Core\\Model\\PromotionCouponInterface;\nuse Sylius\\Component\\Core\\Promotion\\Modifier\\OrderPromotionsUsageModifierInterface;\nuse Sylius\\Component\\Promotion\\Model\\PromotionInterface;\n// use Symfony\\Component\\DependencyInjection\\Attribute\\AsDecorator;\n\n// #[AsDecorator(decorates: 'sylius.promotion_usage_modifier')]\nfinal class AtomicOrderPromotionsUsageModifier implements OrderPromotionsUsageModifierInterface\n{\n /** @var Connection */\n private $connection;\n\n public function __construct(Connection $connection)\n {\n $this->connection = $connection;\n }\n\n public function increment(OrderInterface $order): void\n {\n foreach ($order->getPromotions() as $promotion) {\n $this->incrementPromotionUsage($promotion);\n }\n\n /** @var PromotionCouponInterface|null $coupon */\n $coupon = $order->getPromotionCoupon();\n if (null === $coupon) {\n return;\n }\n\n $this->incrementCouponUsage($coupon, $order);\n }\n\n public function decrement(OrderInterface $order): void\n {\n foreach ($order->getPromotions() as $promotion) {\n $this->decrementPromotionUsage($promotion);\n }\n\n /** @var PromotionCouponInterface|null $coupon */\n $coupon = $order->getPromotionCoupon();\n if (null === $coupon) {\n return;\n }\n\n if (OrderInterface::STATE_CANCELLED === $order->getState() && !$coupon->isReusableFromCancelledOrders()) {\n return;\n }\n\n $this->decrementCouponUsage($coupon);\n }\n\n private function incrementPromotionUsage(PromotionInterface $promotion): void\n {\n $affected = $this->doExecuteStatement(\n 'UPDATE sylius_promotion\n SET used = used + 1\n WHERE id = :id AND (usage_limit IS NULL OR used < usage_limit)',\n ['id' => $promotion->getId()]\n );\n\n if (0 === $affected) {\n throw new OptimisticLockException(sprintf('Promotion \"%s\" is no longer applicable.', $promotion->getCode()), $promotion);\n }\n\n $newUsed = (int) $this->doFetchOne(\n 'SELECT used FROM sylius_promotion WHERE id = :id',\n ['id' => $promotion->getId()]\n );\n\n $promotion->setUsed($newUsed);\n }\n\n private function decrementPromotionUsage(PromotionInterface $promotion): void\n {\n $this->doExecuteStatement(\n 'UPDATE sylius_promotion SET used = GREATEST(used - 1, 0) WHERE id = :id',\n ['id' => $promotion->getId()]\n );\n\n $newUsed = (int) $this->doFetchOne(\n 'SELECT used FROM sylius_promotion WHERE id = :id',\n ['id' => $promotion->getId()]\n );\n\n $promotion->setUsed($newUsed);\n }\n\n private function incrementCouponUsage(PromotionCouponInterface $coupon, OrderInterface $order): void\n {\n $row = $this->doFetchAssociative(\n 'SELECT used, usage_limit, per_customer_usage_limit FROM sylius_promotion_coupon WHERE id = :id FOR UPDATE',\n ['id' => $coupon->getId()]\n );\n\n if (false === $row) {\n throw new OptimisticLockException(sprintf('Promotion coupon \"%s\" is no longer applicable.', $coupon->getCode()), $coupon);\n }\n\n if (null !== $row['usage_limit'] && (int) $row['used'] >= (int) $row['usage_limit']) {\n throw new OptimisticLockException(sprintf('Promotion coupon \"%s\" is no longer applicable.', $coupon->getCode()), $coupon);\n }\n\n if (null !== $row['per_customer_usage_limit']) {\n $this->assertPerCustomerCouponUsageLimitNotReached(\n $coupon,\n $order,\n (int) $row['per_customer_usage_limit']\n );\n }\n\n $this->doExecuteStatement(\n 'UPDATE sylius_promotion_coupon SET used = used + 1 WHERE id = :id',\n ['id' => $coupon->getId()]\n );\n\n $coupon->setUsed((int) $row['used'] + 1);\n }\n\n private function assertPerCustomerCouponUsageLimitNotReached(\n PromotionCouponInterface $coupon,\n OrderInterface $order,\n int $perCustomerUsageLimit\n ): void {\n $customer = $order->getCustomer();\n if (null === $customer || null === $customer->getId()) {\n return;\n }\n\n $sql = 'SELECT o.id FROM sylius_order o\n WHERE o.customer_id = :customerId\n AND o.promotion_coupon_id = :couponId\n AND o.state != :stateCart';\n $params = [\n 'customerId' => $customer->getId(),\n 'couponId' => $coupon->getId(),\n 'stateCart' => OrderInterface::STATE_CART,\n ];\n\n if ($coupon->isReusableFromCancelledOrders()) {\n $sql .= ' AND o.state != :stateCancelled';\n $params['stateCancelled'] = OrderInterface::STATE_CANCELLED;\n }\n\n $sql .= ' FOR UPDATE';\n\n $count = count($this->doFetchAllAssociative($sql, $params));\n\n if ($count >= $perCustomerUsageLimit) {\n throw new OptimisticLockException(sprintf('Promotion coupon \"%s\" is no longer applicable.', $coupon->getCode()), $coupon);\n }\n }\n\n private function decrementCouponUsage(PromotionCouponInterface $coupon): void\n {\n $this->doExecuteStatement(\n 'UPDATE sylius_promotion_coupon SET used = GREATEST(used - 1, 0) WHERE id = :id',\n ['id' => $coupon->getId()]\n );\n\n $newUsed = (int) $this->doFetchOne(\n 'SELECT used FROM sylius_promotion_coupon WHERE id = :id',\n ['id' => $coupon->getId()]\n );\n\n $coupon->setUsed($newUsed);\n }\n\n /** @return int Number of affected rows */\n private function doExecuteStatement(string $sql, array $params): int\n {\n if (method_exists($this->connection, 'executeStatement')) {\n return $this->connection->executeStatement($sql, $params);\n }\n\n return $this->connection->executeUpdate($sql, $params);\n }\n\n /** @return mixed|false */\n private function doFetchOne(string $sql, array $params)\n {\n if (method_exists($this->connection, 'fetchOne')) {\n return $this->connection->fetchOne($sql, $params);\n }\n\n return $this->connection->fetchColumn($sql, $params);\n }\n\n /** @return array|false */\n private function doFetchAssociative(string $sql, array $params)\n {\n if (method_exists($this->connection, 'fetchAssociative')) {\n return $this->connection->fetchAssociative($sql, $params);\n }\n\n return $this->connection->fetchAssoc($sql, $params);\n }\n\n /** @return array[] */\n private function doFetchAllAssociative(string $sql, array $params): array\n {\n if (method_exists($this->connection, 'fetchAllAssociative')) {\n return $this->connection->fetchAllAssociative($sql, $params);\n }\n\n return $this->connection->fetchAll($sql, $params);\n }\n}\n```\n\n#### Step 2. Register the service\n\n**Option A:** If your app uses autowiring and supports the `#[AsDecorator]` attribute, uncomment it in the class and no further configuration is necessary.\n\n**Option B:** Manually register the service in `config/services.yaml`:\n\n```yaml\nservices:\n App\\Modifier\\AtomicOrderPromotionsUsageModifier:\n decorates: 'sylius.promotion_usage_modifier'\n arguments: ['@doctrine.dbal.default_connection']\n```\n\n#### Step 3. Update exception mapping (optional)\n\nCheck if your `api_platform` configuration maps `OptimisticLockException` to a code and update it if not:\n```yaml\napi_platform:\n ...\n exception_to_status:\n ...\n Doctrine\\ORM\\OptimisticLockException: 409\n```\n\n#### Step 4. Clear cache\n\n```bash\nbin/console cache:clear\n```\n\n### Reporters\n\nWe would like to extend our gratitude to the following individuals for their detailed reporting and responsible disclosure of this vulnerability:\n- @whiteov3rflow\n- Bartłomiej Nowiński\n\n### For more information\n\nIf you have any questions or comments about this advisory:\n\n- Open an issue in [Sylius issues](https://github.com/Sylius/Sylius/issues)\n- Email us at [security@sylius.com](mailto:security@sylius.com)",
0 commit comments