Summary
Apache Log4j Core's XmlLayout fails to sanitize characters forbidden by the XML 1.0 specification, leading to malformed XML whenever log messages or MDC values contain such characters.
Depending on the StAX implementation, this can cause downstream XML parsers to reject log documents or throw exceptions, resulting in dropped or undelivered log events.
Affected context
This vulnerability affects applications that:
- Use Apache Log4j Core up to and including 2.25.3
- Configure XmlLayout for log output (e.g., XML log files, XML over streams)
- Rely on StAX-based XML processing—either the JRE built-in implementation or alternatives like Woodstox
The issue manifests when log messages, parameters, or mapped diagnostic context (MDC) values contain characters forbidden by the XML 1.0 specification, which are then written directly into the XML output without sanitization.
Exploit preconditions
- Attack surface: Any code path that logs attacker-controlled or untrusted data using XmlLayout.
- Required access: Ability to influence log content (e.g., user input, headers, message bodies, or MDC values that are recorded in logs).
- Trigger point: A logging call that formats output using XmlLayout while containing XML 1.0 forbidden characters.
- Why exploitation is possible: XmlLayout does not sanitize or filter forbidden XML characters before emitting them, relying on StAX output; depending on the StAX implementation, this can silently produce malformed XML or raise exceptions during logging, causing log records to be dropped.
Why this happens
The root cause lies in improper output encoding and validation when generating XML logs.
XmlLayout assumes that escaping common XML metacharacters is sufficient but does not enforce the XML 1.0 character set rules, allowing forbidden control characters and other invalid code points to pass through.
This behavior maps to CWE-116: Improper Encoding or Escaping of Output, where data is emitted into a structured output format (XML) without ensuring it conforms to the specification, causing downstream consumers to fail when processing the malformed documents.
Potential impact
- Log entries may be malformed, causing XML parsers to reject entire documents.
- Downstream log-processing, auditing, or SIEM systems can drop affected records, reducing observability.
- Logging calls may fail with exceptions (depending on the StAX implementation), preventing events from reaching their intended appenders.
- Critical security or operational events can be missing from primary logs, complicating incident response and compliance reporting.
- Internal status logs might contain partial information, leading to divergent logging views between operational logs and internal diagnostics.
General mitigation guidance
Mitigation typically focuses on enforcing XML-safe output and limiting trust in log input sources.
Common defensive patterns include:
- Applying strict character filtering or normalization before writing to XML-based layouts, ensuring conformance with XML 1.0 character rules.
- Encapsulating XmlLayout usage with a sanitation layer that rejects or replaces forbidden characters in log messages and MDC values.
- Segregating untrusted or high-variance input into non-XML log channels, reserving XML layouts for structured, validated data.
- Implementing robust error-handling around logging logic to prevent dropped events when output failures occur, such as fallbacks to plain-text appenders.
- Minimizing direct logging of raw user-controlled content and, where necessary, constraining its form (e.g., whitelisting expected character ranges for identifiers or tokens).
How teams detect issues like this
Organizations can detect this class of issue through:
- Static analysis rules that flag XML emitters and layout implementations that do not explicitly validate against XML 1.0 character sets.
- Code review checklists that require verification of encoding and character-set handling whenever logs are written in structured formats (XML, JSON).
- Runtime monitoring for logging-related exceptions, particularly in environments using alternative StAX implementations such as Woodstox, and correlating these with missing events in primary logs.
- Log processing pipeline health checks that alert on malformed XML documents, parser failures, or unexpected drops in ingest volume for XML-based log streams.
- Policy enforcement in CI/CD that discourages or gates the introduction of new XML-based log outputs without explicit conformance tests.

