Skip to content

MapMessage.asXml(): replace invalid XML characters with U+FFFD#4079

Merged
ppkarwasz merged 5 commits into2.25.xfrom
fix/2.25.x/map-message-control-characters
Mar 24, 2026
Merged

MapMessage.asXml(): replace invalid XML characters with U+FFFD#4079
ppkarwasz merged 5 commits into2.25.xfrom
fix/2.25.x/map-message-control-characters

Conversation

@ppkarwasz
Copy link
Copy Markdown
Contributor

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.

Related to #4077 and #4078, but this should have no impact on log output.

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.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with U+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.

@vy vy added the api Affects the public API label Mar 24, 2026
@vy vy added this to the 2.25.4 milestone Mar 24, 2026
@vy vy added the bug Incorrect, unexpected, or unintended behavior of existing code label Mar 24, 2026
@ppkarwasz ppkarwasz merged commit 55e0ece into 2.25.x Mar 24, 2026
7 checks passed
@ppkarwasz ppkarwasz deleted the fix/2.25.x/map-message-control-characters branch March 24, 2026 22:58
@github-project-automation github-project-automation bot moved this from Approved to Merged in Log4j pull request tracker Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Affects the public API bug Incorrect, unexpected, or unintended behavior of existing code

Projects

Development

Successfully merging this pull request may close these issues.

3 participants