fix(db): refuse the column notation without parentheses and aclitem in plugin SQL

Postgres reads x.f as the call f(x) when x has no column f. The guard counted
that spelling only after ")." or "].", so over a function in FROM that
returns a single value, or a parameter, the allowlist was bypassed:
"select g.obj_description from generate_series(...) g" returned the comments
on every object of the database, and g.current_schemas, g.txid_status and
$1.ts_token_type passed as well. A refused catalog function after a dot is
now refused too, except the names in COLUMN_NAMES (money, name, oid, version,
xid, xml), which take no argument or only convert it to their type, so t.name
keeps working. PGPLUG-021 holds that list to pg_proc on the lane's server.

The type aclitem names roles: '=r'::aclitem printed the cluster's first
superuser and 'x=r/x'::aclitem told whether a role exists. It is refused in
every spelling like the reg* types.

The allowlist itself moves to assets/postgres/plugin-functions.txt, beside
the catalog list it is checked against. As TypeScript it spelled 'like' and
'json_each', names of Postgres functions that the SQLite dialect gate reads
as SQLite SQL.
This commit is contained in:
Maurice
2026-11-17 23:30:29 +00:00
parent adcda60f7d
commit 636fc7d0a6
9 changed files with 588 additions and 449 deletions
+10 -7
View File
@@ -219,18 +219,21 @@ What changes on Postgres:
aggregates and window functions (`count`, `sum`, `string_agg`, `row_number`, `lag`, ...),
UUIDs (`gen_random_uuid`), ranges, geometry, network addresses and full text search
(`to_tsvector`, `ts_rank`, ...). Any other function of Postgres's catalog is refused
however you spell the call (`f(...)`, `"f"(...)`, the column notation `(x).f`): the ones
that read the catalogs or the server rather than their arguments (`obj_description`,
`col_description`, `format_type`, `version`, `current_database`, `inet_server_addr`,
`txid_current`, ...), and so is every new function a later Postgres adds until TREK has
however you spell the call (`f(...)`, `"f"(...)`, the column notation `(x).f`, `t.f`,
`$1.f`): the ones that read the catalogs or the server rather than their arguments
(`obj_description`, `col_description`, `format_type`, `version`, `current_database`,
`inet_server_addr`, `txid_current`, ...), and so is every new function a later Postgres adds until TREK has
decided about it. The keywords `current_user`, `session_user`, `current_role`, `user`,
`system_user`, `current_catalog` and `current_schema` are refused too. `coalesce`,
`nullif`, `greatest`, `least`, `CASE` and `CAST` are syntax and always work. The complete
list is `PLUGIN_FUNCTIONS` in TREK's
[`plugin-sql-functions.ts`](https://github.com/liketrek/TREK/blob/main/server/src/db/engine/postgres/plugin-sql-functions.ts).
list is TREK's
[`plugin-functions.txt`](https://github.com/liketrek/TREK/blob/main/server/assets/postgres/plugin-functions.txt).
A table or type of yours may still share a refused function's name where it cannot be a
call (`INSERT INTO version (...)`, `CREATE TABLE version (...)`, `REFERENCES version (id)`);
as the table of `CREATE INDEX ... ON` it cannot, so name it differently.
as the table of `CREATE INDEX ... ON` it cannot, so name it differently. Postgres reads
`t.f` as the call `f(t)` when `t` has no column `f`, so a column named like a refused
function cannot follow a table's name and a dot either (`name`, `version`, `money`, `oid`,
`xid` and `xml` can); write it without the table's name, or name it differently.
- **You change your own objects only.** Right before an `ALTER`, `DROP`, `REFRESH`,
`TRUNCATE` or `CREATE INDEX ... ON` runs, Postgres resolves the object it names (and the
tables a `CREATE`/`ALTER TABLE` ties to with `REFERENCES`, `INHERITS`, `PARTITION OF`,