我在Zend_Form中使用Zend的CSRF令牌时遇到了一些麻烦...
每次提交表单时,我都会收到验证错误(与CSRF使用的“相同”)
在我的项目早期,我没有遇到任何问题,但当时我只是在我的视图中使用echo $this->form;
,其中form
从控制器传递。如果我回到这个'echo .. $ form',我可以让问题消失,但我现在需要分别回应每个元素,因为我需要对每个元素及其渲染方式进行大量控制。我还需要最严格的安全措施,特别是像CSRF这样的基础知识。
这就是我使用表单元素呈现“登录”页面的方式,问题显然从这里开始,但在任何地方我都有一个表单(无处不在:) :)
<form id="login" enctype="application/x-www-form-urlencoded" method="<?php echo $form->getMethod() ?>" action="<?php echo $form->getAction() ?>">
<div class="login-section">
<div class="form-container inputsLogin">
<?php echo $form->agency->renderLabel() . $form->agency->renderViewHelper() ?>
<br /><br />
<?php
echo $form->username->renderLabel() .
$this->formText('username', '', array(
'required' => True,
));
echo $form->password->renderLabel() .
$this->formPassword('password', '', array(
'required' => True,
));
echo $form->returnUrl->renderLabel() . $form->returnUrl->renderViewHelper();
echo $form->csrf_token->renderLabel() . $form->csrf_token;
?>
<div class="element">
<?php echo $this->formSubmit('submit', 'Login') ?>
</div>
</div>
</div>
</form>
我已经扩展了Zend_Form来添加CSRF元素,但是在项目的早期我在每个表单类中都有它。我像$ form中的任何其他元素一样抓取CSRF元素并将其回显到视图中。我怀疑我的CSRF生成了两次,但期待第一次,但我不知道。 我已经尝试过几样东西......回显标签,不回显标签,在我的视图中完全生成csrf元素,设置超时(或不设置)。
另外,为了清楚起见,这里是我如何在表单类中声明我的CSRF元素...
class Application_Form_Actform extends Zend_Form {
function __construct()
{
parent::__construct();
$this->addElement('hash', 'csrf_token', array(
'required' => 'true',
'salt' => get_class($this) . 'unique',
'ignore' => true,
'timeout' => 60000,
'errorMessages' => array(
'Identical' => 'Form may have timed out, or there is an attempt at forgery (Security issue). Please submit form again.'
),
));
}
}
更新:
我想我得到两个令牌,这是var_dump($_SESSION);
的输出:
array(2) { '__ZF' => array(1) { 'Zend_Form_Element_Hash_Application_Form_Loginunique_csrf_token' => array(2) { 'ENNH' => int(1) 'ENT' => int(1347378030) } }
'Zend_Form_Element_Hash_Application_Form_Loginunique_csrf_token' => array(1) { 'hash' => string(32) "e3df9bf3a76cf53a804b337b8b513191" } }
这是视图源中的标记:
<input type="hidden" name="csrf_token" value="a8727d61c656c64861181c7141d766a5" id="csrf_token" />