Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkgs/mcpx/internal/logstore/logstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ 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`,
`PRAGMA read_uncommitted`,
`EXPLAIN SELECT 1`,
}
for _, q := range ok {
Expand All @@ -640,6 +646,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 {
Expand Down Expand Up @@ -676,6 +689,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`,
Comment on lines +703 to +707

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add a registry-driven guard for bare PRAGMA classification

The new regression coverage checks a fixed list of action names, but no test enumerates the new bareReadPragmas registry or SQLite's PRAGMA inventory. Consequently, classification changes outside these examples can go untested, contrary to the source testing rule requiring a registry-driven guard for class-wide fixes. Add table-driven coverage of the read registry and an independent action/refusal oracle, including schema-qualified and assignment permutations; retain these examples as regression cases.

AGENTS.md reference: pkgs/mcpx/internal/AGENTS.md:L11-L12

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c44c1a6. Added a registry-driven class guard in pkgs/mcpx/internal/logstore/sql_internal_test.go (internal test package, per the existing pattern in this directory):

  • TestBareReadPragmaRegistryGuard iterates bareReadPragmas itself: every registered read must pass CheckReadOnly bare, schema-qualified (main.<name>), and upper-case, and its assignment form (<name>=1) must be refused — as must its call form, except for the established function-form reads whose argument names an object.
  • TestBareActionPragmasStayRefused is the independent oracle: the action names (optimize, wal_checkpoint, incremental_vacuum, shrink_memory) are hardcoded from SQLite's PRAGMA inventory, not derived from the registry, and each must be absent from the allowlist and refused bare, schema-qualified, upper-case, and in call form.

The example-based cases in logstore_test.go are retained as regression cases. Verified: go test ./internal/logstore -count=1 pass; the guard set also passes -race -shuffle=on -count=5.

`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)
Expand Down
69 changes: 64 additions & 5 deletions pkgs/mcpx/internal/logstore/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -60,17 +64,55 @@ 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,
"read_uncommitted": 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,
Comment on lines +97 to +98

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the read_uncommitted setting query in the allowlist

For users inspecting the connection's isolation setting, mcpx log sql 'PRAGMA read_uncommitted' now fails because read_uncommitted is absent from this allowlist. This previously accepted statement is a genuine setting read: SQLite documents the bare query separately from its assignment form. Include it among the known reads while continuing to reject its value-carrying forms, so blocking action PRAGMAs does not also remove this read-only diagnostic.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c44c1a6. read_uncommitted is added to bareReadPragmas — the bare setting query passes again, while PRAGMA read_uncommitted=ON and PRAGMA read_uncommitted(1) remain refused by the value-form branches. PRAGMA read_uncommitted is also retained as a guard-level regression example in TestReadOnlyGuardRejectsAnythingThatWrites, and the new registry guard in sql_internal_test.go covers its permutations. Verified: go test ./internal/logstore -count=1 pass.

"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,
"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.
// 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, ";"))
Expand All @@ -91,6 +133,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
}
Expand Down
61 changes: 61 additions & 0 deletions pkgs/mcpx/internal/logstore/sql_internal_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
Loading