This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new 68efb35690 fix: Reject unknown root namespaces when strictly reading
POMs (fixes #10985) (#13882)
68efb35690 is described below
commit 68efb356909488888a672fd1808abf54ecdebda2
Author: Vincent Potuček <[email protected]>
AuthorDate: Tue Nov 17 06:29:56 2026 +0100
fix: Reject unknown root namespaces when strictly reading POMs (fixes
#10985) (#13882)
* [#10985] Reject unknown root namespaces when strictly reading POMs
The generated MavenStaxReader verified that child elements share the root
element's namespace, but never validated the root namespace itself, so
<project xmlns="https://ff.com"> was accepted.
When the existing (previously unused) `enforceNamespace` template parameter
is set, reader-stax.vm now emits the list of accepted root namespaces,
derived from the model's XmlModelMetadata for every model version between
`minimalVersion` and the generated version (nothing is hardcoded in the
shared template). In strict mode, a root element in any other namespace is
rejected; no namespace remains allowed.
Only the POM reader sets `enforceNamespace`, so readers for settings,
toolchains, metadata, plugin descriptors, lifecycles and extensions are
unchanged. Lenient reads are unaffected, so POMs consumed from remote
repositories keep working; project POMs fail with a "Malformed POM" error.
Fixes #10985
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MTr23zm3fn7Y3i8UGg5Lvi
* Rename test helper artifact id: missing-namespace → unrecognized-namespace
The POM helper always provides a namespace (it's just unrecognized), so
"missing-namespace" was misleading. "unrecognized-namespace" better
conveys the intent of the test.
* refactor: use @ParameterizedTest for namespace acceptance/rejection tests
---------
Co-authored-by: punkratz <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Guillaume Nodet <[email protected]>
---
.../maven/impl/model/DefaultModelBuilderTest.java | 46 ++++++++++++++
.../resources/poms/factory/random-namespace.xml | 26 ++++++++
impl/maven-support/pom.xml | 5 ++
.../apache/maven/model/v4/MavenStaxReaderTest.java | 66 ++++++++++++++++++++
src/mdo/reader-stax.vm | 71 ++++++++++++++++++++++
5 files changed, 214 insertions(+)
diff --git
a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelBuilderTest.java
b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelBuilderTest.java
index 62d75549a8..26670a43c1 100644
---
a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelBuilderTest.java
+++
b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelBuilderTest.java
@@ -307,6 +307,52 @@ public void testPropertiesAndProfiles() {
assertEquals("21",
result.getEffectiveModel().getProperties().get("maven.compiler.release"));
}
+ /**
+ * A project POM whose root element is in an unknown namespace must fail
the build
+ * (<a href="https://github.com/apache/maven/issues/10985">#10985</a>).
+ */
+ @Test
+ void testRandomNamespaceIsRejectedForProjectBuild() {
+ ModelBuilderRequest request = ModelBuilderRequest.builder()
+ .session(session)
+ .requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT)
+ .source(Sources.buildSource(getPom("random-namespace")))
+ .build();
+
+ ModelBuilderException e = assertThrows(
+ ModelBuilderException.class, () ->
builder.newSession().build(request));
+ assertTrue(
+ e.getProblemCollector()
+ .problems()
+ .anyMatch(p -> p.getSeverity() ==
BuilderProblem.Severity.ERROR
+ && p.getMessage().contains("Unrecognized
namespace 'https://ff.com'")),
+ () -> "Expected an error about the unrecognized namespace,
got: "
+ + e.getProblemCollector().problems().toList());
+ }
+
+ /**
+ * POMs consumed from repositories are parsed leniently: an unknown root
namespace must not
+ * break the resolution of existing artifacts.
+ */
+ @Test
+ void testRandomNamespaceIsToleratedForDependencyModel() {
+ ModelBuilderRequest request = ModelBuilderRequest.builder()
+ .session(session)
+
.requestType(ModelBuilderRequest.RequestType.CONSUMER_DEPENDENCY)
+ .source(Sources.buildSource(getPom("random-namespace")))
+ .build();
+
+ ModelBuilderResult result = builder.newSession().build(request);
+
+ assertEquals("random-namespace",
result.getEffectiveModel().getArtifactId());
+ assertTrue(
+ result.getProblemCollector()
+ .problems()
+ .noneMatch(p -> p.getMessage().contains("Unrecognized
namespace")),
+ () -> "Unexpected namespace problem: "
+ + result.getProblemCollector().problems().toList());
+ }
+
/**
* Models built at {@link
ModelBuilderRequest.RequestType#CONSUMER_DEPENDENCY} come from a
* dependency POM resolved from a repository. Their file, property, and
condition activators
diff --git
a/impl/maven-impl/src/test/resources/poms/factory/random-namespace.xml
b/impl/maven-impl/src/test/resources/poms/factory/random-namespace.xml
new file mode 100644
index 0000000000..975eb62dc7
--- /dev/null
+++ b/impl/maven-impl/src/test/resources/poms/factory/random-namespace.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<!-- https://github.com/apache/maven/issues/10985: the root namespace is
neither empty nor a POM namespace -->
+<project xmlns="https://ff.com">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its</groupId>
+ <artifactId>random-namespace</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>pom</packaging>
+</project>
diff --git a/impl/maven-support/pom.xml b/impl/maven-support/pom.xml
index 8b879693fd..c40a6fd4ab 100644
--- a/impl/maven-support/pom.xml
+++ b/impl/maven-support/pom.xml
@@ -81,6 +81,11 @@ under the License.
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git
a/impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderTest.java
b/impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderTest.java
index 8f5b4c355f..959fb7869d 100644
---
a/impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderTest.java
+++
b/impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderTest.java
@@ -27,6 +27,8 @@
import org.apache.maven.api.model.InputSource;
import org.apache.maven.api.model.Model;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -281,6 +283,70 @@ void testLocationReportingForListElements() throws
Exception {
assertEquals(5, module3Location.getColumnNumber(), "Module 3 should
start at column 5");
}
+ @Test
+ void testRandomRootNamespaceIsRejectedInStrictMode() {
+ // https://github.com/apache/maven/issues/10985
+ String xml = pomWithNamespace("https://ff.com");
+
+ XMLStreamException ex = assertThrows(XMLStreamException.class, () ->
fromXml(xml));
+ assertTrue(
+ ex.getMessage().contains("Unrecognized namespace
'https://ff.com' for root element 'project'"),
+ ex.getMessage());
+
assertTrue(ex.getMessage().contains("http://maven.apache.org/POM/4.0.0"),
ex.getMessage());
+ assertNotNull(ex.getLocation());
+ assertEquals(1, ex.getLocation().getLineNumber());
+ }
+
+ @ParameterizedTest
+ // only exact matches are accepted, no prefix matching
+ @ValueSource(
+ strings = {
+ "http://maven.apache.org/POM/",
+ "http://maven.apache.org/POM/4.0",
+ "http://maven.apache.org/POM/4.0.0/",
+ "http://maven.apache.org/POM/9.9.9",
+ // namespaces are case-sensitive and the scheme is part of the
namespace name
+ "https://maven.apache.org/POM/4.0.0",
+ "http://maven.apache.org/pom/4.0.0",
+ // namespaces of other Maven models are not valid for a POM
+ "http://maven.apache.org/METADATA/1.1.0",
+ "http://maven.apache.org/SETTINGS/1.0.0"
+ })
+ void testUnknownRootNamespacesAreRejectedInStrictMode(String ns) {
+ XMLStreamException ex = assertThrows(XMLStreamException.class, () ->
fromXml(pomWithNamespace(ns)), ns);
+ assertTrue(ex.getMessage().contains("Unrecognized namespace '" + ns +
"'"), ex.getMessage());
+ }
+
+ @ParameterizedTest
+ @ValueSource(
+ strings = {
+ "http://maven.apache.org/POM/4.0.0",
+ "http://maven.apache.org/POM/4.1.0",
+ "http://maven.apache.org/POM/4.2.0"
+ })
+ void testKnownRootNamespacesAreAcceptedInStrictMode(String ns) throws
XMLStreamException {
+ assertEquals(ns, fromXml(pomWithNamespace(ns)).getNamespaceUri(), ns);
+ }
+
+ @Test
+ void testRandomRootNamespaceIsAcceptedInLenientMode() throws
XMLStreamException {
+ // POMs consumed from repositories are read leniently and must keep
working
+ Model model = new MavenStaxReader().read(new
StringReader(pomWithNamespace("https://ff.com")), false, null);
+
+ assertEquals("https://ff.com", model.getNamespaceUri());
+ assertEquals("unrecognized-namespace", model.getArtifactId());
+ }
+
+ private static String pomWithNamespace(String namespace) {
+ return "<project xmlns=\"" + namespace + "\">\n"
+ + " <modelVersion>4.0.0</modelVersion>\n"
+ + " <groupId>org.apache.maven.its</groupId>\n"
+ + " <artifactId>unrecognized-namespace</artifactId>\n"
+ + " <version>1.0-SNAPSHOT</version>\n"
+ + " <packaging>pom</packaging>\n"
+ + "</project>";
+ }
+
private Model fromXml(String xml) throws XMLStreamException {
MavenStaxReader reader = new MavenStaxReader();
return reader.read(new StringReader(xml), true, null);
diff --git a/src/mdo/reader-stax.vm b/src/mdo/reader-stax.vm
index 5c13c30452..0a4ae9ec3e 100644
--- a/src/mdo/reader-stax.vm
+++ b/src/mdo/reader-stax.vm
@@ -27,6 +27,36 @@
#set ( $rootUcapName = $Helper.capitalise( $root.name ) )
#set ( $rootLcapName = $Helper.uncapitalise( $root.name ) )
#
+#if ( $enforceNamespace )
+ ## Compute the root namespaces this reader accepts: one per model version
supported by the
+ ## reader, derived from the XML namespace declared in the .mdo file (never
hardcoded here).
+ ## The set of versions is collected the same way as in model-version.vm.
+ #set ( $xmlModelMetadata = $model.getMetadata(
"org.codehaus.modello.plugins.xml.metadata.XmlModelMetadata" ) )
+ #set ( $String = $model.getClass().forName( "java.lang.String" ) )
+ #set ( $TreeSet = $model.getClass().forName( "java.util.TreeSet" ) )
+ #set ( $Version = $model.getClass().forName(
"org.codehaus.modello.model.Version" ) )
+ #set ( $nsVersions = $TreeSet.newInstance() )
+ #foreach ( $class in $model.allClasses )
+ #set ( $dummy = $nsVersions.add( $class.versionRange.fromVersion ) )
+ #foreach ( $field in $class.allFields )
+ #if ( ! $Helper.xmlFieldMetadata( $field ).transient )
+ #set ( $dummy = $nsVersions.add( $field.versionRange.fromVersion ) )
+ #end
+ #end
+ #end
+ #set ( $dummy = $nsVersions.add( $version ) )
+ #set ( $dummy = $nsVersions.remove( $Version.getConstructor( $String
).newInstance( "0.0.0" ) ) )
+ #if ( $minimalVersion )
+ #set ( $minimalNsVersion = $Version.getConstructor( $String ).newInstance(
$minimalVersion ) )
+ #set ( $dummy = $nsVersions.add( $minimalNsVersion ) )
+ #set ( $nsVersions = $nsVersions.tailSet( $minimalNsVersion, true ) )
+ #end
+ #set ( $nsVersions = $nsVersions.headSet( $version, true ) )
+ #set ( $allowedNamespaces = [] )
+ #foreach ( $nsVersion in $nsVersions )
+ #set ( $dummy = $allowedNamespaces.add( $xmlModelMetadata.getNamespace(
$nsVersion ) ) )
+ #end
+#end
#set ( $needXmlContext = false )
#foreach ( $class in $model.allClasses )
#set ( $allFields = $Helper.xmlFields( $class ) )
@@ -86,6 +116,21 @@ import static java.util.Map.entry;
@Generated
public class ${className} {
+#if ( $enforceNamespace )
+
+ /**
+ * The XML namespaces accepted on the root element {@code <${rootTag}>}
when reading in strict mode,
+ * in addition to no namespace at all.
+ */
+ private static final List<String> ALLOWED_NAMESPACES = List.of(
+#foreach ( $allowedNamespace in $allowedNamespaces )
+#if ( $foreach.hasNext )
+ "${allowedNamespace}",
+#else
+ "${allowedNamespace}");
+#end
+#end
+#end
public static interface ContentTransformer {
/**
@@ -278,6 +323,11 @@ public class ${className} {
// fallback, already expected a XMLStreamException due to
invalid XML
throw new XMLStreamException("Duplicated tag:
'${rootTag}'", parser.getLocation(), null);
}
+#if ( $enforceNamespace )
+ if (strict) {
+ checkRootNamespace(parser);
+ }
+#end
#if ( $locationTracking )
$rootLcapName = parse${rootUcapName}(parser, strict,
parser.getNamespaceURI(), inputSrc);
#elseif ( $needXmlContext )
@@ -633,6 +683,27 @@ public class ${className} {
#end
#end## end #foreach ( $class in $model.allClasses )
+#if ( $enforceNamespace )
+ /**
+ * Ensures the root element is either in no namespace or in one of the
{@link #ALLOWED_NAMESPACES}.
+ *
+ * @param parser the parser, positioned on the root element
+ * @throws XMLStreamException if the root element namespace is not
recognized
+ */
+ private static void checkRootNamespace(XMLStreamReader parser) throws
XMLStreamException {
+ String ns = parser.getNamespaceURI();
+ if (ns != null && !ns.isEmpty() && !ALLOWED_NAMESPACES.contains(ns)) {
+ throw new XMLStreamException(
+ String.format("Unrecognized namespace '%s' for root
element '%s': expected no namespace or one of %s",
+ ns,
+ parser.getLocalName(),
+ ALLOWED_NAMESPACES),
+ parser.getLocation(),
+ null);
+ }
+ }
+
+#end
private void checkNamespace(XMLStreamReader parser, boolean strict, String
namespace) throws XMLStreamException {
if (strict) {
String ns = parser.getNamespaceURI();