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.
This commit is contained in:
Kristoffer Dalby committed 2026-11-11 16:58:07 +01:00
1 parent b6e6c6f08c
commit 1a44a0ac54
3 files changed
+39 -1

No files matched your search

+1
View File
@@ -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
+4 -1
View File
@@ -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
@@ -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)
})
}
}