flawopen.com/Go में SQL injection/GORM Raw SQL और SQL injection
क्या GORM Raw() SQL injection से सुरक्षित है? Go में parameter binding
Placeholder को value के लिए बंद लिफाफा समझें: database driver value को SQL निर्देशों से अलग भेजता है। यदि placeholder query में रहे और value अलग argument के रूप में दी जाए तो GORM Raw() उसे सुरक्षित रूप से bind कर सकता है। User input को SQL string में format करने से लिफाफा निर्देशों में मिल जाता है और query बदल सकती है।
// उपयोगकर्ता-नियंत्रित पाठ SQL syntax बन जाता है db.Raw(fmt.Sprintf( "SELECT * FROM users WHERE id = %s", userID, )).Scan(&user)
// Driver userID को value के रूप में bind करता है db.Raw( "SELECT * FROM users WHERE id = ?", userID, ).Scan(&user)
GORM Raw SQL और parameter binding के सुरक्षित नियम
जब placeholders SQL में रहें और arguments अलग दिए जाएँ, GORM और Go का database/sql values को bind करते हैं, जैसे db.Where("name = ?", userInput) या db.Raw("SELECT ... WHERE id = ?", userID)। Placeholder value दर्शाता है, SQL syntax नहीं: table name, column name, sort direction और operators को value bind करके सुरक्षित नहीं किया जा सकता। Dynamic identifiers को fixed allowlist से चुनें। Order, Select, Table और Raw जैसे methods में user input SQL fragments से जोड़ने पर injection संभव है।
- ✓SQL structure स्थिर रखें और हर user-controlled value को अलग placeholder argument के रूप में दें।
- ✓Dynamic table, column और sort names को application द्वारा तय fixed allowlist से चुनें।
- ✓
Raw,Exec,Order,SelectऔरTableजैसे SQL-fragment methods में concatenation या formatting जाँचें। - ✓Query से पहले numeric identifiers को अपेक्षित Go type में बदलें और conversion errors संभालें।
Codebase कैसे जाँचें
rg -n --glob '*.go' '\.(Raw|Exec|Order|Select|Table)\s*\(' .
rg -n --glob '*.go' 'fmt\.(Sprintf|Sprint)' .GORM Raw SQL और parameter binding के प्रश्न
नहीं। Where("name = ?", input) जैसी conditions, जिनमें values अलग दी जाती हैं, parameter binding करती हैं। Input को condition string में जोड़ना या raw SQL syntax के रूप में देना अब भी query injection कर सकता है। कुछ methods unescaped identifiers या SQL fragments लेते हैं; arguments जाँचें और dynamic structure को allowlist से सीमित करें।
नहीं। Placeholders data values दर्शाते हैं। Dynamic identifier या sort direction के लिए application-defined fixed options चुनें और SQL structure में केवल validated constant डालें।
आधिकारिक स्रोत और संदर्भ
- GORM दस्तावेज़ — SQL injection वाले methods
- GORM दस्तावेज़ — Raw SQL और parameterized queries
- flawopen.com — Go में SQL injection