GORM использует плейсхолдеры аргументов database/sql
для построения запросов SQL, которые автоматически экранирует аргументы во избежание инъекции SQL
NOTE The SQL from Logger is not fully escaped like the one executed, be careful when copying and executing it in SQL console
Условие запроса
User’s input should be only used as an argument, for example:
userInput := "jinzhu;drop table users;" |
Строчные условия
// will be escaped |
When retrieving objects with number primary key by user’s input, you should check the type of variable.
userInputID := "1=1;drop table users;" |
Методы SQL инъекции
To support some features, some inputs are not escaped, be careful when using user’s input with those methods
db.Select("name; drop table users;").First(&user) |
The general rule to avoid SQL injection is don’t trust user-submitted data, you can perform whitelist validation to test user input against an existing set of known, approved, and defined input, and when using user’s input, only use them as an argument.