Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Deprecate TSFE->makeCacheHash()
The functionality is moved into a new PSR-15 middleware to base the logic on the request object directly, and to make the validation more flexible when validating page parameters for site-based routing. The previous deprecation to add the request object to the method has been reverted. Resolves: #86411 Releases: master Change-Id: I294fae7e7c0f9eb1e128a88238dabdd8ed27619f Reviewed-on: https://review.typo3.org/58420 Reviewed-by: Jan Helke <typo3@helke.de> Tested-by: Jan Helke <typo3@helke.de> Tested-by: TYPO3com <no-reply@typo3.com> Reviewed-by: Oliver Hader <oliver.hader@typo3.org> Reviewed-by: Frank Naegler <frank.naegler@typo3.org> Tested-by: Frank Naegler <frank.naegler@typo3.org>
- Loading branch information
Showing
10 changed files
with
185 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...xt/core/Documentation/Changelog/master/Deprecation-86411-TSFE-makeCacheHash.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.. include:: ../../Includes.txt | ||
|
||
=========================================== | ||
Deprecation: #86411 - TSFE->makeCacheHash() | ||
=========================================== | ||
|
||
See :issue:`86411` | ||
|
||
Description | ||
=========== | ||
|
||
The method :php:`TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->makeCacheHash()` | ||
which acts for validating the `&cHash` GET parameter against other given GET parameters | ||
has been marked as deprecated, as this functionality has been moved into a PSR-15 middleware. | ||
|
||
|
||
Impact | ||
====== | ||
|
||
Calling the method directly will trigger a deprecation message. | ||
|
||
|
||
Affected Installations | ||
====================== | ||
|
||
TYPO3 installations with extensions calling the PHP method directly. | ||
|
||
|
||
Migration | ||
========= | ||
|
||
Ensure to use the PSR-15 middleware stack with the PageParameterValidator in use to verify a | ||
given cHash signature against given query parameters. | ||
|
||
.. index:: Frontend, FullyScanned |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
typo3/sysext/frontend/Classes/Middleware/PageParameterValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TYPO3\CMS\Frontend\Middleware; | ||
|
||
/* | ||
* This file is part of the TYPO3 CMS project. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
* | ||
* The TYPO3 project - inspiring people to share! | ||
*/ | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use TYPO3\CMS\Core\TimeTracker\TimeTracker; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Frontend\Controller\ErrorController; | ||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
use TYPO3\CMS\Frontend\Page\CacheHashCalculator; | ||
use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons; | ||
|
||
/** | ||
* This middleware validates given request parameters. | ||
*/ | ||
class PageParameterValidator implements MiddlewareInterface | ||
{ | ||
|
||
/** | ||
* The cHash Service class used for cHash related functionality | ||
* | ||
* @var CacheHashCalculator | ||
*/ | ||
protected $cacheHashCalculator; | ||
|
||
/** | ||
* @var TypoScriptFrontendController | ||
*/ | ||
protected $controller; | ||
|
||
/** | ||
* @param TypoScriptFrontendController|null $controller | ||
*/ | ||
public function __construct(TypoScriptFrontendController $controller = null) | ||
{ | ||
$this->controller = $controller ?? $GLOBALS['TSFE']; | ||
$this->cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class); | ||
} | ||
|
||
/** | ||
* Validates the &cHash parameter against the other $queryParameters / GET parameters | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $handler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$pageNotFoundOnValidationError = (bool)($GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError'] ?? true); | ||
if ($this->controller->no_cache && !$pageNotFoundOnValidationError) { | ||
// No need to test anything if caching was already disabled. | ||
} else { | ||
// Evaluate the cache hash parameter | ||
$queryParams = $request->getQueryParams(); | ||
if (!empty($queryParams) && !$this->evaluateCacheHashParameter($queryParams, $pageNotFoundOnValidationError)) { | ||
return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction( | ||
$request, | ||
'Request parameters could not be validated (&cHash comparison failed)', | ||
['code' => PageAccessFailureReasons::CACHEHASH_COMPARISON_FAILED] | ||
); | ||
} | ||
} | ||
return $handler->handle($request); | ||
} | ||
|
||
/** | ||
* Calculates a hash string based on additional parameters in the url. | ||
* | ||
* Calculated hash is stored in $this->controller->cHash_array. | ||
* This is used to cache pages with more parameters than just id and type. | ||
* | ||
* @see TypoScriptFrontendController::reqCHash() | ||
* @param array $queryParams GET parameters | ||
* @param bool $pageNotFoundOnCacheHashError see $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError'] | ||
* @return bool if false, then a PageNotFound response is triggered | ||
*/ | ||
protected function evaluateCacheHashParameter(array $queryParams, bool $pageNotFoundOnCacheHashError): bool | ||
{ | ||
if ($this->controller->cHash) { | ||
// Make sure we use the page uid and not the page alias | ||
$queryParams['id'] = $this->controller->id; | ||
$this->controller->cHash_array = $this->cacheHashCalculator->getRelevantParameters(GeneralUtility::implodeArrayForUrl('', $queryParams)); | ||
$cHash_calc = $this->cacheHashCalculator->calculateCacheHash($this->controller->cHash_array); | ||
if (!hash_equals($cHash_calc, $this->controller->cHash)) { | ||
// Early return to trigger the error controller | ||
if ($pageNotFoundOnCacheHashError) { | ||
return false; | ||
} | ||
$this->controller->no_cache = true; | ||
$this->getTimeTracker()->setTSlogMessage('The incoming cHash "' . $this->controller->cHash . '" and calculated cHash "' . $cHash_calc . '" did not match, so caching was disabled. The fieldlist used was "' . implode(',', array_keys($this->controller->cHash_array)) . '"', 2); | ||
} | ||
// No cHash is set, check if that is correct | ||
} elseif ($this->cacheHashCalculator->doParametersRequireCacheHash(GeneralUtility::implodeArrayForUrl('', $queryParams))) { | ||
// Will disable caching | ||
$this->controller->reqCHash(); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @return TimeTracker | ||
*/ | ||
protected function getTimeTracker(): TimeTracker | ||
{ | ||
return GeneralUtility::makeInstance(TimeTracker::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters