Summary
OpenEMR has a second-order SQL injection vulnerability in which a low-privileged authenticated user can store a malicious SQL fragment in a patient-column preference. The fragment may be executed later when dynamic_finder.php incorporates the stored value into a SELECT query.
Affected context
The issue affects OpenEMR versions before 8.5.0. It occurs when a value submitted through library/ajax/addlistitem.php is stored as the recent_patient_columns option's option_id, then later used by dynamic_finder.php to construct a SELECT query. Risk depends on the stored value reaching that query without adequate validation or safe SQL construction.
Exploit preconditions
- Attack surface: OpenEMR's authenticated application functionality involving patient-column preferences
- Required access: A low-privileged authenticated account able to submit and store the relevant preference value
- Trigger point: A later operation that causes
dynamic_finder.phpto use the stored value in a SELECT query - Why exploitation is possible: The stored value is incorporated into SQL dynamically rather than being safely handled as data
Why this happens
This is a second-order SQL injection flaw mapped to CWE-89: Improper Neutralization of Special Elements used in an SQL Command. A value that may appear harmless when initially stored is later treated as part of SQL syntax when concatenated into a query. Storing input does not make it trustworthy; values must be safely handled at the point they are used in a database operation.
Potential impact
- An attacker may be able to influence database query behavior.
- Depending on query context and database permissions, sensitive records could be exposed.
- Database data could be modified or deleted if the vulnerable query path permits it.
- Application features that rely on the affected query could be disrupted.
General mitigation guidance
Common defensive patterns include:
- Use parameterized queries for values and avoid concatenating user-controlled data into SQL.
- Where dynamic identifiers or SQL fragments are required, select them from a strict, predefined allowlist rather than accepting arbitrary stored values.
- Validate data at both storage and use boundaries; treat persisted user-controlled values as untrusted.
- Apply least-privilege database permissions to limit the effects of unintended query behavior.
How teams detect issues like this
- Use static analysis and taint tracking to identify user-controlled values flowing from storage into SQL construction.
- Review code paths where preferences or other persisted input are later concatenated into queries.
- Enforce parameterized-query and approved-identifier allowlist policies in code review and CI checks.
- Monitor database and application logs for unexpected query patterns or errors associated with the affected functionality.

