繁体   English   中英

生产中的CSRF错误:ActionController :: InvalidCrossOriginRequest

[英]CSRF Error in Production: ActionController::InvalidCrossOriginRequest

我仅在生产环境中的注释控制器上收到以下错误(在开发中工作正常)。 用户流程如下:我有一个jQuery,当按下特定按钮时该jQuery将运行,该按钮呈现部分文件以添加新注释(一种简单形式)。 控制器中.js请求的response_to方法用于new.js.erb文件。 这应该相对容易做到,但是Rails(我正在使用Rails 4.1.1)代码或我的服务器(Rackspace Cloud Server)上出现了问题。 错误如下:

CommentsController#new中的ActionController :: InvalidCrossOriginRequest安全警告:另一个网站上的嵌入式标记请求受保护的JavaScript。 如果您知道自己在做什么,请继续对此操作禁用伪造保护,以允许跨域JavaScript嵌入。

我已经在我的注释控制器中尝试了以下代码(不起作用)。 它只是在浏览器中将.js文件呈现为文本字符串(javascript不起作用)。

protect_from_forgery except: :new
skip_before_action :verify_authenticity_token

我尝试用application_controller.rb文件中的:: exception方法删除protect_from_forgery,但是它不起作用(只是将JavaScript在浏览器中呈现为文本字符串)。

我试过用“ protect_from_forgery with::null_session”替换“ protect:from :: exception”,但这也不起作用(给出与上面相同的InvalidCrossOriginRequest错误)。

我没有办法解决此问题。 同样,它仅在生产中发生。 在我的本地计算机上(通过localhost),一切正常。 我的评论控制器的代码如下:

  class CommentsController < ApplicationController
  # before_action :set_comment, only: [:show, :edit, :update, :destroy]
  before_action :load_topic
  before_action :authenticate_user!
  # protect_from_forgery except: :new
  # skip_before_action :verify_authenticity_token

  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
  end

  # GET /comments/new
  def new
    @comment = Comment.new
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = @topic.comments.new(comment_params)
    @comment.user_id = current_user.id
    respond_to do |format|
      if @comment.save
        format.html { redirect_to @topic, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
        format.js
      else
        format.html { redirect_to @article, alert: 'Unable to add comment' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
        format.js { render 'fail_create.js.erb'}
      end
    end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
        format.json { render :show, status: :ok, location: @comment }
      else
        format.html { render :edit }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment = @topic.comments.find(params[:id])
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to @topic, notice: 'Comment was successfully deleted.' }
      format.json { head :no_content }
      format.js
    end
  end

  private

    def load_topic
      @topic = Topic.find(params[:topic_id])
    end

    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:topic_id, :body, :name)
    end
end

任何有关解决此问题的建议将不胜感激。

这是否真的发生在真实用户上,还是您仅在日志/监控中看到此错误?

当抓取工具访问您的网站时,通常会发生此错误(在您的开发环境中显然不会发生)。

文档建议您将这些添加到控制器操作中:

skip_before_action :verify_authenticity_token, if: :json_request?

protected

def json_request?
  request.format.json?
end

但是,如果不是这种情况,我认为您实际上有一个CORS问题。 2个可能的原因:

  • 您的网站可以通过HTTP和HTTPS访问吗? 这些是不同的起源!
  • 您有多个域运行此站点吗? 尝试检查/记录请求标头,以查看起源之间是否存在任何差异。

如果您编辑主机文件并将域指向本地服务器,则也可以尝试在开发中重现此内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM