Summary
HAPI FHIR's XHTML parser does not limit element nesting depth, allowing deeply nested FHIR narrative content to trigger unbounded recursion and a StackOverflowError. An attacker who can submit crafted FHIR JSON or XML may crash parsing or validation worker threads, disrupting services that process attacker-supplied resources.
Affected context
The issue occurs in applications using HAPI FHIR's XhtmlParser to process FHIR resources containing narrative content in text.div. It is most relevant when parsing or validation services accept FHIR JSON or XML from untrusted users or external systems and do not impose a maximum XHTML element nesting depth.
Exploit preconditions
- Attack surface: FHIR resource parsing or validation that processes XHTML narrative content
- Required access: Ability to submit or cause processing of a crafted FHIR JSON or XML resource
- Trigger point: Parsing a deeply nested
text.divnarrative - Why exploitation is possible: The parser recursively processes nested elements without enforcing a maximum depth, allowing stack consumption to grow until the worker encounters a
StackOverflowError
Why this happens
The underlying bug is uncontrolled recursive processing of attacker-influenced structured input. parseElementInner() and parseElement() continue calling one another for nested XHTML elements without a depth limit or equivalent resource bound. This maps primarily to CWE-674: Uncontrolled Recursion, with related concerns under CWE-20: Improper Input Validation and CWE-400: Uncontrolled Resource Consumption.
Potential impact
- Parsing or validation worker threads may terminate unexpectedly
- FHIR validator or ingestion services could become unavailable
- Requests containing malicious narratives could consume excessive stack resources
- Applications may experience reduced throughput or broader denial of service
- Recovery activity could delay processing of legitimate healthcare interoperability messages
General mitigation guidance
Mitigation generally focuses on bounding parser resource consumption and reducing trust in externally supplied narrative content.
Common defensive patterns include:
- Enforcing a configurable maximum element nesting depth before or during recursive parsing
- Using iterative parsing or bounded traversal where practical
- Applying limits to request size, narrative size, recursion depth, and processing time
- Validating XHTML structure against a strict schema or permitted-content policy
- Isolating parsing workers and assigning least-privilege permissions
- Applying request throttling, concurrency limits, and controlled failure handling
- Treating parser errors as isolated request failures rather than allowing worker-wide disruption
How teams detect issues like this
Organizations can look for this weakness through:
- Static analysis rules that identify recursive parser calls lacking depth or resource guards
- Code review checks for recursive processing of user-controlled XML, HTML, or tree-structured data
- Fuzz testing with increasingly deep but syntactically valid nested elements
- Runtime monitoring for repeated
StackOverflowErrorevents, parser crashes, elevated latency, or worker restarts - Policy enforcement requiring bounded parsing and resource quotas for externally supplied FHIR content
Automated security and code-quality review tools may help identify recursive input-processing paths, but should be supplemented with targeted parser testing.

