Skip to content

Commit

Permalink
Csrf enhancements to element and validator
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmartin committed Aug 26, 2012
1 parent 57c7d09 commit 2af8190
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
53 changes: 44 additions & 9 deletions library/Zend/Form/Element/Csrf.php
Expand Up @@ -32,22 +32,57 @@ class Csrf extends Element implements InputProviderInterface, ElementPrepareAwar
'type' => 'hidden',
);

/**
* @var array
*/
protected $csrfValidatorOptions = array();

/**
* @var CsrfValidator
*/
protected $validator;
protected $csrfValidator;

/**
* @return array
*/
public function getCsrfValidatorOptions()
{
return $this->csrfValidatorOptions;
}

/**
* @param array $options
* @return Csrf
*/
public function setCsrfValidatorOptions(array $options)
{
$this->csrfValidatorOptions = $options;
return $this;
}

/**
* Get CSRF validator
*
* @return CsrfValidator
*/
protected function getValidator()
public function getCsrfValidator()
{
if (null === $this->validator) {
$this->validator = new CsrfValidator(array('name' => $this->getName()));
if (null === $this->csrfValidator) {
$csrfOptions = $this->getCsrfValidatorOptions();
$csrfOptions = array_merge($csrfOptions, array('name' => $this->getName()));
$this->csrfValidator = new CsrfValidator($csrfOptions);
}
return $this->validator;
return $this->csrfValidator;
}

/**
* @param \Zend\Validator\Csrf $validator
* @return Csrf
*/
public function setCsrfValidator(CsrfValidator $validator)
{
$this->csrfValidator = $validator;
return $this;
}

/**
Expand All @@ -59,7 +94,7 @@ protected function getValidator()
*/
public function getValue()
{
$validator = $this->getValidator();
$validator = $this->getCsrfValidator();
return $validator->getHash();
}

Expand All @@ -73,7 +108,7 @@ public function getValue()
public function getAttributes()
{
$attributes = parent::getAttributes();
$validator = $this->getValidator();
$validator = $this->getCsrfValidator();
$attributes['value'] = $validator->getHash();
return $attributes;
}
Expand All @@ -94,7 +129,7 @@ public function getInputSpecification()
array('name' => 'Zend\Filter\StringTrim'),
),
'validators' => array(
$this->getValidator(),
$this->getCsrfValidator(),
),
);
}
Expand All @@ -104,6 +139,6 @@ public function getInputSpecification()
*/
public function prepareElement(Form $form)
{
$this->getValidator()->getHash(true);
$this->getCsrfValidator()->getHash(true);
}
}
11 changes: 7 additions & 4 deletions library/Zend/Validator/Csrf.php
Expand Up @@ -65,7 +65,7 @@ class Csrf extends AbstractValidator

/**
* TTL for CSRF token
* @var int
* @var int|null
*/
protected $timeout = 300;

Expand Down Expand Up @@ -243,12 +243,12 @@ public function getSessionName()
/**
* Set timeout for CSRF session token
*
* @param int $ttl
* @param int|null $ttl
* @return Csrf
*/
public function setTimeout($ttl)
{
$this->timeout = (int) $ttl;
$this->timeout = ($ttl !== null) ? (int)$ttl : null;
return $this;
}

Expand All @@ -271,7 +271,10 @@ protected function initCsrfToken()
{
$session = $this->getSession();
//$session->setExpirationHops(1, null, true);
$session->setExpirationSeconds($this->getTimeout());
$timeout = $this->getTimeout();
if (null !== $timeout) {
$session->setExpirationSeconds($timeout);
}
$session->hash = $this->getHash();
}

Expand Down
17 changes: 17 additions & 0 deletions tests/ZendTest/Form/Element/CsrfTest.php
Expand Up @@ -40,4 +40,21 @@ public function testProvidesInputSpecificationThatIncludesValidatorsBasedOnAttri
}
}
}

public function testAllowSettingCustomCsrfValidator()
{
$element = new CsrfElement('foo');
$validatorMock = $this->getMock('Zend\Validator\Csrf');
$element->setCsrfValidator($validatorMock);
$this->assertEquals($validatorMock, $element->getCsrfValidator());
}

public function testAllowSettingCsrfValidatorOptions()
{
$element = new CsrfElement('foo');
$element->setCsrfValidatorOptions(array('timeout' => 777));
$validator = $element->getCsrfValidator();
$this->assertEquals('foo', $validator->getName());
$this->assertEquals(777, $validator->getTimeout());
}
}
20 changes: 17 additions & 3 deletions tests/ZendTest/Validator/CsrfTest.php
Expand Up @@ -90,10 +90,24 @@ public function testTimeoutHasDefaultValue()
$this->assertEquals(300, $this->validator->getTimeout());
}

public function testTimeoutIsMutable()
public function timeoutValuesDataProvider()
{
return array(
// timeout expected
array(600, 600),
array(null, null),
array("0", 0),
array("100", 100),
);
}

/**
* @dataProvider timeoutValuesDataProvider
*/
public function testTimeoutIsMutable($timeout, $expected)
{
$this->validator->setTimeout(600);
$this->assertEquals(600, $this->validator->getTimeout());
$this->validator->setTimeout($timeout);
$this->assertEquals($expected, $this->validator->getTimeout());
}

public function testAllOptionsMayBeSetViaConstructor()
Expand Down

0 comments on commit 2af8190

Please sign in to comment.