Summary
OpenMRS Core uses Apache Velocity to evaluate database-stored criteria strings in ConceptReferenceRangeUtility.evaluateCriteria() without sandboxing or secure Uberspector configuration.
A user with the Manage Concepts privilege can inject malicious Velocity expressions into a concept's reference range criteria, leading to arbitrary code execution whenever observations are validated against that concept.
Affected context
This vulnerability occurs in OpenMRS Core instances where:
- The platform version is between 2.7.0 and before 2.7.9 or 2.8.6
- Concept reference range criteria are stored as strings in the database and evaluated via Apache Velocity
- The VelocityEngine is initialized without a secure Uberspector or sandbox configuration
The issue specifically affects evaluation paths that invoke ConceptReferenceRangeUtility.evaluateCriteria(), such as validation of observations against concepts with reference range criteria.
Exploit preconditions
- Attack surface: Application-level functionality for managing concepts and validating observations.
- Required access: A user account with the Manage Concepts privilege, or any integration that can persist or modify reference range criteria for concepts.
- Trigger point: Any user action or API call that causes OpenMRS to validate an observation against a concept whose reference range criteria includes attacker-controlled Velocity template content.
- Why exploitation is possible: The VelocityEngine is configured with default reflection capabilities (UberspectImpl) and no secure Uberspector, allowing arbitrary method calls and Java reflection from within templates. Criteria strings are treated as trusted templates and executed directly in a context that exposes $patient, $obs, and $fn with access to rich domain and service objects.
Why this happens
The root cause is the direct evaluation of database-stored strings as Apache Velocity templates using a non-sandboxed engine configuration.
This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where user-controllable data is interpreted as executable expressions without strict constraints.
By initializing the VelocityEngine with default reflection support (UberspectImpl) and omitting secure mechanisms such as a NoOp or restricted Uberspector, OpenMRS effectively grants template expressions unrestricted access to Java reflection and application services.
Because the reference range criteria field is editable by users with the Manage Concepts privilege and then executed on each validation, these criteria act as an untrusted scripting surface inside the application runtime.
Potential impact
- Arbitrary code execution within the OpenMRS application process via Java reflection and method invocation.
- Full compromise of the OpenMRS instance's data, including reading or modifying patient records and observations.
- Abuse of the exposed service layer ($fn) to perform unauthorized operations, such as creating, altering, or deleting clinical data or configuration.
- Escalation from a configuration-level privilege (Manage Concepts) to runtime execution capabilities.
- Potential lateral movement within the hosting environment, depending on the application's deployment and host permissions.
General mitigation guidance
Effective mitigation patterns focus on eliminating or strictly constraining dynamic template execution and moving toward data-driven criteria evaluation:
- Replace template-based evaluation with a declarative, data-only representation of reference range criteria that is parsed rather than executed.
- If templating is required, configure Apache Velocity with a secure Uberspector or equivalent sandbox that disallows arbitrary reflection and restricts accessible classes and methods.
- Run any scripting or templating logic under the principle of least privilege, limiting both the objects injected into the template context and the operations they can perform.
- Treat database-stored expressions as untrusted input; subject them to stringent validation and allow only a constrained subset of operations or syntax.
- Introduce explicit separation between configuration authorship and execution, including review or approval workflows for high-impact criteria changes.
How teams detect issues like this
Detection efforts can focus on both code-level patterns and runtime behavior:
- Use static analysis or code review checklists to flag any usage of templating engines (like Velocity) where:
- Templates originate from database or user-controlled fields, and
- The engine is configured without a secure Uberspector, sandbox, or restricted context.
- Search the codebase for direct calls into VelocityEngine or similar APIs that operate on non-constant strings or configuration fields edited via the UI.
- Introduce automated policy checks in CI/CD that enforce the use of restricted or secure configurations for any template engine.
- At runtime, log and monitor template evaluation entry points, including the content or structure of reference range criteria, to detect suspicious constructs such as reflection calls or unexpected method invocation patterns.

