Ruby on rails Rails:Sql注入问题?
我的控制器通过以下方式查找用户:Ruby on rails Rails:Sql注入问题?,ruby-on-rails,ruby-on-rails-3,sql-injection,Ruby On Rails,Ruby On Rails 3,Sql Injection,我的控制器通过以下方式查找用户: @user = User.find_by_identifier!(params[:id]) 在我的用户模型中,我有 class User < ActiveRecord::Base def to_param identifier end private def create_identifier SecureRandom.urlsafe_base64(9) end end class用户{:
@user = User.find_by_identifier!(params[:id])
在我的用户模型中,我有
class User < ActiveRecord::Base
def to_param
identifier
end
private
def create_identifier
SecureRandom.urlsafe_base64(9)
end
end
class用户
问:从SQL注入点来看,这安全吗?这是怎么回事,因为尽管我读了很多文章,但我对SQL注入一无所知。在我自己的控制台中进行的一个快速实验表明,
find\u by\u identifier代码>对SQL注入是安全的
irb(main):005:0> User.find_by_email! "i am sneaky '; drop table woot;"
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'derp ''; drop table woot;' LIMIT 1
请注意,生成的SQL查询如何逃避恶意的单引号
我认为模型中的to_参数
和创建标识符
是不相关的。这是有道理的。我还有另一个sql查询,它在用户模型中使用where条件。我在索引操作中使用它从与当前用户相同的城市获取用户:scope:same_city,lambda{{user}连接(:profile)。其中(:profiles=>{:city=>user.profile.city})
这会是一种威胁吗?对不起,我是一个彻头彻尾的noob(尽管我在这个网站上获得了所有的分数和徽章),这一点在