Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

Fix XSS, SSRF, and missing session verification (#1081)

Dave Earleyf918718c4665
diff --git a/backend/app/Services/Application/Handlers/EventSettings/UpdateEventSettingsHandler.php b/backend/app/Services/Application/Handlers/EventSettings/UpdateEventSettingsHandler.php
index b668a7d22..771953480 100644
--- a/backend/app/Services/Application/Handlers/EventSettings/UpdateEventSettingsHandler.php
+++ b/backend/app/Services/Application/Handlers/EventSettings/UpdateEventSettingsHandler.php
@@ -27,12 +27,9 @@ class UpdateEventSettingsHandler
return $this->databaseManager->transaction(function () use ($settings) {
$this->eventSettingsRepository->updateWhere(
attributes: [
- 'post_checkout_message' => $settings->post_checkout_message
- ?? $this->purifier->purify($settings->post_checkout_message),
- 'pre_checkout_message' => $settings->pre_checkout_message
- ?? $this->purifier->purify($settings->pre_checkout_message),
- 'email_footer_message' => $settings->email_footer_message
- ?? $this->purifier->purify($settings->email_footer_message),
+ 'post_checkout_message' => $this->purifier->purify($settings->post_checkout_message),
+ 'pre_checkout_message' => $this->purifier->purify($settings->pre_checkout_message),
+ 'email_footer_message' => $this->purifier->purify($settings->email_footer_message),
'support_email' => $settings->support_email,
'require_attendee_details' => $settings->require_attendee_details,
'attendee_details_collection_method' => $settings->attendee_details_collection_method->name,
@@ -51,8 +48,7 @@ class UpdateEventSettingsHandler
'maps_url' => trim($settings->maps_url),
'location_details' => $settings->location_details?->toArray(),
'is_online_event' => $settings->is_online_event,
- 'online_event_connection_details' => $settings->online_event_connection_details
- ?? $this->purifier->purify($settings->online_event_connection_details),
+ 'online_event_connection_details' => $this->purifier->purify($settings->online_event_connection_details),
'seo_title' => $settings->seo_title,
'seo_description' => $settings->seo_description,
@@ -64,8 +60,7 @@ class UpdateEventSettingsHandler
// Payment settings
'payment_providers' => $settings->payment_providers,
- 'offline_payment_instructions' => $settings->offline_payment_instructions
- ?? $this->purifier->purify($settings->offline_payment_instructions),
+ 'offline_payment_instructions' => $this->purifier->purify($settings->offline_payment_instructions),
'allow_orders_awaiting_offline_payment_to_check_in' => $settings->allow_orders_awaiting_offline_payment_to_check_in,
// Invoice settings
diff --git a/backend/app/Services/Application/Handlers/Order/CompleteOrderHandler.php b/backend/app/Services/Application/Handlers/Order/CompleteOrderHandler.php
index 4daf4c2e2..49abae7e4 100644
--- a/backend/app/Services/Application/Handlers/Order/CompleteOrderHandler.php
+++ b/backend/app/Services/Application/Handlers/Order/CompleteOrderHandler.php
@@ -22,6 +22,7 @@ use HiEvents\DomainObjects\Status\OrderPaymentStatus;
use HiEvents\DomainObjects\Status\OrderStatus;
use HiEvents\Events\OrderStatusChangedEvent;
use HiEvents\Exceptions\ResourceConflictException;
+use HiEvents\Exceptions\UnauthorizedException;
use HiEvents\Helper\IdHelper;
use HiEvents\Repository\Eloquent\Value\Relationship;
use HiEvents\Repository\Interfaces\AffiliateRepositoryInterface;
@@ -38,6 +39,7 @@ use HiEvents\Services\Application\Handlers\Order\DTO\OrderQuestionsDTO;
use HiEvents\Services\Domain\Payment\Stripe\EventHandlers\PaymentIntentSucceededHandler;
use HiEvents\Services\Domain\Product\ProductQuantityUpdateService;
use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService;
+use HiEvents\Services\Infrastructure\Session\CheckoutSessionManagementService;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Events\OrderEvent;
use Illuminate\Support\Collection;
@@ -59,6 +61,7 @@ class CompleteOrderHandler
private readonly ProductPriceRepositoryInterface $productPriceRepository,
private readonly DomainEventDispatcherService $domainEventDispatcherService,
private readonly EventSettingsRepositoryInterface $eventSettingsRepository,
+ private readonly CheckoutSessionManagementService $sessionManagementService,
)
{
}
@@ -289,6 +292,13 @@ class CompleteOrderHandler
throw new ResourceNotFoundException(__('Order not found'));
}
+ if ($order->getSessionId() === null
+ || !$this->sessionManagementService->verifySession($order->getSessionId())) {
+ throw new UnauthorizedException(
+ __('Sorry, we could not verify your session. Please restart your order.')
+ );
+ }
+
$this->validateOrder($order);
return $order;
diff --git a/backend/app/Services/Application/Handlers/Order/TransitionOrderToOfflinePaymentHandler.php b/backend/app/Services/Application/Handlers/Order/TransitionOrderToOfflinePaymentHandler.php
index b7d577734..9a03323d3 100644
--- a/backend/app/Services/Application/Handlers/Order/TransitionOrderToOfflinePaymentHandler.php
+++ b/backend/app/Services/Application/Handlers/Order/TransitionOrderToOfflinePaymentHandler.php
@@ -19,6 +19,7 @@ use HiEvents\Services\Domain\Product\ProductQuantityUpdateService;
use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Events\OrderEvent;
+use HiEvents\Services\Infrastructure\Session\CheckoutSessionManagementService;
use Illuminate\Database\DatabaseManager;
class TransitionOrderToOfflinePaymentHandler
@@ -29,7 +30,7 @@ class TransitionOrderToOfflinePaymentHandler
private readonly DatabaseManager $databaseManager,
private readonly EventSettingsRepositoryInterface $eventSettingsRepository,
private readonly DomainEventDispatcherService $domainEventDispatcherService,
-
+ private readonly CheckoutSessionManagementService $sessionManagementService,
)
{
}
@@ -42,6 +43,17 @@ class TransitionOrderToOfflinePaymentHandler
->loadRelation(OrderItemDomainObject::class)
->findByShortId($dto->orderShortId);
+ if ($order === null) {
+ throw new ResourceConflictException(__('Order not found'));
+ }
+
+ if ($order->getSessionId() === null
+ || !$this->sessionManagementService->verifySession($order->getSessionId())) {
+ throw new UnauthorizedException(
+ __('Sorry, we could not verify your session. Please restart your order.')
+ );
+ }
+
/** @var EventSettingDomainObject $eventSettings */
$eventSettings = $this->eventSettingsRepository->findFirstWhere([
'event_id' => $order->getEventId(),
diff --git a/backend/app/Services/Application/Handlers/ProductCategory/CreateProductCategoryHandler.php b/backend/app/Services/Application/Handlers/ProductCategory/CreateProductCategoryHandler.php
index 30ffc4008..fd51e0e50 100644
--- a/backend/app/Services/Application/Handlers/ProductCategory/CreateProductCategoryHandler.php
+++ b/backend/app/Services/Application/Handlers/ProductCategory/CreateProductCategoryHandler.php
@@ -5,11 +5,13 @@ namespace HiEvents\Services\Application\Handlers\ProductCategory;
use HiEvents\DomainObjects\ProductCategoryDomainObject;
use HiEvents\Services\Application\Handlers\ProductCategory\DTO\UpsertProductCategoryDTO;
use HiEvents\Services\Domain\ProductCategory\CreateProductCategoryService;
+use HiEvents\Services\Infrastructure\HtmlPurifier\HtmlPurifierService;
class CreateProductCategoryHandler
{
public function __construct(
private readonly CreateProductCategoryService $productCategoryService,
+ private readonly HtmlPurifierService $purifier,
)
{
}
@@ -20,7 +22,7 @@ class CreateProductCategoryHandler
$productCategory->setName($dto->name);
$productCategory->setIsHidden($dto->is_hidden);
$productCategory->setEventId($dto->event_id);
- $productCategory->setDescription($dto->description);
+ $productCategory->setDescription($this->purifier->purify($dto->description));
$productCategory->setNoProductsMessage(
$dto->no_products_message ?? __('There are no products available in this category'
));
diff --git a/backend/app/Services/Application/Handlers/ProductCategory/EditProductCategoryHandler.php b/backend/app/Services/Application/Handlers/ProductCategory/EditProductCategoryHandler.php
index 574183a57..ae013ef50 100644
--- a/backend/app/Services/Application/Handlers/ProductCategory/EditProductCategoryHandler.php
+++ b/backend/app/Services/Application/Handlers/ProductCategory/EditProductCategoryHandler.php
@@ -5,11 +5,13 @@ namespace HiEvents\Services\Application\Handlers\ProductCategory;
use HiEvents\DomainObjects\ProductCategoryDomainObject;
use HiEvents\Repository\Interfaces\ProductCategoryRepositoryInterface;
use HiEvents\Services\Application\Handlers\ProductCategory\DTO\UpsertProductCategoryDTO;
+use HiEvents\Services\Infrastructure\HtmlPurifier\HtmlPurifierService;
class EditProductCategoryHandler
{
public function __construct(
private readonly ProductCategoryRepositoryInterface $productCategoryRepository,
+ private readonly HtmlPurifierService $purifier,
)
{
}
@@ -20,7 +22,7 @@ class EditProductCategoryHandler
attributes: [
'name' => $dto->name,
'is_hidden' => $dto->is_hidden,
- 'description' => $dto->description,
+ 'description' => $this->purifier->purify($dto->description),
'no_products_message' => $dto->no_products_message ?? __('There are no products available in this category'),
],
where: [
diff --git a/backend/app/Services/Infrastructure/HtmlPurifier/HtmlPurifierService.php b/backend/app/Services/Infrastructure/HtmlPurifier/HtmlPurifierService.php
index efefb4f44..fc13a850d 100644
--- a/backend/app/Services/Infrastructure/HtmlPurifier/HtmlPurifierService.php
+++ b/backend/app/Services/Infrastructure/HtmlPurifier/HtmlPurifierService.php
@@ -20,10 +20,10 @@ class HtmlPurifierService
$this->config->set('Cache.SerializerPath', $cachePath);
}
- public function purify(?string $html): string
+ public function purify(?string $html): ?string
{
if ($html === null) {
- return '';
+ return null;
}
return $this->htmlPurifier->purify($html, $this->config);
diff --git a/backend/app/Validators/Rules/NoInternalUrlRule.php b/backend/app/Validators/Rules/NoInternalUrlRule.php
index 24096a108..5e3360ef8 100644
--- a/backend/app/Validators/Rules/NoInternalUrlRule.php
+++ b/backend/app/Validators/Rules/NoInternalUrlRule.php
@@ -100,14 +100,14 @@ class NoInternalUrlRule implements ValidationRule
private function isPrivateIpAddress(string $host): bool
{
- $ip = gethostbyname($host);
+ $ip = $this->resolveAndNormalize($host);
- if ($ip === $host && !filter_var($host, FILTER_VALIDATE_IP)) {
- return false;
+ if ($ip === false) {
+ return true;
}
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
- return false;
+ return true;
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
@@ -120,4 +120,29 @@ class NoInternalUrlRule implements ValidationRule
return false;
}
+
+ private function resolveAndNormalize(string $host): string|false
+ {
+ if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
+ $binary = inet_pton($host);
+ if ($binary !== false && strlen($binary) === 16) {
+ $prefix = substr($binary, 0, 12);
+ if ($prefix === "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff") {
+ return inet_ntop(substr($binary, 12));
+ }
+ }
+ return $host;
+ }
+
+ if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
+ return $host;
+ }
+
+ $ip = gethostbyname($host);
+ if ($ip === $host) {
+ return false;
+ }
+
+ return $ip;
+ }
}
diff --git a/backend/tests/Unit/Services/Application/Handlers/Order/CompleteOrderHandlerTest.php b/backend/tests/Unit/Services/Application/Handlers/Order/CompleteOrderHandlerTest.php
index 5fefab769..4d820ff70 100644
--- a/backend/tests/Unit/Services/Application/Handlers/Order/CompleteOrderHandlerTest.php
+++ b/backend/tests/Unit/Services/Application/Handlers/Order/CompleteOrderHandlerTest.php
@@ -25,6 +25,7 @@ use HiEvents\Services\Domain\Product\ProductQuantityUpdateService;
use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Events\OrderEvent;
+use HiEvents\Services\Infrastructure\Session\CheckoutSessionManagementService;
use Illuminate\Database\Connection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Bus;
@@ -48,6 +49,7 @@ class CompleteOrderHandlerTest extends TestCase
private DomainEventDispatcherService $domainEventDispatcherService;
private AffiliateRepositoryInterface|MockInterface $affiliateRepository;
private EventSettingsRepositoryInterface $eventSettingsRepository;
+ private CheckoutSessionManagementService|MockInterface $sessionManagementService;
protected function setUp(): void
{
@@ -65,6 +67,8 @@ class CompleteOrderHandlerTest extends TestCase
$this->domainEventDispatcherService = Mockery::mock(DomainEventDispatcherService::class);
$this->affiliateRepository = Mockery::mock(AffiliateRepositoryInterface::class);
$this->eventSettingsRepository = Mockery::mock(EventSettingsRepositoryInterface::class);
+ $this->sessionManagementService = Mockery::mock(CheckoutSessionManagementService::class);
+ $this->sessionManagementService->shouldReceive('verifySession')->andReturn(true)->byDefault();
$this->completeOrderHandler = new CompleteOrderHandler(
$this->orderRepository,
@@ -75,6 +79,7 @@ class CompleteOrderHandlerTest extends TestCase
$this->productPriceRepository,
$this->domainEventDispatcherService,
$this->eventSettingsRepository,
+ $this->sessionManagementService,
);
}
@@ -296,6 +301,7 @@ class CompleteOrderHandlerTest extends TestCase
{
return (new OrderDomainObject())
->setEmail(null)
+ ->setSessionId('test-session-id')
->setReservedUntil(Carbon::now()->addHour()->toDateTimeString())
->setStatus($status->name)
->setId(1)
diff --git a/backend/tests/Unit/Validators/Rules/NoInternalUrlRuleTest.php b/backend/tests/Unit/Validators/Rules/NoInternalUrlRuleTest.php
index 64484c0b2..ebe90737a 100644
--- a/backend/tests/Unit/Validators/Rules/NoInternalUrlRuleTest.php
+++ b/backend/tests/Unit/Validators/Rules/NoInternalUrlRuleTest.php
@@ -92,6 +92,15 @@ class NoInternalUrlRuleTest extends TestCase
$this->assertFalse($this->validate('http://[::1]/webhook'));
}
+ public function testRejectsIpv4MappedIpv6Addresses(): void
+ {
+ $this->assertFalse($this->validate('http://[::ffff:127.0.0.1]/webhook'));
+ $this->assertFalse($this->validate('http://[::ffff:169.254.169.254]/latest/meta-data/'));
+ $this->assertFalse($this->validate('http://[::ffff:10.0.0.1]/internal'));
+ $this->assertFalse($this->validate('http://[::ffff:192.168.1.1]/admin'));
+ $this->assertFalse($this->validate('http://[::ffff:172.16.0.1]/api'));
+ }
+
public function testRejectsNonHttpSchemes(): void
{
$this->assertFalse($this->validate('file:///etc/passwd'));