-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Go: Add database
source models for the database/sql
and database/sql/driver
packages
#18402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
egregius313
merged 8 commits into
github:main
from
egregius313:egregius313/go/mad/database/database-sql
Jan 7, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8093d57
`database/sql` and `database/sql/driver` source models
egregius313 80ad349
`database/sql` summary models for `Row` types
egregius313 e9fdc8a
database source tests
egregius313 3e65c8d
`database/sql` tests
egregius313 128c02b
`database/sql/driver` tests
egregius313 0f06ddc
Change note
egregius313 60cf1ec
Update test results
egregius313 b3d8c6b
Add error handling to test
egregius313 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
4 changes: 4 additions & 0 deletions
4
...b/change-notes/2025-01-03-database-sql-and-database-sql-driver-source-models.md
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,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* `database` local source models have been added for the `database/sql` and `database/sql/driver` packages. |
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
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
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
5 changes: 5 additions & 0 deletions
5
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/sink.go
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,5 @@ | ||
package test | ||
|
||
func sink(x ...any) {} | ||
|
||
func ignore(...any) {} |
2 changes: 2 additions & 0 deletions
2
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/source.expected
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,2 @@ | ||
testFailures | ||
invalidModelRow |
6 changes: 6 additions & 0 deletions
6
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/source.ext.yml
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,6 @@ | ||
extensions: | ||
- addsTo: | ||
pack: codeql/threat-models | ||
extensible: threatModelConfiguration | ||
data: | ||
- ["database", true, 0] |
19 changes: 19 additions & 0 deletions
19
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/source.ql
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,19 @@ | ||
import go | ||
import ModelValidation | ||
import utils.test.InlineExpectationsTest | ||
|
||
module SourceTest implements TestSig { | ||
string getARelevantTag() { result = "source" } | ||
|
||
predicate hasActualResult(Location location, string element, string tag, string value) { | ||
exists(ActiveThreatModelSource s | | ||
s.hasLocationInfo(location.getFile().getAbsolutePath(), location.getStartLine(), | ||
location.getStartColumn(), location.getEndLine(), location.getEndColumn()) and | ||
element = s.toString() and | ||
value = "" and | ||
tag = "source" | ||
) | ||
} | ||
} | ||
|
||
import MakeTest<SourceTest> |
2 changes: 2 additions & 0 deletions
2
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test.expected
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,2 @@ | ||
testFailures | ||
invalidModelRow |
7 changes: 7 additions & 0 deletions
7
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test.ext.yml
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,7 @@ | ||
extensions: | ||
|
||
- addsTo: | ||
pack: codeql/threat-models | ||
extensible: threatModelConfiguration | ||
data: | ||
- ["database", true, 0] |
15 changes: 15 additions & 0 deletions
15
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test.ql
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,15 @@ | ||
import go | ||
import semmle.go.dataflow.ExternalFlow | ||
import ModelValidation | ||
import experimental.frameworks.CleverGo | ||
import utils.test.InlineFlowTest | ||
|
||
module Config implements DataFlow::ConfigSig { | ||
predicate isSource(DataFlow::Node source) { source instanceof ActiveThreatModelSource } | ||
|
||
predicate isSink(DataFlow::Node sink) { | ||
sink.asExpr() = any(CallExpr c | c.getTarget().getName() = "sink").getAnArgument() | ||
} | ||
} | ||
|
||
import TaintFlowTest<Config> |
157 changes: 157 additions & 0 deletions
157
go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/test_database_sql.go
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,157 @@ | ||
package test | ||
|
||
import ( | ||
"database/sql" | ||
) | ||
|
||
// test querying a Conn | ||
func testConnQuery(conn *sql.Conn) { | ||
rows, err := conn.QueryContext(nil, "SELECT * FROM users") // $ source | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var id int | ||
var name string | ||
err = rows.Scan(&id, &name) | ||
if err != nil { | ||
return | ||
} | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
} | ||
|
||
row := conn.QueryRowContext(nil, "SELECT * FROM users WHERE id = 1") // $ source | ||
|
||
var id int | ||
var name string | ||
|
||
err = row.Scan(&id, &name) | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
} | ||
|
||
// test querying a DB | ||
func testDBQuery(db *sql.DB) { | ||
example, err := db.Query("SELECT * FROM users") // $ source | ||
ignore(example) | ||
|
||
rows, err := db.QueryContext(nil, "SELECT * FROM users") // $ source | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var id int | ||
var name string | ||
err = rows.Scan(&id, &name) | ||
egregius313 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
} | ||
|
||
row := db.QueryRowContext(nil, "SELECT * FROM users WHERE id = 1") // $ source | ||
|
||
var id int | ||
var name string | ||
|
||
err = row.Scan(&id, &name) | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
|
||
dog := db.QueryRow("SELECT * FROM dogs WHERE id = 1") // $ source | ||
ignore(dog) | ||
} | ||
|
||
// test querying a Stmt | ||
func testStmtQuery(stmt *sql.Stmt) { | ||
example, err := stmt.Query("SELECT * FROM users") // $ source | ||
ignore(example) | ||
|
||
rows, err := stmt.QueryContext(nil, "SELECT * FROM users") // $ source | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var id int | ||
var name string | ||
err = rows.Scan(&id, &name) | ||
egregius313 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
} | ||
|
||
row := stmt.QueryRowContext(nil, "SELECT * FROM users WHERE id = 1") // $ source | ||
|
||
var id int | ||
var name string | ||
|
||
err = row.Scan(&id, &name) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
|
||
dog := stmt.QueryRow("SELECT * FROM dogs WHERE id = 1") // $ source | ||
ignore(dog) | ||
} | ||
|
||
// test querying a Tx | ||
func testTxQuery(tx *sql.Tx) { | ||
example, err := tx.Query("SELECT * FROM users") // $ source | ||
ignore(example) | ||
|
||
rows, err := tx.QueryContext(nil, "SELECT * FROM users") // $ source | ||
if err != nil { | ||
return | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var id int | ||
var name string | ||
err = rows.Scan(&id, &name) | ||
egregius313 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
} | ||
|
||
row := tx.QueryRowContext(nil, "SELECT * FROM users WHERE id = 1") // $ source | ||
|
||
var id int | ||
var name string | ||
|
||
err = row.Scan(&id, &name) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
sink(id, name) // $ hasTaintFlow="id" hasTaintFlow="name" | ||
|
||
dog := tx.QueryRow("SELECT * FROM dogs WHERE id = 1") // $ source | ||
ignore(dog) | ||
} |
23 changes: 23 additions & 0 deletions
23
...t/library-tests/semmle/go/dataflow/flowsources/local/database/test_database_sql_driver.go
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,23 @@ | ||
package test | ||
|
||
import "database/sql/driver" | ||
|
||
func testQueryer(q driver.Queryer) { | ||
rows, err := q.Query("SELECT * FROM users", make([]driver.Value, 0)) // $ source | ||
ignore(rows, err) | ||
} | ||
|
||
func testQueryerContext(q driver.QueryerContext) { | ||
rows, err := q.QueryContext(nil, "SELECT * FROM users", make([]driver.NamedValue, 0)) // $ source | ||
ignore(rows, err) | ||
} | ||
|
||
func testStmt(stmt driver.Stmt) { | ||
rows, err := stmt.Query(make([]driver.Value, 0)) // $ source | ||
ignore(rows, err) | ||
} | ||
|
||
func testStmtContext(stmt driver.StmtQueryContext) { | ||
rows, err := stmt.QueryContext(nil, make([]driver.NamedValue, 0)) // $ source | ||
ignore(rows, err) | ||
} |
10 changes: 8 additions & 2 deletions
10
go/ql/test/query-tests/Security/CWE-078/StoredCommand.expected
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
21 changes: 14 additions & 7 deletions
21
go/ql/test/query-tests/Security/CWE-079/StoredXss.expected
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 |
---|---|---|
@@ -1,27 +1,34 @@ | ||
#select | ||
| StoredXss.go:13:21:13:36 | ...+... | StoredXss.go:13:21:13:31 | call to Name | StoredXss.go:13:21:13:36 | ...+... | Stored cross-site scripting vulnerability due to $@. | StoredXss.go:13:21:13:31 | call to Name | stored value | | ||
| stored.go:30:22:30:25 | name | stored.go:18:3:18:28 | ... := ...[0] | stored.go:30:22:30:25 | name | Stored cross-site scripting vulnerability due to $@. | stored.go:18:3:18:28 | ... := ...[0] | stored value | | ||
| stored.go:61:22:61:25 | path | stored.go:59:30:59:33 | definition of path | stored.go:61:22:61:25 | path | Stored cross-site scripting vulnerability due to $@. | stored.go:59:30:59:33 | definition of path | stored value | | ||
edges | ||
| StoredXss.go:13:21:13:31 | call to Name | StoredXss.go:13:21:13:36 | ...+... | provenance | | | ||
| stored.go:18:3:18:28 | ... := ...[0] | stored.go:25:14:25:17 | rows | provenance | | | ||
| stored.go:25:14:25:17 | rows | stored.go:25:24:25:26 | &... | provenance | FunctionModel | | ||
| stored.go:25:14:25:17 | rows | stored.go:25:29:25:33 | &... | provenance | FunctionModel | | ||
| stored.go:18:3:18:28 | ... := ...[0] | stored.go:25:14:25:17 | rows | provenance | Src:MaD:1 | | ||
| stored.go:25:14:25:17 | rows | stored.go:25:14:25:34 | []type{args} | provenance | MaD:2 | | ||
| stored.go:25:14:25:17 | rows | stored.go:25:14:25:34 | []type{args} [array] | provenance | MaD:2 | | ||
| stored.go:25:14:25:34 | []type{args} | stored.go:25:24:25:26 | &... | provenance | | | ||
| stored.go:25:14:25:34 | []type{args} | stored.go:25:29:25:33 | &... | provenance | | | ||
| stored.go:25:14:25:34 | []type{args} | stored.go:30:22:30:25 | name | provenance | | | ||
| stored.go:25:14:25:34 | []type{args} [array] | stored.go:25:24:25:26 | &... | provenance | | | ||
| stored.go:25:14:25:34 | []type{args} [array] | stored.go:25:29:25:33 | &... | provenance | | | ||
| stored.go:25:24:25:26 | &... | stored.go:25:14:25:34 | []type{args} [array] | provenance | | | ||
| stored.go:25:29:25:33 | &... | stored.go:25:14:25:34 | []type{args} [array] | provenance | | | ||
| stored.go:25:29:25:33 | &... | stored.go:30:22:30:25 | name | provenance | | | ||
| stored.go:59:30:59:33 | definition of path | stored.go:61:22:61:25 | path | provenance | | | ||
models | ||
| 1 | Source: database/sql; DB; true; Query; ; ; ReturnValue[0]; database; manual | | ||
| 2 | Summary: database/sql; Rows; true; Scan; ; ; Argument[receiver]; Argument[0].ArrayElement; taint; manual | | ||
nodes | ||
| StoredXss.go:13:21:13:31 | call to Name | semmle.label | call to Name | | ||
| StoredXss.go:13:21:13:36 | ...+... | semmle.label | ...+... | | ||
| stored.go:18:3:18:28 | ... := ...[0] | semmle.label | ... := ...[0] | | ||
| stored.go:25:14:25:17 | rows | semmle.label | rows | | ||
| stored.go:25:14:25:34 | []type{args} | semmle.label | []type{args} | | ||
| stored.go:25:14:25:34 | []type{args} [array] | semmle.label | []type{args} [array] | | ||
| stored.go:25:24:25:26 | &... | semmle.label | &... | | ||
| stored.go:25:29:25:33 | &... | semmle.label | &... | | ||
| stored.go:30:22:30:25 | name | semmle.label | name | | ||
| stored.go:59:30:59:33 | definition of path | semmle.label | definition of path | | ||
| stored.go:61:22:61:25 | path | semmle.label | path | | ||
subpaths | ||
#select | ||
| StoredXss.go:13:21:13:36 | ...+... | StoredXss.go:13:21:13:31 | call to Name | StoredXss.go:13:21:13:36 | ...+... | Stored cross-site scripting vulnerability due to $@. | StoredXss.go:13:21:13:31 | call to Name | stored value | | ||
| stored.go:30:22:30:25 | name | stored.go:18:3:18:28 | ... := ...[0] | stored.go:30:22:30:25 | name | Stored cross-site scripting vulnerability due to $@. | stored.go:18:3:18:28 | ... := ...[0] | stored value | | ||
| stored.go:61:22:61:25 | path | stored.go:59:30:59:33 | definition of path | stored.go:61:22:61:25 | path | Stored cross-site scripting vulnerability due to $@. | stored.go:59:30:59:33 | definition of path | stored value | |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.