gnodet-bot commented on code in PR #2149: URL: https://github.com/apache/maven-resolver/pull/2149#discussion_r4068877065
########## maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SystemProxyAuthenticationStrategy.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.eclipse.aether.transport.http; + +import java.util.Map; +import java.util.Queue; + +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthOption; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.AuthState; +import org.apache.http.auth.MalformedChallengeException; +import org.apache.http.auth.NTCredentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.AuthSchemes; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.conn.routing.RouteInfo; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; + +/** + * Adds system proxy credentials only while selecting authentication for the current route's proxy. + */ +final class SystemProxyAuthenticationStrategy extends ProxyAuthenticationStrategy implements HttpRequestInterceptor { + private static final String SYSTEM_PROXY = SystemProxyAuthenticationStrategy.class.getName() + ".proxy"; + + @Override + public void process(HttpRequest request, HttpContext context) { + HttpHost authenticatedProxy = (HttpHost) context.getAttribute(SYSTEM_PROXY); + RouteInfo route = HttpClientContext.adapt(context).getHttpRoute(); + if (authenticatedProxy != null && route != null && !authenticatedProxy.equals(route.getProxyHost())) { + // HttpClient retains Basic proxy authentication across redirects, even when the proxy changes. + AuthState state = HttpClientContext.adapt(context).getProxyAuthState(); + if (state != null) { + state.reset(); + } + request.removeHeaders("Proxy-Authorization"); + context.removeAttribute(SYSTEM_PROXY); + } + } + + @Override + public Queue<AuthOption> select( + Map<String, Header> challenges, HttpHost authhost, HttpResponse response, HttpContext context) + throws MalformedChallengeException { + Queue<AuthOption> options = super.select(challenges, authhost, response, context); + if (!options.isEmpty()) { + return options; + } + RouteInfo route = HttpClientContext.adapt(context).getHttpRoute(); + if (route == null || !authhost.equals(route.getProxyHost())) { + return options; + } + CredentialsProvider credentials = credentials(authhost, "http"); + if (credentials == null) { + credentials = credentials(authhost, "https"); + } + if (credentials == null) { + return options; + } + // A child context keeps proxy secrets out of server authentication, including after redirects. + HttpClientContext proxyContext = HttpClientContext.adapt(new BasicHttpContext(context)); + proxyContext.setCredentialsProvider(credentials); Review Comment: ⚠️ **Bug: wrong credential-lookup protocol order for HTTPS routes.** This hardcodes `"http"` as the first attempt, then falls back to `"https"`. For an HTTPS proxy route, this tries `http.proxyUser`/`http.proxyPassword` before `https.proxyUser`/`https.proxyPassword` — the opposite of what users expect. The master branch equivalent (PR #2148) fixes this by reading the route's actual target protocol first: ```suggestion String routeProtocol = route.getTargetHost().getSchemeName().toLowerCase(java.util.Locale.ENGLISH); CredentialsProvider credentials = credentials(authhost, routeProtocol); String fallbackProtocol = "https".equalsIgnoreCase(routeProtocol) ? "http" : "https"; if (credentials == null) { credentials = credentials(authhost, fallbackProtocol); } ``` You'll also need `import java.util.Locale;`. ########## maven-resolver-transport-http/src/main/java/org/eclipse/aether/transport/http/SystemProxyAuthenticationStrategy.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.eclipse.aether.transport.http; + +import java.util.Map; +import java.util.Queue; + +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthOption; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.AuthState; +import org.apache.http.auth.MalformedChallengeException; +import org.apache.http.auth.NTCredentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.AuthSchemes; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.conn.routing.RouteInfo; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; + +/** + * Adds system proxy credentials only while selecting authentication for the current route's proxy. + */ +final class SystemProxyAuthenticationStrategy extends ProxyAuthenticationStrategy implements HttpRequestInterceptor { + private static final String SYSTEM_PROXY = SystemProxyAuthenticationStrategy.class.getName() + ".proxy"; + + @Override + public void process(HttpRequest request, HttpContext context) { + HttpHost authenticatedProxy = (HttpHost) context.getAttribute(SYSTEM_PROXY); + RouteInfo route = HttpClientContext.adapt(context).getHttpRoute(); + if (authenticatedProxy != null && route != null && !authenticatedProxy.equals(route.getProxyHost())) { + // HttpClient retains Basic proxy authentication across redirects, even when the proxy changes. + AuthState state = HttpClientContext.adapt(context).getProxyAuthState(); + if (state != null) { + state.reset(); + } + request.removeHeaders("Proxy-Authorization"); + context.removeAttribute(SYSTEM_PROXY); + } + } + + @Override + public Queue<AuthOption> select( + Map<String, Header> challenges, HttpHost authhost, HttpResponse response, HttpContext context) + throws MalformedChallengeException { + Queue<AuthOption> options = super.select(challenges, authhost, response, context); + if (!options.isEmpty()) { + return options; + } + RouteInfo route = HttpClientContext.adapt(context).getHttpRoute(); + if (route == null || !authhost.equals(route.getProxyHost())) { + return options; + } + CredentialsProvider credentials = credentials(authhost, "http"); + if (credentials == null) { + credentials = credentials(authhost, "https"); + } + if (credentials == null) { + return options; + } + // A child context keeps proxy secrets out of server authentication, including after redirects. + HttpClientContext proxyContext = HttpClientContext.adapt(new BasicHttpContext(context)); + proxyContext.setCredentialsProvider(credentials); + options = super.select(challenges, authhost, response, proxyContext); + if (!options.isEmpty()) { + context.setAttribute(SYSTEM_PROXY, authhost); + } + return options; + } + + private static CredentialsProvider credentials(HttpHost proxy, String protocol) { + String prefix = protocol + ".proxy"; + if (!proxy.getHostName().equalsIgnoreCase(System.getProperty(prefix + "Host"))) { + return null; + } + try { + if (proxy.getPort() != Integer.parseInt(System.getProperty(prefix + "Port"))) { + return null; + } + } catch (NumberFormatException e) { + return null; + } + String username = System.getProperty(prefix + "User"); + if (username == null) { + return null; Review Comment: ⚠️ **Bug: default-port proxies silently fail to authenticate.** `Integer.parseInt(System.getProperty(prefix + "Port"))` — if `http.proxyPort` is not set, `System.getProperty()` returns `null` and `Integer.parseInt(null)` throws `NumberFormatException`, which is caught here and returns `null` (no credentials). This means a proxy on the default HTTP port (80) or HTTPS port (443) — where users commonly omit the port system property — will never match, so authentication silently fails with HTTP 407. The master branch equivalent handles this with a `defaultPort()` helper: ```suggestion try { String configuredPort = System.getProperty(prefix + "Port"); int port = configuredPort == null ? defaultPort(protocol) : Integer.parseInt(configuredPort); if (proxy.getPort() != port) { return null; } } catch (NumberFormatException e) { return null; } ``` Add `private static int defaultPort(String protocol) { return "https".equalsIgnoreCase(protocol) ? 443 : 80; }` at the bottom of the class, and add a corresponding test: `testSystemProxyCredentialsMustMatch("Port", null)` currently tests that null-port returns 407 — this is correct behaviour for an unrecognised port, but there's no test that a proxy on its default port (without a port property) succeeds. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
