Skip to content

Commit

Permalink
Merge remote-tracking branch 'SocalNick/feature/prevent-circular-forw…
Browse files Browse the repository at this point in the history
…ard'
  • Loading branch information
EvanDotPro committed May 2, 2012
2 parents 94506a4 + f371e6b commit f30be2f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions library/Zend/Mvc/Controller/Plugin/Forward.php
Expand Up @@ -14,6 +14,14 @@ class Forward extends AbstractPlugin
{
protected $event;
protected $locator;
protected $maxNestedForwards = 10;
protected $numNestedForwards = 0;

public function setMaxNestedForwards($maxNestedForwards)
{
$this->maxNestedForwards = (int) $maxNestedForwards;
return $this;
}

/**
* Dispatch another controller
Expand Down Expand Up @@ -48,8 +56,15 @@ public function dispatch($name, array $params = null)
$event->setRouteMatch($matches);
}

if ($this->numNestedForwards > $this->maxNestedForwards) {
throw new Exception\DomainException("Circular forwarding detected: greater than $this->maxNestedForwards nested forwards");
}
$this->numNestedForwards++;

$return = $controller->dispatch($event->getRequest(), $event->getResponse());

$this->numNestedForwards--;

if ($cachedMatches) {
$event->setRouteMatch($cachedMatches);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Zend/Mvc/Controller/Plugin/ForwardTest.php
Expand Up @@ -73,6 +73,16 @@ public function testDispatchRaisesDomainExceptionIfDiscoveredControllerIsNotDisp
$this->plugin->dispatch('bogus');
}

public function testDispatchRaisesDomainExceptionIfCircular()
{
$this->setExpectedException('Zend\Mvc\Exception\DomainException', 'Circular forwarding');
$sampleController = $this->controller;
$sampleController->getLocator()->add('sample', function() use ($sampleController) {
return $sampleController;
});
$this->plugin->dispatch('sample', array('action' => 'test-circular'));
}

public function testPluginDispatchsRequestedControllerWhenFound()
{
$result = $this->plugin->dispatch('forward');
Expand Down
5 changes: 5 additions & 0 deletions tests/Zend/Mvc/Controller/TestAsset/SampleController.php
Expand Up @@ -15,4 +15,9 @@ public function testSomeStrangelySeparatedWordsAction()
{
return array('content' => 'Test Some Strangely Separated Words');
}

public function testCircularAction()
{
return $this->forward()->dispatch('sample', array('action' => 'test-circular'));
}
}

0 comments on commit f30be2f

Please sign in to comment.