Rails 可能 SQL 注入错误
Rails possible SQL injection error
我正在尝试编写一个基于 sql 查询的方法,这是我目前所写的内容
class Hospital
class Doctors < ActiveRecord::Base
self.table_name = 'vDoctorDetails'
def self.doctor_status(user_id)
doctor_department = ActiveRecord::Base.connection.quote('Abc')
doctor_status = ActiveRecord::Base.connection.quote('Y')
Doctors
.select('vDoctorDetails.DoctorInfo')
.where("vDoctorDetails.doctor_id = #{user_id}"}
.where("vDoctorDetails.doctor_department = #{doctor_department}"}
.where("vDoctorDetails.doctor_status = #{doctor_status}"}
.first
end
end
end
我将遵循胖模型和瘦控制器的概念,因此在模型中创建此方法。当我在控制台中测试它时它工作正常但是当我试图将它部署到 github master 分支时,brakeman pro 抛出错误
sql injection found near .select('vDoctorDetails.DoctorInfo')
.where("vDoctorDetails.doctor_id = #{user_id}"}
我尝试创建作用域,但随后我将不得不调用控制器中的所有作用域。编写此方法的最佳方式是什么,这样我就可以摆脱 sql 注入错误?
尝试:
Doctors
.select('vDoctorDetails.DoctorInfo')
.where('vDoctorDetails.doctor_id = ?', user_id)
.where('vDoctorDetails.doctor_department = ?', doctor_department)
.where('vDoctorDetails.doctor_status = ?', doctor_status)
.first
您可以像这样重写整个查询
def self.doctor_status(user_id)
where(doctor_id: user_id, doctor_department: 'Abc', doctor_status: 'Y')
.select('DoctorInfo')
.first
end
和 Rails 会负责正确引用值并添加 table 名称。
在 Rails 指南中阅读有关 query syntax and hash conditions 的内容。
我正在尝试编写一个基于 sql 查询的方法,这是我目前所写的内容
class Hospital
class Doctors < ActiveRecord::Base
self.table_name = 'vDoctorDetails'
def self.doctor_status(user_id)
doctor_department = ActiveRecord::Base.connection.quote('Abc')
doctor_status = ActiveRecord::Base.connection.quote('Y')
Doctors
.select('vDoctorDetails.DoctorInfo')
.where("vDoctorDetails.doctor_id = #{user_id}"}
.where("vDoctorDetails.doctor_department = #{doctor_department}"}
.where("vDoctorDetails.doctor_status = #{doctor_status}"}
.first
end
end
end
我将遵循胖模型和瘦控制器的概念,因此在模型中创建此方法。当我在控制台中测试它时它工作正常但是当我试图将它部署到 github master 分支时,brakeman pro 抛出错误
sql injection found near .select('vDoctorDetails.DoctorInfo')
.where("vDoctorDetails.doctor_id = #{user_id}"}
我尝试创建作用域,但随后我将不得不调用控制器中的所有作用域。编写此方法的最佳方式是什么,这样我就可以摆脱 sql 注入错误?
尝试:
Doctors
.select('vDoctorDetails.DoctorInfo')
.where('vDoctorDetails.doctor_id = ?', user_id)
.where('vDoctorDetails.doctor_department = ?', doctor_department)
.where('vDoctorDetails.doctor_status = ?', doctor_status)
.first
您可以像这样重写整个查询
def self.doctor_status(user_id)
where(doctor_id: user_id, doctor_department: 'Abc', doctor_status: 'Y')
.select('DoctorInfo')
.first
end
和 Rails 会负责正确引用值并添加 table 名称。
在 Rails 指南中阅读有关 query syntax and hash conditions 的内容。