Summary
SurrealDB has an authorization bypass in which authenticated users can bind protected variables as FOR loop variables and supply forged values. If table permissions rely on those variables, a VIEWER may be able to read, modify, or delete data beyond their authorization.
Affected context
The issue affects SurrealDB versions from 1.0.0 to before 4.0.0 when queries use nested FOR loops that bind protected variables such as $access, $auth, or $token. It becomes risky when table permission predicates rely on these variables and the query is run by an authenticated user with limited privileges, such as a VIEWER.
Exploit preconditions
- Attack surface: SurrealDB query execution
- Required access: An authenticated account able to submit queries, including a VIEWER role
- Trigger point: Evaluation of a query containing nested FOR loops that bind protected variables
- Why exploitation is possible: Forged loop-variable values can shadow protected variables used by table permission predicates
Why this happens
The authorization logic does not reliably distinguish protected context variables from user-controlled FOR loop bindings. As a result, permission predicates may evaluate attacker-supplied values instead of trusted authorization state. This is an authorization enforcement flaw mapped to CWE-863: Incorrect Authorization.
Potential impact
- A VIEWER may be able to read records outside their permitted scope.
- An authenticated user could modify or delete data they should not control.
- Applications relying on table permissions may experience data exposure or integrity loss.
General mitigation guidance
Defensive patterns focus on keeping authorization context trustworthy and ensuring permissions cannot be influenced by query-local bindings.
- Treat protected authorization variables as reserved and prevent query constructs from shadowing them.
- Evaluate permissions using trusted server-side identity and access context.
- Use secure-by-default permission rules that deny access unless explicitly allowed.
- Limit query capabilities and user privileges to the minimum required.
- Avoid relying on client-controlled or query-controlled values as the sole basis for authorization.
How teams detect issues like this
- Review query parsing and evaluation paths for FOR bindings that can shadow protected variables.
- Use static analysis or code review checks to flag variable-name collisions involving authorization context.
- Test permission predicates with nested query constructs and low-privilege roles.
- Enforce policies that reserve security-sensitive variable names and require authorization checks to use trusted context.
Automated code review tools may help identify risky binding and permission-evaluation patterns.

