‪TYPO3CMS  9.5
PageArgumentValidator.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
23 use Psr\Log\LoggerAwareInterface;
24 use Psr\Log\LoggerAwareTrait;
34 
38 class ‪PageArgumentValidator implements MiddlewareInterface, LoggerAwareInterface
39 {
40  use LoggerAwareTrait;
41 
47  protected ‪$cacheHashCalculator;
48 
52  protected ‪$controller;
53 
58  {
59  $this->controller = ‪$controller ?? ‪$GLOBALS['TSFE'];
60  $this->cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class);
61  }
62 
70  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
71  {
72  $pageNotFoundOnValidationError = (bool)(‪$GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError'] ?? true);
73  $pageArguments = $request->getAttribute('routing', null);
74  if ($this->controller->no_cache && !$pageNotFoundOnValidationError) {
75  // No need to test anything if caching was already disabled.
76  } else {
77  // Evaluate the cache hash parameter or dynamic arguments when coming from a Site-based routing
78  if ($pageArguments instanceof ‪PageArguments) {
79  $queryParams = $pageArguments->getDynamicArguments();
80  } else {
81  $queryParams = $request->getQueryParams();
82  }
83  if (!empty($queryParams) && !$this->‪evaluateCacheHashParameter($queryParams, $pageNotFoundOnValidationError)) {
84  // cHash was given, but nothing to be calculated, so let's do a redirect to the current page
85  // but without the cHash
86  if ($this->controller->cHash && empty($this->controller->cHash_array)) {
87  $uri = $request->getUri();
88  unset($queryParams['cHash']);
89  $uri = $uri->withQuery(‪HttpUtility::buildQueryString($queryParams));
90  return new ‪RedirectResponse($uri, 308);
91  }
92  return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
93  $request,
94  'Request parameters could not be validated (&cHash comparison failed)',
96  );
97  }
98  }
99  return $handler->handle($request);
100  }
101 
113  protected function ‪evaluateCacheHashParameter(array $queryParams, bool $pageNotFoundOnCacheHashError): bool
114  {
115  if ($this->controller->cHash !== '') {
116  // Make sure we use the page uid and not the page alias
117  $queryParams['id'] = $this->controller->id;
118  $relevantParameters = $this->cacheHashCalculator->getRelevantParameters(‪HttpUtility::buildQueryString($queryParams));
119  $this->controller->cHash_array = $relevantParameters;
120  // cHash was given, but nothing to be calculated, so cHash is unset and all is good.
121  if (empty($relevantParameters)) {
122  $this->logger->notice('The incoming cHash "' . $this->controller->cHash . '" is given but not needed. cHash is unset');
123  return false;
124  }
125  $calculatedCacheHash = $this->cacheHashCalculator->calculateCacheHash($relevantParameters);
126  if (!hash_equals($calculatedCacheHash, $this->controller->cHash)) {
127  // Early return to trigger the error controller
128  if ($pageNotFoundOnCacheHashError) {
129  return false;
130  }
131  $this->controller->no_cache = true;
132  $this->‪getTimeTracker()->‪setTSlogMessage('The incoming cHash "' . $this->controller->cHash . '" and calculated cHash "' . $calculatedCacheHash . '" did not match, so caching was disabled. The fieldlist used was "' . implode(',', array_keys($this->controller->cHash_array)) . '"', 2);
133  }
134  // No cHash is set, check if that is correct
135  } elseif ($this->cacheHashCalculator->doParametersRequireCacheHash(‪HttpUtility::buildQueryString($queryParams))) {
136  // Will disable caching
137  $this->controller->reqCHash();
138  }
139  return true;
140  }
141 
145  protected function ‪getTimeTracker(): ‪TimeTracker
146  {
147  return GeneralUtility::makeInstance(TimeTracker::class);
148  }
149 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:25
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator\$cacheHashCalculator
‪CacheHashCalculator $cacheHashCalculator
Definition: PageArgumentValidator.php:46
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator\$controller
‪TypoScriptFrontendController $controller
Definition: PageArgumentValidator.php:50
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator\getTimeTracker
‪TimeTracker getTimeTracker()
Definition: PageArgumentValidator.php:143
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PageArgumentValidator.php:68
‪TYPO3\CMS\Frontend\Controller\ErrorController
Definition: ErrorController.php:35
‪TYPO3\CMS\Core\TimeTracker\TimeTracker\setTSlogMessage
‪setTSlogMessage($content, $num=0)
Definition: TimeTracker.php:193
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:4
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator
Definition: PageArgumentValidator.php:39
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:160
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:27
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator\__construct
‪__construct(TypoScriptFrontendController $controller=null)
Definition: PageArgumentValidator.php:55
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Page\PageAccessFailureReasons\CACHEHASH_COMPARISON_FAILED
‪const CACHEHASH_COMPARISON_FAILED
Definition: PageAccessFailureReasons.php:35
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:24
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator\evaluateCacheHashParameter
‪bool evaluateCacheHashParameter(array $queryParams, bool $pageNotFoundOnCacheHashError)
Definition: PageArgumentValidator.php:111
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:27
‪TYPO3\CMS\Frontend\Page\PageAccessFailureReasons
Definition: PageAccessFailureReasons.php:23