Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset golang/GO-GORM-SQLI-001 --project .About This Rule
Understanding the vulnerability and how it is detected
GORM is the most widely used Go ORM library. Its query builder methods (Where, Find, First, Create, etc.) automatically parameterize struct conditions — those are generally safe. However, GORM provides Raw() and Exec() methods for executing arbitrary SQL, and these are vulnerable to injection when user input is concatenated into the query string rather than passed as parameterized arguments.
GORM's parameterization syntax uses `?` positional placeholders for MySQL/SQLite and `@name` named parameters for all databases. The critical mistake is using fmt.Sprintf or string concatenation to build the query string passed to Raw()/Exec().
**CVE-2024-37896** (gin-vue-admin, CVSS 9.8 Critical): The most prominent recent Go SQL injection CVE involved a GORM-based application where query parameters were passed to GORM methods without sanitization, enabling authenticated attackers to perform full SQL injection. gin-vue-admin has 20,000+ GitHub stars and is used in many production administrative systems.
**GORM Security documentation** explicitly warns: "Raw SQL user inputs should be sanitized to prevent SQL injection." The GORM docs list known unsafe patterns, including passing user input directly to Raw(), Exec(), Order(), Group(), and Having().
**The false sense of security problem**: Developers using GORM often assume the ORM prevents SQL injection everywhere. The Raw/Exec escape hatch is frequently added for complex queries and is not protected by GORM's builder safety.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Bypasses ORM Safety Assumptions
GORM's structured query methods (Where with struct, Find, First) are parameterized automatically. The Raw/Exec pattern breaks this guarantee. Developers who trust GORM for safety may not inspect Raw/Exec calls during security review.
Full Database Exfiltration via UNION
UNION SELECT injection against a GORM Raw() call can dump arbitrary tables. GORM automatically scans UNION results into the destination struct — making exfiltrated data immediately available to the attacker in the response.
Blind Injection via Scan()
Even when Raw().Scan() appears to limit output to the expected struct fields, blind time-based injection (pg_sleep, SLEEP, WAITFOR) enables data exfiltration via timing channels.
How to Fix
Recommended remediation steps
- 1Use ? positional placeholders in GORM Raw() and Exec(): db.Raw("WHERE id = ?", id).
- 2Use @name named parameters for complex queries with multiple reused values.
- 3Prefer GORM's struct-based query builder (Where, Find, First, Create) over Raw/Exec.
- 4Validate dynamic column/table names against an explicit allowlist before use.
- 5Enable GORM's Logger in development to review generated SQL for unexpected patterns.
- 6See GORM security documentation for the authoritative list of unsafe patterns.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
Tracks taint from HTTP framework sources (net/http, gin, echo, fiber) to gorm.DB Raw() and Exec() methods with global inter-procedural scope.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Go
SQL Injection via GORM Query Builder Methods
User-controlled input flows into GORM query builder methods (Order, Where, Group, Having) that accept raw SQL string fragments — GORM does not escape these clause arguments.
SQL Injection via database/sql
User-controlled input reaches database/sql query methods without parameterization, enabling SQL injection — ranked
SQL Injection via sqlx
User-controlled input reaches sqlx query methods without parameterization — sqlx's convenience wrappers (Get, Select, NamedExec) are also vulnerable when used with raw string concatenation.
Frequently Asked Questions
Common questions about SQL Injection via GORM Raw/Exec
New feature
Get these findings posted directly on your GitHub pull requests
The SQL Injection via GORM Raw/Exec rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.