Summary
WeKnora's query engine contained a SQL injection flaw in how it validated PostgreSQL array and row expressions, enabling attackers to bypass input protections.
By embedding dangerous PostgreSQL functions inside these expressions and abusing large object and library loading features, an unauthenticated attacker could reach remote code execution on the database server with database user privileges.
Affected context
This vulnerability arises in WeKnora deployments where user-controlled input is translated into PostgreSQL queries that include array expressions (e.g., {...} or ARRAY[...]) and row expressions (ROW(...)).
The custom validation layer focuses on top-level query structure but does not recursively inspect nested nodes inside these composite expressions.
In environments where the database account has access to large object operations or can load server-side libraries, this gap in validation can turn what appears to be a constrained query interface into a powerful execution primitive.
Exploit preconditions
- Attack surface: Publicly exposed WeKnora endpoints that accept user input for building or filtering database queries.
- Required access: Network access to the WeKnora service; no authentication is strictly required if the affected endpoints are anonymous.
- Trigger point: Submission of crafted query parameters that are serialized into PostgreSQL array or row expressions and passed through the insufficient validation layer.
- Why exploitation is possible: The validator treats array and row expressions as atomic, trusted units and does not descend into their child nodes to inspect embedded function calls, operators, or expressions. This allows dangerous SQL constructs to be "smuggled" inside these containers, bypassing blacklist/whitelist checks and ultimately invoking PostgreSQL features (large objects, dynamic library loading) that can be chained into arbitrary code execution.
Why this happens
At its core, this is a SQL Injection (CWE-89) issue caused by incomplete validation of dynamically constructed SQL queries.
The query builder attempts to enforce safety by validating allowed operations, but it does so at an insufficient depth: array and row expressions are validated only at the outer node, while their internal expression trees remain unchecked.
This creates a mismatch between the framework's security model ("only safe SQL is allowed") and PostgreSQL's actual expressive power inside composite expressions, where arbitrary functions and operators can be nested.
Because validation is not structural and recursive, untrusted user input can be embedded in these expressions in ways the validator fails to recognize as dangerous, leading to unintended execution of attacker-controlled SQL, and, with PostgreSQL's advanced features, escalation to OS-level code execution.
Potential impact
- Execution of arbitrary SQL statements within the database context.
- Remote code execution on the database server with the privileges of the database user.
- Exfiltration or corruption of sensitive data stored in the database.
- Creation or modification of database roles, tables, and functions where permissions allow.
- Abuse of PostgreSQL large object and file-related capabilities to read or write files accessible to the database process.
- Establishment of a persistent foothold on the database host and potential pivoting to other systems.
General mitigation guidance
Effective mitigation focuses on removing the need to trust dynamically assembled SQL and ensuring input never alters query structure:
- Prefer parameterized queries and bind parameters for all user-supplied values, including those appearing inside array and row expressions, so that the database treats them strictly as data.
- Replace ad hoc validation and string concatenation with structured query builders or ORM-style abstractions that generate syntactically safe SQL trees by construction.
- Where dynamic query composition is unavoidable, implement recursive, AST-level validation that walks the entire expression tree, including child nodes inside arrays, rows, and functions, and rejects any unapproved constructs.
- Restrict the database role used by the application to the least privileges required: avoid granting capabilities for large object manipulation, file access, extension management, or dynamic library loading.
- Apply defense-in-depth controls such as database-level allowlists for installed extensions, hardened
postgresql.confsettings that limit unsafe features, and OS-level controls (e.g., restricted directories, SELinux/AppArmor profiles) around the database process.
How teams detect issues like this
Detection is best approached from both code and runtime perspectives:
- Static analysis and code review
- Flag any path that constructs SQL from raw strings and interpolates user input, especially where arrays (
ARRAY[...],{...}) orROW(...)constructs are built manually. - During review, look for validators that check only "top-level" patterns (e.g., simple regex or shallow parse) instead of traversing full parse trees.
- Enforce development policies that require parameterized queries and prohibit unchecked string concatenation for SQL.
- Automated scanning
- Use SAST tools configured with rules for SQL injection patterns and dynamic SQL construction, focusing on languages and libraries used by WeKnora.
- Integrate these tools into CI to block merges when insecure SQL construction patterns are detected.
- Runtime monitoring
- Enable and regularly inspect database logs for anomalous queries containing unexpected functions, large object operations, or library/extension management commands.
- Configure alerts on suspicious patterns, such as repeated use of
lo_*functions,COPY-to/from-program/file constructs, or extension loading attempts triggered from application accounts.

