Skip to content

Commit 8e85f4d

Browse files
authored
Merge pull request github#13601 from pwntester/ruby/add_bun_support
Go: Add support for Bun library
2 parents 84bfd10 + b380853 commit 8e85f4d

File tree

11 files changed

+3038
-1
lines changed

11 files changed

+3038
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lgtm,codescanning
2+
* Support for the [Bun framework](https://bun.uptrace.dev/) has been added.
3+

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,47 @@ 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 Bun package. */
298+
private string packagePath() { result = package("github.com/uptrace/bun", "") }
299+
300+
/** A model for sinks of Bun. */
301+
private class BunSink extends SQL::QueryString::Range {
302+
BunSink() {
303+
exists(Function f, string m, int arg | this = f.getACall().getArgument(arg) |
304+
f.hasQualifiedName(packagePath(), m) and
305+
m = "NewRawQuery" and
306+
arg = 1
307+
)
308+
or
309+
exists(Method f, string tp, string m, int arg | this = f.getACall().getArgument(arg) |
310+
f.hasQualifiedName(packagePath(), tp, m) and
311+
(
312+
tp = ["DB", "Conn"] and
313+
m = ["ExecContext", "PrepareContext", "QueryContext", "QueryRowContext"] and
314+
arg = 1
315+
or
316+
tp = ["DB", "Conn"] and
317+
m = ["Exec", "NewRaw", "Prepare", "Query", "QueryRow", "Raw"] and
318+
arg = 0
319+
or
320+
tp.matches("%Query") and
321+
m =
322+
[
323+
"ColumnExpr", "DistinctOn", "For", "GroupExpr", "Having", "ModelTableExpr",
324+
"OrderExpr", "TableExpr", "Where", "WhereOr"
325+
] and
326+
arg = 0
327+
or
328+
tp = "RawQuery" and
329+
m = "NewRaw" and
330+
arg = 0
331+
)
332+
)
333+
}
334+
}
335+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
| bun.go:25:22:25:30 | untrusted | github.com/uptrace/bun | NewRawQuery |
2+
| bun.go:27:22:27:30 | untrusted | github.com/uptrace/bun.DB | ExecContext |
3+
| bun.go:28:25:28:33 | untrusted | github.com/uptrace/bun.DB | PrepareContext |
4+
| bun.go:29:23:29:31 | untrusted | github.com/uptrace/bun.DB | QueryContext |
5+
| bun.go:30:26:30:34 | untrusted | github.com/uptrace/bun.DB | QueryRowContext |
6+
| bun.go:32:10:32:18 | untrusted | github.com/uptrace/bun.DB | Exec |
7+
| bun.go:33:12:33:20 | untrusted | github.com/uptrace/bun.DB | NewRaw |
8+
| bun.go:34:13:34:21 | untrusted | github.com/uptrace/bun.DB | Prepare |
9+
| bun.go:35:11:35:19 | untrusted | github.com/uptrace/bun.DB | Query |
10+
| bun.go:36:14:36:22 | untrusted | github.com/uptrace/bun.DB | QueryRow |
11+
| bun.go:37:9:37:17 | untrusted | github.com/uptrace/bun.DB | Raw |
12+
| bun.go:39:28:39:36 | untrusted | github.com/uptrace/bun.SelectQuery | ColumnExpr |
13+
| bun.go:40:28:40:36 | untrusted | github.com/uptrace/bun.SelectQuery | DistinctOn |
14+
| bun.go:41:21:41:29 | untrusted | github.com/uptrace/bun.SelectQuery | For |
15+
| bun.go:42:27:42:35 | untrusted | github.com/uptrace/bun.SelectQuery | GroupExpr |
16+
| bun.go:43:24:43:32 | untrusted | github.com/uptrace/bun.SelectQuery | Having |
17+
| bun.go:44:32:44:40 | untrusted | github.com/uptrace/bun.SelectQuery | ModelTableExpr |
18+
| bun.go:45:27:45:35 | untrusted | github.com/uptrace/bun.SelectQuery | OrderExpr |
19+
| bun.go:46:27:46:35 | untrusted | github.com/uptrace/bun.SelectQuery | TableExpr |
20+
| bun.go:47:23:47:31 | untrusted | github.com/uptrace/bun.SelectQuery | Where |
21+
| bun.go:48:25:48:33 | untrusted | github.com/uptrace/bun.SelectQuery | WhereOr |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
)
11+
12+
func getUntrustedString() string {
13+
return "trouble"
14+
}
15+
16+
func main() {
17+
untrusted := getUntrustedString()
18+
19+
ctx := context.Background()
20+
sqlite, err := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared")
21+
if err != nil {
22+
panic(err)
23+
}
24+
db := bun.NewDB(sqlite, sqlitedialect.New())
25+
bun.NewRawQuery(db, untrusted)
26+
27+
db.ExecContext(ctx, untrusted)
28+
db.PrepareContext(ctx, untrusted)
29+
db.QueryContext(ctx, untrusted)
30+
db.QueryRowContext(ctx, untrusted)
31+
32+
db.Exec(untrusted)
33+
db.NewRaw(untrusted)
34+
db.Prepare(untrusted)
35+
db.Query(untrusted)
36+
db.QueryRow(untrusted)
37+
db.Raw(untrusted)
38+
39+
db.NewSelect().ColumnExpr(untrusted)
40+
db.NewSelect().DistinctOn(untrusted)
41+
db.NewSelect().For(untrusted)
42+
db.NewSelect().GroupExpr(untrusted)
43+
db.NewSelect().Having(untrusted)
44+
db.NewSelect().ModelTableExpr(untrusted)
45+
db.NewSelect().OrderExpr(untrusted)
46+
db.NewSelect().TableExpr(untrusted)
47+
db.NewSelect().Where(untrusted)
48+
db.NewSelect().WhereOr(untrusted)
49+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import go
2+
3+
from SQL::QueryString qs, Function func, string a, string b
4+
where
5+
func.hasQualifiedName(a, b) and
6+
qs = func.getACall().getSyntacticArgument(_)
7+
select qs, a, b
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module pwntester/bun
2+
3+
go 1.19
4+
5+
require (
6+
github.com/uptrace/bun v1.1.14
7+
github.com/uptrace/bun/dialect/sqlitedialect v1.1.14
8+
github.com/uptrace/bun/driver/sqliteshim v1.1.14
9+
)
10+
11+
require (
12+
github.com/dustin/go-humanize v1.0.1 // indirect
13+
github.com/google/uuid v1.3.0 // indirect
14+
github.com/jinzhu/inflection v1.0.0 // indirect
15+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
16+
github.com/mattn/go-isatty v0.0.19 // indirect
17+
github.com/mattn/go-sqlite3 v1.14.16 // indirect
18+
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
19+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
20+
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
21+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
22+
golang.org/x/mod v0.10.0 // indirect
23+
golang.org/x/sys v0.8.0 // indirect
24+
golang.org/x/tools v0.9.1 // indirect
25+
lukechampine.com/uint128 v1.3.0 // indirect
26+
modernc.org/cc/v3 v3.40.0 // indirect
27+
modernc.org/ccgo/v3 v3.16.13 // indirect
28+
modernc.org/libc v1.22.6 // indirect
29+
modernc.org/mathutil v1.5.0 // indirect
30+
modernc.org/memory v1.5.0 // indirect
31+
modernc.org/opt v0.1.3 // indirect
32+
modernc.org/sqlite v1.22.1 // indirect
33+
modernc.org/strutil v1.1.3 // indirect
34+
modernc.org/token v1.1.0 // indirect
35+
)

go/ql/test/library-tests/semmle/go/frameworks/SQL/bun/vendor/github.com/uptrace/bun/dialect/sqlitedialect/stub.go

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/ql/test/library-tests/semmle/go/frameworks/SQL/bun/vendor/github.com/uptrace/bun/driver/sqliteshim/stub.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)