Skip to content

Validate namespace and local name in require - #36915

Merged
bclozel merged 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/xmlstreamreader-require
Oct 21, 2026
Merged

bclozel merged 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/xmlstreamreader-require

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

AbstractXMLStreamReader (the base for XMLEventStreamReader, reachable via the public
StaxUtils.createEventStreamReader(XMLEventReader)) implements
require(int expectedType, String namespaceURI, String localName).

Problem

require only compared the event type; the namespaceURI and localName arguments were ignored
entirely:

public void require(int expectedType, String namespaceURI, String localName) throws XMLStreamException {
    int eventType = getEventType();
    if (eventType != expectedType) {
        throw new XMLStreamException("Expected [" + expectedType + "] but read [" + eventType + "]");
    }
}

So a mismatched name or namespace passed silently, contrary to the
XMLStreamReader#require(int, String, String) contract: when namespaceURI/localName are
non-null they must match the current event, otherwise an XMLStreamException is thrown. The method
has only ever validated the event type since the class was introduced, and there are no in-tree
callers, so it went unnoticed.

Fix

Validate localName and namespaceURI against the current event. A test is added to
XMLEventStreamReaderTests.

Note on scope

This reader exposes a name and namespace only for START_ELEMENT and END_ELEMENT
(getName() is defined only for those states). Accordingly, a non-null name or namespace on any
other event type is treated as a mismatch and reported via XMLStreamException. ENTITY_REFERENCE
local-name validation (permitted by the spec) is intentionally not attempted, because this reader
cannot expose entity names — getName() throws IllegalStateException outside of element events.
This keeps require honest (it never silently accepts a non-matching name) within what the reader
can actually observe; happy to adjust the scope if maintainers prefer a different boundary.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2026
@bclozel bclozel added the in: core Issues in core modules (aop, beans, core, context, expression) label Sep 4, 2026
AbstractXMLStreamReader.require(int, String, String) only checked the
event type and ignored the namespaceURI and localName arguments, so a
mismatched name or namespace passed silently, contrary to the
XMLStreamReader#require contract where a non-null namespaceURI or
localName must match the current event.

Validate the local name and namespace against the current event. This
reader exposes a name only for START_ELEMENT and END_ELEMENT, so a
non-null name or namespace on any other event is reported as a
mismatch.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@bclozel bclozel self-assigned this Oct 21, 2026
@bclozel bclozel added type: bug A general bug type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on type: bug A general bug labels Oct 21, 2026
@bclozel bclozel added this to the 7.1.0-RC2 milestone Oct 21, 2026
@bclozel
bclozel force-pushed the fix/xmlstreamreader-require branch from ce33040 to d4fa386 Compare October 21, 2026 12:26
@bclozel
bclozel merged commit d4fa386 into spring-projects:main Oct 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants