Summary
An SQL injection flaw in OpenEMR's Immunization module allows any authenticated user to inject arbitrary SQL into database queries by abusing the patient_id parameter.
Under typical deployments, this can lead to full database compromise, exposure of protected health information (PHI), credential theft, and in some configurations potential remote code execution via database-level features.
Affected context
The vulnerability occurs in OpenEMR installations that use the Immunization module and accept user-controlled patient_id values as part of database lookup operations.
It is relevant where application users (including low-privileged or non-clinical roles) can influence patient identifiers through requests that reach the affected code path.
The risk is elevated in environments where the OpenEMR database stores PHI, user credentials, or where the database account has broad privileges, including write access or capabilities that can be chained to code execution.
Exploit preconditions
- Attack surface: Web application endpoints associated with the Immunization module that consume a
patient_idparameter. - Required access: Any authenticated user account with the ability to reach the vulnerable functionality (e.g., viewing or managing immunizations).
- Trigger point: Submission of crafted
patient_idvalues in HTTP requests that are later interpolated into SQL WHERE clauses. - Why exploitation is possible: The application constructs SQL statements by directly concatenating user-supplied
patient_idinput into query strings without parameterized queries or robust escaping, allowing attackers to alter the structure of the SQL command.
Why this happens
This issue stems from building SQL statements through string concatenation using untrusted input, a canonical example of CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
Instead of binding the patient_id as a typed parameter, the code interpolates it into the WHERE clause, so any SQL metacharacters or payloads included by an attacker are treated as part of the executable SQL.
The underlying bug class is insufficient separation between data and control instructions in database interactions, combined with over-trusting authenticated user input within the application boundary.
Potential impact
- Full read access to the OpenEMR database, including PHI and other sensitive medical records.
- Modification or deletion of clinical data, appointments, billing records, and audit trails.
- Theft or manipulation of user and administrator credentials stored in the database.
- Creation of backdoor accounts or alteration of authorization-related tables.
- In some deployments, potential remote code execution via database-native features (e.g., execution of OS commands, loading of arbitrary extensions, or abuse of stored procedures), depending on database configuration and privileges.
- Regulatory and compliance impact due to PHI exposure (e.g., HIPAA-related consequences in applicable jurisdictions).
General mitigation guidance
Effective mitigation focuses on consistently separating SQL control logic from user-supplied data and minimizing database privileges:
- Replace all string-concatenated queries involving
patient_idand similar inputs with parameterized queries or prepared statements, ensuring the database driver handles escaping and typing. - Adopt a central data access layer that enforces the use of safe query construction patterns and discourages ad hoc SQL string building.
- Apply strict input validation to identifiers (e.g., numeric-only patient IDs) to reduce the attack surface, while not treating validation as a substitute for parameterization.
- Ensure the application's database account follows least-privilege principles, granting only the minimal read/write operations required and avoiding unnecessary administrative rights.
- Implement secure-by-default coding standards that ban direct concatenation of untrusted input in SQL and encourage the use of ORM or query builder patterns where appropriate.
How teams detect issues like this
Organizations can detect and monitor for this class of issue using a combination of preventive and detective controls:
- Static analysis: Configure SAST tools or linters to flag dynamic SQL construction where user-controlled variables are concatenated into query strings, especially in modules handling PHI.
- Code review: During peer review, explicitly look for direct use of request parameters in SQL, absence of prepared statements, and any bypass of centralized data access utilities.
- Runtime monitoring: Use database auditing or application-layer logging to identify anomalous query patterns, such as unexpected UNION operations, stacked queries, or syntax errors in
patient_id-driven queries. - Security testing: Integrate automated and manual application security testing (including authenticated SQL injection tests) into CI/CD workflows to catch regressions or new injection points.

