Copilot commented on code in PR #2372:
URL: https://github.com/apache/maven-resolver/pull/2372#discussion_r4744401085
##########
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:
Names such as `Proxy-Token` pass validation, but Java HttpClient removes
them on direct requests and requests through HTTPS tunnels, so a repository
requiring that token receives no credential with the JDK transport. OpenJDK's
default-branch [prefix
filter](https://github.com/openjdk/jdk/blob/8fcb1c5b69c6144a3447591dfda10a4cecd134bc/src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java#L311-L316)
and [request
filtering](https://github.com/openjdk/jdk/blob/8fcb1c5b69c6144a3447591dfda10a4cecd134bc/src/java.net.http/share/classes/jdk/internal/net/http/HttpConnection.java#L411-L416)
confirm this behavior. Reject the `Proxy-` prefix case-insensitively, as with
`Sec-`, and add validation tests for mixed-case names.
--
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]