Skip to content

Commit c9ae164

Browse files
authored
Add a mode that refuses a query string with more than one statement (#12)
* Add a mode that refuses a query string with more than one statement CovenantSQL checks each query before its miners run it, but SQLite splits a string into statements by its own lexical rules, so a second statement that the checker parses differently, for example inside what it takes for a comment, runs unchecked. With SQLiteDriver.SingleStatement set, prepare checks the rest of the string after the first statement byte by byte, following the tokenizer of the bundled SQLite, and refuses anything but whitespace, comments and semicolons with ErrMultipleStatements. The rest is never prepared on the connection, and the C fast path of Exec without arguments, which steps each statement before Go sees the rest, is not used. Exec, Query, Prepare, transactions and the statement cache all go through prepare. The default keeps running batches. * Check the rest once and scan block comments in linear time SQLite skips leading empty statements itself and stops after the first statement, so one check of the rest after the first prepare is enough; prepare no longer prepares the rest after an empty statement. The block comment scan searched the whole rest of the string for a NUL at every comment, which is quadratic in the number of comments. It now searches the comment only. The README no longer says nothing of a refused string runs: a first statement that acts when it is prepared still does. The tests check that Query and Prepare refuse the string themselves, and cover a line comment followed by a second statement, CR in a line comment, a semicolon in a string literal and a NUL. * Test accepted strings without arguments and with a cancelled context The accepted strings were only run with an argument, so Exec without arguments, which would otherwise take the C fast path, was not tested with them. Run each also without arguments. Cancel the context during and after a call, and check that nothing beyond the first statement ran. Note in the README that unkeyed SQLiteDriver literals must switch to field names, as the struct has a new field.
1 parent 2d1b78d commit c9ae164

5 files changed

Lines changed: 321 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Without a current time, a column declared `NOT NULL DEFAULT CURRENT_TIMESTAMP` c
3232
- `sqlite3.go` defines the SQLCipher compile options, turns off the default logging of SQLCipher to stderr, and adds the `_crypto_key` DSN parameter.
3333
- `Exec` without arguments reports missing arguments again, which go-sqlite3 v1.14.52 does not.
3434
- `sqlite3_nocurrenttime.go` adds the `nocurrenttime` VFS, and `sqlite3_opt_omit_localtime.go` the `sqlite_omit_localtime` tag.
35+
- `SQLiteDriver.SingleStatement` refuses a query string that holds more than one statement with `ErrMultipleStatements`, without stepping it or preparing its second statement. Unkeyed `SQLiteDriver` literals, such as `SQLiteDriver{nil, nil}`, must switch to field names.
3536
- `sqlite3_opt_icu.go` adds the libraries that a static link of ICU needs.
3637
- The CIFuzz workflow, which builds the OSS-Fuzz project of go-sqlite3, is removed.
3738

‎sqlite3.go‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ const (
483483
type SQLiteDriver struct {
484484
Extensions []string
485485
ConnectHook func(*SQLiteConn) error
486+
// SingleStatement makes the connections refuse, with
487+
// ErrMultipleStatements, a query string that holds more than one SQL
488+
// statement, in Exec, Query and Prepare alike. Leading empty statements
489+
// and trailing semicolons, whitespace and comments are allowed. The
490+
// check runs when the first statement is prepared: no statement of a
491+
// refused string is stepped, and the rest of the string is never
492+
// compiled on the connection. A statement that acts when it is
493+
// prepared, such as PRAGMA case_sensitive_like or SQLCipher's PRAGMA
494+
// key and rekey, still acts when it is the first statement of a
495+
// refused string.
496+
SingleStatement bool
486497
}
487498

488499
// SQLiteConn implements driver.Conn.
@@ -509,6 +520,8 @@ type SQLiteConn struct {
509520
// only field safe to read without the lock.
510521
stmtCache []*SQLiteStmt
511522
stmtCacheEnabled bool
523+
// singleStatement is SQLiteDriver.SingleStatement, immutable after Open.
524+
singleStatement bool
512525
}
513526

514527
// SQLiteTx implements driver.Tx.
@@ -1064,8 +1077,9 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err
10641077
}
10651078

10661079
func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
1067-
// Fast path: no args, no context cancellation → single CGO call per statement
1068-
if len(args) == 0 && ctx.Done() == nil {
1080+
// Fast path: no args, no context cancellation → single CGO call per statement.
1081+
// It steps each statement before Go sees the rest of the string.
1082+
if len(args) == 0 && ctx.Done() == nil && !c.singleStatement {
10691083
return c.execNoArgs(query)
10701084
}
10711085

@@ -1752,7 +1766,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
17521766
}
17531767

17541768
// Create connection to SQLite
1755-
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
1769+
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock, singleStatement: d.SingleStatement}
17561770
if stmtCacheSize > 0 {
17571771
conn.stmtCache = make([]*SQLiteStmt, 0, stmtCacheSize)
17581772
conn.stmtCacheEnabled = true
@@ -2201,7 +2215,18 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
22012215
return nil, c.lastError()
22022216
}
22032217
var t string
2204-
if tail != nil && *tail != '\000' {
2218+
if c.singleStatement {
2219+
// SQLite skips leading empty statements and stops after the first
2220+
// statement. Check the rest as SQLite reads it, without preparing it.
2221+
var rest string
2222+
if tail != nil {
2223+
rest = query[uintptr(unsafe.Pointer(tail))-uintptr(unsafe.Pointer(pquery)):]
2224+
}
2225+
if !onlySQLiteTrivia(rest) {
2226+
C.sqlite3_finalize(s)
2227+
return nil, ErrMultipleStatements
2228+
}
2229+
} else if tail != nil && *tail != '\000' {
22052230
t = strings.TrimSpace(C.GoString(tail))
22062231
}
22072232
ss := &SQLiteStmt{c: c, s: s, t: t}

‎sqlite3_single_statement.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package sqlite3
2+
3+
import (
4+
"errors"
5+
"strings"
6+
)
7+
8+
// ErrMultipleStatements is returned in single-statement mode, see
9+
// SQLiteDriver.SingleStatement, for a query string that holds more than
10+
// one SQL statement.
11+
var ErrMultipleStatements = errors.New("sqlite3: more than one statement in the query")
12+
13+
// onlySQLiteTrivia reports whether the tokenizer of the bundled SQLite,
14+
// sqlite3GetToken in sqlite3-binding.c, finds nothing in s but whitespace,
15+
// comments and semicolons, that is, no further statement.
16+
func onlySQLiteTrivia(s string) bool {
17+
for i := 0; i < len(s); {
18+
switch c := s[i]; {
19+
case c == 0:
20+
// SQLite reads no further than a NUL.
21+
return true
22+
case c == ';':
23+
i++
24+
case c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r':
25+
// A space token starts with one of these, not '\v', and goes on
26+
// over sqlite3Isspace, which includes '\v'.
27+
for i++; i < len(s) && (s[i] == ' ' || s[i] >= '\t' && s[i] <= '\r'); i++ {
28+
}
29+
case strings.HasPrefix(s[i:], "--"):
30+
// Up to a LF or the end.
31+
j := strings.IndexAny(s[i:], "\n\x00")
32+
if j < 0 {
33+
return true
34+
}
35+
i += j
36+
case strings.HasPrefix(s[i:], "/*") && i+2 < len(s) && s[i+2] != 0:
37+
// Up to the first "*/" or the end; comments do not nest. A "/*"
38+
// at the very end is a slash and a star, not a comment.
39+
j := strings.Index(s[i+2:], "*/")
40+
if j < 0 || strings.IndexByte(s[i+2:i+2+j], 0) >= 0 {
41+
return true
42+
}
43+
i += 2 + j + 2
44+
case strings.HasPrefix(s[i:], "\xef\xbb\xbf"):
45+
// A UTF-8 byte order mark is a space token.
46+
i += 3
47+
default:
48+
return false
49+
}
50+
}
51+
return true
52+
}

‎sqlite3_single_statement_test.go‎

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
//go:build cgo
2+
3+
package sqlite3
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
"errors"
9+
"strings"
10+
"testing"
11+
"time"
12+
)
13+
14+
func init() {
15+
sql.Register("sqlite3_single_statement", &SQLiteDriver{SingleStatement: true})
16+
}
17+
18+
func openSingleStatement(t *testing.T, dsn string) *sql.DB {
19+
t.Helper()
20+
db, err := sql.Open("sqlite3_single_statement", dsn)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
t.Cleanup(func() { db.Close() })
25+
// One connection, so that a PRAGMA would show on the next query.
26+
db.SetMaxOpenConns(1)
27+
if _, err := db.Exec("CREATE TABLE r (v); CREATE TABLE s (v)"); !errors.Is(err, ErrMultipleStatements) {
28+
t.Fatalf("batch error = %v, want ErrMultipleStatements", err)
29+
}
30+
for _, q := range []string{"CREATE TABLE r (v)", "CREATE TABLE s (v)"} {
31+
if _, err := db.Exec(q); err != nil {
32+
t.Fatal(err)
33+
}
34+
}
35+
return db
36+
}
37+
38+
// singleStatementCalls runs query, with args, through every way the driver
39+
// executes a string, and returns their errors.
40+
func singleStatementCalls(db *sql.DB, query string, args ...any) map[string]error {
41+
ctx, cancel := context.WithCancel(context.Background())
42+
defer cancel()
43+
errs := map[string]error{}
44+
_, errs["Exec"] = db.Exec(query, args...)
45+
_, errs["ExecContext"] = db.ExecContext(ctx, query, args...)
46+
for name, run := range map[string]func() (*sql.Rows, error){
47+
"Query": func() (*sql.Rows, error) { return db.Query(query, args...) },
48+
"QueryContext": func() (*sql.Rows, error) { return db.QueryContext(ctx, query, args...) },
49+
} {
50+
rows, err := run()
51+
errs[name] = err
52+
if err == nil {
53+
for rows.Next() {
54+
}
55+
errs[name+" rows"] = rows.Err()
56+
rows.Close()
57+
}
58+
}
59+
for name, prepare := range map[string]func() (*sql.Stmt, error){
60+
"Prepare": func() (*sql.Stmt, error) { return db.Prepare(query) },
61+
"PrepareContext": func() (*sql.Stmt, error) { return db.PrepareContext(ctx, query) },
62+
} {
63+
stmt, err := prepare()
64+
errs[name] = err
65+
if err == nil {
66+
_, errs[name+" Exec"] = stmt.Exec(args...)
67+
stmt.Close()
68+
}
69+
}
70+
tx, err := db.BeginTx(ctx, nil)
71+
if err == nil {
72+
if _, err = tx.Exec(query, args...); err == nil {
73+
err = tx.Commit()
74+
} else {
75+
tx.Rollback()
76+
}
77+
}
78+
errs["Tx"] = err
79+
return errs
80+
}
81+
82+
func TestSingleStatement(t *testing.T) {
83+
for _, dsn := range []string{":memory:", ":memory:?_stmt_cache_size=8"} {
84+
t.Run(dsn, func(t *testing.T) {
85+
db := openSingleStatement(t, dsn)
86+
count := func() (n int) {
87+
t.Helper()
88+
if err := db.QueryRow("SELECT count(*) FROM r").Scan(&n); err != nil {
89+
t.Fatal(err)
90+
}
91+
return n
92+
}
93+
94+
for _, q := range []struct {
95+
query string
96+
args []any
97+
}{
98+
{"INSERT INTO r VALUES (1); INSERT INTO r VALUES (2)", nil},
99+
{"INSERT INTO r VALUES (?); INSERT INTO r VALUES (?)", []any{1, 2}},
100+
{"INSERT INTO r VALUES (1); x", nil},
101+
{"INSERT INTO r VALUES (1); /* outer /* inner */ SELECT 2; -- */", nil},
102+
{"INSERT INTO r VALUES (1); --x\nSELECT 2", nil},
103+
{"INSERT INTO r VALUES (1); /*", nil},
104+
{"INSERT INTO r VALUES (1);\v", nil},
105+
{"CREATE TABLE d (v); SELECT 1", nil},
106+
{"; INSERT INTO r VALUES (1); SELECT 2", nil},
107+
} {
108+
for call, err := range singleStatementCalls(db, q.query, q.args...) {
109+
if !errors.Is(err, ErrMultipleStatements) {
110+
t.Errorf("%s(%q): error = %v, want ErrMultipleStatements", call, q.query, err)
111+
}
112+
}
113+
}
114+
if n := count(); n != 0 {
115+
t.Errorf("refused strings inserted %d rows", n)
116+
}
117+
var tables int
118+
if err := db.QueryRow("SELECT count(*) FROM sqlite_schema WHERE name = 'd'").Scan(&tables); err != nil || tables != 0 {
119+
t.Errorf("refused string created table d: %d, %v", tables, err)
120+
}
121+
122+
for _, query := range []string{
123+
"INSERT INTO r VALUES (?);",
124+
"INSERT INTO r VALUES (?) ;; ",
125+
"; /* c */ ; INSERT INTO r VALUES (?);",
126+
"INSERT INTO r VALUES (?); --x",
127+
"INSERT INTO r VALUES (?); /*x",
128+
"INSERT INTO r VALUES (?); \v",
129+
"INSERT INTO r VALUES (?);\xef\xbb\xbf",
130+
"INSERT INTO r VALUES (?); --x\rSELECT 2",
131+
"INSERT INTO r VALUES (? || '; SELECT 2');",
132+
"INSERT INTO r VALUES (?);\x00SELECT 2",
133+
} {
134+
for _, q := range []struct {
135+
query string
136+
args []any
137+
}{
138+
{query, []any{1}},
139+
{strings.ReplaceAll(query, "?", "1"), nil},
140+
} {
141+
before := count()
142+
for call, err := range singleStatementCalls(db, q.query, q.args...) {
143+
if err != nil {
144+
t.Errorf("%s(%q): %v", call, q.query, err)
145+
}
146+
}
147+
// Exec, ExecContext, Prepare, PrepareContext and Tx insert;
148+
// Query and QueryContext do too, as they step the INSERT.
149+
if n := count() - before; n != 7 {
150+
t.Errorf("%q inserted %d rows, want 7", q.query, n)
151+
}
152+
}
153+
}
154+
155+
before := count()
156+
if _, err := db.Exec("CREATE TRIGGER tr AFTER INSERT ON s BEGIN INSERT INTO r VALUES (1); INSERT INTO r VALUES (2); END;"); err != nil {
157+
t.Fatal(err)
158+
}
159+
if _, err := db.Exec("INSERT INTO s VALUES (1)"); err != nil {
160+
t.Fatal(err)
161+
}
162+
if n := count() - before; n != 2 {
163+
t.Errorf("the trigger inserted %d rows, want 2", n)
164+
}
165+
})
166+
}
167+
}
168+
169+
// TestSingleStatementCancel cancels the context of a call during and after
170+
// it, and checks that nothing beyond the first statement ran.
171+
func TestSingleStatementCancel(t *testing.T) {
172+
db := openSingleStatement(t, ":memory:")
173+
counts := func() (r, s int) {
174+
t.Helper()
175+
if err := db.QueryRow("SELECT (SELECT count(*) FROM r), (SELECT count(*) FROM s)").Scan(&r, &s); err != nil {
176+
t.Fatal(err)
177+
}
178+
return r, s
179+
}
180+
const slow = "INSERT INTO r WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c) SELECT x FROM c LIMIT 100000000"
181+
182+
for _, q := range []string{slow + "; INSERT INTO s VALUES (1)", slow + "; -- INSERT INTO s VALUES (1)"} {
183+
ctx, cancel := context.WithCancel(context.Background())
184+
time.AfterFunc(50*time.Millisecond, cancel)
185+
_, err := db.ExecContext(ctx, q)
186+
<-ctx.Done()
187+
if err == nil {
188+
t.Errorf("%q: no error", q)
189+
}
190+
if r, s := counts(); r != 0 || s != 0 {
191+
t.Errorf("%q: r has %d rows and s %d, want none", q, r, s)
192+
}
193+
}
194+
195+
ctx, cancel := context.WithCancel(context.Background())
196+
if _, err := db.ExecContext(ctx, "INSERT INTO r VALUES (1); -- INSERT INTO s VALUES (1)"); err != nil {
197+
t.Fatal(err)
198+
}
199+
cancel()
200+
if r, s := counts(); r != 1 || s != 0 {
201+
t.Errorf("r has %d rows and s %d, want 1 and 0", r, s)
202+
}
203+
}
204+
205+
// TestSingleStatementTailNotPrepared checks that the rest of a refused
206+
// string is not compiled: PRAGMA case_sensitive_like acts when it is
207+
// prepared.
208+
func TestSingleStatementTailNotPrepared(t *testing.T) {
209+
db := openSingleStatement(t, ":memory:")
210+
if _, err := db.Query("SELECT 1; PRAGMA case_sensitive_like=1"); !errors.Is(err, ErrMultipleStatements) {
211+
t.Fatalf("error = %v, want ErrMultipleStatements", err)
212+
}
213+
var like bool
214+
if err := db.QueryRow("SELECT 'a' LIKE 'A'").Scan(&like); err != nil {
215+
t.Fatal(err)
216+
}
217+
if !like {
218+
t.Error("LIKE became case sensitive")
219+
}
220+
}
221+
222+
func TestSingleStatementOffRunsBatches(t *testing.T) {
223+
db, err := sql.Open("sqlite3", ":memory:")
224+
if err != nil {
225+
t.Fatal(err)
226+
}
227+
defer db.Close()
228+
db.SetMaxOpenConns(1)
229+
if _, err := db.Exec("CREATE TABLE r (v); INSERT INTO r VALUES (1); INSERT INTO r VALUES (2)"); err != nil {
230+
t.Fatal(err)
231+
}
232+
var n int
233+
if err := db.QueryRow("SELECT count(*) FROM r").Scan(&n); err != nil || n != 2 {
234+
t.Errorf("count = %d, %v, want 2", n, err)
235+
}
236+
}

‎static_mock.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ func init() {
2121

2222
type (
2323
SQLiteDriver struct {
24-
Extensions []string
25-
ConnectHook func(*SQLiteConn) error
24+
Extensions []string
25+
ConnectHook func(*SQLiteConn) error
26+
SingleStatement bool
2627
}
2728
SQLiteConn struct{}
2829
)

0 commit comments

Comments
 (0)