Blog Post aktualisiert am 4.2.2022
Kennt ihr das auch? Ihr habt schon lange keinen TypeNum 98, 99, 100 oder 3135 mehr in eurer Konfiguration der Website und dennoch gibt es immer noch solche Requests auf eure Seite. Diese führen dann zu einer Fehlermeldung und einem Status 500. Noch schlimmer ist es, wenn irgendwelche Hackingscripts den TypeNum mit SQL-Queries zumüllen und dies dann sogar in den Cache von TYPO3 wandert. Dann sehen die internen Links z.B. so aus:
domain.org/page1/?type=UNION%20SELECT%20FROM%20...
Wenn ihr nicht alle diese Types in der .htaccess blocken wollt, könnt ihr euch auch über PSR-15 Middleware bereits relativ früh ins System hängen und den angeforderten Type mit den Type-Settings in der Site Konfiguration abgleichen. Im nachfolgenden Beispiel schmeißen wir eine 404 Meldung.
EXT:sitepackage/Configuration/RequestMiddlewares.php:
<?php
return [
'frontend' => [
'sitepackage-undefinedtypenumerrorhandling' => [
'target' => \In2code\In2template\Middleware\UndefinedTypeNumErrorHandling::class,
'before' => [
'typo3/cms-redirects/redirecthandler'
],
'after' => [
'typo3/cms-frontend/site',
]
]
]
];
EXT:sitepackage/Classes/Middleware/UndefinedTypeNumErrorHandling.php:
<?php
declare(strict_types=1);
namespace In2code\In2template\Middleware;
use In2code\In2template\Exception\ConfigurationMissingException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\MathUtility;
/**
* Class UndefinedTypeNumErrorHandling
* to simply show 404 page if a typeNum is given, that is not defined in siteconfiguration.
* This avoids annoying log entries and delivers a 404 to the (e.g.) bot that is calling the outdated url.
*/
class UndefinedTypeNumErrorHandling implements MiddlewareInterface
{
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @throws ConfigurationMissingException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->isTypeNumSet($request) && $this->isTypeNumAllowed($request) === false) {
/** @var Site $site */
$site = $request->getAttribute('site');
try {
$errorHandler = $site->getErrorHandler(404);
return $errorHandler->handlePageError($request, 'Given type is not registered in site configuration');
} catch (Throwable $exception) {
throw new ConfigurationMissingException('No 404 error handler given in site configuration', 1643989065);
}
}
return $handler->handle($request);
}
/**
* @param ServerRequestInterface $request
* @return bool
*/
protected function isTypeNumSet(ServerRequestInterface $request): bool
{
return array_key_exists('type', $request->getQueryParams());
}
/**
* @param ServerRequestInterface $request
* @return bool
*/
protected function isTypeNumAllowed(ServerRequestInterface $request): bool
{
/** @var Site $site */
$site = $request->getAttribute('site');
if (!empty($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map'])) {
$allowedTypeNums = array_values($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map']);
$type = $request->getQueryParams()['type'];
if (MathUtility::canBeInterpretedAsInteger($type)) {
return in_array((int)$type, $allowedTypeNums, true);
}
}
return false;
}
}
Hinweis: Wenn ihr dieses oder ein ähnliches Snippet verwendet, müsst ihr darauf achten, dass alle eure TypeNum-Definitionen auch in der Site-Configuration aufgenommen wurde - z.B.:
rootPageId: 1
routes:
-
route: robots.txt
type: staticText
content: "Disallow: /typo3/\r\n"
routeEnhancers:
PageTypeSuffix:
type: PageType
default: /
index: ''
suffix: /
map:
print.html: 99
preview.html: 1560777975
newsletter.html: 1562349004
pixel.png: 1561894816