Skip to content

[Security] make ExceptionEvent handle all throwables #34319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
make ExceptionEvent handle all throwables
  • Loading branch information
xabbuh committed Nov 11, 2019
commit eba2d8efc9a72ea081938c0bde0b5a8dcb6e6cb9
19 changes: 7 additions & 12 deletions src/Symfony/Component/HttpKernel/Event/ExceptionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,33 @@
*/
final class ExceptionEvent extends RequestEvent
{
/**
* The exception object.
*
* @var \Exception
*/
private $exception;
private $throwable;

/**
* @var bool
*/
private $allowCustomResponseCode = false;

public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Exception $e)
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e)
{
parent::__construct($kernel, $request, $requestType);

$this->setException($e);
$this->setThrowable($e);
}

public function getException(): \Exception
public function getThrowable(): \Throwable
{
return $this->exception;
return $this->throwable;
}

/**
* Replaces the thrown exception.
*
* This exception will be thrown if no response is set in the event.
*/
public function setException(\Exception $exception): void
public function setThrowable(\Throwable $exception): void
{
$this->exception = $exception;
$this->throwable = $exception;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function __construct($controller, LoggerInterface $logger = null, bool $d

public function logKernelException(ExceptionEvent $event)
{
$e = FlattenException::createFromThrowable($event->getException());
$e = FlattenException::createFromThrowable($event->getThrowable());

$this->logException($event->getException(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
$this->logException($event->getThrowable(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
}

public function onKernelException(ExceptionEvent $event, string $eventName = null, EventDispatcherInterface $eventDispatcher = null)
Expand All @@ -53,7 +53,7 @@ public function onKernelException(ExceptionEvent $event, string $eventName = nul
return;
}

$exception = $event->getException();
$exception = $event->getThrowable();
$request = $this->duplicateRequest($exception, $event->getRequest());

try {
Expand Down Expand Up @@ -98,7 +98,7 @@ public static function getSubscribedEvents(): array
];
}

protected function logException(\Exception $exception, string $message)
protected function logException(\Throwable $exception, string $message)
{
if (null !== $this->logger) {
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
Expand All @@ -112,7 +112,7 @@ protected function logException(\Exception $exception, string $message)
/**
* Clones the request for the exception.
*/
protected function duplicateRequest(\Exception $exception, Request $request): Request
protected function duplicateRequest(\Throwable $exception, Request $request): Request
{
$attributes = [
'_controller' => $this->controller,
Expand Down