Golang中的查询字符串安全吗?

huangapple go评论70阅读模式
英文:

Are query strings in golang safe?

问题

考虑以下对传递在URL上的URL参数userId的获取:

userId := http.Request.URL.Query().Get("userId")

这样使用是否安全(已经转义并准备好用于数据库调用),还是在使用之前需要对其进行转义/清理?

英文:

Consider the following fetch of the URLParam userId passed on a URL:

userId := http.Request.URL.Query().Get("userId")

Is this safe (escaped and ready to be used in a db call) as it is or do I need to escape it /sanitize it before use?

答案1

得分: 8

这不是数据库安全的做法,你应该在将任何内容放入数据库之前使用数据库驱动程序的转义功能。

你应该使用像sql.DB.Query()这样的函数,它允许你传递参数并正确转义它们。http://golang.org/pkg/database/sql/#DB.Query

例如:

userId := http.Request.URL.Query().Get("userId")

rows, err := db.Query("SELECT * FROM users WHERE id=?", userId)
英文:

This is not db-safe, and you should use the database driver's escaping before putting anything in it.

You should use functions like sql.DB.Query() that let you pass arguments and properly escape them. http://golang.org/pkg/database/sql/#DB.Query

e.g.

userId := http.Request.URL.Query().Get("userId")

rows, err := db.Query("SELECT * FROM users WHERE id=?", userId)

huangapple
  • 本文由 发表于 2014年8月22日 22:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/25448954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定