避免 sql 注入的最佳方法是什么? [英] What is the best possible way to avoid the sql injection?

查看:52
本文介绍了避免 sql 注入的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ruby​​ 1.8.7 和 rails 2.3.2

I am using ruby 1.8.7 and rails 2.3.2

以下代码容易出现sql注入

The following code is prone to sql injection

params[:id] = "1) OR 1=1--"
User.delete_all("id = #{params[:id]}")

我的问题是通过执行以下操作将是避免 sql 注入的最佳解决方案.如果没有,那么最好的方法是什么?

My question is by doing the following will be the best solution to avoid sql injection or not. If not then what is the best way to do so?

User.delete_all("id = #{params[:id].to_i}")

推荐答案

关于:

User.where(id: params[:id]).delete_all


好的,对于 Rails 2.x 很抱歉:


Ok sorry for Rails 2.x its:

User.delete_all(["id = ?", params[:id]])

检查文档

顺便说一句,请确保您要使用 delete_all 而不是 destroy_all,前者不会触发回调.

Btw, be sure you want to use delete_all instead of destroy_all, the former doesn't trigger callbacks.

这篇关于避免 sql 注入的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆