Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BRIDGE-10): escaping db path when creating connection #407

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
}
Loading