Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Security] Type error caused by SecurityDataCollector::getVoters()  #54225

Copy link
Copy link
Closed
@fritzmg

Description

@fritzmg
Issue body actions

Symfony version(s) affected

6.4.x, 7.x

Description

SecurityDataCollector::getVoters() currently has the return type array|Data. However, the following type error can occur:

Twig\Error\RuntimeError:
An exception has been thrown during the rendering of a template ("Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector::getVoters(): Return value must be of type Symfony\Component\VarDumper\Cloner\Data|array, null returned").

  at vendor\symfony\security-bundle\Resources\views\Collector\security.html.twig:394
TypeError:
Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector::getVoters(): Return value must be of type Symfony\Component\VarDumper\Cloner\Data|array, null returned

  at vendor\symfony\security-bundle\DataCollector\SecurityDataCollector.php:310
  at Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector->getVoters()
     (vendor\twig\twig\src\Extension\CoreExtension.php:1635)

This is because the given AccessDecisionManager instance might be of type TraceableAccessDecisionManager - but not return any voters. In such a case $this->data['voters'] will not be initialized at all and thus SecurityDataCollector::getVoters() would return null, which is not allowed by the return type. The Twig template for the Profiler also seems to assume the getVoters() can return null - which was actually the case in previous Symfony versions, as there was only a return type annotation.

That being said - the core reason why there are not voters collected in the first place is something else entirely. The TraceableAccessDecisionManager does not / cannot take a decorated AccessDecisionManager into account, as the getVoters() method is not part of an interface. Thus, when you decorate Symfony's default AccessDecisionManager, no voters will be retrieved from the new service - unless you happen to implement a getVoters() method as well (see reproduction below).

How to reproduce

A simple reproduction is to decorate the default security.access.decision_manager like so:

# config/services.yaml
services:
    App\Security\DecoratedAccessDecisionManager:
        decorates: security.access.decision_manager
// src/Security/DecoratedAccessDecisionManager.php
namespace App\Security;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;

class DecoratedAccessDecisionManager implements AccessDecisionManagerInterface
{
    public function __construct(public readonly AccessDecisionManagerInterface $inner)
    {
    }

    public function decide(TokenInterface $token, array $attributes, mixed $object = null): bool
    {
        return $this->inner->decide($token, $attributes, $object);
    }
}

If you then open the profiler and go to the Security tab, the aforementioned error will occur.

Possible Solution

Either null needs to be added as an allowed return type for SecurityDataCollector::getVoters() - or $this->data['voters'] always needs to be initialized with data.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.