【问题标题】:Rails 5 InvalidAuthenticityToken, but token is presentRails 5 InvalidAuthenticityToken,但存在令牌
【发布时间】:2017-11-06 11:18:26
【问题描述】:

我得到 ActionController::InvalidAuthenticityToken 保存一个简单的资源..

存在 CSRF 元标记:

<meta name="csrf-token" content="Z4fDRsIqCp4cvpCw1Dp6kN7z0uPNF5otp611C80YhUg/oB+AcUy+dz2b5qKyoMMo48LdEvbF3dcBn2d0GqeR+g==" />

以及表单的令牌字段:

<input type="hidden" name="authenticity_token" value="bIMZ5cndd/CTgOwjC3quOp02WlvWdDmhdNQCyKb920HIZz2Y8vvNDlTa7d4PKaJ5pUY6QkHKYCqiHikc2rFsyQ==" />

会话 ID 不会在请求之间保持不变,但我知道这种行为是在发生 CSRF 错误时有意为之。

表格代码:

<%= form_for document do |f| %>
  <% if document.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(document.errors.count, "error") %> prohibited this document from being saved:</h2>

      <ul>
      <% document.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

控制器中的相关操作:

  def new
    @document = Document.new
  end

  def create
    @document = Document.new(document_params)

    respond_to do |format|
      if @document.save
        format.html { redirect_to @document, notice: 'Document was successfully created.' }
        format.json { render :show, status: :created, location: @document }
      else
        format.html { render :new }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

不知道如何解决这个问题,出于显而易见的原因,我不愿意关闭 CSRF 保护。

编辑:我正在通过一个 nginx 容器作为代理,也许我的配置不正确?这是配置:

upstream backend {
    server service:3000;
}

server {
    listen 80 default_server;

    error_page 500 502 503 504 /500.html;
    error_log /dev/stdout info;
    access_log /dev/stdout;

    location / {
        proxy_pass http://backend;
    }
}

【问题讨论】:

  • 你在用设计吗?
  • 不,还没有添加任何额外的宝石
  • 用可能相关的信息更新了我的问题
  • 你能告诉我们之前的动作链吗,在 Rails 5 中 protect_from_forgery 默认为 prepend false 这意味着它不一定在依赖于真实性的之前的动作之前运行作为authenticate_user!。无论如何,请尝试将prepend: true 添加到您的protect_from_forgery 通话中。
  • Prepend: true 没有区别。

标签: ruby-on-rails ruby-on-rails-5 csrf csrf-protection


【解决方案1】:

对于在 nginx 代理后面遇到此问题的任何其他人,可以通过向 nginx 中添加代理配置来解决此问题,如 here 所述:

我错过了这些:

proxy_set_header        X-Real_IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header        X-NginX-Proxy   true;
proxy_set_header        Host            $http_host;
proxy_set_header        Upgrade         $http_upgrade;
proxy_pass_header       Set-Cookie;

【讨论】:

    【解决方案2】:

    我在这个问题上苦苦挣扎了好几天,直到我终于在我的 nginx 设置中偶然发现了这个缺失的行:

    location @puma { 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header Host $http_host; 
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto https;   # Needed to avoid 'WARNING: Can't verify CSRF token authenticity'
        proxy_pass http://puma; 
    }
    

    添加缺少的行“proxy_set_header X-Forwarded-Proto https;”后,我所有的 CSRF 令牌错误都退出了。

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 2019-06-12
      • 2017-11-13
      • 2023-03-23
      • 2016-11-14
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2017-04-12
      相关资源
      最近更新 更多