Summary
An SQL injection vulnerability in OpenEMR's Patient REST API allows authenticated API users to inject arbitrary SQL expressions through the _sort parameter, which is used directly in an ORDER BY clause.
When exploited, this flaw can expose protected health information (PHI), disclose or modify database contents, and potentially compromise credentials stored in the database.
Affected context
The vulnerability occurs in OpenEMR deployments that:
- Expose the Patient REST API endpoint
- Grant API access tokens or credentials to users or integrations
- Allow clients to pass a
_sortparameter that is reflected into SQLORDER BYclauses
It manifests when user-controlled sort field names are concatenated into SQL statements without validation or proper identifier escaping. The risk is present in environments where the database contains PHI or other sensitive operational data, which is typical for electronic health record systems.
Exploit preconditions
- Attack surface: Patient REST API endpoint that supports sorting via a
_sortparameter. - Required access: Valid, authenticated API access (e.g., API key, bearer token, or equivalent credentials).
- Trigger point: Sending crafted
_sortvalues that are incorporated into the SQL query'sORDER BYclause. - Why exploitation is possible: The application treats
_sortas a trusted identifier and injects it into SQL without: - Strict allowlisting of sortable columns, or
- Proper identifier quoting/escaping, or
- Use of parameterized constructs for dynamic ordering.
As a result, attacker-controlled input can alter the structure of the SQL query.
Why this happens
This vulnerability stems from dynamic SQL construction where user input is directly interpolated into an ORDER BY clause.
Instead of mapping the _sort parameter to a predefined set of allowed column names, the code appears to:
- Accept arbitrary field names from the client, and
- Embed them into the query string without robust validation or escaping.
This behavior corresponds to CWE-89: Improper Neutralization of Special Elements used in an SQL Command, where attacker-supplied data influences SQL syntax rather than being treated as inert values. Even though the context is an ORDER BY clause, unvalidated identifiers and expressions can still be abused to inject additional SQL fragments or manipulate query semantics.
Potential impact
- Exposure of Protected Health Information (PHI) via unauthorized reads of patient or related tables.
- Unauthorized access to or enumeration of other sensitive database records.
- Extraction of password hashes or authentication tokens stored in the database.
- Tampering with clinical data, billing records, or configuration tables, leading to data integrity issues.
- Escalation of privileges if sensitive configuration or access control data can be modified.
- Potential pivoting to underlying infrastructure if database-level features or stored procedures can be abused.
General mitigation guidance
Addressing this class of vulnerability usually focuses on removing direct trust in client-supplied identifiers and expressions:
- Apply strict allowlists for sortable fields: Map
_sortvalues to a fixed, hard-coded set of column names and directions (e.g.,name,dob, with optionalasc/descflags). Reject or default on unknown values. - Avoid concatenating raw sort parameters into SQL: Use query builder abstractions or ORM facilities that separate SQL structure from user input, even for
ORDER BYclauses. - Normalize and validate direction flags: If clients can specify ascending/descending ordering, restrict input to known tokens (
asc,desc) and map them to safe constants. - Enforce least privilege on database accounts: Configure the application's database user with the minimal privileges required to operate, reducing the blast radius of a successful injection.
- Centralize query construction patterns: Implement shared helper functions or services for building paginated/sorted queries to ensure consistent, secure handling of filter and sort parameters across endpoints.
How teams detect issues like this
Detection often combines code-centric and behavior-centric approaches:
- Static analysis: Configure SAST tools to flag string-based SQL construction, especially where HTTP parameters (e.g.,
_sort) are concatenated into queries or passed intoORDER BYclauses. - Code review checklists: During manual reviews, scrutinize:
- Any dynamic query building around filtering, searching, and sorting.
- Places where field names or directions come directly from API parameters.
- Runtime monitoring: Enable database query logging or application-level telemetry to detect anomalous queries, such as unexpected syntax in
ORDER BYstatements or repeated query errors. - Security testing: Incorporate automated and manual injection tests covering query parameters, headers, and sorting/filtering options in API penetration tests and CI security pipelines.

