2016-11-21 5 views
1

私はUserFrostingで簡単なフォームを送信しようとしていますが、テストではデータの変更なしに成功メッセージのみが表示されます。私はLesson 2からのガイダンスに従ったが、私はCSRF問題に遭遇した:UserFrostingフォーム - 無効または不足しているCSRFトークン

Invalid or missing CSRF token.

は、私が何をしないのです:

UserFrostingは、次のエラーを返しますか?小枝ファイルの一番下に追加されたスクリプトの一部で

<form class="form-horizontal" role="form" name="requestDescription" action="{{site.uri.public}}/soap/requests/desc/edit/{{ keyname }}" method="post"> 
    <div class="form-group"> 
     <label for="input_group" class="col-md-2 control-label">Name</label> 
     <div class="col-md-10"> 
      <input type="text" id="input_name" class="form-control" name="lgname" placeholder="{{ name }}"> 
     </div> 
    </div> 
    <div class="form-group text-center"> 
     <button type="submit" class="btn btn-success text-center">Update</button> 
    </div> 
</form> 

:をUserFrostingこの時点までは:(

にフォームを消化するのは非常に簡単でした。ここ

<script> 
    $(document).ready(function() { 
     // Load the validator rules for this form 
     var validators = {{validators | raw}}; 
     ufFormSubmit(
       $("form[name='requestDescription']"), 
       validators, 
       $("#userfrosting-alerts"), 
       function(data, statusText, jqXHR) { 
        // Reload the page on success 
        window.location.reload(true); 
       } 
     ); 
    }); 
</script> 

は私ですコントローラからの2つの機能:

public function soapRequestDescriptionEditPage($keyname){ 
    if (!$this->_app->user->checkAccess('uri_soap_requests')){ 
     $this->_app->notFound(); 
    } 
    $requestDetails = $this->soapRequestReadMeta($keyname); 

    $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/soap-request-description-edit.json"); 
    $this->_app->jsValidator->setSchema($schema); 

    $this->_app->render('soap/soap-request-description-edit.twig', [ 
     "name" => $requestDetails['name'], 
     "description" => $requestDetails['description'], 
     "keyname" => $keyname, 
     "validators" => $this->_app->jsValidator->rules() 
    ]); 
} 

public function test(){ 
    if (!$this->_app->user->checkAccess('uri_soap_requests')) { 
     $this->_app->notFound(); 
    } 
    $post = $this->_app->request->post(); 
    $ms = $this->_app->alerts; 
    $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/soap-request-description-edit.json"); 
    $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post); 
    $ms->addMessageTranslated("success", "Everyone's title has been updated!", $post); 
    $rf->sanitize(); 
    if (!$rf->validate()) { 
     $this->_app->halt(400); 
    } 
    $data = $rf->data(); 
} 

Entr index.phpのファイルから居住:

$app->post('/soap/requests/desc/edit/:request_id/?', function() use ($app) { 
    $controller = new UF\SoapController($app); 
    return $controller->test(); 
}); 
$app->get('/soap/requests/desc/edit/:request_id/?', function ($request_id) use ($app) { 
    $controller = new UF\SoapController($app); 
    return $controller->soapRequestDescriptionEditPage($request_id); 
}); 

最後に、スキーマ:

{ 
    "lgname" : { 
    "validators" : { 
     "length" : { 
     "min" : 1, 
     "max" : 150, 
     "message" : "The new title must be between 1 and 150 characters long." 
     } 
    }, 
    "sanitizers" : { 
     "raw" : "" 
    } 
    } 
} 
+0

あなたはすべてにCSRFトークンを送信する必要がありますが、CSRFがここhttps://www.owasp.org/index.php/あるものを見つけることができます要求Cross-Site_Request_Forgery_(CSRF) – MONTYHS

+1

を参照してください。https://github.com/userfrosting/UserFrosting/wiki/On-the-matter-このエラーの詳細については、CSRFトークンを参照してください。また、私はあなたのURLとコントローラの "石鹸"がSOAPを指しているのかどうかは分かりませんが、UserFrostingはほとんどRESTfulなアプローチを使用しています。 – alexw

答えて

0

は、それは私のコードは大丈夫だったことが判明。 UserFrostingフォーム処理に影響する、ページ上に関連のないJavaScriptエラーがありました。これらのエラーを修正することで、UserFrostingはフォームを処理できました。自己へ

注...それjavascriptのエラーをコンソールに検索する:)

0

を4 UserFrostingのとおり習慣にするには、明示的にフォームに隠されたCSRFの入力フィールドを追加する必要があります。あなたは小枝のincludeタグを使用して挿入することができ、これらのフィールドを含み、部分テンプレートforms/csrf.html.twigがあります:フォームなしで行われた要求のために

<form class="form-horizontal" role="form" name="requestDescription" action="{{site.uri.public}}/soap/requests/desc/edit/{{ keyname }}" method="post"> 
    {% include "forms/csrf.html.twig" %} 
    <div class="form-group"> 
     <label for="input_group" class="col-md-2 control-label">Name</label> 
     <div class="col-md-10"> 
      <input type="text" id="input_name" class="form-control" name="lgname" placeholder="{{ name }}"> 
     </div> 
    </div> 
    <div class="form-group text-center"> 
     <button type="submit" class="btn btn-success text-center">Update</button> 
    </div> 
</form> 

(例えば、それはJavascriptで純粋に構築されている場合)、あなたはグローバルsite.csrf変数からCSRFトークン名と値をつかむことができます。

var userName = 'luke'; 
var fieldName = 'lgname'; 

var data = { 
    'value': fieldValue 
}; 

data[site.csrf.keys.name] = site.csrf.name; 
data[site.csrf.keys.value] = site.csrf.value; 

var url = site.uri.public + '/api/users/u/' + userName + '/' + fieldName; 

return $.ajax({ 
    type: "PUT", 
    url: url, 
    data: data 
}).done(function (response) { 
    window.location.reload(); 
}); 
関連する問題