MapMessage.asXml(): replace invalid XML characters with U+FFFD#4079
Merged
MapMessage.asXml(): replace invalid XML characters with U+FFFD#4079
Conversation
This change sanitizes the output of `MapMessage.asXml()` by replacing characters not permitted in XML 1.0 with the Unicode replacement character (`U+FFFD`). Although `MapMessage.asXml()` is not currently used by any layout, this aligns its behavior with `XmlLayout` and `Log4j1XmlLayout`, ensuring consistent and well-formed XML output across the codebase.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR sanitizes MapMessage.asXml() output to guarantee XML 1.0 well-formedness by replacing XML-invalid characters with the Unicode replacement character (U+FFFD), aligning behavior with the existing XML layouts referenced in #4077 and #4078.
Changes:
- Update
StringBuilders.escapeXml()to replace XML 1.0–invalid BMP characters and invalid surrogate sequences withU+FFFD, while still escaping& < > " '. - Update
MapMessage.asXml()to XML-escape both entry keys (attribute values) and entry values. - Expand unit tests to cover invalid XML characters and surrogate cases; add changelog entry.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/changelog/.2.x.x/4079_map-message-control-characters.xml | Adds changelog entry for MapMessage XML sanitization. |
| log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java | Enhances XML escaping to replace XML-invalid characters with U+FFFD and handle surrogate pairs. |
| log4j-api/src/main/java/org/apache/logging/log4j/message/MapMessage.java | Escapes XML in both MapMessage entry keys and values when rendering XML. |
| log4j-api-test/src/test/java/org/apache/logging/log4j/util/StringBuildersTest.java | Refactors/extends tests for escapeXml() with parameterized coverage of invalid characters. |
| log4j-api-test/src/test/java/org/apache/logging/log4j/message/MapMessageTest.java | Strengthens XML formatting test to include invalid characters and non-BMP characters. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
log4j-api-test/src/test/java/org/apache/logging/log4j/util/StringBuildersTest.java
Outdated
Show resolved
Hide resolved
log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java
Outdated
Show resolved
Hide resolved
vy
approved these changes
Mar 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change sanitizes the output of
MapMessage.asXml()by replacing characters not permitted in XML 1.0 with the Unicode replacement character (U+FFFD).Although
MapMessage.asXml()is not currently used by any layout, this aligns its behavior withXmlLayoutandLog4j1XmlLayout, ensuring consistent and well-formed XML output across the codebase.Related to #4077 and #4078, but this should have no impact on log output.