custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT32886/CallWebhookActionFix.php line 57

Open in your IDE?
  1. <?php
  2. namespace Swag\Security\Fixes\NEXT32886;
  3. use Doctrine\DBAL\Connection;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  6. use Shopware\Core\Content\Media\File\FileUrlValidatorInterface;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Swag\FlowBuilderProfessional\Core\Content\Flow\Dispatching\Action\CallWebhookAction;
  11. class CallWebhookActionFix extends CallWebhookAction
  12. {
  13.     /**
  14.      * @var FileUrlValidatorInterface
  15.      */
  16.     private $fileUrlValidator;
  17.     /**
  18.      * @var LoggerInterface
  19.      */
  20.     private $logger;
  21.     /**
  22.      * @var Connection
  23.      */
  24.     private $connection;
  25.     public function setFileUrlValidator(FileUrlValidatorInterface $fileUrlValidator): void
  26.     {
  27.         $this->fileUrlValidator $fileUrlValidator;
  28.     }
  29.     public function setLogger(LoggerInterface $logger): void
  30.     {
  31.         $this->logger $logger;
  32.     }
  33.     public function setConnection(Connection $connection): void
  34.     {
  35.         $this->connection $connection;
  36.     }
  37.     public function handleFlow(StorableFlow $flow): void
  38.     {
  39.         $url $flow->getConfig()['baseUrl'];
  40.         if (!$this->validate($url$flow->getName(), $flow->getFlowState()->getSequenceId())) {
  41.             return;
  42.         }
  43.         parent::handleFlow($flow);
  44.     }
  45.     public function handle(FlowEvent $event): void
  46.     {
  47.         $url $event->getConfig()['baseUrl'];
  48.         if (!$this->validate($url$event->getName(), $event->getFlowState()->getSequenceId())) {
  49.             return;
  50.         }
  51.         parent::handle($event);
  52.     }
  53.     private function validate(string $urlstring $eventNamestring $sequenceId): bool
  54.     {
  55.         if ($this->fileUrlValidator->isValid($url)) {
  56.             return true;
  57.         }
  58.         $this->logger->error('Webhook url is not valid: Webhook urls must be publicly accessible.');
  59.         $webhookEventId Uuid::randomBytes();
  60.         $this->connection->executeStatement(
  61.             'INSERT INTO
  62.                 `webhook_event_log` (id, delivery_status, timestamp, webhook_name, event_name, url, request_content, response_content, created_at)
  63.                 VALUES (:webhookEventId, :deliveryStatus, :timestamp, :webhookName, :eventName, :url, :requestContent, :responseContent, :createdAt)',
  64.             [
  65.                 'webhookEventId' => $webhookEventId,
  66.                 'deliveryStatus' => 'failed',
  67.                 'timestamp' => time(),
  68.                 'webhookName' => $url,
  69.                 'eventName' => $eventName,
  70.                 'url' => $url,
  71.                 'requestContent' => '{}',
  72.                 'responseContent' => \json_encode([
  73.                     'message' => 'Webhook url is not valid: Webhook urls must be publicly accessible.',
  74.                 ]),
  75.                 'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  76.             ]
  77.         );
  78.         $this->connection->executeStatement(
  79.             'INSERT INTO `swag_sequence_webhook_event_log` (sequence_id, webhook_event_log_id)
  80.                 VALUES (:sequenceId, :webhookEventId)',
  81.             [
  82.                 'sequenceId' => Uuid::fromHexToBytes($sequenceId),
  83.                 'webhookEventId' => $webhookEventId,
  84.             ]
  85.         );
  86.         return false;
  87.     }
  88. }