Skip to content

Commit e1abbd0

Browse files
Prometheus2677FPiety0521
authored andcommitted
sqlite3: Explicitly reject invalid x-no-tx-wrap values
1 parent de8e3bc commit e1abbd0

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

database/sqlite3/sqlite3.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ func (m *Sqlite) Open(url string) (database.Driver, error) {
109109
migrationsTable = DefaultMigrationsTable
110110
}
111111

112-
noTxWrap, err := strconv.ParseBool(qv.Get("x-no-tx-wrap"))
113-
if err != nil {
114-
noTxWrap = false
112+
noTxWrap := false
113+
if v := qv.Get("x-no-tx-wrap"); v != "" {
114+
noTxWrap, err = strconv.ParseBool(v)
115+
if err != nil {
116+
return nil, fmt.Errorf("x-no-tx-wrap: %s", err)
117+
}
115118
}
116119

117120
mx, err := WithInstance(db, &Config{

database/sqlite3/sqlite3_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"path/filepath"
99
"testing"
1010

11+
"github.com/stretchr/testify/assert"
12+
1113
"github.com/golang-migrate/migrate/v4"
1214
dt "github.com/golang-migrate/migrate/v4/database/testing"
1315
_ "github.com/golang-migrate/migrate/v4/source/file"
@@ -138,3 +140,23 @@ func TestNoTxWrap(t *testing.T) {
138140
// (Transactions in sqlite may not be nested.)
139141
dt.Test(t, d, []byte("BEGIN; CREATE TABLE t (Qty int, Name string); COMMIT;"))
140142
}
143+
144+
func TestNoTxWrapInvalidValue(t *testing.T) {
145+
dir, err := ioutil.TempDir("", "sqlite3-driver-test")
146+
if err != nil {
147+
return
148+
}
149+
defer func() {
150+
if err := os.RemoveAll(dir); err != nil {
151+
t.Error(err)
152+
}
153+
}()
154+
t.Logf("DB path : %s\n", filepath.Join(dir, "sqlite3.db"))
155+
p := &Sqlite{}
156+
addr := fmt.Sprintf("sqlite3://%s?x-no-tx-wrap=yeppers", filepath.Join(dir, "sqlite3.db"))
157+
_, err = p.Open(addr)
158+
if assert.Error(t, err) {
159+
assert.Contains(t, err.Error(), "x-no-tx-wrap")
160+
assert.Contains(t, err.Error(), "invalid syntax")
161+
}
162+
}

0 commit comments

Comments
 (0)