Skip to content

Commit

Permalink
Don't use exceptions for flow control
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <[email protected]>
  • Loading branch information
kamil-tekiela authored and MauricioFauth committed Nov 13, 2024
1 parent 747ac5f commit 319763e
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Http/Middleware/RequestProblemChecking.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;

use function __;
use function count;
Expand All @@ -27,23 +26,24 @@ public function __construct(private readonly Template $template, private readonl

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
throw new RuntimeException(__('GLOBALS overwrite attempt'));
}

/**
* protect against possible exploits - there is no need to have so many variables
*/
if (count($_REQUEST) >= 1000) {
throw new RuntimeException(__('possible exploit'));
}
} catch (RuntimeException $exception) {
if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);

return $response->write($this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'error_message' => $exception->getMessage(),
'error_message' => __('GLOBALS overwrite attempt'),
]));
}

/**
* protect against possible exploits - there is no need to have so many variables
*/
if (count($_REQUEST) >= 1000) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);

return $response->write($this->template->render('error/generic', [
'lang' => $GLOBALS['lang'] ?? 'en',
'error_message' => __('possible exploit'),
]));
}

Expand Down

0 comments on commit 319763e

Please sign in to comment.