在使用“database/sql"时如何防止 Go 中的 SQL 注入攻击? [英] How can I prevent SQL injection attacks in Go while using "database/sql"?

查看:30
本文介绍了在使用“database/sql"时如何防止 Go 中的 SQL 注入攻击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建我的第一个 web 应用程序并希望更好地理解 SQL 注入 (https://github.com/astaxie/build-web-application-with-golang/blob/master/en/eBook/09.4.md).

Building my first web-app and want to understand SQL injection better (https://github.com/astaxie/build-web-application-with-golang/blob/master/en/eBook/09.4.md).

仅仅总是使用database/sql"库并使用?"构造查询,我可以获得多少针对 SQL 注入的保护?而不是连接字符串?在这种情况下,我还需要担心什么样的 SQL 注入攻击?

How much protection against SQL injection do I get from just always using the 'database/sql' library and constructing queries using '?' instead of concatting strings? What kind of SQL injection attacks will I still have to worry about in that case?

推荐答案

只要你使用 准备查询,你很安全.

As long as you're using Prepare or Query, you're safe.

// this is safe
db.Query("SELECT name FROM users WHERE age=?", req.FormValue("age"))
// this allows sql injection.
db.Query("SELECT name FROM users WHERE age=" + req.FormValue("age"))

这篇关于在使用“database/sql"时如何防止 Go 中的 SQL 注入攻击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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