Skip to content

[Security] Return 403 instead of 500 when no firewall is defined #49193

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
Feb 7, 2023
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
30 changes: 17 additions & 13 deletions src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ public function logKernelException(ExceptionEvent $event)
// There's no specific status code defined in the configuration for this exception
if (!$throwable instanceof HttpExceptionInterface) {
$class = new \ReflectionClass($throwable);
$attributes = $class->getAttributes(WithHttpStatus::class, \ReflectionAttribute::IS_INSTANCEOF);

if ($attributes) {
/** @var WithHttpStatus $instance */
$instance = $attributes[0]->newInstance();
do {
if ($attributes = $class->getAttributes(WithHttpStatus::class, \ReflectionAttribute::IS_INSTANCEOF)) {
/** @var WithHttpStatus $instance */
$instance = $attributes[0]->newInstance();

$throwable = new HttpException($instance->statusCode, $throwable->getMessage(), $throwable, $instance->headers);
$event->setThrowable($throwable);
}
$throwable = new HttpException($instance->statusCode, $throwable->getMessage(), $throwable, $instance->headers);
$event->setThrowable($throwable);
break;
}
} while ($class = $class->getParentClass());
}

$e = FlattenException::createFromThrowable($throwable);
Expand Down Expand Up @@ -185,14 +187,16 @@ private function resolveLogLevel(\Throwable $throwable): string
}
}

$attributes = (new \ReflectionClass($throwable))->getAttributes(WithLogLevel::class);
$class = new \ReflectionClass($throwable);

if ($attributes) {
/** @var WithLogLevel $instance */
$instance = $attributes[0]->newInstance();
do {
if ($attributes = $class->getAttributes(WithLogLevel::class)) {
/** @var WithLogLevel $instance */
$instance = $attributes[0]->newInstance();

return $instance->level;
}
return $instance->level;
}
} while ($class = $class->getParentClass());

if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() >= 500) {
return LogLevel::CRITICAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testHandleWithLoggerAndCustomConfiguration()
public function testHandleWithLogLevelAttribute()
{
$request = new Request();
$event = new ExceptionEvent(new TestKernel(), $request, HttpKernelInterface::MAIN_REQUEST, new WarningWithLogLevelAttribute());
$event = new ExceptionEvent(new TestKernel(), $request, HttpKernelInterface::MAIN_REQUEST, new ChildOfWarningWithLogLevelAttribute());
$logger = new TestLogger();
$l = new ErrorListener('not used', $logger);

Expand Down Expand Up @@ -280,6 +280,7 @@ public static function exceptionWithAttributeProvider()
{
yield [new WithCustomUserProvidedAttribute(), 208, ['name' => 'value']];
yield [new WithGeneralAttribute(), 412, ['some' => 'thing']];
yield [new ChildOfWithGeneralAttribute(), 412, ['some' => 'thing']];
}
}

Expand Down Expand Up @@ -341,7 +342,15 @@ class WithGeneralAttribute extends \Exception
{
}

class ChildOfWithGeneralAttribute extends WithGeneralAttribute
{
}

#[WithLogLevel(LogLevel::WARNING)]
class WarningWithLogLevelAttribute extends \Exception
{
}

class ChildOfWarningWithLogLevelAttribute extends WarningWithLogLevelAttribute
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

namespace Symfony\Component\Security\Core\Exception;

use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;

/**
* AccessDeniedException is thrown when the account has not the required role.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
#[WithHttpStatus(403)]
class AccessDeniedException extends RuntimeException
{
private array $attributes = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Security\Core\Exception;

use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

/**
Expand All @@ -19,6 +20,7 @@
* @author Fabien Potencier <fabien@symfony.com>
* @author Alexander <iam.asm89@gmail.com>
*/
#[WithHttpStatus(401)]
class AuthenticationException extends RuntimeException
{
/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

namespace Symfony\Component\Security\Http\EntryPoint\Exception;

use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;

/**
* Thrown by generic decorators when a decorated authenticator does not implement
* {@see AuthenticationEntryPointInterface}.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
#[WithHttpStatus(401)]
class NotAnEntryPointException extends \RuntimeException
{
}