Cat*_*lin 17 forms ruby-on-rails csrf
在我的rails应用程序中,在所有页面上,在head部分中有以下2个元标记:
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="027GUZBeEkmv..." />
Run Code Online (Sandbox Code Playgroud)
在未使用部分呈现的表单上,存在隐藏authenticity_token字段
<input type="hidden" name="authenticity_token" value="D5TddQruJppDD3..." />
Run Code Online (Sandbox Code Playgroud)
但是,如果我只是加载这样的表单,这个字段会错过:
<%= render 'shared/comment_form' %>
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?我应该手动添加一个authenticity_token,如果是,我如何验证它?
编辑:
共享/ _comment_form.html.erb
<%= form_for([@post, @comment], :html => { :onsubmit => "validateCommentForm(event)" }, remote:true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Add to the article. Make it be more" %>
</div>
<%= f.submit "Save", class: "btn btn-info" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
此外,添加<input type="hidden" name="authenticity_token" id="authenticity_token" value="ANYTHING" />到该表单仍然设法发布信息并创建新记录...
Thi*_*yen 41
在您的情况下,我们有两种方法:
添加authenticity_token: true表单选项
手动将authenticity_token字段添加到表单中,如下所示:
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
好的,所以这似乎是关于远程表单,而不是通过局部加载的表单:
将config.action_view.embed_authenticity_token_in_remote_forms的默认值更改为false。此更改破坏了无需使用JavaScript也可以工作的远程表单,因此,如果需要这种行为,可以将其设置为true或在表单选项中显式传递authenticity_token:true。
在这里找到答案:https://github.com/rails/rails/issues/10608
| 归档时间: |
|
| 查看次数: |
12718 次 |
| 最近记录: |