Copilot commented on code in PR #2372:
URL: https://github.com/apache/maven-resolver/pull/2372#discussion_r4743873662


##########
maven-resolver-transport-jdk-parent/maven-resolver-transport-jdk11/src/main/java/org/eclipse/aether/transport/jdk/JdkTransporter.java:
##########
@@ -793,10 +791,11 @@ private HttpClient createClient(RepositorySystemSession 
session, RemoteRepositor
         try (AuthenticationContext repoAuthContext = 
AuthenticationContext.forRepository(session, repository)) {
             if (repoAuthContext != null) {
                 sslContext = 
repoAuthContext.get(AuthenticationContext.SSL_CONTEXT, SSLContext.class);
-
+                Set<String> tokenHeaders = 
HttpTransporterUtils.putTokenHeaders(repoAuthContext, headers);
+                crossOriginExcludedHeaders.addAll(tokenHeaders);
                 String username = 
repoAuthContext.get(AuthenticationContext.USERNAME);
-                // authentication may carry no credentials at all (e.g. only 
SSL context)
-                if (username != null) {
+                // Authorization token wins over username and password; 
username may be absent (e.g. only SSL)
+                if (username != null && 
!tokenHeaders.contains("Authorization")) {

Review Comment:
   With `HTTP_ORIGIN_SCOPED_HEADERS=false`, a custom token such as 
`Private-Token` forces manual redirects, but `Authorization` is absent from the 
drop set. If preemptive Basic authentication is also enabled, 
`redirectRequest()` copies those credentials to a cross-origin target, 
bypassing the scoped authenticator. Add `Authorization` to the drop set 
whenever server credentials are configured, independently of configured-header 
scoping, and test this combined authentication/redirect case.



##########
maven-resolver-transport-jdk-parent/maven-resolver-transport-jdk11/src/main/java/org/eclipse/aether/transport/jdk/JdkTransporter.java:
##########
@@ -615,12 +617,7 @@ private <T> HttpResponse<T> send(HttpRequest request, 
HttpResponse.BodyHandler<T
                     throw new IOException("Too many redirects for " + 
request.uri() + " (max " + MAX_REDIRECTS
                             + "), last redirect target: " + target);
                 }
-                current = redirectRequest(
-                        current,
-                        response.statusCode(),
-                        target,
-                        baseUri,
-                        originScopedHeaders ? crossOriginExcludedHeaders : 
Set.of());
+                current = redirectRequest(current, response.statusCode(), 
target, baseUri, crossOriginExcludedHeaders);

Review Comment:
   For a redirect chain repository → another origin → repository, the first hop 
removes the token headers, and this call builds the return request from that 
already-stripped request. `redirectRequest()` only copies existing headers, so 
the repository receives no token and token-only authentication fails with 401. 
This also occurs with configured-header scoping disabled. Retain the token 
values separately and reapply them when a redirect targets the repository 
origin; add a round-trip redirect regression test.



##########
maven-resolver-transport-jetty/src/main/java/org/eclipse/aether/transport/jetty/JettyTransporter.java:
##########
@@ -604,10 +606,14 @@ private HttpClient createClient() throws RuntimeException 
{
         SSLContext sslContext = null;
         BasicAuthentication basicAuthentication = null;
         Charset credentialsEncoding = 
HttpTransporterUtils.getHttpCredentialsEncoding(session, repository);
+        Set<String> tokenHeaders = Collections.emptySet();
         try (AuthenticationContext repoAuthContext = 
AuthenticationContext.forRepository(session, repository)) {
             if (repoAuthContext != null) {
                 sslContext = 
repoAuthContext.get(AuthenticationContext.SSL_CONTEXT, SSLContext.class);
-
+                tokenHeaders = 
HttpTransporterUtils.putTokenHeaders(repoAuthContext, headers);

Review Comment:
   Bearer authentication fails after a same-origin redirect because Jetty's 
[request-copy 
implementation](https://github.com/jetty/jetty.project/blob/f684e3f5618d73b6931b6a603761f35cc2a64fcd/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpRequest.java#L152-L158)
 removes `Authorization` (default-branch source). The token has no 
authentication-store entry here, and the origin-scoping listener only removes 
headers, so the redirected request has no token and receives 401. Retain the 
token values and reapply them on requests targeting the repository origin while 
preserving cross-origin stripping; add a same-origin bearer redirect test.



##########
maven-resolver-util/src/main/java/org/eclipse/aether/util/connector/transport/http/HttpTransporterUtils.java:
##########
@@ -115,6 +120,117 @@ public static Map<String, String> 
getHttpHeaders(RepositorySystemSession session
                 ConfigurationProperties.HTTP_HEADERS);
     }
 
+    /**
+     * RFC 9110 field name ("token" characters).
+     */
+    private static final Pattern TOKEN_HEADER_NAME = 
Pattern.compile("[!#$%&'*+.^_`|~0-9A-Za-z-]+");
+
+    /**
+     * Headers managed by the HTTP clients or connection level, refused as 
token header. Besides the headers refused by
+     * the Java HttpClient (JDK transport), this includes the ones {@code 
HttpURLConnection} (URL transport) silently
+     * drops, so a token header behaves the same with every transport.
+     */
+    private static final Set<String> RESTRICTED_TOKEN_HEADERS = new 
TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+
+    static {
+        RESTRICTED_TOKEN_HEADERS.addAll(Arrays.asList(
+                "Access-Control-Request-Headers",
+                "Access-Control-Request-Method",
+                "Connection",
+                "Content-Length",
+                "Content-Transfer-Encoding",
+                "Expect",
+                "Host",
+                "Keep-Alive",
+                "Origin",
+                "Proxy-Authorization",
+                "TE",
+                "Trailer",
+                "Transfer-Encoding",
+                "Upgrade",
+                "Via"));
+    }
+
+    /**
+     * Tells whether given name is allowed as HTTP token header name ({@link 
AuthenticationContext#HTTP_TOKEN_HEADERS}):
+     * it must be a valid RFC 9110 field name, and not a header managed by the 
HTTP clients or the connection. Usable
+     * for early validation, for example of user settings; {@link 
#putTokenHeaders(AuthenticationContext, Map)} applies
+     * the same rule.
+     *
+     * @param name the header name, may be {@code null}
+     * @return {@code true} if the name is allowed
+     * @since 2.0.27
+     */
+    public static boolean isValidTokenHeaderName(String name) {
+        return name != null && TOKEN_HEADER_NAME.matcher(name).matches() && 
!RESTRICTED_TOKEN_HEADERS.contains(name);

Review Comment:
   This accepts names such as `Sec-Token`, but the URL transport's 
`HttpURLConnection` rejects every header beginning with `Sec-`, 
case-insensitively, by default. Such an authentication passes validation yet 
cannot authenticate with that transport. OpenJDK's default-branch 
implementation shows the prefix restriction here: 
https://github.com/openjdk/jdk/blob/d1b30a7eb96ddf5b8828797af9123f384163eba4/src/java.base/share/classes/sun/net/www/protocol/http/HttpURLConnection.java#L484-L490.
 Reject that prefix here as well so early validation and transport behavior 
stay consistent.



-- 
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]

Reply via email to