From 39e4841082bc63117d48e5c369173cdc82efb3f4 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:48:45 -0600 Subject: [PATCH 1/2] fix(mcpx): refuse bare action PRAGMAs in the read-only log-sql guard (#6638) CheckReadOnly admitted any bare PRAGMA, so PRAGMA optimize, PRAGMA wal_checkpoint, PRAGMA incremental_vacuum and PRAGMA shrink_memory executed on the writable store through the read-only mcpx log sql surface. Bare PRAGMAs now pass only from an explicit allowlist of known reads (schema qualifier stripped, as in the function-form branch); unknown names are refused by default. incremental_vacuum disposition: refused - its bare form performs vacuum work. Regression coverage at guard level and RunSQL level, following TestRunSQLRefusesPragmaAssignments. --- pkgs/mcpx/internal/logstore/logstore_test.go | 44 +++++++++++++ pkgs/mcpx/internal/logstore/sql.go | 68 ++++++++++++++++++-- 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/pkgs/mcpx/internal/logstore/logstore_test.go b/pkgs/mcpx/internal/logstore/logstore_test.go index e104123df6..96a73449bb 100644 --- a/pkgs/mcpx/internal/logstore/logstore_test.go +++ b/pkgs/mcpx/internal/logstore/logstore_test.go @@ -617,6 +617,11 @@ func TestReadOnlyGuardRejectsAnythingThatWrites(t *testing.T) { `PRAGMA journal_mode`, `PRAGMA user_version`, `PRAGMA user_version;`, + `PRAGMA schema_version`, + `PRAGMA database_list`, + `PRAGMA compile_options`, + `PRAGMA main.journal_mode`, + `PRAGMA integrity_check`, `EXPLAIN SELECT 1`, } for _, q := range ok { @@ -640,6 +645,13 @@ func TestReadOnlyGuardRejectsAnythingThatWrites(t *testing.T) { `PRAGMA user_version(42)`, `PRAGMA journal_mode(DELETE)`, `PRAGMA schema.user_version=42`, + `PRAGMA optimize`, + `PRAGMA wal_checkpoint`, + `PRAGMA incremental_vacuum`, + `PRAGMA shrink_memory`, + `PRAGMA main.optimize`, + `PRAGMA main.wal_checkpoint;`, + `PRAGMA not_a_real_pragma`, ``, } for _, q := range bad { @@ -676,6 +688,38 @@ func TestRunSQLRefusesPragmaAssignments(t *testing.T) { } } +func TestRunSQLRefusesBareActionPragmas(t *testing.T) { + dir := t.TempDir() + chainFixture(t, dir) + st := open(t, dir) + ingest(t, st) + // Bare PRAGMAs with no value can still act on the database: + // optimize rewrites sqlite_stat1, wal_checkpoint forces WAL + // frames into the main file, incremental_vacuum performs vacuum + // work, shrink_memory returns memory to the OS. None of them may + // run through the read-only surface, with or without a schema + // qualifier. + for _, q := range []string{ + `PRAGMA optimize`, + `PRAGMA wal_checkpoint`, + `PRAGMA incremental_vacuum`, + `PRAGMA shrink_memory`, + `PRAGMA main.wal_checkpoint`, + } { + if _, err := st.RunSQL(q); err == nil { + t.Errorf("RunSQL executed a bare action PRAGMA: %q", q) + } + } + // The established bare reads still run. + table, err := st.RunSQL(`PRAGMA journal_mode`) + if err != nil { + t.Fatal(err) + } + if len(table.Rows) != 1 || table.Rows[0][0] != "wal" { + t.Errorf("journal_mode is %v, want wal (the store DSN sets WAL mode)", table.Rows) + } +} + 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 c7168ee0f3..36347c2b6d 100644 --- a/pkgs/mcpx/internal/logstore/sql.go +++ b/pkgs/mcpx/internal/logstore/sql.go @@ -16,7 +16,11 @@ import ( // 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. +// rather than a value to assign. A bare PRAGMA (no value at all) is +// not automatically a question either — PRAGMA optimize and +// PRAGMA wal_checkpoint take no value and still change the database +// — so bare forms pass only when the name is in bareReadPragmas, +// the known reads. var readOnlyVerbs = map[string]bool{ "select": true, "explain": true, "pragma": true, "with": true, "values": true, } @@ -60,6 +64,42 @@ func CheckReadOnly(query string) error { // 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. +// bareReadPragmas are the PRAGMAs whose bare form — no value, no +// argument list — is a read: the name alone asks SQLite about the +// database, a setting, or a check result, and changes nothing. Any +// other bare PRAGMA is refused, because several bare PRAGMAs are +// actions, not questions: PRAGMA optimize rewrites sqlite_stat1, +// PRAGMA wal_checkpoint forces WAL frames into the main database +// file, PRAGMA incremental_vacuum performs vacuum work (its bare +// form shrank the freelist in rollback-journal mode), and +// PRAGMA shrink_memory releases memory back to the OS. Those four +// are deliberately absent here; refusing unknown names by default +// keeps the next action PRAGMA out as well. +var bareReadPragmas = map[string]bool{ + "analysis_limit": true, "application_id": true, "auto_vacuum": true, + "automatic_index": true, "busy_timeout": true, "cache_size": true, + "cache_spill": true, "cell_size_check": true, + "checkpoint_fullfsync": true, "collation_list": true, + "compile_options": true, "data_version": true, "database_list": true, + "defer_foreign_keys": true, "encoding": true, + "foreign_key_check": true, "foreign_keys": true, "freelist_count": true, + "full_column_names": true, "fullfsync": true, "function_list": true, + "hard_heap_limit": true, "ignore_check_constraints": true, + "integrity_check": true, "journal_mode": true, + "journal_size_limit": true, "legacy_alter_table": true, + "legacy_file_format": true, "locking_mode": true, + "max_page_count": true, "max_recursive": true, "mmap_size": true, + "module_list": true, "page_count": true, "page_size": true, + "pragma_list": true, "query_only": true, "quick_check": true, + "recursive_triggers": true, "reverse_unordered_selects": true, + "schema_version": true, "secure_delete": true, + "short_column_names": true, "soft_heap_limit": true, + "synchronous": true, "table_list": true, "temp_store": true, + "threads": true, "trusted_schema": true, "user_version": true, + "wal_autocheckpoint": true, "wal_index": true, + "writable_schema": true, +} + var functionFormReadPragmas = map[string]bool{ "table_info": true, "table_xinfo": true, "table_list": true, "index_list": true, "index_info": true, "index_xinfo": true, @@ -67,10 +107,11 @@ var functionFormReadPragmas = map[string]bool{ "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. +// checkReadOnlyPragma refuses a PRAGMA that carries a value, and any +// bare PRAGMA whose name is not a known read. Bare reads (PRAGMA +// journal_mode, PRAGMA user_version) pass via bareReadPragmas; 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, ";")) @@ -91,6 +132,23 @@ func checkReadOnlyPragma(trimmed string) error { 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 + } + // Bare form: no value and no argument list. The name alone must be + // a known read — see bareReadPragmas. A schema qualifier + // (main.wal_checkpoint) is stripped the same way as in the + // function-form branch, so it cannot smuggle an action name past + // the allowlist. + fields := strings.Fields(rest) + if len(fields) != 1 { + return fmt.Errorf("PRAGMA %q is not a well-formed read PRAGMA", rest) + } + name := fields[0] + if j := strings.LastIndex(name, "."); j >= 0 { + name = name[j+1:] + } + if !bareReadPragmas[strings.ToLower(name)] { + return fmt.Errorf("PRAGMA %q is not a known read PRAGMA; only the bare reads in the allowlist are allowed here", rest) } return nil } From c44c1a61ee44017faed09c5b85beb569c6ef5c71 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:55:18 -0600 Subject: [PATCH 2/2] =?UTF-8?q?fix(mcpx):=20disposition=20Codex=20findings?= =?UTF-8?q?=20on=20PR=20#6700=20=E2=80=94=20read=5Funcommitted=20read=20+?= =?UTF-8?q?=20registry=20guard=20(#6638)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit P2: PRAGMA read_uncommitted bare is a genuine setting read; added to bareReadPragmas (value forms still refused), with PRAGMA read_uncommitted retained as a guard-level regression example. P1: registry-driven class guard per pkgs/mcpx/internal/AGENTS.md — new sql_internal_test.go iterates bareReadPragmas asserting every registered read passes bare/schema-qualified/upper-case and its assignment (and, for bare-only reads, call) forms are refused; an independent hardcoded action oracle (optimize, wal_checkpoint, incremental_vacuum, shrink_memory) asserts those names stay out of the allowlist and refused in every form. --- pkgs/mcpx/internal/logstore/logstore_test.go | 1 + pkgs/mcpx/internal/logstore/sql.go | 1 + .../internal/logstore/sql_internal_test.go | 61 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 pkgs/mcpx/internal/logstore/sql_internal_test.go diff --git a/pkgs/mcpx/internal/logstore/logstore_test.go b/pkgs/mcpx/internal/logstore/logstore_test.go index 96a73449bb..96f2f05b0a 100644 --- a/pkgs/mcpx/internal/logstore/logstore_test.go +++ b/pkgs/mcpx/internal/logstore/logstore_test.go @@ -622,6 +622,7 @@ func TestReadOnlyGuardRejectsAnythingThatWrites(t *testing.T) { `PRAGMA compile_options`, `PRAGMA main.journal_mode`, `PRAGMA integrity_check`, + `PRAGMA read_uncommitted`, `EXPLAIN SELECT 1`, } for _, q := range ok { diff --git a/pkgs/mcpx/internal/logstore/sql.go b/pkgs/mcpx/internal/logstore/sql.go index 36347c2b6d..d9276d3620 100644 --- a/pkgs/mcpx/internal/logstore/sql.go +++ b/pkgs/mcpx/internal/logstore/sql.go @@ -91,6 +91,7 @@ var bareReadPragmas = map[string]bool{ "max_page_count": true, "max_recursive": true, "mmap_size": true, "module_list": true, "page_count": true, "page_size": true, "pragma_list": true, "query_only": true, "quick_check": true, + "read_uncommitted": true, "recursive_triggers": true, "reverse_unordered_selects": true, "schema_version": true, "secure_delete": true, "short_column_names": true, "soft_heap_limit": true, diff --git a/pkgs/mcpx/internal/logstore/sql_internal_test.go b/pkgs/mcpx/internal/logstore/sql_internal_test.go new file mode 100644 index 0000000000..bcc8dd2830 --- /dev/null +++ b/pkgs/mcpx/internal/logstore/sql_internal_test.go @@ -0,0 +1,61 @@ +package logstore + +import ( + "strings" + "testing" +) + +// TestBareReadPragmaRegistryGuard is the class guard for the bare-PRAGMA +// fix (#6638): it iterates the bareReadPragmas registry itself, so a name +// added to or dropped from the allowlist is classified consistently in +// every form — bare, schema-qualified, and upper-case — and no +// registered read can be turned into an assignment. +func TestBareReadPragmaRegistryGuard(t *testing.T) { + for name := range bareReadPragmas { + for _, q := range []string{ + "PRAGMA " + name, + "PRAGMA main." + name, + "PRAGMA " + strings.ToUpper(name) + ";", + } { + if err := CheckReadOnly(q); err != nil { + t.Errorf("registered bare read rejected: %q: %v", q, err) + } + } + // The assignment form of a registered read stays refused. + if err := CheckReadOnly("PRAGMA " + name + "=1"); err == nil { + t.Errorf("assignment form of a bare read was allowed: %q", name) + } + // The call form stays refused too, except for the function-form + // reads whose argument names an object to inspect. + if !functionFormReadPragmas[name] { + if err := CheckReadOnly("PRAGMA " + name + "(1)"); err == nil { + t.Errorf("call form of a bare-only read was allowed: %q", name) + } + } + } +} + +// TestBareActionPragmasStayRefused is the independent oracle for the +// action side of the class: these names come from SQLite's PRAGMA +// inventory, not from the registry under test, and every one must stay +// out of the allowlist and refused in every form. +func TestBareActionPragmasStayRefused(t *testing.T) { + actions := []string{ + "optimize", "wal_checkpoint", "incremental_vacuum", "shrink_memory", + } + for _, name := range actions { + if bareReadPragmas[name] { + t.Errorf("action PRAGMA %q is in the bare read allowlist", name) + } + for _, q := range []string{ + "PRAGMA " + name, + "PRAGMA main." + name, + "PRAGMA " + strings.ToUpper(name), + "PRAGMA " + name + "(1)", + } { + if err := CheckReadOnly(q); err == nil { + t.Errorf("action PRAGMA was allowed: %q", q) + } + } + } +}