Skip to content

Commit

Permalink
fix: use sanitized shop url in before registration starts event
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzKrafeld committed Jan 16, 2025
1 parent a6912f8 commit b33526f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 127 deletions.
52 changes: 25 additions & 27 deletions src/Registration/RegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopware\App\SDK\Registration;

use Http\Discovery\Psr17Factory;
use Nyholm\Psr7\Uri;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -62,32 +63,33 @@ public function register(RequestInterface $request): ResponseInterface
$this->shopSecretGeneratorInterface->generate()
);

$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop));
$sanitizedShop = $this->getSanitizedShop($shop);
$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $sanitizedShop));

$this->shopRepository->createShop($shop);
$this->shopRepository->createShop($sanitizedShop);
} else {
$shop->setShopUrl($queries['shop-url']);

$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop));
$sanitizedShop = $this->getSanitizedShop($shop);
$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $sanitizedShop));

$this->shopRepository->updateShop($shop);
$this->shopRepository->updateShop($sanitizedShop);
}

$this->logger->info('Shop registration request received', [
'shop-id' => $shop->getShopId(),
'shop-url' => $shop->getShopUrl(),
'shop-id' => $sanitizedShop->getShopId(),
'shop-url' => $sanitizedShop->getShopUrl(),
]);

$psrFactory = new Psr17Factory();

$data = [
// old shop is needed because the shop url is not sanitized
'proof' => $this->responseSigner->getRegistrationSignature($this->appConfiguration, $shop),
'confirmation_url' => $this->appConfiguration->getRegistrationConfirmUrl(),
'secret' => $shop->getShopSecret(),
];

$this->fixShopUrlInDatabase($shop);

$response = $psrFactory->createResponse(200);

return $response
Expand Down Expand Up @@ -145,34 +147,30 @@ public function registerConfirm(RequestInterface $request): ResponseInterface

private function sanitizeShopUrl(string $shopUrl): string
{
$parsedUrl = parse_url($shopUrl);
$uri = new Uri($shopUrl);

$protocol = $parsedUrl['scheme'] ?? '';
$host = $parsedUrl['host'] ?? '';
$path = $parsedUrl['path'] ?? '';
$port = $parsedUrl['port'] ?? '';
$protocol = $uri->getScheme();
$host = $uri->getHost();
$path = $uri->getPath();
$port = $uri->getPort();

/** @var string $normalizedPath */
$normalizedPath = preg_replace('#/{2,}#', '/', $path);
$normalizedPath = rtrim($normalizedPath, '/');

return sprintf(
'%s://%s%s%s',
$protocol,
$host,
$port ? ':' . $port : null,
$normalizedPath
);
$url = $protocol . '://' . $host;
if ($port) {
$url .= ':' . $port;
}
$url .= $normalizedPath;

return $url;
}

private function fixShopUrlInDatabase(ShopInterface $shop): void
private function getSanitizedShop(ShopInterface $shop): ShopInterface
{
$sanitizedShopUrl = $this->sanitizeShopUrl($shop->getShopUrl());
$sanitizedShop = clone $shop;

Check warning on line 172 in src/Registration/RegistrationService.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "CloneRemoval": @@ @@ } private function getSanitizedShop(ShopInterface $shop): ShopInterface { - $sanitizedShop = clone $shop; + $sanitizedShop = $shop; return $sanitizedShop->setShopUrl($this->sanitizeShopUrl($shop->getShopUrl())); } }


if ($shop->getShopUrl() !== $sanitizedShopUrl) {
$shop->setShopUrl($sanitizedShopUrl);
$this->shopRepository->updateShop($shop);
}
return $sanitizedShop->setShopUrl($this->sanitizeShopUrl($shop->getShopUrl()));
}
}
Loading

0 comments on commit b33526f

Please sign in to comment.