Skip to content

Commit c93fb4c

Browse files
authored
Merge pull request #18493 from egregius313/egregius313/go/mad/database/mongodb
Go: `database` local sources for MongoDB
2 parents 4ef64cd + 8aa9dd7 commit c93fb4c

File tree

6 files changed

+650
-0
lines changed

6 files changed

+650
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added `database` source models for database methods from the `go.mongodb.org/mongo-driver/mongo` package.
5+

go/ql/lib/ext/go.mongodb.org.mongo-driver.mongo.model.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
extensions:
2+
- addsTo:
3+
pack: codeql/go-all
4+
extensible: sourceModel
5+
data:
6+
- ["go.mongodb.org/mongo-driver/mongo", "Client", True, "Watch", "", "", "ReturnValue[0]", "database", "manual"]
7+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "Aggregate", "", "", "ReturnValue[0]", "database", "manual"]
8+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "Distinct", "", "", "ReturnValue[0]", "database", "manual"]
9+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "Find", "", "", "ReturnValue[0]", "database", "manual"]
10+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "FindOne", "", "", "ReturnValue", "database", "manual"]
11+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "FindOneAndDelete", "", "", "ReturnValue", "database", "manual"]
12+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "FindOneAndReplace", "", "", "ReturnValue", "database", "manual"]
13+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "FindOneAndUpdate", "", "", "ReturnValue", "database", "manual"]
14+
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "Watch", "", "", "ReturnValue[0]", "database", "manual"]
15+
- ["go.mongodb.org/mongo-driver/mongo", "Database", True, "Aggregate", "", "", "ReturnValue[0]", "database", "manual"]
16+
- ["go.mongodb.org/mongo-driver/mongo", "Database", True, "Watch", "", "", "ReturnValue[0]", "database", "manual"]
217
- addsTo:
318
pack: codeql/go-all
419
extensible: sinkModel
@@ -17,3 +32,12 @@ extensions:
1732
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "UpdateMany", "", "", "Argument[1]", "nosql-injection", "manual"]
1833
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "UpdateOne", "", "", "Argument[1]", "nosql-injection", "manual"]
1934
- ["go.mongodb.org/mongo-driver/mongo", "Collection", True, "Watch", "", "", "Argument[1]", "nosql-injection", "manual"]
35+
- addsTo:
36+
pack: codeql/go-all
37+
extensible: summaryModel
38+
data:
39+
- ["go.mongodb.org/mongo-driver/mongo", "ChangeStream", True, "Decode", "", "", "Argument[receiver]", "Argument[0]", "taint", "manual"]
40+
- ["go.mongodb.org/mongo-driver/mongo", "Cursor", True, "All", "", "", "Argument[receiver]", "Argument[1]", "taint", "manual"]
41+
- ["go.mongodb.org/mongo-driver/mongo", "Cursor", True, "Decode", "", "", "Argument[receiver]", "Argument[0]", "taint", "manual"]
42+
- ["go.mongodb.org/mongo-driver/mongo", "SingleResult", True, "Decode", "", "", "Argument[receiver]", "Argument[0]", "taint", "manual"]
43+
- ["go.mongodb.org/mongo-driver/mongo", "SingleResult", True, "Raw", "", "", "Argument[receiver]", "ReturnValue[0]", "taint", "manual"]

go/ql/test/library-tests/semmle/go/dataflow/flowsources/local/database/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.22.5
55
require (
66
gorm.io/gorm v1.23.0
77
github.com/jmoiron/sqlx v1.4.0
8+
go.mongodb.org/mongo-driver/mongo v1.17.2
89
)
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package test
2+
3+
//go:generate depstubber -vendor go.mongodb.org/mongo-driver/mongo Client,Collection,Database
4+
5+
import (
6+
"context"
7+
8+
"go.mongodb.org/mongo-driver/mongo"
9+
)
10+
11+
func test_mongo_driver_mongo_collection(coll *mongo.Collection, ctx context.Context, pipeline any) {
12+
cursor, err := coll.Aggregate(ctx, pipeline) // $ source
13+
if err != nil {
14+
return
15+
}
16+
17+
var users []User
18+
19+
err = cursor.All(ctx, &users)
20+
21+
sink(users) // $ hasTaintFlow="users"
22+
23+
distinct, err := coll.Distinct(ctx, "name", nil) // $ source
24+
if err != nil {
25+
return
26+
}
27+
28+
sink(distinct) // $ hasTaintFlow="distinct"
29+
30+
cursor2, err := coll.Find(ctx, nil) // $ source
31+
if err != nil {
32+
return
33+
}
34+
35+
sink(cursor2) // $ hasTaintFlow="cursor2"
36+
37+
var user1, user2, user3, user4 User
38+
39+
single1 := coll.FindOne(ctx, nil) // $ source
40+
if err != nil {
41+
return
42+
}
43+
44+
single1.Decode(&user1)
45+
46+
sink(user1) // $ hasTaintFlow="user1"
47+
48+
single2 := coll.FindOneAndDelete(ctx, nil) // $ source
49+
if err != nil {
50+
return
51+
}
52+
53+
single2.Decode(&user2)
54+
55+
sink(user2) // $ hasTaintFlow="user2"
56+
57+
single3 := coll.FindOneAndReplace(ctx, nil, nil) // $ source
58+
if err != nil {
59+
return
60+
}
61+
62+
single3.Decode(&user3)
63+
64+
sink(user3) // $ hasTaintFlow="user3"
65+
66+
single4 := coll.FindOneAndUpdate(ctx, nil, nil) // $ source
67+
if err != nil {
68+
return
69+
}
70+
71+
single4.Decode(&user4)
72+
73+
sink(user4) // $ hasTaintFlow="user4"
74+
75+
changeStream, err := coll.Watch(ctx, pipeline) // $ source
76+
if err != nil {
77+
return
78+
}
79+
80+
for changeStream.Next(ctx) {
81+
var userCs User
82+
changeStream.Decode(&userCs)
83+
sink(userCs) // $ hasTaintFlow="userCs"
84+
}
85+
}
86+
87+
func test_mongo_driver_mongo_database(db *mongo.Database, ctx context.Context, pipeline any) {
88+
agg, err := db.Aggregate(ctx, pipeline) // $ source
89+
90+
if err != nil {
91+
return
92+
}
93+
94+
var user User
95+
agg.Decode(&user)
96+
sink(user) // $ hasTaintFlow="user"
97+
98+
changeStream, err := db.Watch(ctx, pipeline) // $ source
99+
if err != nil {
100+
return
101+
}
102+
103+
for changeStream.Next(ctx) {
104+
var userCs User
105+
changeStream.Decode(&userCs)
106+
sink(userCs) // $ hasTaintFlow="userCs"
107+
}
108+
}
109+
110+
func test_mongo_driver_mongo_Client(client *mongo.Client, ctx context.Context) {
111+
changestream, err := client.Watch(ctx, nil) // $ source
112+
if err != nil {
113+
return
114+
}
115+
116+
for changestream.Next(ctx) {
117+
var user User
118+
changestream.Decode(&user)
119+
sink(user) // $ hasTaintFlow="user"
120+
}
121+
}

0 commit comments

Comments
 (0)