Repository navigation
With plugins_require_parse, refuse string function bodies and DO blocks while a table is listed in table access - #37
Conversation
A function or procedure body given as a string, and a DO block, is code the parser doesn't read, so table access can't see the tables it names. While table access blocks any table, it refuses CREATE FUNCTION with a string body. DO and string-bodied CREATE PROCEDURE don't parse; with plugins_require_parse, an unparsed query holding one is refused while any table is gated, as an unparsed VACUUM is. Bodies given as SQL (BEGIN ATOMIC, RETURN) are parsed and checked as before. Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at d94ff3de: not clear. One blocker and one design decision for the author.
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 blocker is resolved.
Code. It does what it says for the statements it sees, and the tests hold up: each new or changed assert fails with the fix reverted.
Blocker. The docs and the description say a string-bodied CREATE FUNCTION is refused whenever a table is blocked. Without plugins_require_parse, a CREATE FUNCTION ... COST 1 AS $$...$$ (unparsed) or a DO block that creates the function is forwarded, so that's not a guarantee. Details are inline at docs/plugin-registration.md:220-225.
Design question (the author decides).
- Under
plugins_require_parse, the PR closes a real gap.DO $$ BEGIN EXECUTE '... secr' || 'ets'; END $$and string-bodied functions could read a blocked table, and nothing caught them. This matches the existing strict-modeVACUUMrule. - Without it, the always-on parsed refusal costs a lot for little protection. It breaks every migration that creates a plpgsql or SQL function with a quoted body (
updated_at/audit trigger functions in Rails, Django, Flyway, Liquibase, Alembic and Prisma;pg_dumprestores), and it's bypassed withCOST 1or aDOwrapper. - Recommendation: apply the parsed refusal only when
plugins_require_parseis on. Then strict mode means one thing, refusing code pgcat can't read, and other pools keep today's name-based guarantee, documented as such.- Per-pool scope (any blocked table refuses all string bodies) follows from the body being opaque, so it's justified.
- A text check of bodies isn't a security rule, as the description itself notes.
- Parsing
LANGUAGE sqlbodies is a good follow-up for strict-mode compatibility.
- Either way, other built-ins also run SQL given as text and stay open in both modes. Don't describe the policy as "no string body can reach a blocked table":
SELECT query_to_xml('select * from secrets', true, false, '')parses and is allowed, andts_statanddblinkbehave the same way.
Nonblocking (inline). standard_conforming_strings = off makes the tokenizer and the server lex strings differently. Also: make has_string_body fail closed, a cheap prefilter before tokenizing, CONFIG.md wording, the documented-limitation test, and one question about the gated set.
Description (nonblocking apart from the blocker text):
- Quoted: "while any table is blocked on a pool, no function body given as a string is accepted." Suggested: "while any table is blocked on a pool, table access refuses a parsed
CREATE FUNCTIONwith a string body. Withplugins_require_parse, unparsedDO,CREATE FUNCTIONandCREATE PROCEDUREare refused too, so no string body reaches the server. Without it, aDOblock or aCREATE FUNCTIONthe parser can't read is still forwarded." - Quoted: "This PR is independent of #35. If #35 lands first, its module-doc sentence ... should then point to this rule."
git merge-treereports a content conflict with #35 insrc/plugins/table_access.rs(theuse sqlparser::ast::Ellipsislist, and thefoundchain inrun()).docs/plugin-registration.mdmerges cleanly but contradicts itself: #35's bullet says string function bodies "aren't read", and this one says they're refused. Suggested: "This PR is independent of #35 in behavior but conflicts intable_access.rs. Whichever lands second should drop 'a function body given as a string' from #35's 'names inside strings' list, in both the module doc anddocs/plugin-registration.md, and point to this rule." - Title, optional: "Refuse string function bodies, and with plugins_require_parse DO blocks, while a table is blocked".
- The receipt "
cargo test --lib(393 passed)" and the 42501 code are accurate.
What ran:
cargo +stable test --libat this head: 393 passed, after forcing a rebuild of a stale shared target.- A negative control: reverting both checks fails all three new or changed tests.
cargo fmt --checkis clean.- Probes of
runs_string_body, parsing and tokenizing. - Limits: no Postgres server was run, and clippy wasn't run.
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
Created with Perplexity |
Table access refuses a string-bodied CREATE FUNCTION only when the parser reads it. Without plugins_require_parse, a DO block or a CREATE FUNCTION with an option the parser can't read (COST, ROWS) is forwarded, so the docs and CONFIG.md now say the refusal is a guarantee only with that setting. A router test pins it: those statements don't parse, and they're refused with the setting and forwarded without it, even when the body names a gated table. has_string_body now fails closed: any CREATE FUNCTION or CREATE PROCEDURE whose body isn't RETURN or BEGIN ATOMIC, or that has none, counts as unread, so a body form a later parser version gives isn't allowed unread. The table-scoped test keeps a forwarded example of SQL that builds a name, and CONFIG.md mentions the refusal of text that can't be tokenized. Co-authored-by: Perplexity <noreply@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
|
Delta
Still open: the policy itself. The parsed refusal is still unconditional at this head. Once the author decides, a fresh pass on a different model will review that head. Reviewer: Claude Opus, High effort, delta check of the changed hunks. I didn't rerun the tests; CI on this head is green, and the author's negative controls are reported in the PR. Comment-only. Created with Perplexity |
Without plugins_require_parse, an unparsed DO block or CREATE FUNCTION is forwarded anyway, so refusing the parsed ones guarded nothing there. Table access now refuses a parsed string body only on pools that set it, and checks names only on the others, as before. Text that can't be tokenized is only read as a possible DO block or function if it has one of those words, which also skips tokenizing most long queries. Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at 01e57dc: not clear yet. There's one blocker.
- Blocker:
runs_string_bodymissesDO/CREATE FUNCTION/CREATE PROCEDUREplaced afterCOPY ... FROM STDIN;(inline onsrc/query_router.rs).
Option (a) is sound: TableAccess::run refuses parsed string bodies only when plugins_require_parse() is set, and has_string_body now fails closed. Negative controls confirm the tests check both modes: forcing the condition always on fails 2 tests, and forcing it off fails 3. The title and description match the head, apart from the blocker. There are three nonblocking inline notes. A pre-existing tokenizer follow-up goes in a separate comment.
Evidence: cargo +stable test --lib gives 395 passed, cargo +stable fmt --check is clean, and CI is 10/10 green. Probe tests and a PostgreSQL 18 wire check confirmed the blocker. Integration harnesses against a live deployment weren't run.
Review passes: Claude Opus 5.5 High (initial and delta), then a fresh Gemini 4 Carbon High final. This is the final pass that found the blocker. After the fix I'll run a delta and a new independent final on the new head.
Created with Perplexity
The tokenizer reads the lines after `COPY ... FROM STDIN;` as the COPY's data, so a DO or a string-bodied CREATE FUNCTION or PROCEDURE on them wasn't seen, though the server runs them as statements. The data is now tokenized again and put back after the rest of the `;` line, so a statement that starts there and goes on in the data is read whole. Data ended by a `\.` line, which the server can read as part of a comment, is refused, and so is a query with more than four of these COPYs. The statement check matches keywords instead of lowercased strings, the prefilter comment says what the prefilter skips, and the plugin docs say the string-body rule applies while gated_tables is non-empty. Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at d62fb0e: not clear yet. There is one new blocker, and it is not in the COPY change.
- The COPY FROM STDIN blocker is fixed. I checked the splice against sqlparser 0.64's
capture_copy_data. Unterminated data runs to the end of the input, so there's at most one such capture and it's last in the source. The rest-of-line tokens come fromnext_token, which never producesCopyData. Retokenizing the data with a fresh tracker either finds statements or a nested capture, which the loop handles, and the bound keeps the cost linear. I also probed a few cases at this head with a scratch test, and each was refused: a multi-line comment on the;line,COPY a FROM STDIN; COPY b FROM\nSTDIN; DO ..., and a nested COPY with DO on the same data line. - Blocker:
runs_string_bodygets the lowercased query, and dollar-quote tags are case-sensitive, so mixed-case tags can hide aDOblock (inline atsrc/query_router.rs:2136). - Nonblocking: the docs paragraph doesn't mention the inline-COPY refusals (inline).
The title and description match this head.
Evidence: CI is green on d62fb0e (10/10). I ran the 46 query_router lib tests locally, plus a scratch test (not committed) and one query against PostgreSQL 18.6. I didn't rerun the full suite. Review: Claude Opus 5.5, High effort, delta pass. The fresh GPT-6.2 Astra High final will run on the head that fixes the blocker.
Created with Perplexity
might_touch_gated lowercased the query before it looked for DO and CREATE FUNCTION or PROCEDURE statements. Dollar-quote tags are case-sensitive, so the tokenizer could end a `$a$` string at `$A$`, read a later DO block as part of a string, and forward it. runs_string_body now gets the text as written; its keyword and substring checks already ignore case. CONFIG.md and the plugin docs also say that inline COPY FROM STDIN data ending with a `\.` line, or nesting more than four COPYs, is refused. Co-authored-by: Perplexity <noreply@perplexity.ai>
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at c7e5cd2: not clear. The only blocker is wording in the docs and description; the enforcement is sound.
- The dollar-tag fix is verified.
runs_string_body(original)is in place, the case is a refused body, and it passes as both simple query and Parse. - The COPY repair holds against sqlparser 0.64's capture code. The final pass covered LF, CR and CRLF, an empty tail, multi-line strings and comments on the
;line, two COPYs on one line, and the 4/5 boundary. - Blocker: CONFIG.md and the plugin-registration docs say the refusal follows the pool's
plugins_require_parse, but a registered process-wide plugin also turns it on (inline at CONFIG.md:419-422). The description has the same issue (top-level comment). - Nonblocking: the COPY clause needs the word qualifier, and the depth test should use literals (inline).
Evidence: CI 10/10 green. cargo test --lib passed locally (395). The final pass ran focused and scratch tests plus a constant mutation (4 to 3). Passes on this head: Claude Opus 5.5 High (delta) and GPT-6.2 Astra High (fresh independent final). Once a head fixes only this wording, I'll check it with a docs-only delta.
Created with Perplexity
|
Description: two rollout statements don't match this head (blocker-level, wording only)
The title is fine. Created with Perplexity |
The refusal follows the effective plugins_require_parse, which a registered process-wide plugin turns on for every pool. CONFIG.md and the plugin docs now say so. Their COPY clause also says it applies only to text with one of the three words. The depth test uses the documented 4 and 5, and a new case pins that a registered process-wide plugin makes table access refuse a parsed string body on a pool without plugins_require_parse. Co-authored-by: Perplexity <noreply@perplexity.ai>
|
Re the description comment: both statements now use your replacement wording. The Policy section defines strict mode as the pool setting Created with Perplexity |
artem-pplx
left a comment
There was a problem hiding this comment.
Verdict at 3ea9be8: clear.
- The blocker from c7e5cd2 is resolved: CONFIG.md, the plugin-registration docs and the body's Policy/Impact now describe strict mode as the pool flag or a registered process-wide plugin, including configured plugins' gated tables.
- The non-test code is byte-identical to c7e5cd2: both
src/hunks are insidemod test. So the enforcement clearance from that head carries over: the COPY FROM STDIN repair, original-case tokenization and the prefilter. - The new test pins the effective-flag rule: with the pool flag false, a string body is allowed with an empty registry and refused with an allow-only registered plugin. The depth test now pins the documented four allowed and five refused.
- Two small optional wording edits to the description are in a separate comment, along with a ledger of follow-ups.
Evidence: CI 10/10 green. Locally, the two string-body tests pass at this head, and cargo test --lib (395) passed at c7e5cd2 with the same runtime code. Passes on this head: Claude Opus 5.5 High (delta) and a fresh GPT-6.2 Astra High (bounded delta, second model). Before that: GPT-6.2 Astra High (independent final) at c7e5cd2, Gemini 4 Carbon High (final) at 01e57dc, and Claude Opus 5.5 High (pass 1 and deltas).
Created with Perplexity
|
Description: optional precision edits (nonblocking)
The title is accurate. Created with Perplexity |
|
Review ledger (follow-ups outside this PR's scope; none block it)
Disposition: recorded once. I won't reopen this review for these items. Created with Perplexity |
Why
Table access checks the tables a statement names. A function body given as a string (
CREATE FUNCTION ... LANGUAGE sql AS $$ SELECT ... FROM t $$, or plpgsql, or any other language) is opaque to the parser. A function created this way can read a table listed in table access (pools.<pool>.plugins.table_access.tables) that table access never saw, and later calls to it name only the function.DOblocks and string-bodiedCREATE PROCEDUREdon't parse at all, and the unparsed-query check only looks for listed names in their text, which a body can build at run time.Policy
On a pool with
plugins_require_parse, while any table is listed in table access, no string body reaches the server. Table access refuses a parsedCREATE FUNCTIONorCREATE PROCEDUREwith a string body, and unparsedDO,CREATE FUNCTIONandCREATE PROCEDUREare refused too. The guarantee holds only in strict mode: the pool setsplugins_require_parse, or a process-wide plugin is registered, which makes pgcat require parsing on every pool. Otherwise, when the pool flag is off and no process-wide plugin is registered, built-in table access checks names only, as on main: parsed or not, a string body is forwarded, because an unparsedDOblock orCREATE FUNCTION(one withCOSTorROWS) would be forwarded anyway. Either way, functions that run SQL given as text (query_to_xml,dblink) aren't refused.plugins_require_parse, table access refusesCREATE [OR REPLACE] FUNCTIONandCREATE PROCEDUREunless the body is SQL it can read. That meansRETURN exprorBEGIN ATOMIC ... ENDfor a function, andBEGIN ATOMIC ... ENDfor a procedure. String bodies (AS '...',AS $$...$$, with or without a link symbol, in any language) are refused, and so is any body form the parser adds later. The 42501 message ispermission for a function body given as a string denied.plugins_require_parse, an unparsed query is refused while any table is listed in table access or gated by a configured plugin, if one of its statements starts withDOorCREATE [OR REPLACE] FUNCTION|PROCEDURE. This follows the existing rule for unparsedVACUUM.do,functionorprocedure(in any case) isn't tokenized, since keywords can't be escaped. Text with one of them that can't be tokenized is refused.COPY ... FROM STDIN;as the COPY's data, but the server runs them as statements, since COPY data comes in messages of its own. So that data is tokenized again and put back after the rest of the;line, and a statement that starts on that line and goes on in the data is read whole. Data ended by a\.line, which the server can read as part of a comment or string, is refused. So is a query with more than four such COPYs, which keeps the cost linear.standard_conforming_strings = on. A session that turns that off can make pgcat and the server end a string in different places. That affects every name check, not just this one, and is left for a separate change.BEGIN ATOMIC ... END,RETURN expr) is parsed, so it's allowed and checked by the tables it names, with or withoutplugins_require_parse.identity_tenant_parameterare covered either way: its tenant guard already refuses everyDOandCREATE [OR REPLACE] FUNCTION|PROCEDURE, parsed or not.Alternatives, if this is too strict for strict-mode pools that create functions:
'secr' || 'ets') and pass.LANGUAGE sqlbodies and check them likeBEGIN ATOMICbodies, refusing other languages and bodies that don't parse. This is precise for SQL functions but still refuses plpgsql.Change
table_access.rs:has_string_body, applied to nested statements as well, and a refusal that applies when the router requires parsing (QueryRouter::plugins_require_parse). It comes after theVACUUMof every table check, and allows only the body forms listed above.query_router.rs:runs_string_body, used bymight_touch_gatedwhile any plugin gates a table, which runs only withplugins_require_parse. A word check comes before tokenizing. UnterminatedCOPY ... FROM STDINdata is tokenized again, and each statement's first four keywords are matched without allocating.configured_tests.rs: withplugins_require_parse, a table-scoped configured plugin now refuses the unparsedDOblock that builds a listed name. Without the setting it still forwards it. Aquery_to_xmlcall that builds the name is still forwarded, which pins that limitation.docs/plugin-registration.mdstate the strict-mode guarantee, that built-in table access checks names only when the pool flag is off and no process-wide plugin is registered, and that configured plugins handle parsed string bodies for their own tables. Both say which inlineCOPY ... FROM STDINdata is refused, anddocs/plugin-registration.mdsays the unparsed string-body rule applies whilegated_tablesis non-empty.Impact
Strict pools (
plugins_require_parse, or any pool while a process-wide plugin is registered) whose table access lists a table can no longer create functions or procedures with string bodies through pgcat. Strict pools where table access or a configured plugin gates any table refuse unparsedDO,CREATE FUNCTIONandCREATE PROCEDURE. This newly affects pools that forwarded them under a table-scoped plugin; existing blanket refusals are unchanged. Migrations that need them must run against the server directly or useBEGIN ATOMICbodies. Other pools behave as on main.This PR is independent of #35 in behavior but conflicts in
table_access.rs(theuse sqlparser::astlist and the helper functions aftergranted()). Whichever lands second should change "a function body given as a string" in #35's "names inside strings" list, in both the module doc anddocs/plugin-registration.md. It should say that the body is refused withplugins_require_parse(this rule) and unread without it.Tested locally:
cargo test --lib(395 passed). These tests carry most of the coverage:test_string_function_bodies_are_refused_with_plugins_require_parse: a strict router refuses every string body,AS $$ SELECT count(*) FROM secrets $$included. A router without the setting allows them, and both refuse aBEGIN ATOMIC/RETURNbody that namessecrets.test_parsed_string_bodies_are_refused_only_with_plugins_require_parse: the same, through the router's plugin path, withsecretsstill listed afterplugins_require_parseis turned off. With the pool flag off, a string body that names no listed table is forwarded with no plugin registered and refused once an allow-only process-wide plugin is registered.test_only_sql_function_bodies_are_read: a hand-built procedure bodyAs(..)and a missing function body are refused in strict mode.test_unparsed_string_bodies_are_rejected_while_a_table_is_gated:CREATE FUNCTION ... COST 1doesn't parse, is refused withplugins_require_parse, and is forwarded without it. Untokenizable text is refused only if it has one of the words. ADO,CREATE FUNCTIONorCREATE PROCEDUREafterCOPY ... FROM STDIN;is refused. That covers one split across the;line and the data, data ended by\.inside a comment, and five nested COPYs.COPY do_log FROM STDIN;\nSELECT 1and four nested COPYs are forwarded. ADOblock between strings with mixed-case dollar-quote tags ($a$ ... $A$ ... $a$) is refused.unparsed_queries_are_refused_unless_the_plugin_is_table_scopedNegative controls:
plugins_require_parse(the earlier behavior) fails the first two tests.runs_string_bodyfails the fourth at the first COPY case. Re-checking the COPY data on its own, without putting it back after the;line, fails it at the splitCREATE FUNCTIONcase. Tokenizing the lowercased text fails it at the mixed-case tag case. SettingCOPY_DATA_READSto 3 fails it at the four nested COPYs.QueryRouter::plugins_require_parsefails the second at the process-wide plugin case.has_string_bodyfails the third.plugins_require_parsefails the fourth and fifth.cargo fmt --checkis clean, andcargo clippy --libreports nothing in the changed code. In CI, thetestjob passes on this head: 395 + 4 Rust tests, and 297 Python tests passed with 14 xfailed.Created with Perplexity