This is an automated email from the ASF dual-hosted git repository. robertlazarski pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-core.git
commit 17f3ae5ce9113da511785aecf060780fb031d516 Author: Robert Lazarski <[email protected]> AuthorDate: Sat Nov 21 07:39:13 2026 -1000 Keep repo plugin service directories inside the repository create-repository used the service name from services.xml as a directory name unchecked, so a name with ".." wrote the descriptor and its sibling files outside the services directory. Such a name now fails the build. Co-Authored-By: Claude Fable 5.2 <[email protected]> --- modules/tool/axis2-repo-maven-plugin/pom.xml | 5 ++ .../maven2/repo/AbstractCreateRepositoryMojo.java | 24 ++++++- .../repo/ServiceDirectoryContainmentTest.java | 74 ++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/modules/tool/axis2-repo-maven-plugin/pom.xml b/modules/tool/axis2-repo-maven-plugin/pom.xml index 6929ce5df1..3629c3fa1d 100644 --- a/modules/tool/axis2-repo-maven-plugin/pom.xml +++ b/modules/tool/axis2-repo-maven-plugin/pom.xml @@ -105,6 +105,11 @@ <type>mar</type> <scope>test</scope> </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/modules/tool/axis2-repo-maven-plugin/src/main/java/org/apache/axis2/maven2/repo/AbstractCreateRepositoryMojo.java b/modules/tool/axis2-repo-maven-plugin/src/main/java/org/apache/axis2/maven2/repo/AbstractCreateRepositoryMojo.java index a2c93b23b3..78adb1ef5c 100644 --- a/modules/tool/axis2-repo-maven-plugin/src/main/java/org/apache/axis2/maven2/repo/AbstractCreateRepositoryMojo.java +++ b/modules/tool/axis2-repo-maven-plugin/src/main/java/org/apache/axis2/maven2/repo/AbstractCreateRepositoryMojo.java @@ -183,6 +183,28 @@ public abstract class AbstractCreateRepositoryMojo extends AbstractMojo { protected abstract File[] getClassDirectories(); + /** + * Resolves the directory a service description is written to. + * <p> + * The name comes from a services.xml that may have been generated from somebody + * else's WSDL, where it is the wsdl:service name verbatim, so it can carry + * separators and "..". Keep this check: without it the name decides where the + * descriptor and the files next to it are written. + */ + static File resolveServiceDirectory(File parentDirectory, String serviceName) + throws MojoFailureException, IOException { + if (serviceName == null || serviceName.isEmpty()) { + throw new MojoFailureException("The service has no name"); + } + File serviceDirectory = new File(parentDirectory, serviceName); + String canonicalParent = parentDirectory.getCanonicalPath(); + if (!serviceDirectory.getCanonicalPath().startsWith(canonicalParent + File.separator)) { + throw new MojoFailureException("The service name '" + serviceName + + "' resolves to a location outside " + canonicalParent); + } + return serviceDirectory; + } + private static void applyParameters(OMElement parentElement, Parameter[] parameters) { if (parameters == null) { return; @@ -343,7 +365,7 @@ public abstract class AbstractCreateRepositoryMojo extends AbstractMojo { } String serviceName = serviceElement.getAttributeValue(new QName("name")); log.info("Building service " + serviceName); - metaInfDirectory = new File(new File(parentDirectory, serviceName), "META-INF"); + metaInfDirectory = new File(resolveServiceDirectory(parentDirectory, serviceName), "META-INF"); metaInfDirectory.mkdirs(); if (serviceDescription.getScope() != null) { serviceElement.addAttribute("scope", serviceDescription.getScope(), null); diff --git a/modules/tool/axis2-repo-maven-plugin/src/test/java/org/apache/axis2/maven2/repo/ServiceDirectoryContainmentTest.java b/modules/tool/axis2-repo-maven-plugin/src/test/java/org/apache/axis2/maven2/repo/ServiceDirectoryContainmentTest.java new file mode 100644 index 0000000000..ecca24f666 --- /dev/null +++ b/modules/tool/axis2-repo-maven-plugin/src/test/java/org/apache/axis2/maven2/repo/ServiceDirectoryContainmentTest.java @@ -0,0 +1,74 @@ +/* + * 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.axis2.maven2.repo; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.File; +import java.nio.file.Path; + +import org.apache.maven.plugin.MojoFailureException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * The service name in a services.xml must not move the service directory out of + * the repository's services directory. + */ +public class ServiceDirectoryContainmentTest { + + @TempDir + Path temp; + + @Test + public void ordinaryNameStaysInTheServicesDirectory() throws Exception { + File services = new File(temp.toFile(), "repository/services"); + assertEquals(new File(services, "DemoService"), + AbstractCreateRepositoryMojo.resolveServiceDirectory(services, "DemoService")); + } + + @Test + public void traversingNameIsRefused() { + File services = new File(temp.toFile(), "repository/services"); + assertThrows(MojoFailureException.class, () -> + AbstractCreateRepositoryMojo.resolveServiceDirectory(services, "../../../../escaped")); + assertThrows(MojoFailureException.class, () -> + AbstractCreateRepositoryMojo.resolveServiceDirectory(services, "a/../../escaped")); + assertFalse(new File(temp.toFile().getParentFile(), "escaped").exists()); + } + + @Test + public void nameResolvingToTheServicesDirectoryItselfIsRefused() { + File services = new File(temp.toFile(), "repository/services"); + assertThrows(MojoFailureException.class, () -> + AbstractCreateRepositoryMojo.resolveServiceDirectory(services, ".")); + assertThrows(MojoFailureException.class, () -> + AbstractCreateRepositoryMojo.resolveServiceDirectory(services, "")); + } + + @Test + public void absoluteNameIsKeptUnderTheServicesDirectory() throws Exception { + File services = new File(temp.toFile(), "repository/services"); + File resolved = AbstractCreateRepositoryMojo.resolveServiceDirectory(services, "/etc/demo"); + assertEquals(new File(services, "etc/demo").getCanonicalFile(), resolved.getCanonicalFile()); + } +}
