Skip to content

Commit bf02fde

Browse files
authored
[11.x] Enhance malformed request handling (#50735)
* Convert SuspiciousOperationException to BadRequestHttpException This is suggested per https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/HttpFoundation/Exception/RequestExceptionInterface.php * Update existing test on SuspiciousOperationException handling * Add integration test for malformed requests * Return 400 code on all exceptions extending RequestExceptionInterface
1 parent 57610f7 commit bf02fde

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838
use Symfony\Component\Console\Application as ConsoleApplication;
3939
use Symfony\Component\Console\Exception\CommandNotFoundException;
4040
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
41-
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
41+
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
4242
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
4343
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
4444
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
45+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
4546
use Symfony\Component\HttpKernel\Exception\HttpException;
4647
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
4748
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -143,7 +144,7 @@ class Handler implements ExceptionHandlerContract
143144
ModelNotFoundException::class,
144145
MultipleRecordsFoundException::class,
145146
RecordsNotFoundException::class,
146-
SuspiciousOperationException::class,
147+
RequestExceptionInterface::class,
147148
TokenMismatchException::class,
148149
ValidationException::class,
149150
];
@@ -630,7 +631,7 @@ protected function prepareException(Throwable $e)
630631
),
631632
$e instanceof AuthorizationException && ! $e->hasStatus() => new AccessDeniedHttpException($e->getMessage(), $e),
632633
$e instanceof TokenMismatchException => new HttpException(419, $e->getMessage(), $e),
633-
$e instanceof SuspiciousOperationException => new NotFoundHttpException('Bad hostname provided.', $e),
634+
$e instanceof RequestExceptionInterface => new BadRequestHttpException('Bad request.', $e),
634635
$e instanceof RecordsNotFoundException => new NotFoundHttpException('Not found.', $e),
635636
default => $e,
636637
};

tests/Foundation/FoundationExceptionsHandlerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,15 @@ function ($argument) use (&$argumentActual) {
359359
$this->assertEquals($argumentExpected, $argumentActual);
360360
}
361361

362-
public function testSuspiciousOperationReturns404WithoutReporting()
362+
public function testSuspiciousOperationReturns400WithoutReporting()
363363
{
364364
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true);
365365
$this->request->shouldReceive('expectsJson')->once()->andReturn(true);
366366

367367
$response = $this->handler->render($this->request, new SuspiciousOperationException('Invalid method override "__CONSTRUCT"'));
368368

369-
$this->assertEquals(404, $response->getStatusCode());
370-
$this->assertStringContainsString('"message": "Bad hostname provided."', $response->getContent());
369+
$this->assertEquals(400, $response->getStatusCode());
370+
$this->assertStringContainsString('"message": "Bad request."', $response->getContent());
371371

372372
$logger = m::mock(LoggerInterface::class);
373373
$this->container->instance(LoggerInterface::class, $logger);

tests/Integration/Foundation/ExceptionHandlerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ public function testItHasFallbackErrorMessageForUnknownStatusCodes()
124124
]);
125125
}
126126

127+
public function testItReturns400CodeOnMalformedRequests()
128+
{
129+
// HTTP request...
130+
$this->post('test-route', ['_method' => '__construct'])
131+
->assertStatus(400)
132+
->assertSeeText('Bad Request'); // see https://github.com/symfony/symfony/blob/1d439995eb6d780531b97094ff5fa43e345fc42e/src/Symfony/Component/ErrorHandler/Resources/views/error.html.php#L12
133+
134+
// JSON request...
135+
$this->postJson('test-route', ['_method' => '__construct'])
136+
->assertStatus(400)
137+
->assertExactJson([
138+
'message' => 'Bad request.',
139+
]);
140+
}
141+
127142
#[DataProvider('exitCodesProvider')]
128143
public function testItReturnsNonZeroExitCodesForUncaughtExceptions($providers, $successful)
129144
{

0 commit comments

Comments
 (0)