Skip to content

Commit 04cadf9

Browse files
fix(git): Use Apache HttpClient for JGit HTTP transport
JGit defaults to `java.net.HttpURLConnection` for HTTP(S) connections [1]. This implementation caches Basic Auth credentials [2] [3], which causes problems when accessing multiple repositories on the same host with different credentials. For example, when cloning GitHub repositories over HTTPS, `HttpURLConnection` stores credentials under the cache key `s:BASIC:https:github.com:443:GitHub`. Cloning a second repository with different credentials then reuses the cached credentials from the first clone, resulting in authentication failures. Switch JGit's HTTP transport to Apache HttpClient, which does not exhibit this credential caching behavior. [1]: https://github.com/eclipse-jgit/jgit/blob/v7.4.0.202509020913-r/org.eclipse.jgit/src/org/eclipse/jgit/transport/http/JDKHttpConnection.java [2]: https://bugs.openjdk.org/browse/JDK-6626700 [3]: https://github.com/openjdk/jdk/blob/676e6fd8d5152f4e0d14ae59ddd7aa0a7127ea58/src/java.base/share/classes/sun/net/www/protocol/http/AuthenticationInfo.java#L306-L308 Signed-off-by: Marcel Bochtler <[email protected]>
1 parent 111cf53 commit 04cadf9

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version.ref = "jac
118118
jakartaMail = { module = "com.sun.mail:jakarta.mail", version.ref = "jakartaMail" }
119119
jerseyCommon = { module = "org.glassfish.jersey.core:jersey-common", version.ref = "jerseyCommon" }
120120
jgit = { module = "org.eclipse.jgit:org.eclipse.jgit", version.ref = "jgit" }
121+
jgit-http-apache = { module = "org.eclipse.jgit:org.eclipse.jgit.http.apache", version.ref = "jgit" }
121122
jgit-ssh-apache = { module = "org.eclipse.jgit:org.eclipse.jgit.ssh.apache", version.ref = "jgit" }
122123
jgit-ssh-apache-agent = { module = "org.eclipse.jgit:org.eclipse.jgit.ssh.apache.agent", version.ref = "jgit" }
123124
jiraRestClient-api = { module = "com.atlassian.jira:jira-rest-java-client-api", version.ref = "jiraRestClient" }

plugins/version-control-systems/git/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ dependencies {
4040

4141
implementation(libs.jgit)
4242

43+
implementation(libs.jgit.http.apache)
44+
4345
implementation(libs.jgit.ssh.apache) {
4446
exclude(group = "org.apache.sshd", module = "sshd-sftp")
4547
.because("it is not required for cloning via SSH and causes issues with GraalVM native images")

plugins/version-control-systems/git/src/main/kotlin/Git.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ import org.eclipse.jgit.lib.ObjectIdRef
3535
import org.eclipse.jgit.lib.SymbolicRef
3636
import org.eclipse.jgit.transport.CredentialItem
3737
import org.eclipse.jgit.transport.CredentialsProvider
38+
import org.eclipse.jgit.transport.HttpTransport
3839
import org.eclipse.jgit.transport.SshSessionFactory
3940
import org.eclipse.jgit.transport.TagOpt
4041
import org.eclipse.jgit.transport.URIish
42+
import org.eclipse.jgit.transport.http.apache.HttpClientConnectionFactory
4143
import org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory
4244
import org.eclipse.jgit.transport.sshd.JGitKeyCache
4345
import org.eclipse.jgit.transport.sshd.ServerKeyDatabase
@@ -104,6 +106,10 @@ class Git(
104106
) : VersionControlSystem() {
105107
companion object {
106108
init {
109+
// Use Apache HttpClient for HTTP transport in JGit instead of the default java.net.HttpURLConnection based
110+
// transport. This avoids caching credentials too eagerly for basic authentication.
111+
HttpTransport.setConnectionFactory(HttpClientConnectionFactory())
112+
107113
// Make sure that JGit uses the exact same authentication information as ORT itself. This addresses
108114
// discrepancies in the way .netrc files are interpreted between JGit's and ORT's implementation.
109115
CredentialsProvider.setDefault(AuthenticatorCredentialsProvider)

0 commit comments

Comments
 (0)