Summary
Electric, a Postgres sync engine, contains an error-based SQL injection vulnerability in the order_by parameter of the /v1/shape API.
Any authenticated user who can call this endpoint may craft malicious ORDER BY expressions to read, modify, or delete data in the backing PostgreSQL database.
Affected context
The vulnerability exists in Electric deployments running affected versions of the Postgres sync engine where:
- The
/v1/shapeAPI endpoint is exposed to authenticated users, and - The
order_byrequest parameter is incorporated into SQL queries for PostgreSQL without strict validation, normalization, or parameterization.
It becomes relevant in multi-tenant or user-facing environments where clients can influence sorting behavior for synced data and where database permissions allow broad access through the application account.
Exploit preconditions
- Attack surface: HTTP(S) interface for the ElectricSQL
/v1/shapeAPI. - Required access: Valid authenticated access to the API (e.g., a legitimate Electric client or compromised client credentials).
- Trigger point: Submission of a crafted
order_byparameter that is directly interpolated into the SQL ORDER BY clause. - Why exploitation is possible: The application builds SQL statements by concatenating untrusted
order_byinput into the query text, instead of using parameterized queries or a strict allowlist of sortable columns. This enables attackers to append or alter SQL fragments and cause the database to return detailed error messages (error-based SQL injection), which can be leveraged to enumerate schema and manipulate data.
Why this happens
This issue is a classic SQL Injection (CWE-89) caused by unsafe construction of SQL queries from user-controllable input.
Rather than treating the order_by parameter as untrusted and mapping it to known-safe column identifiers, the implementation allows arbitrary expressions from the client to be embedded into the ORDER BY clause.
ORDER BY segments are often overlooked as "non-data-bearing", but when they are built through string concatenation or templating without proper validation, the database query parser treats the injected tokens as full SQL, enabling attackers to execute additional operations using the database's own privilege context.
The underlying bug class is failure to enforce strict separation between data and code in SQL, especially in secondary query components such as sorting, grouping, and filtering clauses.
Potential impact
- Attackers may read arbitrary data from any table accessible to the application's database role.
- Attackers may insert, update, or delete rows, leading to data corruption, destruction, or unauthorized changes.
- Attackers may exfiltrate sensitive information such as user records, secrets stored in the database, or internal metadata.
- Attackers may tamper with synchronization state, potentially causing data divergence or propagation of malicious data to clients.
- Attackers may leverage database-level capabilities (e.g., functions, extensions) to escalate impact, depending on configuration.
General mitigation guidance
Effective mitigation focuses on treating query structure as immutable and input as data only:
- Replace ad hoc string concatenation with parameterized query builders or ORM methods that do not allow arbitrary SQL fragments from clients.
- Normalize and strictly validate the
order_byparameter using an allowlist of supported columns and fixed sort directions (e.g.,asc/desc), mapping user input to predefined tokens rather than passing it through directly. - Encapsulate query construction in a small, reviewed layer that exposes only safe sorting options and does not accept raw SQL expressions from callers.
- Apply least-privilege to the database role used by the sync engine so that even if a query is manipulated, it cannot access or modify unnecessary tables or schemas.
- Enforce consistent API schemas and reject unexpected or malformed sorting expressions early, before they reach the query layer.
These patterns reduce both the injection surface and the blast radius if a similar issue slips through.
How teams detect issues like this
Detection usually combines code-level analysis and runtime monitoring:
- Static analysis: Search for dynamic SQL construction where request parameters like
order_by,sort,column, ordirectionare concatenated into query strings or template fragments. Custom rules can flag uses of string interpolation in ORDER BY, GROUP BY, or LIMIT clauses. - Code review: Pay special attention to API handlers that translate client-side query options (sorting, filtering, pagination) into SQL. Reviewers should verify that only allowlisted fields and directions are permitted and that ORMs or query builders are used safely.
- Automated tooling: Integrate SAST tools or linters that understand SQL injection patterns into CI pipelines, particularly for layers that handle query customization.
- Runtime monitoring: Inspect database logs and application telemetry for anomalous query shapes, such as ORDER BY clauses containing unexpected functions, subqueries, or concatenated literals, and correlate them with client requests for investigation.
Consistent review and policy enforcement around dynamic query construction greatly improve the chance of catching similar vulnerabilities early.

