pREST _groupby function-expression SQL injection (incomplete fix of CVE-2025-58450)
Description
SQL injection (CWE-89) through the _groupby query parameter, a sibling path the v2.3.0 _select fix did not cover. It is reachable unauthenticated in the default configuration, or by an authenticated low-privilege reader of a single table.
The v2.3.0 fix (commit e88dabb) routed the _select field through a strict new gate, sanitizeSelectField (postgres.go:947, :1003, :2097). The _groupby path stayed on the much weaker isSafeSQLExpression. In adapters/postgres/postgres.go, GroupByClause (line 1946) sends any _groupby field containing balanced parens down the raw-SQL-expression branch (lines 1999 to 2006), gated only by isSafeSQLExpression (called at line 2001, defined at 2043). That gate checks just the outermost function name against a 13-entry allowlist, and its character allowlist permits spaces, single quotes and parens. It does block ;, --, /*, unbalanced parens and an outer pg_ prefix. It does not block a trailing UNION SELECT ..., a nested (SELECT ...) subquery, or an inner pg_ function, all three of which sanitizeSelectField rejects.
A request is enough:
GET /{database}/{schema}/{table}?_select=id&_groupby=upper((SELECT+1))+UNION+SELECT+current_usercontrollers/crud.go:131 calls GroupByClause unconditionally and concatenates the result into sqlSelect at crud.go:133, executed at crud.go:176 with only the WHERE values parameterized. The only thing standing in front of it is a table read-permission check, which the default config leaves passable unauthenticated. Same pipeline and same attacker model as the anchor advisory.
I verified this with an in-package Go test under ./adapters/postgres/ (PASS). isSafeSQLExpression accepts four payloads that GroupByClause then emits raw, among them GROUP BY upper((SELECT 1)) UNION SELECT current_user and GROUP BY abs((SELECT 1 FROM pg_sleep(3))). Negative controls carrying ; and -- are rejected, and sanitizeSelectField rejects the exact UNION payload the sibling gate waves through. I re-confirmed at HEAD that _groupby is gated by isSafeSQLExpression at postgres.go:2001 while _select uses sanitizeSelectField at postgres.go:947.
What I did not verify is end-to-end UNION exfiltration against a live Postgres, which needs column and type alignment. The pg_sleep denial-of-service primitive needs no alignment and works unconditionally. Character-by-character blind extraction is constrained, since commas are pre-split and =, < and > are blocked.
The other builders look sound to me: WhereByRequest, OrderByRequest, JoinByRequest, CountByRequest, the jsonb and tsquery paths, ->>having and ident.IsValid. The _groupby non-having function branch is the one weak sibling.
The fix would be to route _groupby through the same strict gate as _select, rejecting nested subqueries, UNION, inner pg_ functions and anything past a single allowlisted function call, or else to parameterize or whitelist the group-by columns outright.
CVE-2025-58450 (GHSA-p46v-f2x8-qp98, GO-2025-3941) and GHSA-qvx3-q8vx-9q3c, which covers _select and was fixed in v2.3.0, are the closest prior reports. Neither mentions _groupby or GROUP BY, and I found no existing advisory, issue or pull request covering this path.
I used AI assistance while investigating this.