Summary
OpenEMR's Standard REST API has an SQL injection flaw in patient insurance and employer searches because query-string parameter names are incorporated into SQL as column identifiers without escaping. An authenticated client, including a low-privilege patient-portal OAuth2 token, may be able to access patient PHI, user records, or API client secrets by sending crafted parameter names to the affected endpoints.
Affected context
The issue occurs in the Standard REST API patient insurance and employer search routes:
/api/patient/{puuid}/insurance/api/patient/{puuid}/employer
It becomes a risk when parameter names from a request are used as SQL column identifiers without safe handling. Exploitability depends on the API being reachable and the caller having valid access to it.
Exploit preconditions
- Attack surface: The Standard REST API insurance and employer search endpoints
- Required access: Authentication; the description notes that low-privilege patient-portal OAuth2 tokens may be sufficient
- Trigger point: Processing query-string parameter names in a search request
- Why exploitation is possible: Parameter names are concatenated into SQL as column identifiers without escaping or an equivalent safe constraint
Why this happens
The API constructs SQL using request-controlled parameter names as column identifiers. Unlike values, identifiers generally cannot be safely handled by ordinary value parameterization; they should be selected from a fixed, trusted set or safely quoted using database-specific mechanisms. Allowing untrusted input to influence SQL structure creates an injection flaw, mapped to CWE-89: Improper Neutralization of Special Elements used in an SQL Command.
Potential impact
- An attacker with qualifying API access may retrieve patient PHI.
- User records could be exposed.
- API client secrets could be disclosed, potentially enabling further unauthorized access.
General mitigation guidance
Defensive patterns include keeping request data out of SQL structure and using prepared statements for data values. Where clients need to select searchable fields, map accepted external names to a strict, server-controlled allowlist of database columns rather than incorporating arbitrary names into queries. Validate requests against an explicit schema, minimize the data returned by each endpoint, and apply least-privilege database and API authorization so that a compromised or over-permissioned client has limited reach.
How teams detect issues like this
- Use static analysis to identify SQL construction that incorporates request-derived strings, especially where those strings determine table or column names.
- Review search handlers for dynamic identifier selection, incomplete allowlists, and authorization checks that do not constrain returned records.
- Monitor API and database activity for unusual search parameters, broad or unexpected record access, and repeated queries across patient or user data.
- Enforce code-review and CI policies requiring approved query-building patterns and explicit identifier allowlists; automated security review tools may help flag risky SQL construction.

