-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18402 from egregius313/egregius313/go/mad/databas…
…e/database-sql Go: Add `database` source models for the `database/sql` and `database/sql/driver` packages
- Loading branch information
Showing
15 changed files
with
291 additions
and
29 deletions.
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 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 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 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 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 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 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 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 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 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 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 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 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) | ||
|
||
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) | ||
|
||
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) | ||
|
||
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 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 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 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 | |