Summary
OpenEMR's patient portal OnsiteDocument query API can incorporate request-controlled bitwise filter values directly into a SQL WHERE clause without quoting them safely. An authenticated patient portal user may exploit this behavior to read database records beyond their own, including other patients' information and staff password hashes.
Affected context
The issue affects the OnsiteDocument query API in OpenEMR versions before 8.4.0. It becomes risky when request parameters, including bitwise criteria such as Id_BitwiseAnd, are accepted and their values are inserted unquoted into SQL conditions without safe parameter handling.
Exploit preconditions
- Attack surface: The patient portal OnsiteDocument query API
- Required access: An authenticated patient portal account
- Trigger point: A request that supplies a bitwise criteria parameter used to construct the SQL WHERE clause
- Why exploitation is possible: Request-controlled values are incorporated into SQL without safe quoting or parameterization, allowing the query's logic to be altered
Why this happens
The underlying issue is unsafe construction of SQL from request parameters. The API allows mass-assigned filter values to influence a query, then places bitwise criteria values into the WHERE clause without treating them strictly as data. This is CWE-89: Improper Neutralization of Special Elements used in an SQL Command (SQL Injection).
Potential impact
- An attacker may read records belonging to other patients.
- Sensitive information in other database tables could be disclosed.
- Staff password hashes may be exposed, potentially enabling further account compromise.
- Broader database confidentiality could be affected, depending on the data accessible to the query.
General mitigation guidance
Defensive patterns focus on ensuring request data cannot alter SQL structure:
- Use parameterized queries for values and construct SQL expressions from trusted, fixed templates.
- Allowlist accepted filter names and types; reject unknown or unexpected fields rather than mass-assigning request parameters.
- Validate bitwise criteria against a narrowly defined set of permitted values and operators.
- Apply least-privilege database permissions so query paths have access only to the data they require.
- Keep patient-level authorization checks separate from filtering, and enforce them on every query.
How teams detect issues like this
Review query-building code for request parameters that flow into SQL strings, especially filter maps, bitwise criteria, and unquoted values in WHERE clauses. Static analysis and code review can flag string concatenation or interpolation in SQL and mass assignment of request fields. Policy checks can require parameterized queries, allowlisted filters, and tests that verify patient-level authorization is enforced for API results.

