Summary
OpenEMR's Eye Exam PDF generation feature fails to correctly encode user-supplied form data before treating it as HTML.
Under certain conditions, a user with the Notes - my encounters role can craft Eye Exam answers that cause the server to embed arbitrary image files from the filesystem into the generated PDF, leading to an arbitrary file read exposure via the PDF output.
Affected context
The vulnerability exists in OpenEMR deployments where Eye Exam encounter forms are enabled and can be completed by users with the Notes - my encounters role.
When generating a PDF for a completed Eye Exam, the application takes the stored answers and incorporates them into an HTML template that is then rendered as a PDF.
Because these answers are not safely encoded or sanitized before being interpreted as HTML, crafted input can influence resource references in the PDF, enabling inclusion of arbitrary image files that reside on the server and are accessible to the application process.
Exploit preconditions
- Attack surface: Eye Exam form completion and subsequent PDF generation functionality within OpenEMR.
- Required access: Authenticated access with at least the
Notes - my encountersrole (or equivalent) that can fill Eye Exam forms and trigger PDF export. - Trigger point: Submission of specially crafted Eye Exam answers followed by invoking the PDF print/export feature for that encounter.
- Why exploitation is possible: The PDF generation pipeline treats stored form answers as trusted HTML content without proper output encoding, allowing attacker-controlled markup to reference local server files (e.g., images) that are then embedded into the generated PDF returned to the attacker.
Why this happens
The issue stems from improper output encoding of user-supplied data in an HTML-to-PDF workflow.
Instead of encoding Eye Exam form answers as plain text before inserting them into an HTML template, the application processes them as raw HTML.
This aligns with CWE-116: Improper Encoding or Escaping of Output, where untrusted data is rendered within an output context that gives it more authority than intended.
In this case, the unescaped content can inject or alter HTML elements (such as image tags) whose source attributes point to files on the server, causing these resources to be fetched and embedded into the resulting PDF.
Potential impact
- Exposure of arbitrary server-resident image files that are readable by the application process.
- Disclosure of sensitive medical images or internal assets unintentionally stored on the server.
- Leakage of internal file paths and structural information about the server environment.
- Assistance in further attacks by revealing information useful for privilege escalation or lateral movement.
General mitigation guidance
Effective mitigation focuses on enforcing strict output encoding and controlling how user data is incorporated into document templates:
- Encode all user-supplied form values as text when inserting them into HTML or other rich formats, ensuring that they cannot be interpreted as markup or resource references.
- Constrain the allowed HTML, if HTML is required, using a robust allowlist-based sanitizer that strips or neutralizes elements and attributes capable of referencing local resources (for example,
img src,iframe, or similar tags). - Design PDF generation templates so that only application-controlled paths and assets can be referenced; disallow dynamic, user-controlled file paths in any resource attributes.
- Apply the principle of least privilege to the process that generates PDFs, limiting its filesystem read access to only the directories strictly required for operation.
- Treat all form fields as untrusted input regardless of user role, applying the same encoding and sanitization rules to privileged and non-privileged users.
How teams detect issues like this
Detection can rely on a combination of static analysis, code review practices, and runtime monitoring:
- Use static analysis rules to identify template rendering code where user-controlled variables are inserted into HTML or PDF templates without passing through encoding or sanitization functions.
- During manual code review, pay attention to any HTML-to-PDF pipelines, especially those that interpolate form data or notes directly into HTML strings or templates.
- Enforce secure coding policies that require explicit output encoding functions for all untrusted data in templates and views, with checks integrated into code review and CI pipelines.
- Consider automated scanning tools that understand server-side templating patterns and can flag unescaped variables in PDF or document generation logic.
- Monitor logs or application telemetry for unusual patterns of Eye Exam form content or repeated PDF generation requests that may indicate probing for file disclosure.

