Escape state_dir in the db_url derived from it (closes #290) #292

Merged
clawbot merged 6 commits from issue-290-escape-state-dir-in-db-url into next 2026-10-16 04:37:55 +02:00
Showing only changes of commit c593eae603 - Show all commits
@@ -158,9 +158,39 @@ func TestDerivedDBURLOpensStateDirHoldingURLCharacters(t *testing.T) {
t.Logf("state_dir %q gives db_url %q", stateDir, c.DBURL)
db, err := sql.Open("sqlite", c.DBURL)
checkDBURLOpensFileInWALMode(t, c.DBURL, filepath.Join(stateDir, "state.sqlite3"))
}
// TestDerivedDBURLOpensStateDirStartingWithTwoSlashes opens the db_url
// derived from a state_dir that starts with //, and checks that the
// database is state.sqlite3 in that directory, in WAL mode. Written right
// after file:, such a name gives a URL starting file://, and SQLite reads
// the first directory name as a host and refuses to open it.
func TestDerivedDBURLOpensStateDirStartingWithTwoSlashes(t *testing.T) {
t.Parallel()
dir := t.TempDir()
stateDir := "/" + dir
c, err := configFromYAML(t, signingKeyLine+"state_dir: "+stateDir+"\n")
if err != nil {
t.Fatalf("failed to open %q: %v", c.DBURL, err)
t.Fatalf("config with state_dir %q should be valid, got: %v", stateDir, err)
}
t.Logf("state_dir %q gives db_url %q", stateDir, c.DBURL)
checkDBURLOpensFileInWALMode(t, c.DBURL, filepath.Join(dir, "state.sqlite3"))
}
// checkDBURLOpensFileInWALMode opens dbURL with the SQLite driver pixad
// uses, and checks that the database is in WAL mode and is the file
// dbFile.
func checkDBURLOpensFileInWALMode(t *testing.T, dbURL, dbFile string) {
t.Helper()
db, err := sql.Open("sqlite", dbURL)
if err != nil {
t.Fatalf("failed to open %q: %v", dbURL, err)
}
t.Cleanup(func() { _ = db.Close() })
@@ -169,18 +199,16 @@ func TestDerivedDBURLOpensStateDirHoldingURLCharacters(t *testing.T) {
err = db.QueryRowContext(t.Context(), "PRAGMA journal_mode").Scan(&journalMode)
if err != nil {
t.Fatalf("failed to read the journal mode of %q: %v", c.DBURL, err)
t.Fatalf("failed to read the journal mode of %q: %v", dbURL, err)
}
if journalMode != "wal" {
t.Errorf("journal mode of %q = %q, want wal", c.DBURL, journalMode)
t.Errorf("journal mode of %q = %q, want wal", dbURL, journalMode)
}
dbFile := filepath.Join(stateDir, "state.sqlite3")
_, err = os.Stat(dbFile)
if err != nil {
t.Errorf("opening %q did not create %q: %v", c.DBURL, dbFile, err)
t.Errorf("opening %q did not create %q: %v", dbURL, dbFile, err)
}
}