This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Add documentation for rule G201 #12
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| --- | ||
| id: g201 | ||
| title: G201: SQL query construction using format string | ||
| --- | ||
|
|
||
| [SQL injection](https://en.wikipedia.org/wiki/SQL_injection) is one of the top security issues developers make and the consequences of this can be severe. | ||
| Using the format string function in the fmt Golang package to dynamically create an SQL query can easily create a possibility for SQL injection. The reason is that the format string function doesn't escape special characters like ' and it's easy to add second SQL command in the format string. | ||
|
|
||
| ## Example problematic code: | ||
|
|
||
| ``` | ||
| package main | ||
| import ( | ||
| "database/sql" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
| func main(){ | ||
| db, err := sql.Open("sqlite3", ":memory:") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| q := fmt.Sprintf("SELECT * FROM foo where name = '%s'", os.Args[1]) | ||
| rows, err := db.Query(q) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer rows.Close() | ||
| } | ||
| ``` | ||
|
|
||
| ## Gosec command line output | ||
|
|
||
| ``` | ||
| [examples/main.go:14] - G201: SQL string formatting (Confidence: HIGH, Severity: MEDIUM) | ||
| > fmt.Sprintf("SELECT * FROM foo where name = '%s'", os.Args[1]) | ||
| ``` | ||
|
|
||
| ## The right way | ||
|
|
||
| Two of the ways to escape SQL injection when using Golang are: | ||
|
|
||
| 1) use static queries | ||
|
|
||
| ``` | ||
| package main | ||
| import ( | ||
| "database/sql" | ||
| ) | ||
| const staticQuery = "SELECT * FROM foo WHERE age < 32" | ||
| func main(){ | ||
| db, err := sql.Open("sqlite3", ":memory:") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| rows, err := db.Query(staticQuery) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer rows.Close() | ||
| } | ||
| ``` | ||
|
|
||
| 2) use the database/sql | ||
| By using the database/sql package along with argument placeholders you are able to construct SQL statements that are automatically escaped properly. | ||
| The key distinction here is that you aren’t trying to construct the SQL statement yourself, but instead you are providing arguments that can be easily escaped. The underlying driver for database/sql will ultimately be aware of what special characters it needs to handle and will escape them for you, preventing any nefarious SQL from running. | ||
|
|
||
|
|
||
| ``` | ||
| package main | ||
| import ( | ||
| "database/sql" | ||
| "bufio" | ||
|
|
||
| ) | ||
| func main(){ | ||
| db, err := sql.Open("sqlite3", ":memory:") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| in := bufio.NewReader(os.Stdin) | ||
| name, err := in.ReadString('\n') | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| rows, err := db.Query("SELECT * FROM foo WHERE name = ?", name) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer rows.Close() | ||
| } | ||
| ``` | ||
|
|
||
| It is highly recommended to use the database/sql package in Golang instead of fmt package for SQL queries. | ||
|
|
||
| ## See also | ||
|
|
||
| * https://www.calhoun.io/what-is-sql-injection-and-how-do-i-avoid-it-in-go/ | ||
| * https://astaxie.gitbooks.io/build-web-application-with-golang/en/09.4.html | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ | |
| "rules/g102", | ||
| "rules/g103", | ||
| "rules/g104", | ||
| "rules/g107" | ||
| "rules/g107", | ||
| "rules/g201" | ||
| ] | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can also be safe to use a prepared SQL query, or using parametrised queries like
db.Query("SELECT * FROM foo WHERE name = ?", name)in addition to static queries.