# pREST _groupby function-expression SQL injection (incomplete fix of CVE-2025-58450)

pREST's _groupby function-expression branch still uses weak isSafeSQLExpression, allowing UNION/subquery SQL injection after the _select fix.

- Payout: Research
- Severity: high
- Program: pREST
- Found by: arpitjain099
- Disclosed: 2026-11-20
- CVE: CVE-2026-104129
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-v9v2-98xq-627c
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-v9v2-98xq-627c
- Tags: area:web domain:api domain:web impact:data-leak impact:dos kind:research platform:ghsa sev:high tech:go technique:chained technique:incomplete-fix technique:source-review vuln:sqli

**Target:** REST API table endpoint (_groupby query parameter) over Postgres
**Root cause:** The v2.3.0 fix hardened _select with sanitizeSelectField but left the sibling _groupby balanced-paren branch gated only by isSafeSQLExpression, which validates just the outermost function name against a 13-entry allowlist and permits spaces, quotes and parens.
**Steps:**
1. Send `GET /{database}/{schema}/{table}?_select=id&_groupby=upper((SELECT+1))+UNION+SELECT+current_user`
2. GroupByClause (postgres.go:1946) takes the raw-SQL-expression branch for balanced-paren input, gated by isSafeSQLExpression (line 2001, defined at 2043)
3. isSafeSQLExpression accepts the payload (only checks outermost function name + char allowlist; blocks ;, --, /*, unbalanced parens, outer pg_)
4. GroupByClause output is concatenated into sqlSelect at crud.go:133 (called from crud.go:131) and executed at crud.go:176 with only WHERE values parameterized
5. For DoS: `_groupby=abs((SELECT 1 FROM pg_sleep(3)))` executes unconditionally without column alignment
**How found:** Reviewed the v2.3.0 fix commit e88dabb, noticed _select was routed through sanitizeSelectField while the sibling _groupby path still used isSafeSQLExpression; compared the two gates' allowlists and found the weaker one permits UNION, nested subqueries and inner pg_ functions. Confirmed with go test negative controls.
**Exploitation:** Unauthenticated (default config) attacker with table read permission injects UNION SELECT or nested subquery via _groupby to exfiltrate data such as current_user, or uses pg_sleep for blind/DoS. End-to-end UNION exfiltration requires column/type alignment; blind char extraction is constrained by comma pre-splitting and blocked =/</> operators.
**Escalation:** Chains the incomplete fix: the _select gate rejects the same UNION payload, so _groupby is the surviving weak sibling for the same injection primitive.
**Insight:** When a fix hardens one query-parameter builder, enumerate every sibling builder sharing the same sink (here _groupby vs _select) and diff their validation gates; a character allowlist that only checks the outermost function name leaves nested subqueries and trailing UNION open.

## Proof of concept (copied exactly from the original report)

**UNION injection via _groupby request**

```
GET /{database}/{schema}/{table}?_select=id&_groupby=upper((SELECT+1))+UNION+SELECT+current_user. Sink trace: controllers/crud.go:131 calls GroupByClause unconditionally, concatenates it into sqlSelect (crud.go:133), executed at crud.go:176 with only the WHERE values parameterized.
```

**GroupByClause raw emission test payloads**

```
isSafeSQLExpression wrongly accepts four payloads and GroupByClause emits them raw (e.g. GROUP BY upper((SELECT 1)) UNION SELECT current_user, and GROUP BY abs((SELECT 1 FROM pg_sleep(3)))); negative controls with ; and -- are rejected
```

