cstamas commented on code in PR #2372:
URL: https://github.com/apache/maven-resolver/pull/2372#discussion_r4744201235
##########
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:
Fixed in b427645f: when the JDK transporter follows redirects itself,
`redirectRequest()` now always drops `Authorization` on cross-origin hops,
regardless of `HTTP_ORIGIN_SCOPED_HEADERS`. This also covers the
insecure-redirect case. Covered by the shared
`testGet_Redirect_PreemptiveAuthenticationWithTokenNotSent` (token + preemptive
Basic, scoping on/off) and
`JdkTransporterRedirectTest#crossOriginHopAlwaysDropsAuthorization`.
##########
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:
Fixed in b427645f: redirect hops now take their headers from the original
request (method and body still come from the previous hop), with the scoped
headers and `Authorization` dropped only on cross-origin hops. A hop returning
to the repository origin gets the token back, and configured headers too.
Covered by the shared `testGet_Redirect_TokenRestoredOnReturnToOrigin`
(repository → 127.0.0.1 → repository, via a new unauthenticated `/bounce/`
redirect in `HttpServer`) and
`JdkTransporterRedirectTest#hopReturningToOriginRestoresScopedHeaders`. The
same round trip also failed on Jetty, and is fixed there too (see the Jetty
thread).
##########
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:
Fixed in b427645f: `OriginScopedHeadersListener` now reapplies missing token
headers (and scoped configured headers) on requests to the repository origin.
It only adds headers that are absent, so values set per request are kept. This
restores the `Authorization` token that Jetty's `HttpRequest.copy()` drops on
same-origin hops (confirmed in jetty-client 12.1.15), and the headers stripped
on the way out of a round trip. Covered by the shared
`testGet_Redirect_SameOriginKeepsToken` and
`testGet_Redirect_TokenRestoredOnReturnToOrigin`, and
`OriginScopedHeadersListenerTest#reappliesOriginHeadersForOrigin`.
##########
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:
Fixed in b427645f: `isValidTokenHeaderName` now rejects names starting with
`Sec-` (case insensitive). `X-Sec-Token` stays valid. Confirmed on JDK 25:
`HttpURLConnection` silently drops `Sec-Token` and `sec-fetch-mode`. The Maven
settings validator copy (apache/maven#14173) is updated to match.
##########
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:
Fixed in b427645f: `isValidTokenHeaderName` now rejects names starting with
`Sec-` (case insensitive). `X-Sec-Token` stays valid. Confirmed on JDK 25:
`HttpURLConnection` silently drops `Sec-Token` and `sec-fetch-mode`. The Maven
settings validator copy (apache/maven#14173) will be updated to match.
--
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]