-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Disallow arbitrary namespaces in Maven and Metadata readers #11185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arturobernalg
wants to merge
1
commit into
apache:master
Choose a base branch
from
arturobernalg:10985
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
impl/maven-support/src/test/java/org/apache/maven/model/v4/MavenStaxReaderNamespaceTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.maven.model.v4; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
|
||
import java.io.StringReader; | ||
import java.util.Locale; | ||
|
||
import org.apache.maven.api.model.InputSource; | ||
import org.apache.maven.api.model.Model; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class MavenStaxReaderNamespaceTest { | ||
|
||
private static final InputSource NO_INPUT_SOURCE = null; | ||
|
||
private static final String POM_41 = "" | ||
+ "<project xmlns=\"http://maven.apache.org/POM/4.1.0\" " | ||
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" | ||
+ " <modelVersion>4.1.0</modelVersion>" | ||
+ " <groupId>com.acme</groupId>" | ||
+ " <artifactId>demo</artifactId>" | ||
+ " <version>1.0</version>" | ||
+ "</project>"; | ||
|
||
private static final String POM_40 = "" | ||
+ "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" " | ||
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" | ||
+ " <modelVersion>4.0.0</modelVersion>" | ||
+ " <groupId>com.acme</groupId>" | ||
+ " <artifactId>demo</artifactId>" | ||
+ " <version>1.0</version>" | ||
+ "</project>"; | ||
|
||
private static final String POM_NO_NS = "" | ||
+ "<project>" | ||
+ " <modelVersion>4.0.0</modelVersion>" | ||
+ " <groupId>com.acme</groupId>" | ||
+ " <artifactId>demo</artifactId>" | ||
+ " <version>1.0</version>" | ||
+ "</project>"; | ||
|
||
private static final String POM_BAD_NS = "" | ||
+ "<project xmlns=\"http://example.com/not/pom\">" | ||
+ " <modelVersion>4.1.0</modelVersion>" | ||
+ " <groupId>com.acme</groupId>" | ||
+ " <artifactId>demo</artifactId>" | ||
+ " <version>1.0</version>" | ||
+ "</project>"; | ||
|
||
@Test | ||
void acceptsPom41() throws Exception { | ||
MavenStaxReader reader = new MavenStaxReader(); | ||
Model model = reader.read(new StringReader(POM_41), /*strict*/ true, NO_INPUT_SOURCE); | ||
assertNotNull(model); | ||
assertEquals("com.acme", model.getGroupId()); | ||
assertEquals("demo", model.getArtifactId()); | ||
assertEquals("1.0", model.getVersion()); | ||
} | ||
|
||
@Test | ||
void acceptsPom40() throws Exception { | ||
MavenStaxReader reader = new MavenStaxReader(); | ||
Model model = reader.read(new StringReader(POM_40), true, NO_INPUT_SOURCE); | ||
assertNotNull(model); | ||
assertEquals("com.acme", model.getGroupId()); | ||
assertEquals("demo", model.getArtifactId()); | ||
assertEquals("1.0", model.getVersion()); | ||
} | ||
|
||
@Test | ||
void acceptsPomWithoutNamespace() throws Exception { | ||
MavenStaxReader reader = new MavenStaxReader(); | ||
Model model = reader.read(new StringReader(POM_NO_NS), true, NO_INPUT_SOURCE); | ||
assertNotNull(model); | ||
assertEquals("com.acme", model.getGroupId()); | ||
assertEquals("demo", model.getArtifactId()); | ||
assertEquals("1.0", model.getVersion()); | ||
} | ||
|
||
@Test | ||
void rejectsUnexpectedPomNamespace() { | ||
MavenStaxReader reader = new MavenStaxReader(); | ||
XMLStreamException ex = assertThrows( | ||
XMLStreamException.class, () -> reader.read(new StringReader(POM_BAD_NS), true, NO_INPUT_SOURCE)); | ||
// sanity check: message mentions unrecognized namespace | ||
assertTrue(ex.getMessage().toLowerCase(Locale.ROOT).contains("unrecognized pom namespace")); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...aven-support/src/test/java/org/apache/maven/model/v4/MetadataStaxReaderNamespaceTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.maven.model.v4; | ||
|
||
import javax.xml.stream.XMLStreamException; | ||
|
||
import java.io.StringReader; | ||
|
||
import org.apache.maven.api.metadata.Metadata; | ||
import org.apache.maven.metadata.v4.MetadataStaxReader; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class MetadataStaxReaderNamespaceTest { | ||
|
||
private static final String META_110 = "" | ||
+ "<metadata xmlns=\"http://maven.apache.org/METADATA/1.1.0\">" | ||
+ " <groupId>com.acme</groupId>" | ||
+ " <artifactId>demo</artifactId>" | ||
+ " <versioning>" | ||
+ " <latest>1.0</latest>" | ||
+ " <release>1.0</release>" | ||
+ " </versioning>" | ||
+ "</metadata>"; | ||
|
||
private static final String META_NO_NS = "" | ||
+ "<metadata>" | ||
+ " <groupId>com.acme</groupId>" | ||
+ " <artifactId>demo</artifactId>" | ||
+ " <versioning>" | ||
+ " <latest>1.0</latest>" | ||
+ " </versioning>" | ||
+ "</metadata>"; | ||
|
||
private static final String META_BAD_NS = "" | ||
+ "<metadata xmlns=\"http://example.com/not/metadata\">" | ||
+ " <groupId>com.acme</groupId>" | ||
+ " <artifactId>demo</artifactId>" | ||
+ "</metadata>"; | ||
|
||
@Test | ||
void acceptsMetadata110() throws Exception { | ||
MetadataStaxReader reader = new MetadataStaxReader(); | ||
Metadata metadata = reader.read(new StringReader(META_110), /*strict*/ true); | ||
assertNotNull(metadata); | ||
assertEquals("com.acme", metadata.getGroupId()); | ||
assertEquals("demo", metadata.getArtifactId()); | ||
assertNotNull(metadata.getVersioning()); | ||
assertEquals("1.0", metadata.getVersioning().getLatest()); | ||
} | ||
|
||
@Test | ||
void acceptsMetadataWithoutNamespace() throws Exception { | ||
MetadataStaxReader reader = new MetadataStaxReader(); | ||
Metadata metadata = reader.read(new StringReader(META_NO_NS), true); | ||
assertNotNull(metadata); | ||
assertEquals("com.acme", metadata.getGroupId()); | ||
assertEquals("demo", metadata.getArtifactId()); | ||
} | ||
|
||
@Test | ||
void rejectsUnexpectedMetadataNamespace() { | ||
MetadataStaxReader reader = new MetadataStaxReader(); | ||
XMLStreamException ex = | ||
assertThrows(XMLStreamException.class, () -> reader.read(new StringReader(META_BAD_NS), true)); | ||
assertTrue(ex.getMessage().toLowerCase().contains("unrecognized metadata namespace")); | ||
} | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. We have generators to generate specific readers, not a generic one which covers all use cases. So it's wrong to put hardcoded values for one or two models in the output of all readers.
The way to go is to retrieve the namespace using modello. The
helper
needs to be enhanced:https://github.com/codehaus-plexus/modello/blob/ca30aba14346bf0e183efd2c442f061024c40111/modello-plugins/modello-plugin-velocity/src/main/java/org/codehaus/modello/plugin/velocity/Helper.java
It needs a new method
xmlModelMetadata
which would return theXmlModelMetadata
from which we can get the target namespace.Once modello is fixed, we can update the velocity templates to leverage that using
Helper.xmlModelMetadata(model).namespace
.