Skip to content

fix: honor message charset in XMLUtil.getInputSource - #3142

Open
predic8 wants to merge 3 commits into
masterfrom
fix/xmlutil-getinputsource-charset
Open

fix: honor message charset in XMLUtil.getInputSource#3142
predic8 wants to merge 3 commits into
masterfrom
fix/xmlutil-getinputsource-charset

Conversation

@predic8

@predic8 predic8 commented Aug 11, 2026

Copy link
Copy Markdown
Member

Problem

XMLUtil.getInputSource decoded the message body with an InputStreamReader using the JVM default charset, so:

  1. The message's declared Content-Type charset was ignored (-Dfile.encoding decided instead).
  2. The XML encoding declaration had no effect either, since per the XML spec it's only consulted when the parser is fed a byte stream, not a pre-decoded character stream.

This affects both consumers of XMLUtil.getInputSource: XPathExchangeExpression (xpath configuration) and the xpath() built-in function. A non-UTF-8 body with non-ASCII characters could be misdecoded, silently breaking xpath comparisons.

Fix

Pass the byte stream straight to the InputSource and set its encoding from msg.getHeader().getCharset() when the header declares one, letting the parser fall back to its own BOM/declaration detection otherwise — following RFC 7303 precedence.

Tests

Added to XMLUtilTest:

  • getInputSourceHonorsTheMessagesCharset — reproduces the issue's ISO-8859-1 Bönnigheim case with a header charset.
  • getInputSourceFallsBackToTheXmlDeclarationWithoutAHeaderCharset — same body with no header charset, confirming the XML declaration still decides.

Fixes #3129

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved XML processing for messages using non-default character encodings.
    • HTTP-specified character sets are now honored, while XML declarations are used when no character set is provided.
    • Prevented potential character corruption caused by relying on the system’s default encoding.

Previously the body was pre-decoded with an InputStreamReader using the
JVM default charset, so both the message's declared Content-Type
charset and the XML encoding declaration were ignored. Non-UTF-8
bodies with non-ASCII characters could be misdecoded, e.g. silently
breaking XPath comparisons in XPathExchangeExpression and the xpath()
built-in function.

Pass the byte stream to the parser instead and set InputSource
encoding from the message's charset when present, following RFC 7303
precedence.

Fixes #3129

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 236826cd-c386-46d9-823e-1f5dc0a3e93b

📥 Commits

Reviewing files that changed from the base of the PR and between 0fe3208 and cfc2e6a.

📒 Files selected for processing (1)
  • core/src/test/java/com/predic8/membrane/core/util/XMLUtilTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/src/test/java/com/predic8/membrane/core/util/XMLUtilTest.java

📝 Walkthrough

Walkthrough

XMLUtil.getInputSource now preserves XML bodies as bytes, applies the message charset when present, and allows XML declarations to determine encoding otherwise. Tests cover ISO-8859-1 parsing and charset precedence.

Changes

XML charset parsing

Layer / File(s) Summary
Preserve XML encoding metadata
core/src/main/java/com/predic8/membrane/core/util/xml/XMLUtil.java
getInputSource passes the body as a byte stream and sets InputSource encoding when the message contains a charset.
Validate charset precedence
core/src/test/java/com/predic8/membrane/core/util/XMLUtilTest.java
Tests verify message-header charset handling and fallback to the XML declaration for non-ASCII bodies.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related issues

  • Issue 3141 may be related because it also concerns XML parsing with message charset handling, but it targets separate XmlDomBody parsing methods.

Poem

Bytes hop softly through the stream,
Charset flags now guide the dream.
ISO letters shine bright,
XML declarations set things right.
Squeak! No default-charset fright.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: honoring the message charset in XMLUtil.getInputSource.
Linked Issues check ✅ Passed The implementation and tests satisfy issue #3129 by preserving byte-based XML parsing and applying the message charset when provided.
Out of Scope Changes check ✅ Passed All changes support issue #3129 and its stated objectives; no unrelated code changes are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/xmlutil-getinputsource-charset

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core/src/test/java/com/predic8/membrane/core/util/XMLUtilTest.java`:
- Around line 52-55: Update the header-charset fixture in XMLUtilTest so its XML
body omits the encoding declaration while retaining the ISO-8859-1 Content-Type
charset and encoded text. Leave the XML declaration in the separate fallback
test unchanged, then run the targeted XMLUtilTest command.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d0ca1f4e-a6f0-4b32-8d60-8fac4d84ae72

📥 Commits

Reviewing files that changed from the base of the PR and between db04680 and 0fe3208.

📒 Files selected for processing (2)
  • core/src/main/java/com/predic8/membrane/core/util/xml/XMLUtil.java
  • core/src/test/java/com/predic8/membrane/core/util/XMLUtilTest.java

Comment thread core/src/test/java/com/predic8/membrane/core/util/XMLUtilTest.java
@membrane-ci-server

Copy link
Copy Markdown

This pull request needs "/ok-to-test" from an authorized committer.

@rrayst

rrayst commented Aug 13, 2026

Copy link
Copy Markdown
Member

/ok-to-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XMLUtil.getInputSource decodes XML bodies with the JVM default charset

2 participants