我正在开发一个 Rails 项目并使用 Brakeman 作为调试工具。我使用查询从表中获取数据,但在 Brakeman 的测试期间,它指出查询中存在 Sql Injection Possibility。
这是我的查询:
Applicant.all.where("profile_id=#{current_user.profile.id}").first
但我不知道这个查询有什么问题,如果它不安全,那么如何防止它被 SQL 注入?
我正在开发一个 Rails 项目并使用 Brakeman 作为调试工具。我使用查询从表中获取数据,但在 Brakeman 的测试期间,它指出查询中存在 Sql Injection Possibility。
这是我的查询:
Applicant.all.where("profile_id=#{current_user.profile.id}").first
但我不知道这个查询有什么问题,如果它不安全,那么如何防止它被 SQL 注入?
根据rails guide正确的方式使用它来做到这一点
Applicant.where('profile_id = ?', current_user.profile.id).first
OR
Applicant.where(profile_id: current_user.profile.id).first
OR
Applicant.find_by_profile_id(current_user.profile.id)
OR
Applicant.find_by(profile_id: current_user.profile.id)
我认为您正在寻找的东西是:
Applicant.find_by(profile_id: current_user.profile.id)
当您阅读此代码时,更容易理解您在做什么。