Repository navigation
fix(mcpx): refuse bare action PRAGMAs in the read-only log-sql guard (#6638) #6700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For users inspecting the connection's isolation setting, Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in c44c1a6. |
||
| "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, ";")) | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new regression coverage checks a fixed list of action names, but no test enumerates the new
bareReadPragmasregistry 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 👍 / 👎.
There was a problem hiding this comment.
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):TestBareReadPragmaRegistryGuarditeratesbareReadPragmasitself: every registered read must passCheckReadOnlybare, 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.TestBareActionPragmasStayRefusedis 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.goare retained as regression cases. Verified:go test ./internal/logstore -count=1pass; the guard set also passes-race -shuffle=on -count=5.