node.js mysql防注入,防止在JavaScript / Node.js中进行SQL注入

I am using Node.js to create a Discord bot. Some of my code looks as follows:

var info = {

userid: message.author.id

}

connection.query("SELECT * FROM table WHERE userid = '" + message.author.id + "'", info, function(error) {

if (error) throw error;

});

People have said that the way I put in message.author.id is not a secure way. How can I do this? An example?

解决方案

The best way to is to use prepared statements or queries (link to documentation for NPM mysql module: https://github.com/mysqljs/mysql#preparing-queries)

var sql = "SELECT * FROM table WHERE userid = ?";

var inserts = [message.author.id];

sql = mysql.format(sql, inserts);

If prepared statements is not an option (I have no idea why it wouldn't be), a poor man's way to prevent SQL injection is to escape all user-supplied input as described here: https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#MySQL_Escaping

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值