Skip to content

Commit 4228e76

Browse files
Merge pull request #36 from shopware/use-sanitized-shop-url-in-before-registration-starts-event
fix: use sanitized shop url in before registration starts event
2 parents a6912f8 + b33526f commit 4228e76

File tree

2 files changed

+79
-127
lines changed

2 files changed

+79
-127
lines changed

src/Registration/RegistrationService.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Shopware\App\SDK\Registration;
66

77
use Http\Discovery\Psr17Factory;
8+
use Nyholm\Psr7\Uri;
89
use Psr\EventDispatcher\EventDispatcherInterface;
910
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\ResponseInterface;
@@ -62,32 +63,33 @@ public function register(RequestInterface $request): ResponseInterface
6263
$this->shopSecretGeneratorInterface->generate()
6364
);
6465

65-
$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop));
66+
$sanitizedShop = $this->getSanitizedShop($shop);
67+
$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $sanitizedShop));
6668

67-
$this->shopRepository->createShop($shop);
69+
$this->shopRepository->createShop($sanitizedShop);
6870
} else {
6971
$shop->setShopUrl($queries['shop-url']);
7072

71-
$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop));
73+
$sanitizedShop = $this->getSanitizedShop($shop);
74+
$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $sanitizedShop));
7275

73-
$this->shopRepository->updateShop($shop);
76+
$this->shopRepository->updateShop($sanitizedShop);
7477
}
7578

7679
$this->logger->info('Shop registration request received', [
77-
'shop-id' => $shop->getShopId(),
78-
'shop-url' => $shop->getShopUrl(),
80+
'shop-id' => $sanitizedShop->getShopId(),
81+
'shop-url' => $sanitizedShop->getShopUrl(),
7982
]);
8083

8184
$psrFactory = new Psr17Factory();
8285

8386
$data = [
87+
// old shop is needed because the shop url is not sanitized
8488
'proof' => $this->responseSigner->getRegistrationSignature($this->appConfiguration, $shop),
8589
'confirmation_url' => $this->appConfiguration->getRegistrationConfirmUrl(),
8690
'secret' => $shop->getShopSecret(),
8791
];
8892

89-
$this->fixShopUrlInDatabase($shop);
90-
9193
$response = $psrFactory->createResponse(200);
9294

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

146148
private function sanitizeShopUrl(string $shopUrl): string
147149
{
148-
$parsedUrl = parse_url($shopUrl);
150+
$uri = new Uri($shopUrl);
149151

150-
$protocol = $parsedUrl['scheme'] ?? '';
151-
$host = $parsedUrl['host'] ?? '';
152-
$path = $parsedUrl['path'] ?? '';
153-
$port = $parsedUrl['port'] ?? '';
152+
$protocol = $uri->getScheme();
153+
$host = $uri->getHost();
154+
$path = $uri->getPath();
155+
$port = $uri->getPort();
154156

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

159-
return sprintf(
160-
'%s://%s%s%s',
161-
$protocol,
162-
$host,
163-
$port ? ':' . $port : null,
164-
$normalizedPath
165-
);
161+
$url = $protocol . '://' . $host;
162+
if ($port) {
163+
$url .= ':' . $port;
164+
}
165+
$url .= $normalizedPath;
166+
167+
return $url;
166168
}
167169

168-
private function fixShopUrlInDatabase(ShopInterface $shop): void
170+
private function getSanitizedShop(ShopInterface $shop): ShopInterface
169171
{
170-
$sanitizedShopUrl = $this->sanitizeShopUrl($shop->getShopUrl());
172+
$sanitizedShop = clone $shop;
171173

172-
173-
if ($shop->getShopUrl() !== $sanitizedShopUrl) {
174-
$shop->setShopUrl($sanitizedShopUrl);
175-
$this->shopRepository->updateShop($shop);
176-
}
174+
return $sanitizedShop->setShopUrl($this->sanitizeShopUrl($shop->getShopUrl()));
177175
}
178176
}

0 commit comments

Comments
 (0)