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 2b8f4f44c0ac2b53a69e3ece18f94eec56b69635 Author: Robert Lazarski <[email protected]> AuthorDate: Sat Nov 21 07:39:13 2026 -1000 Scope WSDL2Code credentials to the party they were given for The JVM-wide authenticator WSDL2Code installs returned its credentials for any challenge, so a server named by a WSDL import could collect them with a 401. Proxy credentials now answer proxy challenges only, and credentials from the -uri URL answer only that URL's host and port. Co-Authored-By: Claude Fable 5.2 <[email protected]> --- .../src/org/apache/axis2/wsdl/WSDL2Code.java | 84 +++++++++++++++---- .../axis2/wsdl/WSDL2CodeAuthenticatorTest.java | 96 ++++++++++++++++++++++ 2 files changed, 162 insertions(+), 18 deletions(-) diff --git a/modules/codegen/src/org/apache/axis2/wsdl/WSDL2Code.java b/modules/codegen/src/org/apache/axis2/wsdl/WSDL2Code.java index 8a482ac041..1ff0d0b5ea 100644 --- a/modules/codegen/src/org/apache/axis2/wsdl/WSDL2Code.java +++ b/modules/codegen/src/org/apache/axis2/wsdl/WSDL2Code.java @@ -90,21 +90,35 @@ public class WSDL2Code { } private static void checkAuthentication(CommandLineOptionParser commandLineOptionParser) { - + Authenticator authenticator = createAuthenticator(commandLineOptionParser); + if (authenticator != null) { + Authenticator.setDefault(authenticator); + } + } + + /** + * Builds the authenticator for the credentials given on the command line, or + * returns {@code null} if there are none. + */ + static Authenticator createAuthenticator(CommandLineOptionParser commandLineOptionParser) { + String userName = null; String password = null; - - Map allOptions = commandLineOptionParser.getAllOptions(); + // Where the credentials may be sent: null means the proxy, otherwise the + // server the WSDL URL names. + URL origin = null; + + Map allOptions = commandLineOptionParser.getAllOptions(); CommandLineOption userOption = (CommandLineOption) allOptions .get(CommandLineOptionConstants.WSDL2JavaConstants.HTTP_PROXY_USER_OPTION_LONG); CommandLineOption passwordOption = (CommandLineOption) allOptions .get(CommandLineOptionConstants.WSDL2JavaConstants.HTTP_PROXY_PASSWORD_OPTION_LONG); CommandLineOption urlOption = (CommandLineOption) allOptions .get(CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION); - - if(urlOption == null){ - return; - } + + if (urlOption == null) { + return null; + } if (userOption != null) { userName = userOption.getOptionValue(); } @@ -113,12 +127,12 @@ public class WSDL2Code { } if (userName == null) { - // Try to collect user name and password from UserInfo part of the URL. + // Try to collect user name and password from UserInfo part of the URL. URL url = null; try { url = new URL(urlOption.getOptionValue()); } catch (MalformedURLException e) { - return; + return null; } String userInfo = url.getUserInfo(); @@ -131,21 +145,55 @@ public class WSDL2Code { } else { userName = userInfo; } + origin = url; } } - - if (userName != null) { - final String user = userName; - final String pass = password == null ? "" : password; - Authenticator.setDefault(new Authenticator() { - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(user, pass.toCharArray()); + + if (userName == null) { + return null; + } + return new ScopedAuthenticator(userName, password == null ? "" : password, origin); + } + + /** + * Answers a challenge only from the party the credentials were given for. + * <p> + * This authenticator is JVM-wide, and code generation fetches every document the + * WSDL imports, from whatever host the WSDL names. Do not go back to returning the + * credentials unconditionally: any of those hosts could then collect them with a + * 401. Proxy credentials answer proxy challenges only; credentials taken from the + * WSDL URL answer only the host and port of that URL. + */ + static final class ScopedAuthenticator extends Authenticator { + private final String user; + private final String password; + private final URL origin; + + ScopedAuthenticator(String user, String password, URL origin) { + this.user = user; + this.password = password; + this.origin = origin; + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + if (origin == null) { + if (getRequestorType() != RequestorType.PROXY) { + return null; } - }); + } else { + int originPort = origin.getPort() != -1 ? origin.getPort() : origin.getDefaultPort(); + if (getRequestorType() != RequestorType.SERVER + || !origin.getHost().equalsIgnoreCase(getRequestingHost()) + || originPort != getRequestingPort()) { + return null; + } + } + return new PasswordAuthentication(user, password.toCharArray()); } } - + private static void setSystemProperties(CommandLineOptionParser commandLineOptionParser) { Map<String, CommandLineOption> allOptions = commandLineOptionParser.getAllOptions(); // System properties follow "-Dproperty=value" format, only key is required. diff --git a/modules/codegen/test/org/apache/axis2/wsdl/WSDL2CodeAuthenticatorTest.java b/modules/codegen/test/org/apache/axis2/wsdl/WSDL2CodeAuthenticatorTest.java new file mode 100644 index 0000000000..c9a6fb9776 --- /dev/null +++ b/modules/codegen/test/org/apache/axis2/wsdl/WSDL2CodeAuthenticatorTest.java @@ -0,0 +1,96 @@ +/* + * 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.wsdl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.net.Authenticator; +import java.net.Authenticator.RequestorType; +import java.net.PasswordAuthentication; +import java.net.URL; + +import org.apache.axis2.util.CommandLineOptionParser; +import org.junit.jupiter.api.Test; + +/** + * Credentials given to WSDL2Code answer only the party they were given for, not + * every server a WSDL import happens to name. + */ +public class WSDL2CodeAuthenticatorTest { + + private static Authenticator authenticatorFor(String... args) { + return WSDL2Code.createAuthenticator(new CommandLineOptionParser(args)); + } + + private static PasswordAuthentication challenge(Authenticator authenticator, String host, + int port, RequestorType type) throws Exception { + return authenticator.requestPasswordAuthenticationInstance(host, null, port, "http", + "realm", "basic", new URL("http://" + host + ":" + port + "/ext.xsd"), type); + } + + @Test + public void proxyCredentialsAnswerTheProxyOnly() throws Exception { + Authenticator authenticator = authenticatorFor("-uri", "http://wsdl.example/main.wsdl", + "-http-proxy-user", "proxyuser", "-http-proxy-password", "proxypass"); + assertNotNull(authenticator); + + PasswordAuthentication forProxy = + challenge(authenticator, "proxy.example", 3128, RequestorType.PROXY); + assertNotNull(forProxy); + assertEquals("proxyuser", forProxy.getUserName()); + assertEquals("proxypass", new String(forProxy.getPassword())); + + assertNull(challenge(authenticator, "import.example", 80, RequestorType.SERVER), + "an import server's 401 was answered with the proxy credentials"); + assertNull(challenge(authenticator, "wsdl.example", 80, RequestorType.SERVER)); + } + + @Test + public void urlCredentialsAnswerTheWsdlHostOnly() throws Exception { + Authenticator authenticator = + authenticatorFor("-uri", "http://wsdluser:[email protected]:8080/main.wsdl"); + assertNotNull(authenticator); + + PasswordAuthentication forOrigin = + challenge(authenticator, "WSDL.example", 8080, RequestorType.SERVER); + assertNotNull(forOrigin); + assertEquals("wsdluser", forOrigin.getUserName()); + assertEquals("wsdlpass", new String(forOrigin.getPassword())); + + assertNull(challenge(authenticator, "import.example", 8080, RequestorType.SERVER), + "an import server's 401 was answered with the WSDL server's credentials"); + assertNull(challenge(authenticator, "wsdl.example", 9090, RequestorType.SERVER)); + assertNull(challenge(authenticator, "wsdl.example", 8080, RequestorType.PROXY)); + } + + @Test + public void urlCredentialsUseTheDefaultPort() throws Exception { + Authenticator authenticator = + authenticatorFor("-uri", "http://wsdluser:[email protected]/main.wsdl"); + assertNotNull(challenge(authenticator, "wsdl.example", 80, RequestorType.SERVER)); + } + + @Test + public void noCredentialsMeansNoAuthenticator() { + assertNull(authenticatorFor("-uri", "http://wsdl.example/main.wsdl")); + } +}
