Repository navigation
fix(mcpx/logstore): refuse value-carrying PRAGMAs in read-only log sql guard (#1664) #3516
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 |
|---|---|---|
|
|
@@ -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, "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, | ||
|
Comment on lines
+63
to
+67
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.
When inspecting the log schema with 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 afcce32: |
||
| } | ||
|
|
||
| // 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) | ||
|
|
||
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.
Script executed:
Repository: developing-today/code
Length of output: 2478
Script executed:
Repository: developing-today/code
Length of output: 7102
Script executed:
Repository: developing-today/code
Length of output: 5624
Web query:
SQLite PRAGMA syntax pragma-name quoted identifier "PRAGMA "table_info"(records)" documentationResult:
Web query:
SQLite double-quoted identifier allowed as PRAGMA name grammar pragma-stmt nm identifier documentationResult:
Strip identifier quotes before the function-form PRAGMA allowlist lookup.
checkReadOnlyPragmarefuses a quoted function-form read such asPRAGMA "table_info"(records). The name keeps its double quotes, so the lookup missesfunctionFormReadPragmas. The merge base accepted this statement, so the PR introduces the refusal. SQLite treats a double-quoted name as an identifier, so the statement is a valid read. Trim identifier quotes from the name before the lookup, and add the quoted form to the accepted-read test cases.Suggested fix
Based on learnings: "A bug gets a failing-first test in the same PR, covering its permutations, not just the one input that tripped it." Add
PRAGMA "table_info"(records)and an equivalent bracket or backtick form to the accepted-read cases inTestReadOnlyGuardRejectsAnythingThatWrites.Committable suggestion
Source: Learnings