Node.js中用escape解决sql注入

JavaScript09

Node.js中用escape解决sql注入,第1张

这时正常情况下能查询到一条数据,如果将param修改成

let param = 'ns"-- '

sql语句就会变成

select * from tb_nature where nature = "ns"-- " and del_status=1

后面的del_status就会被参数中的 -- 注释掉,失去作用,能查询到多条数据。

如果对param使用escape包装下,就能将参数中的特殊字符进行转义,防止sql的注入。

let sql = 'select * from tb_nature where nature = ' + mysql.escape(param) + ' and del_status=1'

防止sql注入攻击,在数据库方面,针对每一个表的增删改,写存储过程,程序主要靠存储过程操作数据。 在代码中,个别特殊需要数据查询的,如果不能通过存储过程,那就尽量用传参的方式,尽量不要拼接sql。 如果非要拼接,要对拼接字符串进行处理,Tools的如下字符串处理方法可以防止注入攻击: ///  /// 格式化文本(防止SQL注入) ///  ///  ///  public static string AntiSQL(string html) {     Regex regex一 = new Regex(@"<script[\s\S]+", RegexOptions.IgnoreCase)    Regex regex二 = new Regex(@" href *= *[\s\S]*script *:", RegexOptions.IgnoreCase)    Regex regex三 = new Regex(@" on[\s\S]*=", RegexOptions.IgnoreCase)    Regex regex四 = new Regex(@"<iframe[\s\S]+", RegexOptions.IgnoreCase)    Regex regex5 = new Regex(@"<frameset[\s\S]+", RegexOptions.IgnoreCase)    Regex regex一0 = new Regex(@"select", RegexOptions.IgnoreCase)    Regex regex一一 = new Regex(@"update", RegexOptions.IgnoreCase)    Regex regex一二 = new Regex(@"delete", RegexOptions.IgnoreCase)    html = regex一.Replace(html, "") //过滤标记     html = regex二.Replace(html, "") //过滤href=javascript: () 属性     html = regex三.Replace(html, " _disibledevent=") //过滤其它控件的on...事件     html = regex四.Replace(html, "") //过滤iframe     html = regex一0.Replace(html, "s_elect")    html = regex一一.Replace(html, "u_pudate")    html = regex一二.Replace(html, "d_elete")    html = html.Replace("'", "’")    html = html.Replace(" ", " ")    return html