From 1a44a0ac5437f429c447d266a1e77420698b71c8 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 8 Nov 2026 20:15:00 +0000 Subject: [PATCH] sqliteconfig: escape database path in ToURL SQLite ends a file: URI path at '?' or '#' and decodes '%', so such a path opened a different file than configured. --- CHANGELOG.md | 1 + hscontrol/db/sqliteconfig/config.go | 5 ++- hscontrol/db/sqliteconfig/integration_test.go | 34 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 796141b78..af167936e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -167,6 +167,7 @@ is not connected, or refuses is "not found". See [the client docs](https://heads - A `derp.paths` file that decodes to neither regions nor `homeparams` now fails to load instead of being silently ignored - A `derp.paths` file with a key `tailcfg.DERPMap` has no field for (e.g. a misspelt `regionscore`) fails to load instead of the key being ignored, as Tailscale SaaS rejects such a policy `derpMap` [#2932](https://github.com/juanfont/headscale/pull/2932) - A DERP map from `derp.paths` or `derp.urls` must meet the region rules Tailscale SaaS applies to a policy `derpMap`: each region's `regionid` matches its key, each node's `regionid` matches its region, and a region has at least one node. A map that breaks one fails to load. Clients count latency by `regionid`, so a mismatch made them pick a home region the map did not have [#2932](https://github.com/juanfont/headscale/pull/2932) +- `database.sqlite.path` names the file literally. Before, SQLite cut the path at `?` or `#` and decoded `%XX`, so headscale opened another file. A path with `%`, `?` or `#` now opens a different file than before: move the database to the configured path before upgrading, or headscale starts with an empty one - `unix_socket_permission` must be a quoted octal string no higher than `"0777"`, e.g. `"0770"`; an unquoted number or an invalid value now stops headscale from starting instead of silently using `0700` [#3540](https://github.com/juanfont/headscale/pull/3540) - `tls_cert_path`, `tls_key_path`, `tls_letsencrypt_*`, `acme_url` and `acme_email` are deprecated in favour of `tls.cert_path`, `tls.key_path` and `tls.acme.*` [#3554](https://github.com/juanfont/headscale/pull/3554) - The old keys continue to work, with a warning, but will be removed in a future release diff --git a/hscontrol/db/sqliteconfig/config.go b/hscontrol/db/sqliteconfig/config.go index 9d818ae1b..5c5d70b3e 100644 --- a/hscontrol/db/sqliteconfig/config.go +++ b/hscontrol/db/sqliteconfig/config.go @@ -5,6 +5,7 @@ package sqliteconfig import ( "errors" "fmt" + "net/url" "slices" "strings" ) @@ -354,7 +355,9 @@ func (c *Config) ToURL() (string, error) { if c.Path == ":memory:" { baseURL = ":memory:" } else { - baseURL = "file:" + c.Path + // SQLite ends a URI path at '?' or '#' and decodes '%'; escape so the + // configured path names the file literally. + baseURL = "file:" + (&url.URL{Path: c.Path}).EscapedPath() } // Build query parameters diff --git a/hscontrol/db/sqliteconfig/integration_test.go b/hscontrol/db/sqliteconfig/integration_test.go index 00adaa645..57b494a87 100644 --- a/hscontrol/db/sqliteconfig/integration_test.go +++ b/hscontrol/db/sqliteconfig/integration_test.go @@ -3,10 +3,12 @@ package sqliteconfig import ( "context" "database/sql" + "os" "path/filepath" "strings" "testing" + "github.com/stretchr/testify/require" _ "modernc.org/sqlite" ) @@ -278,3 +280,35 @@ func TestJournalModeValidation(t *testing.T) { func contains(str, substr string) bool { return strings.Contains(str, substr) } + +// TestToURLOpensExactPath guards against URI metacharacters in the path: SQLite +// ends a file: URI path at '?' or '#' and decodes '%', so an unescaped path +// opens a different file than the one configured. +func TestToURLOpensExactPath(t *testing.T) { + for _, name := range []string{"q?x", "h#00", "pct%41", "pct%zz", "sp ace"} { + t.Run(name, func(t *testing.T) { + dir := filepath.Join(t.TempDir(), name) + require.NoError(t, os.Mkdir(dir, 0o700)) + + dbPath := filepath.Join(dir, name+".db") + + url, err := Default(dbPath).ToURL() + require.NoError(t, err) + + db, err := sql.Open("sqlite", url) + require.NoError(t, err) + + defer db.Close() + + _, err = db.ExecContext(t.Context(), "CREATE TABLE t (id INTEGER)") + require.NoError(t, err) + + _, err = os.Stat(dbPath) + require.NoError(t, err, "database not created at configured path") + + var mode string + require.NoError(t, db.QueryRowContext(t.Context(), "PRAGMA journal_mode").Scan(&mode)) + require.Equal(t, "wal", mode) + }) + } +}