cstamas commented on code in PR #2372:
URL: https://github.com/apache/maven-resolver/pull/2372#discussion_r4744509236
##########
maven-resolver-util/src/main/java/org/eclipse/aether/util/connector/transport/http/HttpTransporterUtils.java:
##########
@@ -115,6 +120,121 @@ 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, nor
+ * start with {@code Sec-} (silently dropped by {@code HttpURLConnection},
used by the URL transport). 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)
+ && !name.regionMatches(true, 0, "Sec-", 0, 4);
Review Comment:
Fixed in 0769941a: `isValidTokenHeaderName` now also rejects names starting
with `Proxy-` (case insensitive), next to the `Sec-` rule. Confirmed on JDK 25:
the Java HttpClient silently drops `Proxy-Token` on a direct request, while
`HttpURLConnection` sends it. Tests cover `Proxy-Token`, `proxy-token` and
`PROXY-X` (rejected), and `X-Proxy-Token` and `Proxyish` (still valid).
--
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]