This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.9.x by this push:
     new 8d0e438bc3 [MNG-8180] Fail install/deploy if rogue Maven Plugin 
metadata found (#1611)
8d0e438bc3 is described below

commit 8d0e438bc3d39bed2a94e53d713d0dd9895cb344
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Thu Jul 11 18:47:01 2024 +0200

    [MNG-8180] Fail install/deploy if rogue Maven Plugin metadata found (#1611)
    
    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
---
 .../internal/PluginsMetadataGenerator.java         |  23 +++++++++++++-
 .../repository/internal/RepositorySystemTest.java  |  29 ++++++++++++++++++
 .../simple/rogue-plugin/1.0/rogue-plugin-1.0.jar   | Bin 0 -> 7446 bytes
 .../simple/rogue-plugin/1.0/rogue-plugin-1.0.pom   |  31 +++++++++++++++++++
 .../repo/ut/simple/rogue-plugin/maven-metadata.xml |  34 +++++++++++++++++++++
 5 files changed, 116 insertions(+), 1 deletion(-)

diff --git 
a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java
 
b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java
index b3d68847ec..b37992df30 100644
--- 
a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java
+++ 
b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java
@@ -27,6 +27,7 @@ import java.util.Date;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Objects;
 import java.util.jar.JarFile;
 import java.util.zip.ZipEntry;
 
@@ -135,9 +136,23 @@ class PluginsMetadataGenerator implements 
MetadataGenerator {
                             String artifactId = 
root.getChild("artifactId").getValue();
                             String goalPrefix = 
root.getChild("goalPrefix").getValue();
                             String name = root.getChild("name").getValue();
-                            return new PluginInfo(groupId, artifactId, 
goalPrefix, name);
+                            // sanity check: plugin descriptor extracted from 
artifact must have same GA
+                            if (Objects.equals(artifact.getGroupId(), groupId)
+                                    && 
Objects.equals(artifact.getArtifactId(), artifactId)) {
+                                return new PluginInfo(groupId, artifactId, 
goalPrefix, name);
+                            } else {
+                                throw new 
InvalidArtifactPluginMetadataException(
+                                        "Artifact " + artifact.getGroupId() + 
":"
+                                                + artifact.getArtifactId()
+                                                + " JAR (to be 
installed/deployed) contains Maven Plugin metadata for plugin "
+                                                + groupId + ":" + artifactId + 
"; coordinates are conflicting. "
+                                                + "Most probably your JAR 
contains rogue Maven Plugin metadata, "
+                                                + "possible causes may be: 
shaded in Maven Plugin or some rogue resource)");
+                            }
                         }
                     }
+                } catch (RuntimeException e) {
+                    throw e;
                 } catch (Exception e) {
                     // here we can have: IO. ZIP or Plexus Conf Ex: but we 
should not interfere with user intent
                 }
@@ -145,4 +160,10 @@ class PluginsMetadataGenerator implements 
MetadataGenerator {
         }
         return null;
     }
+
+    public static final class InvalidArtifactPluginMetadataException extends 
IllegalArgumentException {
+        InvalidArtifactPluginMetadataException(String s) {
+            super(s);
+        }
+    }
 }
diff --git 
a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RepositorySystemTest.java
 
b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RepositorySystemTest.java
index 8031ca5083..c22db033eb 100644
--- 
a/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RepositorySystemTest.java
+++ 
b/maven-resolver-provider/src/test/java/org/apache/maven/repository/internal/RepositorySystemTest.java
@@ -18,15 +18,19 @@
  */
 package org.apache.maven.repository.internal;
 
+import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.List;
 
+import org.eclipse.aether.DefaultRepositorySystemSession;
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.artifact.DefaultArtifact;
 import org.eclipse.aether.collection.CollectRequest;
 import org.eclipse.aether.collection.CollectResult;
 import org.eclipse.aether.graph.Dependency;
 import org.eclipse.aether.graph.DependencyNode;
+import org.eclipse.aether.installation.InstallRequest;
+import org.eclipse.aether.repository.LocalRepository;
 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
 import org.eclipse.aether.resolution.ArtifactRequest;
@@ -193,4 +197,29 @@ public class RepositorySystemTest extends 
AbstractRepositoryTestCase {
     public void testNewSyncContext() throws Exception {
         // SyncContext newSyncContext( RepositorySystemSession session, 
boolean shared );
     }
+
+    public void testRoguePlugin() throws Exception {
+        Artifact artifact = new DefaultArtifact("ut.simple:rogue-plugin:1.0");
+
+        ArtifactRequest artifactRequest = new ArtifactRequest();
+        artifactRequest.setArtifact(artifact);
+        artifactRequest.addRepository(newTestRepository());
+
+        ArtifactResult artifactResult = system.resolveArtifact(session, 
artifactRequest);
+        checkArtifactResult(artifactResult, "rogue-plugin-1.0.jar");
+
+        InstallRequest installRequest = new InstallRequest();
+        installRequest.addArtifact(artifactResult.getArtifact());
+
+        DefaultRepositorySystemSession loc = new 
DefaultRepositorySystemSession(session);
+        loc.setLocalRepositoryManager(system.newLocalRepositoryManager(
+                session, new 
LocalRepository(Files.createTempDirectory("local").toFile())));
+        try {
+            system.install(loc, installRequest);
+            fail("install should fail");
+        } catch (Exception e) {
+            assertTrue(e instanceof 
PluginsMetadataGenerator.InvalidArtifactPluginMetadataException);
+            assertTrue(e.getMessage().contains("coordinates are conflicting"));
+        }
+    }
 }
diff --git 
a/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/1.0/rogue-plugin-1.0.jar
 
b/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/1.0/rogue-plugin-1.0.jar
new file mode 100644
index 0000000000..8163c13626
Binary files /dev/null and 
b/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/1.0/rogue-plugin-1.0.jar
 differ
diff --git 
a/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/1.0/rogue-plugin-1.0.pom
 
b/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/1.0/rogue-plugin-1.0.pom
new file mode 100644
index 0000000000..84d007fc5f
--- /dev/null
+++ 
b/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/1.0/rogue-plugin-1.0.pom
@@ -0,0 +1,31 @@
+<?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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>ut.simple</groupId>
+  <artifactId>rogue-plugin</artifactId>
+  <version>1.0</version>
+
+  <name>Simple Unit Test Rogue Plugin</name>
+</project>
diff --git 
a/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/maven-metadata.xml
 
b/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/maven-metadata.xml
new file mode 100644
index 0000000000..8618d47389
--- /dev/null
+++ 
b/maven-resolver-provider/src/test/resources/repo/ut/simple/rogue-plugin/maven-metadata.xml
@@ -0,0 +1,34 @@
+<?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.
+-->
+
+<metadata xmlns="http://maven.apache.org/METADATA/1.1.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/METADATA/1.1.0 
http://maven.apache.org/xsd/metadata-1.1.0.xsd";>
+  <groupId>ut.simple</groupId>
+  <artifactId>rogue-plugin</artifactId>
+  <versioning>
+    <latest>1.0</latest>
+    <release>1.0</release>
+    <versions>
+      <version>1.0</version>
+    </versions>
+    <lastUpdated>20111123122038</lastUpdated>
+  </versioning>
+</metadata>
\ No newline at end of file

Reply via email to