Repository navigation
Check every table name a statement gives, and route SELECT INTO to the primary - #35
Conversation
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at 09346f4c: not clear. Two blockers.
Pass 1 of 2. This PR changes a security check, so a second reviewer on a different model will review the same head once the blockers are resolved.
-
Blockers. Two statements name a blocked table, read its rows on PostgreSQL 18, and are still allowed at this head. Both fixes are a few lines.
ALTER INDEX secrets RENAME TO xrenames the table (inline at 224-227).- The
TABLE secretsquery form, nested in a CTE, anINsubquery orCREATE TABLE AS(inline at 274-290).
main has both gaps, so nothing regresses. They block because the new docs and the description say these cases are refused. Narrowing the docs is an alternative, but the code fix is cheaper.
-
Correct.
- The
visit_statementsrecursion covers nested statements. With theStatement::Copyarm removed, the nested-COPY test fails. SELECT ... INTOgoes to the primary. Its test fails whenis_mutation_set_expris reverted.- I found no over-refusal of normal migration DDL beyond the
ON ALL TABLESrule (a question, inline at 305-320).
- The
-
Nonblocking (inline).
- Non-first
LIKE. - The doc examples should name
table_to_xml/query_to_xml. - Four AST walks could be one visitor.
- Non-first
Description (nonblocking):
- Quoted: "Statements that don't name one are unaffected".
GRANT/REVOKE ... ON ALL TABLES IN SCHEMA <any>is refused while any table is blocked. Suggested: "Statements that don't name one are unaffected, exceptGRANT/REVOKE ... ON ALL TABLES IN SCHEMA, which, likeVACUUMwithout a table, is refused while any table is blocked." - Quoted: "the old and new names of a rename". At this head
ALTER INDEX's own name is skipped. Accurate once the first blocker is fixed. - Quoted: "This repository's workflows don't run the test suite on pull requests."
tests.ymlis on main now (#34); this branch predates it, so only CodeQL runs. Suggested: "This branch predates the tests workflow; rebasing onto main runs it." - Terms: the code and tests say "forbidden", this description says "blocked", and #37 says "gated". Pick one, or define "blocked" once (a table listed in the
table_accessplugin'stables).
Overlap with #37: both PRs edit the same region of TableAccess::run and the same use sqlparser::ast::Ellipsis list, so expect a textual conflict. The doc sentence about string function bodies conflicts too (see the inline comment at docs 119-122).
What ran:
cargo +stable test --libat this head: 392 passed, matching the description.TableAccess::runover about 100 statements.- A negative control: the COPY arm removed and the SELECT INTO routing reverted fails all three relevant tests.
- A throwaway local PostgreSQL 18 to confirm the two leaks.
Reviewer: Claude Opus, High effort, pass 1 of 2. Comment-only.
Created with Perplexity
|
Follow-ups outside this PR's scope (from pass 1 at
Added at the final pass on
Added at the rebase check on
Created with Perplexity |
artem-pplx
left a comment
There was a problem hiding this comment.
Final pass at 1862237: both earlier blockers are fixed. One narrow new blocker in the non-first LIKE check (inline).
Verified fixed, each with a negative control (reverting the fix makes its test fail):
ALTER INDEX <t> RENAME TO xnow checks both names (test_names_visit_relations_misses_are_checked).TABLE <t>nested in a CTE,CREATE TABLE AS,IN,UNION,FROM (...)and similar is collected and refused (test_table_command_is_checked).
New blocker:
LIKEafter a column misses type-keyword table names. If a blocked table is namedjson,string,text,uuid,dateor a similar type word,CREATE TABLE c (a int, LIKE json)parses and is allowed, even withplugins_require_parse. The fix and the test are in the inline comment. Docs and the description need no change once it's fixed.
Checked and fine:
- Quoting and case folding, and 1-, 2- and 3-part names.
SELECT ... INTOtargets, including keyword names, and their routing to the primary.ALTER ... RENAMEacross the table, view, materialized view, foreign table and index kinds.- No over-refusal of normal DDL: a quoted
"like"column,CHECK (b LIKE ...),CREATE INDEX secrets ON other. GRANT/REVOKE ON ALL TABLESrefused only when a table is blocked.
Additions to the earlier ledger. Both are optional and predate this PR:
GRANT SELECT, MAINTAIN ON ALL TABLES IN SCHEMA s TO r(MAINTAIN is new in PG 17) doesn't parse in sqlparser 0.64. The unparsed check doesn't treat it as every-table, so it's forwarded even withplugins_require_parse. The same goes forCREATE PUBLICATION ... FOR ALL TABLES. A follow-up could add both to the every-table words, next tovacuum.CREATE PUBLICATION ... FOR TABLE t,SECURITY LABEL ON TABLE t,REINDEX,CLUSTERandALTER SEQUENCE ... RENAMEdon't parse. They're refused withplugins_require_parse(the text names the table) and forwarded without it, as documented.
Ran on this head: cargo fmt --check, cargo test --lib (394 passed), the two negative controls, a 78-case router/plugin probe, and live PostgreSQL 18 runs confirming the scenario.
Reviewer: Gemini 4 Carbon, High effort, final independent pass (pass 2, after Claude Opus High). Comment-only.
Created with Perplexity
artem-pplx
left a comment
There was a problem hiding this comment.
Final pass at 1fd18e7d: clear. No blockers.
Both fixes hold, and each one's test fails when the fix is removed:
- A non-first
LIKE <t>where<t>parses as a built-in type (json,string,timestamptzand others) is now refused. CREATE FOREIGN TABLE c (LIKE secrets) SERVER sis now refused.
No remaining parsed-SQL bypass was found in the LIKE handling:
- Type aliases (
int4,float8,bool,timestamptz,double) print back as the word that was read. - Postgres rejects unquoted multi-word types, arrays and type modifiers after
LIKE. A quoted name keeps its exact identifier. - Postgres 18.6 rejects
LIKEinsideCREATE TYPE ... AS,ALTER TABLE ... ADD,PARTITION OFcolumn lists andCREATE TABLE ... AS.
No over-refusal was found: a quoted "like" column, ordinary column types, and LIKE of an unlisted table named as a type are all still allowed.
Description (nonblocking). In Impact:
-
"the non-test code of
table_access.rsreverted to the previous commit"The previous commit is now
18622378. Reverting to it doesn't failtest_table_command_is_checked. Suggested: "reverted to09346f4". -
"(which also checks every type name the parser knows)"
The loop skips keywords that don't parse after
LIKE, and it tests single words only. Suggested: "(which also checks every single-word type name the parser accepts afterLIKE)".
What ran on this head:
cargo +stable test --lib: 395 passed.fmt --checkandclippy --lib --tests: clean.- Two negative controls, each failing the expected tests.
- Postgres 18.6 grammar checks.
- All 10 CI checks green.
Reviewers: Claude Opus High ran pass 1 and the delta checks. A fresh Gemini 4 Carbon High agent ran this final, in place of the planned Astra model, which isn't available. Pass count: 1 full + 2 delta + 2 finals. The follow-up list in the earlier comment is updated. This is a comment-only review.
Created with Perplexity
…e primary visit_relations reports the relations a statement reads or writes, not every table it names. Table access now also checks the names it skips, in nested statements too: COPY, DROP, GRANT/REVOKE, COMMENT, CREATE/DROP TRIGGER, LIKE, REFERENCES, SELECT INTO targets and RENAME TO. GRANT/REVOKE on all tables in a schema is refused while any table is blocked, as VACUUM of every table is. The module and plugin docs state that the check goes by name. SELECT ... INTO creates a table, so it's a mutation for routing. Co-authored-by: Perplexity <noreply@perplexity.ai>
Postgres renames a table with ALTER INDEX ... RENAME TO, so both names are checked, not only the new one. TABLE t isn't an ObjectName, so visit_relations skips it; the query visitor now collects it in every query, nested ones too (CTEs, IN subqueries, CREATE TABLE AS, set operations). The parser reads a LIKE that isn't a table's first element as a column named like, whose type is the table; that column counts as a LIKE. Tests pin that GRANT/REVOKE on all tables is refused in any schema while any table is listed in table access, that a plain REVOKE is refused, and that neither is refused with no table listed. The docs use "listed in table access", name table_to_xml/query_to_xml, and say which same-named objects are allowed. Co-authored-by: Perplexity <noreply@perplexity.ai>
The parser reads a LIKE after a column as a column named like, and turns a table named as a type it knows (json, text, timestamptz) into that type, so only a custom type was checked. It reads every LIKE in CREATE FOREIGN TABLE the same way, and that statement checked only its parents. Co-authored-by: Perplexity <noreply@perplexity.ai>
…required The "names inside strings" lists said a function body given as a string isn't read. With plugins_require_parse or a process-wide plugin, table access refuses it instead; without either, it's forwarded unread. Co-authored-by: Perplexity <noreply@perplexity.ai>
1fd18e7 to
977783c
Compare
perplexedmontagna
left a comment
There was a problem hiding this comment.
Approved at 977783c4. I found no merge blockers. I traced the name checks through simple queries, extended Parse, and the final rewrite. The regressions cover both names of a rename, nested TABLE reads, and LIKE targets that parse as type names. With read/write splitting on, parsed SELECT INTO selects the primary; ordinary reads remain eligible for replicas.
I ran all 414 Rust library tests and both formatting checks locally. Separate router checks confirmed that EXPLAIN ANALYZE SELECT INTO and a batch with a read after SELECT INTO also select the primary. CI passed.
Two non-blocking follow-ups:
- In the plugin docs, qualify the schema-wide grant/revoke rule as applying to statements PgCat parses. Unsupported syntax follows the pool's existing parse-failure policy; keep PostgreSQL privileges authoritative.
- On a warm pool with
db_activity_based_routingenabled, my router check selected the primary forSELECT INTObut a replica for a separate, immediate read of the new table. The role-selection branch does not mark the target recently written. This is pre-existing; recording the target would extend read-after-write routing to this case.
Created with Perplexity
artem-pplx
left a comment
There was a problem hiding this comment.
Clear at 977783c4: the rebase onto main (9c40703f, which includes #37) is resolved correctly, and the new docs commit is accurate. I checked only the rebase and 977783c, not the logic cleared earlier at 1fd18e7d.
Conflict resolution. git range-diff 388a33a8..1fd18e7d 9c40703f..977783c4 shows three resolutions, all in src/plugins/table_access.rs, and nothing else:
- The
sqlparser::astimport lists are merged, addingCreateFunction,CreateFunctionBodyandCreateProcedureBodyin the first commit andColumnDefandDataTypein the second. - #37's
has_string_bodynow sits after this PR's helpers. - The test helpers
refused_by(router, sql)andrefused_with(tables, sql)both call the newrefused_by_with.
Nothing from #37 was dropped. Outside table_access.rs and docs/, the changes from 1fd18e7d to 977783c4 match main's changes exactly. query_router.rs (plugins_require_parse, rejects_unparsed, the string-body routing) is unchanged from main. has_string_body, strict() and #37's tests (test_string_function_bodies_are_refused_with_plugins_require_parse, test_only_sql_function_bodies_are_read) are all present.
Merged run() denies exactly what either PR denies.
- This PR's three name checks (
visit_relations,unvisited_names,QueryNames) always run first and setfound. - The
every_tabledeny and #37's string-body deny run only whenfoundisNone. Otherwisefoundis denied at the end. - So a statement is refused if either PR alone would refuse it. A parsed
RETURNorBEGIN ATOMICbody still has its tables checked.
Docs commit 977783c. The module doc and docs/plugin-registration.md now say a string function body is refused with plugins_require_parse or a process-wide plugin, and forwarded without either. That matches QueryRouter::plugins_require_parse() (src/query_router.rs:2073-2076): has_process_plugins() || (plugins_enabled() && pool_settings.plugins_require_parse). One optional wording nit is added to the ledger as item 8.
CI: run 44587736714 is green on this head (414 lib tests; 315 Python passed, 14 xfailed). Nothing was rerun locally, because both reviewers found that the source agrees with CI.
Reviewers: two different models, both clearing this exact head, because table access is security-sensitive. Claude Opus High did the first pass, and Gemini 4 Carbon High did a fresh, independent final. One pass each.
Created with Perplexity
Why
Table access refuses a statement that reaches a table listed in table access (the
table_accessplugin'stables), and finds the tables withvisit_relations. That visitor reports the relations a statement reads or writes, not every table name it holds:DROP TABLE t,GRANT ... ON t,COMMENT ON TABLE t,CREATE TRIGGER ... ON t,CREATE TABLE c (LIKE t),REFERENCES t(a), the target ofSELECT ... INTO t, a nestedTABLE t, and both names inALTER ... RENAME TOall passed. Postgres also renames a table withALTER INDEX t RENAME TO n. TheCOPY tandVACUUM tchecks looked only at top-level statements, so aCOPYinside aBEGIN ATOMICbody or a rule's action passed too.Separately,
SELECT ... INTO tcreates a table, but the router classified it as a read.Change
unvisited_nameslists the table names a statement gives thatvisit_relationsskips: the tables ofCOPYandVACUUM, ofDROP(table, foreign table, view, materialized view, index, sequence), ofGRANT/REVOKE, ofCOMMENT ON(and a column's table), ofCREATE/DROP TRIGGER(and a constraint trigger'sFROM),LIKE(also after a column, and anyLIKEinCREATE FOREIGN TABLE, which the parser reads as a column namedlikewhose type is the table, a built-in type when the table is named as one, such asjson), column and tableREFERENCES,INHERITS,ALTER TABLEpartitions, parents, added constraints and columns, and the old and new names of a rename,ALTER INDEXincluded. It runs on every statement throughvisit_statements, nested ones included.SELECT ... INTOtargets andTABLE treads in every query, so a CTE, anINsubquery,CREATE TABLE AS, a set operation or a subquery holding one is covered. A top-levelTABLE tdoesn't parse with sqlparser 0.64, so it's handled as an unparsed query.GRANT/REVOKE ... ON ALL TABLES IN SCHEMAis refused, in any schema, while any table is listed in table access, following the existing rule forVACUUMof every table. The listed names carry no schema, so a name check can't tell which schema holds them.REVOKEis gated likeGRANT: the rule is that a statement naming a listed table is refused, whatever its effect. Migrations or role setup that grant on all tables must connect to the server directly, not through the pooler. Tests pin both choices, and that neither statement is refused when no table is listed.docs/plugin-registration.mdstate that the check goes by name. Views, partition parents, functions, rules and triggers that already exist are allowed by their own names, and so are an index or sequence created with a listed table's name. Names inside strings (foreign tableOPTIONS,dblink,table_to_xml/query_to_xml) aren't read. Those need server privileges. A string function body isn't read either: withplugins_require_parseor a process-wide plugin, table access refuses it, and without either it's forwarded.is_mutation_set_exprreturns true for aSELECTwithINTO.Impact
More statements naming a table listed in table access are refused with the existing 42501 message. Statements that don't name one are unaffected, except
GRANT/REVOKE ... ON ALL TABLES IN SCHEMA, which, likeVACUUMwithout a table, is refused while any table is listed in table access. A role, schema or column with the same name as a listed table is still allowed.SELECT ... INTOgoes to the primary.Tested locally:
cargo test --lib(414 passed), with regression cases intest_names_visit_relations_misses_are_checked,test_table_command_is_checked,test_like_of_a_table_named_as_a_type_is_refused(which also checks every single-word type name the parser accepts afterLIKE),test_every_table_grants_are_refused_only_with_a_forbidden_tableandtest_data_modifying_bodies_and_ctes_go_to_primary. With the new tests kept and the non-test code oftable_access.rsreverted to45a1d26,test_names_visit_relations_misses_are_checkedandtest_table_command_is_checkedfail, and every new refused case (ALTER INDEX, a non-firstLIKE, each nestedTABLE t) is allowed. Reading only custom types afterlike, as before, letsLIKE json,LIKE stringandLIKE timestamptzthrough, and checking only a foreign table's parents lets itsLIKEthrough.cargo fmt --checkpasses andcargo clippy --lib --examples -- -Dwarningsreports nothing on the lines this PR changes. The tests workflow passes at this head (414 library tests, 4 others, and 315 Python tests passed with 14 xfailed).Created with Perplexity