From 31d10f27ab95d7184e9ee1b894fed6f50ed69732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Mon, 17 Jun 2019 17:58:47 +0200 Subject: [PATCH] Remove all malformed cookies in middleware --- .../Classes/Utility/BookingUrlUtility.php | 3 ++- .../Classes/Middleware/TrackingMiddleware.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/public/typo3conf/ext/ep_products/Classes/Utility/BookingUrlUtility.php b/public/typo3conf/ext/ep_products/Classes/Utility/BookingUrlUtility.php index d957c842..dda9feee 100644 --- a/public/typo3conf/ext/ep_products/Classes/Utility/BookingUrlUtility.php +++ b/public/typo3conf/ext/ep_products/Classes/Utility/BookingUrlUtility.php @@ -66,8 +66,9 @@ class BookingUrlUtility $bpnCookie = null; if (isset($_COOKIE[static::COOKIE_NAME])) { - if (false === preg_match('/([a-z]{4}|PA)\d{4}/', $_COOKIE[static::COOKIE_NAME])) { + if (!preg_match('/^([a-z]{4}|PA)\d{4}$/', $_COOKIE[static::COOKIE_NAME])) { setcookie(static::COOKIE_NAME, '', time() - 3600); + unset($_COOKIE[static::COOKIE_NAME]); } else { $trackingCookie = $_COOKIE[static::COOKIE_NAME]; } diff --git a/public/typo3conf/ext/ep_track/Classes/Middleware/TrackingMiddleware.php b/public/typo3conf/ext/ep_track/Classes/Middleware/TrackingMiddleware.php index c3c0b752..d7f75510 100644 --- a/public/typo3conf/ext/ep_track/Classes/Middleware/TrackingMiddleware.php +++ b/public/typo3conf/ext/ep_track/Classes/Middleware/TrackingMiddleware.php @@ -54,6 +54,8 @@ class TrackingMiddleware implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { + $this->cleanupCookies(); + if ($redirect = $this->matchRedirect($request)) { [$uri, $responseCode] = $redirect; @@ -165,6 +167,18 @@ class TrackingMiddleware implements MiddlewareInterface setcookie(static::COOKIE_NAME, $code, $expiratonDate->getTimestamp(), '/'); } + protected function cleanupCookies(): void + { + if (!isset($_COOKIE[static::COOKIE_NAME])) { + return; + } + + if (!preg_match('/^' . static::CODE_PATTERN . '$/', $_COOKIE[static::COOKIE_NAME])) { + setcookie(static::COOKIE_NAME, '', time() - 3600); + unset($_COOKIE[static::COOKIE_NAME]); + } + } + /** * Borrowed from \TYPO3\CMS\Redirects\Service\RedirectService *