8000 [SecurityBundle] Fixed a memory leak in SecurityBundle\Security\FirewallMap by udavka · Pull Request #22605 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[SecurityBundle] Fixed a memory leak in SecurityBundle\Security\FirewallMap #22605

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fixed a memory leak in Symfony\Bundle\SecurityBundle\Security\Firewal…
…lMap
  • Loading branch information
udavka committed May 1, 2017
commit 382bd476b5b3f416f4f1d8962c33701fa404d39c
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public function getListeners(Request $request)
return $context->getContext();
}

/**
* {@inheritdoc}
*/
public function detachListeners(Request $request)
{
unset($this->contexts[$request]);
}

/**
* @return FirewallConfig|null
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Security/Http/Firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function onKernelFinishRequest(FinishRequestEvent $event)
{
$request = $event->getRequest();

$this->map->detachListeners($event->getRequest());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not always work (think of other implementations of the FirewallMapInterface).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by replacing the implementation.


if (isset($this->exceptionListeners[$request])) {
$this->exceptionListeners[$request]->unregister($this->dispatcher);
unset($this->exceptionListeners[$request]);
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Security/Http/FirewallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ public function getListeners(Request $request)

return array(array(), null);
}

/**
* {@inheritdoc}
*/
public function detachListeners(Request $request)
{
foreach ($this->map as $key => $elements) {
if (null === $elements[0] || $elements[0]->matches($request)) {
unset($this->map[$key]);
}
}
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/Security/Http/FirewallMapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ interface FirewallMapInterface
* @return array of the format array(array(AuthenticationListener), ExceptionListener)
*/
public function getListeners(Request $request);

/**
* Cleans up the internal state of the firewall map.
*
* @param Request $request
* @return void
*/
public function detachListeners(Request $request);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new method to an interface is a BC break and not allowed by our BC promise.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by replacing the implementation.

}
39 changes: 39 additions & 0 deletions src/Symfony/Component/Security/Http/Tests/FirewallMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,43 @@ public function testGetListenersWithNoMatchingEntry()
$this->assertEquals(array(), $listeners);
$this->assertNull($exception);
}

public function testDetachListeners()
{
$map = new FirewallMap();

$request1 = new Request();
$matchingMatcher1 = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
$matchingMatcher1
->expects($this->once())
->method('matches')
->with($this->equalTo($request1))
->will($this->returnValue(true))
;

$request2 = new Request();
$matchingMatcher2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestMatcher')->getMock();
$matchingMatcher2
->expects($this->once())
->method('matches')
->with($this->equalTo($request2))
->will($this->returnValue(true))
;

$theListener = $this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ListenerInterface')->getMock();
$theException = $this->getMockBuilder('Symfony\Component\Security\Http\Firewall\ExceptionListener')->disableOriginalConstructor()->getMock();

$map->add($matchingMatcher1, array($theListener), $theException);
$map->add($matchingMatcher2, array($theListener), $theException);

$map->detachListeners($request1);

list($listeners, $exception) = $map->getListeners($request1);
$this->assertEquals(array(), $listeners);
$this->assertNull($exception);

list($listeners, $exception) = $map->getListeners($request2);
$this->assertEquals(array($theListener), $listeners);
$this->assertEquals($theException, $exception);
}
}
0