Skip to content

Commit 03d0000

Browse files
author
Alvaro Muñoz
committed
Add support for Bun library
1 parent 656b4fc commit 03d0000

File tree

2,239 files changed

+4224993
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,239 files changed

+4224993
-0
lines changed
Lines changed: 3 additions & 0 deletions

go/ql/lib/semmle/go/frameworks/SQL.qll

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,52 @@ module Xorm {
289289
}
290290
}
291291
}
292+
293+
/**
294+
* Provides classes for working with the [Bun](https://bun.uptrace.dev/) package.
295+
*/
296+
module Bun {
297+
/** Gets the package name for Xorm. */
298+
string packagePath() { result = package("github.com/uptrace/bun", "") }
299+
300+
/** A model for sinks of XORM. */
301+
private class BunSink extends SQL::QueryString::Range {
302+
BunSink() {
303+
exists(Function f, int arg |
304+
f.(Method)
305+
.hasQualifiedName(packagePath(), ["DB", "Conn"],
306+
["ExecContext", "PrepareContext", "QueryContext", "QueryRowContext"]) and
307+
arg = 1
308+
or
309+
f.(Method)
310+
.hasQualifiedName(packagePath(), ["DB", "Conn"],
311+
["Exec", "NewRaw", "Prepare", "Query", "QueryRow", "Raw"]) and
312+
arg = 0
313+
or
314+
exists(string tp, string m | f.(Method).hasQualifiedName(packagePath(), tp, m) |
315+
tp.matches("%Query") and
316+
m =
317+
[
318+
"ColumnExpr", "DistinctOn", "For", "GroupExpr", "Having", "ModelTableExpr",
319+
"OrderExpr", "TableExpr", "Where", "WhereIn", "WhereInMulti", "WhereOr"
320+
] and
321+
arg = 0
322+
or
323+
tp.matches("%Query") and
324+
m = ["FormatQuery", "With", "WithRecursive"] and
325+
arg = 1
326+
or
327+
tp = "RawQuery" and
328+
m = ["NewRaw"] and
329+
arg = 0
330+
or
331+
tp = "RawQuery" and
332+
m = ["NewRawQuery"] and
333+
arg = 1
334+
)
335+
|
336+
this = f.getACall().getArgument(arg)
337+
)
338+
}
339+
}
340+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
| bun.go:27:10:27:18 | untrusted | github.com/uptrace/bun | DB | Exec |
2+
| bun.go:28:22:28:30 | untrusted | github.com/uptrace/bun | DB | ExecContext |
3+
| bun.go:29:26:29:34 | untrusted | github.com/uptrace/bun | DB | QueryRowContext |
4+
| bun.go:30:28:30:36 | untrusted | github.com/uptrace/bun | SelectQuery | ColumnExpr |
5+
| bun.go:30:28:30:36 | untrusted | github.com/uptrace/bun | countQuery | ColumnExpr |
6+
| bun.go:30:28:30:36 | untrusted | github.com/uptrace/bun | selectExistsQuery | ColumnExpr |
7+
| bun.go:30:28:30:36 | untrusted | github.com/uptrace/bun | selectQueryBuilder | ColumnExpr |
8+
| bun.go:30:28:30:36 | untrusted | github.com/uptrace/bun | whereExistsQuery | ColumnExpr |
9+
| bun.go:31:12:31:20 | untrusted | github.com/uptrace/bun | DB | NewRaw |
10+
| bun.go:32:23:32:31 | untrusted | github.com/uptrace/bun | DB | QueryContext |
11+
| bun.go:33:26:33:34 | untrusted | github.com/uptrace/bun | DB | QueryRowContext |
12+
| bun.go:34:14:34:22 | untrusted | github.com/uptrace/bun | DB | QueryRow |
13+
| bun.go:35:9:35:17 | untrusted | github.com/uptrace/bun | DB | Raw |
14+
| bun.go:36:11:36:19 | untrusted | github.com/uptrace/bun | DB | Query |
15+
| bun.go:37:13:37:21 | untrusted | github.com/uptrace/bun | DB | Prepare |
16+
| bun.go:38:25:38:33 | untrusted | github.com/uptrace/bun | DB | PrepareContext |
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
7+
"github.com/uptrace/bun"
8+
"github.com/uptrace/bun/dialect/sqlitedialect"
9+
"github.com/uptrace/bun/driver/sqliteshim"
10+
"github.com/uptrace/bun/extra/bundebug"
11+
)
12+
13+
func getUntrustedString() string {
14+
return "trouble"
15+
}
16+
17+
func main() {
18+
untrusted := getUntrustedString()
19+
20+
var num int
21+
ctx := context.Background()
22+
sqlite, err := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared")
23+
if err != nil {
24+
panic(err)
25+
}
26+
db := bun.NewDB(sqlite, sqlitedialect.New())
27+
db.Exec(untrusted)
28+
db.ExecContext(ctx, untrusted)
29+
db.QueryRowContext(ctx, untrusted).Scan(&num)
30+
db.NewSelect().ColumnExpr(untrusted).Exec(ctx)
31+
db.NewRaw(untrusted).Scan(ctx, &num)
32+
db.QueryContext(ctx, untrusted)
33+
db.QueryRowContext(ctx, untrusted)
34+
db.QueryRow(untrusted)
35+
db.Raw(untrusted)
36+
db.Query(untrusted)
37+
db.Prepare(untrusted)
38+
db.PrepareContext(ctx, untrusted)
39+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import go
2+
3+
from SQL::QueryString qs, Method meth, string a, string b, string c
4+
where meth.hasQualifiedName(a, b, c) and qs = meth.getACall().getSyntacticArgument(_)
5+
select qs, a, b, c

go/ql/test/library-tests/semmle/go/frameworks/SQL/bun/db/baseline-info.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
sourceLocationPrefix: /Users/pwntester/src/github.com/github/codeql/go/ql/test/library-tests/semmle/go/frameworks/SQL/bun
3+
baselineLinesOfCode: 549377
4+
unicodeNewlines: false
5+
columnKind: utf8
6+
primaryLanguage: go
7+
creationMetadata:
8+
cliVersion: 2.13.3
9+
creationTime: 2023-06-28T10:24:43.573371Z
10+
finalised: true

0 commit comments

Comments
 (0)