Skip to content

Commit c0012c0

Browse files
authored
[MNG-8180] Fail install/deploy if rogue Maven Plugin metadata found (#1612)
Resolver handles transparently the repository metadata, and in case of plugins it peeks into META-INF/maven/plugin.xml of given artifact JAR to figure out needed metadata bits (prefix, name, etc). But, this was done "blindly", while it is expected that GA of JAR artifact without classifier (requirement for maven plugins) and GA in embedded plugin metadata must be same. Decision here is to fail hard, prevent this being installed and deployed, as this is most probably wrong (unsure what maven-indexer or even Sonatype search would do in this case). --- https://issues.apache.org/jira/browse/MNG-8180
1 parent f2a0865 commit c0012c0

File tree

6 files changed

+140
-7
lines changed

6 files changed

+140
-7
lines changed

maven-api-impl/src/main/java/org/apache/maven/internal/impl/resolver/PluginsMetadataGenerator.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Iterator;
2828
import java.util.LinkedHashMap;
2929
import java.util.Map;
30+
import java.util.Objects;
3031
import java.util.jar.JarFile;
3132
import java.util.zip.ZipEntry;
3233

@@ -132,14 +133,34 @@ private PluginInfo extractPluginInfo(Artifact artifact) {
132133
String artifactId = root.getChild("artifactId").getValue();
133134
String goalPrefix = root.getChild("goalPrefix").getValue();
134135
String name = root.getChild("name").getValue();
135-
return new PluginInfo(groupId, artifactId, goalPrefix, name);
136+
// sanity check: plugin descriptor extracted from artifact must have same GA
137+
if (Objects.equals(artifact.getGroupId(), groupId)
138+
&& Objects.equals(artifact.getArtifactId(), artifactId)) {
139+
return new PluginInfo(groupId, artifactId, goalPrefix, name);
140+
} else {
141+
throw new InvalidArtifactPluginMetadataException(
142+
"Artifact " + artifact.getGroupId() + ":"
143+
+ artifact.getArtifactId()
144+
+ " JAR (to be installed/deployed) contains Maven Plugin metadata for plugin "
145+
+ groupId + ":" + artifactId + "; coordinates are conflicting. "
146+
+ "Most probably your JAR contains rogue Maven Plugin metadata, "
147+
+ "possible causes may be: shaded in Maven Plugin or some rogue resource)");
148+
}
136149
}
137150
}
151+
} catch (RuntimeException e) {
152+
throw e;
138153
} catch (Exception e) {
139154
// here we can have: IO. ZIP or Plexus Conf Ex: but we should not interfere with user intent
140155
}
141156
}
142157
}
143158
return null;
144159
}
160+
161+
public static final class InvalidArtifactPluginMetadataException extends IllegalArgumentException {
162+
InvalidArtifactPluginMetadataException(String s) {
163+
super(s);
164+
}
165+
}
145166
}

maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Iterator;
2828
import java.util.LinkedHashMap;
2929
import java.util.Map;
30+
import java.util.Objects;
3031
import java.util.jar.JarFile;
3132
import java.util.zip.ZipEntry;
3233

@@ -133,14 +134,34 @@ private PluginInfo extractPluginInfo(Artifact artifact) {
133134
String artifactId = root.getChild("artifactId").getValue();
134135
String goalPrefix = root.getChild("goalPrefix").getValue();
135136
String name = root.getChild("name").getValue();
136-
return new PluginInfo(groupId, artifactId, goalPrefix, name);
137+
// sanity check: plugin descriptor extracted from artifact must have same GA
138+
if (Objects.equals(artifact.getGroupId(), groupId)
139+
&& Objects.equals(artifact.getArtifactId(), artifactId)) {
140+
return new PluginInfo(groupId, artifactId, goalPrefix, name);
141+
} else {
142+
throw new InvalidArtifactPluginMetadataException(
143+
"Artifact " + artifact.getGroupId() + ":"
144+
+ artifact.getArtifactId()
145+
+ " JAR (to be installed/deployed) contains Maven Plugin metadata for plugin "
146+
+ groupId + ":" + artifactId + "; coordinates are conflicting. "
147+
+ "Most probably your JAR contains rogue Maven Plugin metadata, "
148+
+ "possible causes may be: shaded in Maven Plugin or some rogue resource)");
149+
}
137150
}
138151
}
152+
} catch (RuntimeException e) {
153+
throw e;
139154
} catch (Exception e) {
140155
// here we can have: IO. ZIP or Plexus Conf Ex: but we should not interfere with user intent
141156
}
142157
}
143158
}
144159
return null;
145160
}
161+
162+
public static final class InvalidArtifactPluginMetadataException extends IllegalArgumentException {
163+
InvalidArtifactPluginMetadataException(String s) {
164+
super(s);
165+
}
166+
}
146167
}

maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RepositorySystemTest.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@
1818
*/
1919
package org.apache.maven.repository.internal;
2020

21+
import java.nio.file.Files;
2122
import java.util.Arrays;
2223
import java.util.List;
2324

25+
import org.eclipse.aether.DefaultRepositorySystemSession;
2426
import org.eclipse.aether.artifact.Artifact;
2527
import org.eclipse.aether.artifact.DefaultArtifact;
2628
import org.eclipse.aether.collection.CollectRequest;
2729
import org.eclipse.aether.collection.CollectResult;
2830
import org.eclipse.aether.graph.Dependency;
2931
import org.eclipse.aether.graph.DependencyNode;
32+
import org.eclipse.aether.installation.InstallRequest;
33+
import org.eclipse.aether.repository.LocalRepository;
3034
import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
3135
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
3236
import org.eclipse.aether.resolution.ArtifactRequest;
3337
import org.eclipse.aether.resolution.ArtifactResult;
3438
import org.junit.jupiter.api.Test;
3539

36-
import static org.junit.jupiter.api.Assertions.assertEquals;
37-
import static org.junit.jupiter.api.Assertions.assertFalse;
38-
import static org.junit.jupiter.api.Assertions.assertNotNull;
39-
import static org.junit.jupiter.api.Assertions.assertNull;
40-
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
import static org.junit.jupiter.api.Assertions.*;
4141

4242
class RepositorySystemTest extends AbstractRepositoryTestCase {
4343
@Test
@@ -211,4 +211,30 @@ void testNewLocalRepositoryManager() throws Exception {
211211
void testNewSyncContext() throws Exception {
212212
// SyncContext newSyncContext( RepositorySystemSession session, boolean shared );
213213
}
214+
215+
@Test
216+
void testRoguePlugin() throws Exception {
217+
Artifact artifact = new DefaultArtifact("ut.simple:rogue-plugin:1.0");
218+
219+
ArtifactRequest artifactRequest = new ArtifactRequest();
220+
artifactRequest.setArtifact(artifact);
221+
artifactRequest.addRepository(newTestRepository());
222+
223+
ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
224+
checkArtifactResult(artifactResult, "rogue-plugin-1.0.jar");
225+
226+
InstallRequest installRequest = new InstallRequest();
227+
installRequest.addArtifact(artifactResult.getArtifact());
228+
229+
DefaultRepositorySystemSession loc = new DefaultRepositorySystemSession(session);
230+
loc.setLocalRepositoryManager(
231+
system.newLocalRepositoryManager(session, new LocalRepository(Files.createTempDirectory("local"))));
232+
try {
233+
system.install(loc, installRequest);
234+
fail("install should fail");
235+
} catch (Exception e) {
236+
assertInstanceOf(PluginsMetadataGenerator.InvalidArtifactPluginMetadataException.class, e);
237+
assertTrue(e.getMessage().contains("coordinates are conflicting"));
238+
}
239+
}
214240
}
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
~ Licensed to the Apache Software Foundation (ASF) under one
5+
~ or more contributor license agreements. See the NOTICE file
6+
~ distributed with this work for additional information
7+
~ regarding copyright ownership. The ASF licenses this file
8+
~ to you under the Apache License, Version 2.0 (the
9+
~ "License"); you may not use this file except in compliance
10+
~ with the License. You may obtain a copy of the License at
11+
~
12+
~ http://www.apache.org/licenses/LICENSE-2.0
13+
~
14+
~ Unless required by applicable law or agreed to in writing,
15+
~ software distributed under the License is distributed on an
16+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
~ KIND, either express or implied. See the License for the
18+
~ specific language governing permissions and limitations
19+
~ under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>ut.simple</groupId>
27+
<artifactId>rogue-plugin</artifactId>
28+
<version>1.0</version>
29+
30+
<name>Simple Unit Test Rogue Plugin</name>
31+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
~ Licensed to the Apache Software Foundation (ASF) under one
5+
~ or more contributor license agreements. See the NOTICE file
6+
~ distributed with this work for additional information
7+
~ regarding copyright ownership. The ASF licenses this file
8+
~ to you under the Apache License, Version 2.0 (the
9+
~ "License"); you may not use this file except in compliance
10+
~ with the License. You may obtain a copy of the License at
11+
~
12+
~ http://www.apache.org/licenses/LICENSE-2.0
13+
~
14+
~ Unless required by applicable law or agreed to in writing,
15+
~ software distributed under the License is distributed on an
16+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
~ KIND, either express or implied. See the License for the
18+
~ specific language governing permissions and limitations
19+
~ under the License.
20+
-->
21+
22+
<metadata xmlns="http://maven.apache.org/METADATA/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/METADATA/1.1.0 http://maven.apache.org/xsd/metadata-1.1.0.xsd">
24+
<groupId>ut.simple</groupId>
25+
<artifactId>rogue-plugin</artifactId>
26+
<versioning>
27+
<latest>1.0</latest>
28+
<release>1.0</release>
29+
<versions>
30+
<version>1.0</version>
31+
</versions>
32+
<lastUpdated>20111123122038</lastUpdated>
33+
</versioning>
34+
</metadata>

0 commit comments

Comments
 (0)