Summary
pREST's _groupby handling can allow attacker-controlled SQL structure into a PostgreSQL query because its validation does not sufficiently constrain function expressions. Depending on access controls and the expression used, this may expose data or cause denial of service; default configuration permits unauthenticated access.
Affected context
The issue affects pREST versions before 2.4.0 in the PostgreSQL adapter's handling of the _groupby parameter. The function-expression branch uses a weaker check than the validator used for _select, while the CRUD controller concatenates the GROUP BY expression into the query rather than parameterizing its SQL structure.
Exploit preconditions
- Attack surface: A pREST endpoint that processes
_groupbyfor a PostgreSQL query. - Required access: Default configuration permits unauthenticated access; restricted installations may require a low-privilege account with table-read access.
- Trigger point: The application builds and executes a query containing the supplied GROUP BY expression.
- Why exploitation is possible: Validation checks the outer function name but can permit nested subqueries, trailing UNION syntax, and inner
pg_functions; parameterization of WHERE values does not protect this SQL clause. - Limitations: UNION-based extraction depends on compatible column counts and types and was not verified end to end. A delay-producing function may cause denial of service without that alignment.
Why this happens
This is a SQL injection flaw (CWE-89). The adapter applies a comparatively weak expression check to the _groupby function-expression branch, validating only the outer function against an allowlist while leaving parts of the expression insufficiently constrained. The controller then concatenates that expression into SQL, allowing input to alter query structure; parameterized WHERE values do not make separately concatenated clauses safe.
Potential impact
- An attacker may be able to cause SQL execution beyond the intended GROUP BY expression.
- Data could be exposed if query manipulation succeeds and database permissions allow access.
- Delay-producing expressions could cause slow queries or denial of service.
- The practical impact may be limited by endpoint access controls, database privileges, and query compatibility requirements.
General mitigation guidance
Defensive patterns focus on preventing untrusted input from defining SQL structure:
- Prefer fixed, application-defined GROUP BY choices or map input to a strict set of approved identifiers and expressions.
- Reject unsupported expression forms rather than relying only on an outer function-name allowlist.
- Use parameter binding for data values, while recognizing that SQL identifiers and clause structure generally require safe mapping or query construction rather than value parameters.
- Reduce trust in request parameters and apply strict validation before query construction.
- Restrict database roles to the minimum permissions required and limit access to sensitive endpoints.
How teams detect issues like this
- Use static analysis to find request-derived values concatenated into SQL clauses, especially GROUP BY and other structural query components.
- Review validators for differences between related parameters, such as
_groupbyand_select, and check whether validation covers the complete expression rather than only its outer function. - Monitor query construction and database activity for unexpected subqueries, UNION operations, or unusually long-running requests.
- Enforce code-review and CI policies requiring safe query builders or approved mappings for SQL structure.

