Summary
A stored cross-site scripting (XSS) vulnerability in OpenEMR's patient portal payment flow allows arbitrary JavaScript to be persisted by a portal user and later executed in the browser of a staff member reviewing the payment.
When attacker-controlled payment data is stored without proper output encoding and then rendered into HTML, it can execute in privileged staff sessions, enabling session theft, data access, or user interface manipulation.
Affected context
The vulnerability occurs in OpenEMR's patient portal payment workflow, where payment-related fields submitted by a patient portal user are processed by portal/lib/paylib.php and persisted in backend storage.
When staff users later review these payment submissions via portal/portal_payment.php, the stored values are rendered into the response without proper HTML/JavaScript escaping.
This becomes risky in deployments where patient portal users can freely enter payment notes or descriptors that are later displayed to authenticated staff within the same application origin.
Exploit preconditions
- Attack surface: Web-based patient portal payment submission interface.
- Required access: A valid or compromised patient portal account capable of submitting payment information.
- Trigger point: A staff member opens or reviews the affected payment entry in the
portal/portal_payment.phpinterface. - Why exploitation is possible: User-supplied payment fields are stored and then rendered into an HTML context without proper output encoding or context-aware escaping, allowing embedded script code to be interpreted by the browser.
Why this happens
This issue stems from insufficient output encoding of user-controlled data when displayed in an HTML page, corresponding to CWE-79: Improper Neutralization of Input During Web Page Generation (Cross-Site Scripting).
The payment data accepted via portal/lib/paylib.php is treated as benign text but later inserted into the HTML response in portal/portal_payment.php without applying robust, context-appropriate escaping.
As a result, any HTML or JavaScript fragments embedded in those fields are executed by the browser in the security context of the staff user's session.
Potential impact
- Staff session hijacking via theft of session identifiers or authentication tokens stored in cookies or browser storage.
- Unauthorized access to sensitive medical records and patient data that the compromised staff account can view.
- Manipulation of the staff user interface to alter or falsify patient information and payment records.
- Use of the application as a pivot to perform further actions on behalf of the staff user (e.g., configuration changes, prescription handling, record updates).
- Distribution of malicious scripts to additional internal users who access affected payment records.
General mitigation guidance
Effective mitigation focuses on strengthening output handling and reducing trust in user-submitted data across the full rendering path.
Recommended patterns include:
- Applying strict, context-aware output encoding on all user-controlled fields before insertion into HTML, attributes, JavaScript, or URLs.
- Centralizing templating and response generation so that unsafe direct echoes or concatenations are replaced with framework helpers that auto-escape output.
- Restricting allowed content for payment-related fields to a safe character set (e.g., alphanumeric plus limited punctuation) and validating length and format on both client and server sides.
- Implementing a defense-in-depth Content Security Policy (CSP) that disallows inline scripts and restricts script sources, reducing the impact of any residual XSS issues.
- Following a strict separation of data and code in views, avoiding dynamic construction of HTML or JavaScript with raw user data.
How teams detect issues like this
Detection generally relies on a combination of static review and behavioral testing:
- Static analysis: Use SAST rules to flag locations where user input flows from HTTP request parameters or database fields into HTML output without passing through known escaping APIs or templating engines.
- Code review: During manual review, trace the life cycle of user-controlled fields (such as payment notes) from input handling functions (e.g.,
portal/lib/paylib.php) through to rendering endpoints (portal/portal_payment.php), verifying that all sinks apply appropriate encoding. - Dynamic testing: Employ automated and manual web security testing to inject harmless XSS test payloads into payment fields and observe whether they are reflected or executed in staff-facing views.
- Policy enforcement: Introduce coding standards and CI checks requiring context-aware escaping for all new or modified templates, and optionally integrate automated code scanning tools into the pull request workflow.

