这是我的代码:
type mysqlRepository struct {
Conn *sql.DB
}
func (dbconn *mysqlRepository) GetAll(param map[string]string) (response []models.Subject, err error) {
var result models.Subject
c := 0
q := `
SELECT id, name, teacher, uuid
FROM subject
`
for i, x := range param {
if x != "" {
if c > 0 {
q += ` AND ` + i + ` = ` + x
} else {
q += ` WHERE ` + i + ` = ` + x
}
c++
}
}
query, err := dbconn.Conn.Query(q)
if err != nil {
utils.QueryErrorException(err)
return
}
defer query.Close()
for query.Next() {
errorScanningDataHistory := query.Scan(
&result.ID,
&result.Name,
&result.Teacher,
&result.UUID,
)
utils.QueryErrorException(errorScanningDataHistory)
response = append(response, result)
}
return
}
我尝试像这样使用邮递员并运行良好:http://localhost/public/api/v1/subject?name=robert。它只显示罗伯特作为老师的主题
但是,如果我注入sql命令,它也可以工作:http://localhost/public/api/v1/subject?name=robert OR 1 = 1。但是,它返回所有数据。
如何提高安全性?
梦里花落0921
相关分类