Zend Hash 会话变量在表单提交后丢失

发布于 2024-12-20 11:05:10 字数 2085 浏览 4 评论 0原文

我有一个包含 3 个表单的页面,每个表单都有一个用于 CSRF 保护的 Zend_Form_Element_Hash 。

问题是其中两个与 CSRF 配合良好,但另一个在哈希方面存在问题。

我第一次提交时,它返回“missingToken”错误。之后,如果我尝试再次提交它,它工作正常......

我做了一个 var_dump($_SESSION) 来查看发生了什么,输出是:

在视图中(创建表单时) :

array(7) {
...
    ["__ZF"]=>
    array(3) {
        ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
        array(2) {
            ...
        }
        ["Zend_Form_Element_Hash_registration_csrf"]=>
        array(2) {
            ...
        }
        ["Zend_Form_Element_Hash_login_csrf"]=>
        array(2) {
            ...
        }
    }
    ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "2e348e982e5d8849a7bcb3f42fdd6c0d"
    }
    ["Zend_Form_Element_Hash_registration_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "6fd74223bb158cc3cc780ee29b26ae58"
    }
    ["Zend_Form_Element_Hash_login_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "d07dc1ac514082f1960c300670414399"
    }
}

提交后:

array(6) {
...
    ["__ZF"]=>
    array(2) {
        ["Zend_Form_Element_Hash_login_csrf"]=>
        array(2) {
            ...
        }
        ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
        array(2) {
            ...
        }
    }
    ["Zend_Form_Element_Hash_login_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "d07dc1ac514082f1960c300670414399"
    }
    ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "b9378bec2fd18cf232f451ed602acf0a"
    }
}

如您所见,“Zend_Form_Element_Hash_registration_csrf”已经消失...

我尝试了我所知道的一切,但没有找到什么可以使会话消失...有什么想法吗? Zend 中的什么可能导致这种情况?

顺便说一句,其中 2 个表单是由同一控制器在不同的 Action 中加载的(其中之一是有问题的表单)。难道是session消失的原因?

请帮忙,因为我不知道该怎么做才能找到问题......我被困在这里=S。

----- 编辑 ------

好的,我找到了导致此问题的原因...问题是表单有 Ajax 请求,当它们发生时,CSRF 哈希会过期(跳=1)。

有人知道使用 CSRF + AJAX 调用表单的解决方案吗?

I have a page that contains 3 forms, each one with a Zend_Form_Element_Hash for CSRF protection.

The problem is that 2 of them work well with the CSRF but, the other one have problems with the Hash.

The first time I submit it, it returns the "missingToken" error. After that, if I try to submit it again, it works fine....

I made a var_dump($_SESSION) to see what was going on and the output was:

In the View (when the form is created):

array(7) {
...
    ["__ZF"]=>
    array(3) {
        ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
        array(2) {
            ...
        }
        ["Zend_Form_Element_Hash_registration_csrf"]=>
        array(2) {
            ...
        }
        ["Zend_Form_Element_Hash_login_csrf"]=>
        array(2) {
            ...
        }
    }
    ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "2e348e982e5d8849a7bcb3f42fdd6c0d"
    }
    ["Zend_Form_Element_Hash_registration_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "6fd74223bb158cc3cc780ee29b26ae58"
    }
    ["Zend_Form_Element_Hash_login_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "d07dc1ac514082f1960c300670414399"
    }
}

After submit:

array(6) {
...
    ["__ZF"]=>
    array(2) {
        ["Zend_Form_Element_Hash_login_csrf"]=>
        array(2) {
            ...
        }
        ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
        array(2) {
            ...
        }
    }
    ["Zend_Form_Element_Hash_login_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "d07dc1ac514082f1960c300670414399"
    }
    ["Zend_Form_Element_Hash_routeSearch_csrf"]=>
    array(1) {
        ["hash"]=>
        string(32) "b9378bec2fd18cf232f451ed602acf0a"
    }
}

As you see, "Zend_Form_Element_Hash_registration_csrf" has disappeared...

I tried everything I know but didn't found what could make the session to disappear... Any ideas? What in Zend could cause that?

By the way, 2 of the forms are loaded by the same Controller in different Actions (one of them is the form with problems). Could it be the reason why the session disappears?

Please, help because I don't know anymore what to do to find the problem... I'm stuck here =S.

------ EDIT ------

Ok, I found what is causing this... The problem is that the form have Ajax requests and, when they occur, the CSRF Hash expires (Hop=1).

Anyone knows a solution to use a form with CSRF + AJAX calls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦上情书 2024-12-27 11:05:10

好的,我解决了这个问题,在显示表单的操作中添加 CSRF 元素,并在保存到数据库之前验证它的操作中添加 CSRF 元素,而不是在我的表单类上创建它

。为了防止代码重复,我创建了此类:

class Application_Model_CSRF{

    public static function createCSRF($form, $csrf_salt_name, $field_name="csrf") {
        $element=$form->createElement('hash', $field_name,array(
            'salt' => $csrf_salt_name
        ));
        //Create unique ID if you need to use some Javascript on the CSRF Element
        $element->setAttrib('id',$form->getName().'_'.$element->getId());
        $form->addElement($element);
        return $element;
    }

}

然后,在显示表单的操作和验证表单的其他操作中,我使用以下代码:

$form    = new Application_Form_Account_Registration();
Application_Model_CSRF::createCSRF($form,"registration");

Ok, I solved this adding the CSRF element in the Action that shows the form and in the Action that validates it before saving to the Database, instead of creating it on my Form Class

To prevent the repetition of code, I created this class:

class Application_Model_CSRF{

    public static function createCSRF($form, $csrf_salt_name, $field_name="csrf") {
        $element=$form->createElement('hash', $field_name,array(
            'salt' => $csrf_salt_name
        ));
        //Create unique ID if you need to use some Javascript on the CSRF Element
        $element->setAttrib('id',$form->getName().'_'.$element->getId());
        $form->addElement($element);
        return $element;
    }

}

Then, in the Action that shows the form and in the other Action where it's validated, I use this code:

$form    = new Application_Form_Account_Registration();
Application_Model_CSRF::createCSRF($form,"registration");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文