From dc0edca8870c23b7b8d4ea89984a36ebe4825f3d Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 12 Nov 2026 09:18:19 +0000 Subject: [PATCH] db/sqliteconfig: add query_only to read-only connections mode=ro still allows temp-table writes. ATTACH and VACUUM INTO still write other files; callers running untrusted SQL must reject them. --- hscontrol/db/sqliteconfig/config.go | 18 ++++- hscontrol/db/sqliteconfig/config_test.go | 5 ++ hscontrol/db/sqliteconfig/integration_test.go | 70 +++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/hscontrol/db/sqliteconfig/config.go b/hscontrol/db/sqliteconfig/config.go index ba32a1e76..47ae17ba7 100644 --- a/hscontrol/db/sqliteconfig/config.go +++ b/hscontrol/db/sqliteconfig/config.go @@ -277,7 +277,7 @@ type Config struct { Synchronous Synchronous // synchronous mode (affects durability vs performance) ForeignKeys bool // enable foreign key constraints (data integrity) TxLock TxLock // transaction lock mode (affects write concurrency) - ReadOnly bool // open with SQLite's mode=ro + ReadOnly bool // open with mode=ro and query_only, for untrusted readers } // Default returns the production configuration optimized for Headscale's usage patterns. @@ -301,6 +301,20 @@ func Default(path string) *Config { } } +// ReadOnly returns a configuration for a connection that must never write the +// database: mode=ro opens the file read-only, query_only rejects writes at the +// statement level. No write pragmas are set. It does not stop ATTACH or VACUUM +// INTO from creating other files; callers running untrusted SQL must reject +// those statements themselves. +func ReadOnly(path string) *Config { + return &Config{ + Path: path, + BusyTimeout: DefaultBusyTimeout, + WALAutocheckpoint: -1, + ReadOnly: true, + } +} + // Memory returns a configuration for in-memory databases. func Memory() *Config { return &Config{ @@ -395,7 +409,7 @@ func (c *Config) ToURL() (string, error) { } if c.ReadOnly { - queryParts = append(queryParts, "mode=ro") + queryParts = append(queryParts, "_pragma=query_only=1", "mode=ro") } if len(queryParts) > 0 { diff --git a/hscontrol/db/sqliteconfig/config_test.go b/hscontrol/db/sqliteconfig/config_test.go index 9f233902e..233c3129f 100644 --- a/hscontrol/db/sqliteconfig/config_test.go +++ b/hscontrol/db/sqliteconfig/config_test.go @@ -266,6 +266,11 @@ func TestConfigToURL(t *testing.T) { }, want: "file:/test.db?_pragma=busy_timeout=1000&_pragma=foreign_keys=ON", }, + { + name: "read-only", + config: ReadOnly("/test.db"), + want: "file:/test.db?_pragma=busy_timeout=10000&_pragma=query_only=1&mode=ro", + }, } for _, tt := range tests { diff --git a/hscontrol/db/sqliteconfig/integration_test.go b/hscontrol/db/sqliteconfig/integration_test.go index 1222dca83..869672fd7 100644 --- a/hscontrol/db/sqliteconfig/integration_test.go +++ b/hscontrol/db/sqliteconfig/integration_test.go @@ -327,3 +327,73 @@ func TestToURLOpensExactPath(t *testing.T) { }) } } + +// TestReadOnlyRejectsWrites proves a ReadOnly connection cannot change the +// database, even when a statement lifts query_only. ATTACH and VACUUM INTO +// still create other files; see [ReadOnly]. +func TestReadOnlyRejectsWrites(t *testing.T) { + ctx := context.Background() + dir := t.TempDir() + dbPath := filepath.Join(dir, "ro.db") + + rwURL, err := Default(dbPath).ToURL() + if err != nil { + t.Fatalf("rw url: %v", err) + } + + rw, err := sql.Open("sqlite", rwURL) + if err != nil { + t.Fatalf("open rw: %v", err) + } + defer rw.Close() + + _, err = rw.ExecContext(ctx, "CREATE TABLE t (v INTEGER); INSERT INTO t VALUES (1)") + if err != nil { + t.Fatalf("seed: %v", err) + } + + roURL, err := ReadOnly(dbPath).ToURL() + if err != nil { + t.Fatalf("ro url: %v", err) + } + + ro, err := sql.Open("sqlite", roURL) + if err != nil { + t.Fatalf("open ro: %v", err) + } + defer ro.Close() + + // One connection, so a PRAGMA in one statement carries to the next. + conn, err := ro.Conn(ctx) + if err != nil { + t.Fatalf("conn: %v", err) + } + defer conn.Close() + + writes := []string{ + "INSERT INTO t VALUES (2)", + "UPDATE t SET v = 3", + "DELETE FROM t", + "DROP TABLE t", + "CREATE TABLE x (a)", + "PRAGMA query_only = 0; DELETE FROM t", + "DELETE FROM t", + } + for _, q := range writes { + _, err := conn.ExecContext(ctx, q) + if err == nil { + t.Errorf("%q succeeded on a read-only connection", q) + } + } + + var n int + + err = conn.QueryRowContext(ctx, "SELECT count(*) FROM t WHERE v = 1").Scan(&n) + if err != nil { + t.Fatalf("read: %v", err) + } + + if n != 1 { + t.Errorf("table changed: %d rows with v=1, want 1", n) + } +}