Skip to content

Commit b22da71

Browse files
committed
Fix _auth_* parameter check
Fixes: mattn#724
1 parent 2ea5857 commit b22da71

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

sqlite3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,10 +1522,10 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
15221522
// Before going any further, we need to check that the user
15231523
// has provided an username and password within the DSN.
15241524
// We are not allowed to continue.
1525-
if len(authUser) < 0 {
1525+
if len(authUser) <= 0 {
15261526
return nil, fmt.Errorf("Missing '_auth_user' while user authentication was requested with '_auth'")
15271527
}
1528-
if len(authPass) < 0 {
1528+
if len(authPass) <= 0 {
15291529
return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
15301530
}
15311531

sqlite3_opt_userauth_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ func init() {
6060
file = TempFilename(t)
6161
}
6262

63-
db, err = sql.Open("sqlite3_with_conn", "file:"+file+fmt.Sprintf("?_auth&_auth_user=%s&_auth_pass=%s", username, password))
63+
params := "?_auth"
64+
if len(username) > 0 {
65+
params = fmt.Sprintf("%s&_auth_user=%s", params, username)
66+
}
67+
if len(password) > 0 {
68+
params = fmt.Sprintf("%s&_auth_pass=%s", params, password)
69+
}
70+
db, err = sql.Open("sqlite3_with_conn", "file:"+file+params)
6471
if err != nil {
6572
defer os.Remove(file)
6673
return file, nil, nil, err
@@ -164,6 +171,23 @@ func TestUserAuthCreateDatabase(t *testing.T) {
164171
}
165172
}
166173

174+
func TestUserAuthCreateDatabaseWithoutArgs(t *testing.T) {
175+
_, db, c, err := connect(t, "", "", "")
176+
if err == nil && c == nil && db == nil {
177+
t.Fatal("Should have failed due to missing _auth_* parameters")
178+
}
179+
180+
_, db, c, err = connect(t, "", "", "admin")
181+
if err == nil && c == nil && db == nil {
182+
t.Fatal("Should have failed due to missing _auth_user parameter")
183+
}
184+
185+
_, db, c, err = connect(t, "", "admin", "")
186+
if err == nil && c == nil && db == nil {
187+
t.Fatal("Should have failed due to missing _auth_pass parameter")
188+
}
189+
}
190+
167191
func TestUserAuthLogin(t *testing.T) {
168192
f1, err := create(t, "admin", "admin")
169193
if err != nil {

0 commit comments

Comments
 (0)