bug #35240 [SecurityBundle] Fix collecting traceable listeners info on lazy firewalls (chalasr)

This PR was merged into the 4.4 branch.

Discussion
----------

[SecurityBundle] Fix collecting traceable listeners info on lazy firewalls

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Before:
![Screenshot 2020-01-07 at 05 17 47](https://user-images.githubusercontent.com/7502063/71869007-cbffd400-3110-11ea-86ad-234da28621c4.png)

After:
![Screenshot 2020-01-07 at 05 18 12](https://user-images.githubusercontent.com/7502063/71869014-d9b55980-3110-11ea-8efc-1f1b16b2c372.png)

Commits
-------

a3a9a0e30a [SecurityBundle] Fix collecting traceable listeners info using anonymous: lazy
This commit is contained in:
Fabien Potencier 2020-01-10 10:12:52 +01:00
commit d68a4b0e2e
4 changed files with 156 additions and 47 deletions

View File

@ -12,7 +12,10 @@
namespace Symfony\Bundle\SecurityBundle\Debug;
use Symfony\Bundle\SecurityBundle\EventListener\FirewallListener;
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
use Symfony\Bundle\SecurityBundle\Security\LazyFirewallContext;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Http\Firewall\AbstractListener;
/**
* Firewall collecting called listeners.
@ -21,7 +24,7 @@ use Symfony\Component\HttpKernel\Event\RequestEvent;
*/
final class TraceableFirewallListener extends FirewallListener
{
private $wrappedListeners;
private $wrappedListeners = [];
public function getWrappedListeners()
{
@ -30,14 +33,47 @@ final class TraceableFirewallListener extends FirewallListener
protected function callListeners(RequestEvent $event, iterable $listeners)
{
$wrappedListeners = [];
$wrappedLazyListeners = [];
foreach ($listeners as $listener) {
$wrappedListener = new WrappedListener($listener);
$wrappedListener($event);
$this->wrappedListeners[] = $wrappedListener->getInfo();
if ($listener instanceof LazyFirewallContext) {
\Closure::bind(function () use (&$wrappedLazyListeners, &$wrappedListeners) {
$listeners = [];
foreach ($this->listeners as $listener) {
if ($listener instanceof AbstractListener) {
$listener = new WrappedLazyListener($listener);
$listeners[] = $listener;
$wrappedLazyListeners[] = $listener;
} else {
$listeners[] = function (RequestEvent $event) use ($listener, &$wrappedListeners) {
$wrappedListener = new WrappedListener($listener);
$wrappedListener($event);
$wrappedListeners[] = $wrappedListener->getInfo();
};
}
}
$this->listeners = $listeners;
}, $listener, FirewallContext::class)();
$listener($event);
} else {
$wrappedListener = $listener instanceof AbstractListener ? new WrappedLazyListener($listener) : new WrappedListener($listener);
$wrappedListener($event);
$wrappedListeners[] = $wrappedListener->getInfo();
}
if ($event->hasResponse()) {
break;
}
}
if ($wrappedLazyListeners) {
foreach ($wrappedLazyListeners as $lazyListener) {
$this->wrappedListeners[] = $lazyListener->getInfo();
}
}
$this->wrappedListeners = array_merge($this->wrappedListeners, $wrappedListeners);
}
}

View File

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SecurityBundle\Debug;
use Symfony\Component\Security\Http\Firewall\LegacyListenerTrait;
/**
* @author Robin Chalas <robin.chalas@gmail.com>
*
* @internal
*/
trait TraceableListenerTrait
{
use LegacyListenerTrait;
private $response;
private $listener;
private $time;
private $stub;
/**
* Proxies all method calls to the original listener.
*/
public function __call(string $method, array $arguments)
{
return $this->listener->{$method}(...$arguments);
}
public function getWrappedListener()
{
return $this->listener;
}
}

View File

@ -0,0 +1,72 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SecurityBundle\Debug;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Exception\LazyResponseException;
use Symfony\Component\Security\Http\Firewall\AbstractListener;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
use Symfony\Component\VarDumper\Caster\ClassStub;
/**
* Wraps a lazy security listener.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*
* @internal
*/
final class WrappedLazyListener extends AbstractListener implements ListenerInterface
{
use TraceableListenerTrait;
public function __construct(AbstractListener $listener)
{
$this->listener = $listener;
}
public function supports(Request $request): ?bool
{
return $this->listener->supports($request);
}
/**
* {@inheritdoc}
*/
public function authenticate(RequestEvent $event)
{
$startTime = microtime(true);
try {
$ret = $this->listener->authenticate($event);
} catch (LazyResponseException $e) {
$this->response = $e->getResponse();
throw $e;
} finally {
$this->time = microtime(true) - $startTime;
}
$this->response = $event->getResponse();
return $ret;
}
public function getInfo(): array
{
return [
'response' => $this->response,
'time' => $this->time,
'stub' => $this->stub ?? $this->stub = ClassStub::wrapCallable($this->listener),
];
}
}

View File

@ -12,7 +12,6 @@
namespace Symfony\Bundle\SecurityBundle\Debug;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Http\Firewall\LegacyListenerTrait;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
use Symfony\Component\VarDumper\Caster\ClassStub;
@ -25,13 +24,7 @@ use Symfony\Component\VarDumper\Caster\ClassStub;
*/
final class WrappedListener implements ListenerInterface
{
use LegacyListenerTrait;
private $response;
private $listener;
private $time;
private $stub;
private static $hasVarDumper;
use TraceableListenerTrait;
/**
* @param callable $listener
@ -57,46 +50,12 @@ final class WrappedListener implements ListenerInterface
$this->response = $event->getResponse();
}
/**
* Proxies all method calls to the original listener.
*/
public function __call(string $method, array $arguments)
{
return $this->listener->{$method}(...$arguments);
}
public function getWrappedListener()
{
return $this->listener;
}
public function getInfo(): array
{
if (null !== $this->stub) {
// no-op
} elseif (self::$hasVarDumper ?? self::$hasVarDumper = class_exists(ClassStub::class)) {
$this->stub = ClassStub::wrapCallable($this->listener);
} elseif (\is_array($this->listener)) {
$this->stub = (\is_object($this->listener[0]) ? \get_class($this->listener[0]) : $this->listener[0]).'::'.$this->listener[1];
} elseif ($this->listener instanceof \Closure) {
$r = new \ReflectionFunction($this->listener);
if (false !== strpos($r->name, '{closure}')) {
$this->stub = 'closure';
} elseif ($class = $r->getClosureScopeClass()) {
$this->stub = $class->name.'::'.$r->name;
} else {
$this->stub = $r->name;
}
} elseif (\is_string($this->listener)) {
$this->stub = $this->listener;
} else {
$this->stub = \get_class($this->listener).'::__invoke';
}
return [
'response' => $this->response,
'time' => $this->time,
'stub' => $this->stub,
'stub' => $this->stub ?? $this->stub = ClassStub::wrapCallable($this->listener),
];
}
}