Skip to content

Commit

Permalink
fix(BRIDGE-10): escaping db path when creating connection (#407)
Browse files Browse the repository at this point in the history
fix(BRIDGE-10): escaping db path when creating connections
  • Loading branch information
ElectroNafta committed Apr 22, 2024
1 parent 3734c76 commit 890426e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/db_impl/sqlite3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -287,7 +288,9 @@ func pathExists(path string) (bool, error) {
}

func getDatabaseConn(dir, userID, path string) string {
return fmt.Sprintf("file:%v?cache=shared&_fk=1&_journal=WAL", path)
// We need to escape special characters in the db path, such as #
escapedPath := url.PathEscape(path)
return fmt.Sprintf("file:%v?cache=shared&_fk=1&_journal=WAL", escapedPath)
}

func TestUpdateDBVersion(ctx context.Context, dbPath, userID string, version int) error {
Expand Down
47 changes: 47 additions & 0 deletions internal/db_impl/sqlite3/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sqlite3

import (
"database/sql"
"fmt"
"os"
"path/filepath"
"testing"
)

func TestClient_DBConnectionSpecialCharacterPath(t *testing.T) {
dbDirs := []string{
"#test",
"test_test",
"test#test#test",
"test$test$test",
}

testingDir := t.TempDir()

for _, dirName := range dbDirs {
path := filepath.Join(testingDir, dirName)
if err := os.MkdirAll(path, 0777); err != nil {
fmt.Println("Could not create testing directory, error: ", err)
t.FailNow()
}

filePath := filepath.Join(path, "test.db")

client, err := sql.Open("sqlite3", getDatabaseConn("test", "test", filePath))
if err != nil {
fmt.Println("Could not connect to test database, error: ", err)
t.FailNow()
}

if err := client.Ping(); err != nil {
fmt.Println("Could not ping test database, error: ", err)
client.Close()
t.FailNow()
}

if err := client.Close(); err != nil {
fmt.Println("Could not close test database, error: ", err)
t.FailNow()
}
}
}

0 comments on commit 890426e

Please sign in to comment.