Summary
OpenEMR's patient portal OnsiteDocument.Query endpoint has an SQL injection flaw involving certain criteria parameters. An authenticated patient may exploit it to read database records beyond their own patient scope, including other patients' demographics and users_secure password hashes.
Affected context
The issue occurs in the patient portal's OnsiteDocument.Query endpoint when _BitwiseAnd criteria values are incorporated into SQL queries. It is risky when authenticated portal users can supply these parameters and the resulting query does not properly constrain access to the requesting patient's records.
Exploit preconditions
- Attack surface: Patient portal endpoint that processes OnsiteDocument.Query requests
- Required access: An authenticated patient portal account
- Trigger point: Processing of attacker-controlled _BitwiseAnd criteria parameters
- Why exploitation is possible: The values are placed into SQL without adequate parameterization; escaping quote characters alone does not prevent SQL injection or preserve patient-level query boundaries
Why this happens
The endpoint constructs SQL using criteria values that are not safely bound as parameters. Escaping quote characters is not a reliable substitute for parameterized queries, particularly when values are inserted into SQL in an unquoted form. This is CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
Potential impact
- An attacker could read records outside their own patient scope.
- Sensitive demographic information belonging to other patients may be exposed.
- The database may disclose users_secure password hashes.
- Exposed information could be used for further account or privacy attacks.
General mitigation guidance
Defensive approaches focus on keeping user-controlled values out of SQL syntax and enforcing authorization at the data-access layer. Common patterns include:
- Use parameterized queries with correctly typed and bound values.
- Validate criteria against strict allowlists and expected data types.
- Apply patient-level authorization constraints in every relevant query.
- Limit database account privileges to the minimum required for the endpoint.
- Avoid relying on character escaping as the primary SQL injection defense.
How teams detect issues like this
Review query-building code for concatenation or interpolation of request-controlled criteria, especially values inserted without quoting or binding. Static analysis can flag dynamically constructed SQL and missing parameterization; code review should verify both safe query construction and patient-scope authorization. Runtime policies and database monitoring can also help identify unusual query patterns or access to records beyond expected patient boundaries.

