17

Zend_Form_Element_Hashをフォームの複数チェックボックスフォームに含めました。チェックボックスがクリックされたときにAJAXリクエストを実行するようにjQueryを設定しました。このAJAXリクエストでトークンを渡します。最初のAJAXリクエストはうまく機能しますが、後続のリクエストは失敗します。

トークンが検証されると、セッションから削除される可能性があると思います(ホップ= 1)。

Zend Framework Hashでフォームを保護しながら、AJAXを使用してこれらのリクエストの一部を完了するための攻撃の計画は何ですか?

4

6 に答える 6

10

私はついにZend_Form_Element_Hashの使用をやめ、トークンを手動で作成し、Zend_Sessionに登録して、送信時にチェックしました。

form.php

$myNamespace = new Zend_Session_Namespace('authtoken');
$myNamespace->setExpirationSeconds(900);
$myNamespace->authtoken = $hash = md5(uniqid(rand(),1));
$auth = new Zend_Form_Element_Hidden('authtoken');
$auth->setValue($hash)
     ->setRequired('true')
     ->removeDecorator('HtmlTag')
     ->removeDecorator('Label');    

controller.php

$mysession = new Zend_Session_Namespace('authtoken');
$hash = $mysession->authtoken;
if($hash == $data['authtoken']){
    print "success";
} else {
    print "you fail";
}

これは機能しているように見えますが、それでも物事を比較的健全で安全に保ちます。それでもHash要素を使用したいのですが、AJAXで機能させることができないようです。

皆さんありがとう。

于 2010-03-30T03:52:06.120 に答える
8

これが、ajax形式のハッシュフィールドを処理する方法です。

class AuthController extends Zend_Controller_Action
{
    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('index', 'json')
                      ->initContext();
    }

    public function loginAction()
    {
        $form = new Application_Form_Login();
        $request = $this->getRequest();

        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                // some code ..
            } else {
                // some code ..

                // Regenerate the hash and assign to the view
                $reservationForm->hash->initCsrfToken();
                $this->view->hash = $reservationForm->hash->getValue();
            }
        }
        $this->view->form = $form;
    }
}

そして、ビュースクリプトで..

<? $this->dojo()->enable()
                ->requireModule('dojox.json.query')
                ->onLoadCaptureStart() ?>
function() {
    var form = dojo.byId("login_form")
    dojo.connect(form, "onsubmit", function(event) {
        dojo.stopEvent(event);

        var xhrArgs = {
            form: this,
            handleAs: "json",
            load: function(data) {
                // assign the new hash to the field
                dojo.byId("hash").value = dojox.json.query("$.hash", data);

                // some code ..
            },
            error: function(error) {
                // some code ..
            }
        }
        var deferred = dojo.xhrPost(xhrArgs);
    });
}
<? $this->dojo()->onLoadCaptureEnd() ?>

手遅れではないことを願っています:D

于 2011-03-11T01:21:31.807 に答える
4

解決策があります:

データを含むフォームに加えて、要素のないフォームを作成します。コントローラから、2つのフォームをインスタンス化します。また、コントローラーで、要素ハッシュを空のフォームに追加します。両方のフォームをビジョンに送信する必要があります。次に、コントローラーの「if($ request-> isXmlHttpRequest())」条件で、空のフォームをレンダリングします。次に、メソッド「getValue()」を使用してハッシュ値を取得します。この値は、Ajaxによって応答として送信され、JavaScriptを使用して、すでに廃止されているハッシュ値を置き換える必要があります。ハッシュ用の空のフォームを作成するオプションは、フォームがレンダリングされた場合にIDが再度生成され、新しい情報を置き換える必要があるキャプチャなどの他の要素の問題を回避することです。2つの異なる形式があるため、検証は別々に行われます。後で、いつでもハッシュ(空)フォームを再利用できます。以下はコードの例です。

//In the controller, after instantiating the empty form you add the Hash element to it:
$hash = new Zend_Form_Element_Hash('no_csrf_foo');
$hash_form->addElement('hash', 'no_csrf_foo', array('salt' => 'unique'));

 //...

//Also in the controller, within the condition "if ($request->isXmlHttpRequest())" you render the form (this will renew the session for the next attempt to send the form) and get the new id value:
$hash_form->render($this->view);
$hash_value['hash'] = $hash_form->getElement('no_csrf_foo')->getValue();//The value must be added to the ajax response in JSON, for example. One can use the methods Zend_Json::decode($response) and Zend_Json::encode($array) for conversions between PHP array and JSON.

//---------------------------------------

//In JavaScript, the Ajax response function:
document.getElementById("no_csrf_foo").value = data.hash;//Retrieves the hash value from the Json response and set it to the hash input.

レオ

于 2010-04-01T04:30:12.847 に答える
2

フォームハッシュは原則として優れており、実際には少し悪夢です。これを処理する最善の方法は、リクエストを行ったときに応答とともに新しいハッシュを返し、フォームのマークアップを更新するか、必要に応じてJavaScriptのメモリに保存することだと思います。

新しいハッシュはフォームオブジェクトから利用できる場合があります。または、セッションから読み取ることができます。

于 2010-03-21T15:51:52.807 に答える
1

あなたはあなたの質問で正しい答えをほのめかしました:ホップ数を増やしてください。

オンラインのZFマニュアルにこれについて具体的な言及がありましたが、彼らはマニュアルを更新し、今ではそれを見つけることができません(にやにや笑い)-そうでなければ、私はあなたのためにリンクを投稿したでしょう。

于 2010-03-25T07:09:03.417 に答える
0

ajax側でフォームバリデーターを使用する場合は、次のコードを使用します。

Myform.php

class Application_Form_Myform extends Zend_Form
{ 
    # init function & ... 
    public function generateform($nohash = false)
    { 
        # Some elements
        if(!$nohash)
        {
           $temp_csrf = new Zend_Session_Namespace('temp_csrf'); 
           $my_hash = new Zend_Form_Element_Hash ( 'my_hash' );
           $this->addElement ( $my_hash , 'my_hash');  
           $temp_csrf->hash = $my_hash->getHash();
        }
        # Some other elements
    }
}

AjaxController.php

class AjaxController extends Zend_Controller_Action
{ 
    // init ... 
    public function validateAction()
    { 
         # ... 
         $temp_csrf = new Zend_Session_Namespace('temp_csrf');
         if($temp_csrf->hash == $params['received_hash_from_client'])
         {
             $Myform     = new Application_Form_Myform(); 
             $Myform->generateform(true);
             if($AF_Bill->isValid($params))
             {
                 # Form data is valid
             }else{
                 # Form invalid
             }
         }else{
             # Received hash from client is not valid
         }
         # ... 
    }
}
于 2014-03-16T08:59:34.533 に答える