From c933e9c63f38e8b830b84e3f0643eb649c846dbd Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 05:03:17 -0600 Subject: [PATCH 1/2] fix(mcpx/logstore): refuse value-carrying PRAGMAs in read-only log sql guard CheckReadOnly allow-listed the PRAGMA verb and never inspected the rest of the statement, so PRAGMA journal_mode=DELETE, user_version=42, application_id=7 and writable_schema=ON executed through the documented read-only mcpx log sql surface (triage task 1857 confirmed by executed probes: header rewrites persist across reopen). Refuse PRAGMAs carrying a value in both name = value and name(value) forms. Bare read PRAGMAs and the established function-form reads (table_info, table_xinfo, index_*, foreign_key_*, integrity_check, quick_check) keep working. Correct the header comment to describe the refusal now actually implemented. Guard tests cover rejected assignment shapes and retained reads; a RunSQL-level test shows assignments no longer execute and user_version stays 0. Fixes #1664 Queue task 4217. --- pkgs/mcpx/internal/logstore/logstore_test.go | 41 +++++++++++++++ pkgs/mcpx/internal/logstore/sql.go | 52 +++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/pkgs/mcpx/internal/logstore/logstore_test.go b/pkgs/mcpx/internal/logstore/logstore_test.go index 50e7b313ee..9168e61713 100644 --- a/pkgs/mcpx/internal/logstore/logstore_test.go +++ b/pkgs/mcpx/internal/logstore/logstore_test.go @@ -610,6 +610,12 @@ func TestReadOnlyGuardRejectsAnythingThatWrites(t *testing.T) { ` select 1;`, `WITH t AS (SELECT 1) SELECT * FROM t`, `PRAGMA table_info(records)`, + `PRAGMA table_xinfo(records)`, + `PRAGMA index_list(records)`, + `PRAGMA main.table_info(records)`, + `PRAGMA journal_mode`, + `PRAGMA user_version`, + `PRAGMA user_version;`, `EXPLAIN SELECT 1`, } for _, q := range ok { @@ -625,6 +631,14 @@ func TestReadOnlyGuardRejectsAnythingThatWrites(t *testing.T) { `-- select DELETE FROM records`, `WITH t AS (SELECT 1) DELETE FROM records`, + `PRAGMA journal_mode=DELETE`, + `PRAGMA journal_mode = DELETE`, + `PRAGMA user_version=42`, + `PRAGMA application_id=7`, + `PRAGMA writable_schema=ON`, + `PRAGMA user_version(42)`, + `PRAGMA journal_mode(DELETE)`, + `PRAGMA schema.user_version=42`, ``, } for _, q := range bad { @@ -634,6 +648,33 @@ func TestReadOnlyGuardRejectsAnythingThatWrites(t *testing.T) { } } +func TestRunSQLRefusesPragmaAssignments(t *testing.T) { + dir := t.TempDir() + chainFixture(t, dir) + st := open(t, dir) + ingest(t, st) + for _, q := range []string{ + `PRAGMA user_version=42`, + `PRAGMA application_id=7`, + `PRAGMA user_version(42)`, + `PRAGMA journal_mode=DELETE`, + } { + if _, err := st.RunSQL(q); err == nil { + t.Errorf("RunSQL executed a PRAGMA assignment: %q", q) + } + } + table, err := st.RunSQL(`PRAGMA user_version`) + if err != nil { + t.Fatal(err) + } + if len(table.Rows) != 1 || table.Rows[0][0] != "0" { + t.Errorf("user_version after refused assignments is %v, want 0 (unchanged)", table.Rows) + } + if _, err := st.RunSQL(`PRAGMA table_info(records)`); err != nil { + t.Errorf("RunSQL rejected a function-form read PRAGMA: %v", err) + } +} + func TestRawSQLReturnsColumnsAndRows(t *testing.T) { dir := t.TempDir() chainFixture(t, dir) diff --git a/pkgs/mcpx/internal/logstore/sql.go b/pkgs/mcpx/internal/logstore/sql.go index 2234e99dfb..1033ee7c89 100644 --- a/pkgs/mcpx/internal/logstore/sql.go +++ b/pkgs/mcpx/internal/logstore/sql.go @@ -11,7 +11,12 @@ import ( // from the JSONL at any time, so nothing here is protecting data; it is // protecting someone who typed DELETE meaning SELECT from a confusing // afternoon. PRAGMA is allowed because that is how you ask SQLite about -// itself, and the mutating pragmas need a value that this refuses anyway. +// itself. A PRAGMA that carries a value is not a question, it is an +// assignment, so CheckReadOnly refuses value-carrying PRAGMAs in both +// forms (name = value and name(value)); the only parenthesised forms +// allowed are the established function-form reads in +// functionFormReadPragmas, whose argument names the object to inspect +// rather than a value to assign. var readOnlyVerbs = map[string]bool{ "select": true, "explain": true, "pragma": true, "with": true, "values": true, } @@ -32,6 +37,11 @@ func CheckReadOnly(query string) error { if !readOnlyVerbs[verb] { return fmt.Errorf("%q is not a read-only statement; use SELECT, WITH, VALUES, EXPLAIN or PRAGMA", verb) } + if verb == "pragma" { + if err := checkReadOnlyPragma(trimmed); err != nil { + return err + } + } // A CTE can hide a writer: WITH x AS (...) DELETE FROM records is valid // SQLite, and checking only the first word would wave it through. if verb == "with" { @@ -45,6 +55,46 @@ func CheckReadOnly(query string) error { return nil } +// functionFormReadPragmas are the PRAGMAs whose parenthesised form is a +// read: the argument names a table, index, or check to inspect, it is not +// a value being assigned to the pragma. Any other parenthesised PRAGMA +// (e.g. user_version(42), journal_mode(DELETE)) is an assignment in call +// form and is refused. +var functionFormReadPragmas = map[string]bool{ + "table_info": true, "table_xinfo": true, + "index_list": true, "index_info": true, "index_xinfo": true, + "foreign_key_list": true, "foreign_key_check": true, + "integrity_check": true, "quick_check": true, +} + +// checkReadOnlyPragma refuses a PRAGMA that carries a value. Bare PRAGMAs +// (PRAGMA journal_mode, PRAGMA user_version) are reads and pass; a value +// in either assignment form (= value or (value)) does not, except for the +// function-form reads above. +func checkReadOnlyPragma(trimmed string) error { + rest := strings.TrimSpace(trimmed[len("pragma"):]) + rest = strings.TrimSpace(strings.TrimSuffix(rest, ";")) + if rest == "" { + return fmt.Errorf("PRAGMA with no name is not a read-only statement") + } + if strings.Contains(rest, "=") { + return fmt.Errorf("PRAGMA %q carries a value; only read PRAGMAs with no assigned value are allowed here", rest) + } + if i := strings.Index(rest, "("); i >= 0 { + if !strings.HasSuffix(rest, ")") { + return fmt.Errorf("PRAGMA %q is not a well-formed read PRAGMA", rest) + } + name := rest[:i] + if j := strings.LastIndex(name, "."); j >= 0 { + name = name[j+1:] + } + if !functionFormReadPragmas[strings.ToLower(strings.TrimSpace(name))] { + return fmt.Errorf("PRAGMA %q carries a value; only read PRAGMAs with no assigned value are allowed here", rest) + } + } + return nil +} + func containsWord(s, word string) bool { for i := 0; ; { j := strings.Index(s[i:], word) From afcce321e62f258ca290b73b1d1795adde603f38 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 05:07:28 -0600 Subject: [PATCH 2/2] fix(mcpx/logstore): retain function-form table_list read PRAGMA (Codex review on #3516) --- pkgs/mcpx/internal/logstore/logstore_test.go | 1 + pkgs/mcpx/internal/logstore/sql.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/mcpx/internal/logstore/logstore_test.go b/pkgs/mcpx/internal/logstore/logstore_test.go index 9168e61713..e104123df6 100644 --- a/pkgs/mcpx/internal/logstore/logstore_test.go +++ b/pkgs/mcpx/internal/logstore/logstore_test.go @@ -611,6 +611,7 @@ func TestReadOnlyGuardRejectsAnythingThatWrites(t *testing.T) { `WITH t AS (SELECT 1) SELECT * FROM t`, `PRAGMA table_info(records)`, `PRAGMA table_xinfo(records)`, + `PRAGMA table_list(records)`, `PRAGMA index_list(records)`, `PRAGMA main.table_info(records)`, `PRAGMA journal_mode`, diff --git a/pkgs/mcpx/internal/logstore/sql.go b/pkgs/mcpx/internal/logstore/sql.go index 1033ee7c89..c7168ee0f3 100644 --- a/pkgs/mcpx/internal/logstore/sql.go +++ b/pkgs/mcpx/internal/logstore/sql.go @@ -61,7 +61,7 @@ func CheckReadOnly(query string) error { // (e.g. user_version(42), journal_mode(DELETE)) is an assignment in call // form and is refused. var functionFormReadPragmas = map[string]bool{ - "table_info": true, "table_xinfo": true, + "table_info": true, "table_xinfo": true, "table_list": true, "index_list": true, "index_info": true, "index_xinfo": true, "foreign_key_list": true, "foreign_key_check": true, "integrity_check": true, "quick_check": true,