Ruby on rails 这是Rails中ActiveRecords的安全方法吗?

Ruby on rails 这是Rails中ActiveRecords的安全方法吗?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我使用以下信息让我的客户从我的邮件列表中退订 def index @user = User.find_by_salt(params[:subscribe_code]) if @user.nil? flash[:notice] = "the link is not valid...." render :action => 'index' else Notification.delete_all(:user_id =&

我使用以下信息让我的客户从我的邮件列表中退订

  def index
    @user = User.find_by_salt(params[:subscribe_code]) 
    if @user.nil? 
      flash[:notice] = "the link is not valid...."
      render :action => 'index'
    else    
      Notification.delete_all(:user_id => @user.id)
      flash[:notice] = "you have been unsubscribed....."
      redirect_to :controller => 'home'
    end 
  end 
我的链接看起来像;

因此,上面将随机字符串与我的用户表中的字段进行比较,并相应地从通知表中删除数据

我的问题;这种方法安全吗?有没有更好/更有效的方法

欢迎所有建议。

我认为这样更安全:

@user = User.find :all, :conditions => { salt => params[:subscribe_code] }

这样,您就可以确保Rails知道参数[:subscribe_code]将被转义。不过,可能有很多方法可以编写它。

如果操作不需要身份验证,我看不出您的解决方案有任何错误

如果操作需要身份验证,那么我将确保
salt
属于当前用户

@user = User.find_by_id_and_salt(current_user.id, params[:subscribe_code])
你的链接应该是

否则,“32hj5h2j33j3h333”将作为参数[:id]获取


其他的都可以。假设订阅代码是唯一的。

如果用户的取消订阅链接出错,通知用户是否有那么重要?发生这种情况的可能性有多大?它不是以编程方式生成的,并且该程序是经过测试的吗?如果答案是“是”(提示:应该是),那么我的建议是,不管发生了什么,总是告诉用户他们已经取消订阅

def index
  begin
    @user = User.find_by_salt!(params[:subscribe_code]) 
  rescue ActiveRecord::RecordNotFound
  ensure
    @user.notifications.delete_all if @user
    flash[:notice] = "You have been unsubscribed."
    redirect_to :action => "index"
  end
end

User.find\u by\u salt
调用将转义字符串。所以没有区别。我有一种模糊的感觉,可能是这样的。很高兴知道。谢谢不是真的,他可能有
map.unsubscribe'unsubscribe/:subscribe\u code',:controller=>“where”,:action=>“ever”
。是的,Ryan这就是我拥有的。谢谢,没有身份验证。我只是想确定它是好的。谢谢大家。