Skip to content

Commit f8ef079

Browse files
committed
fix: public github release downloads should not be authenticated
1 parent 72f780a commit f8ef079

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

internal/handlers/git_server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ func getCredentialsForRequest(r *http.Request, credentials *gitCredentialsMap, e
323323
return nil
324324
}
325325

326+
if isPublicGitHubDownload(host, r.URL.Path) {
327+
return nil
328+
}
329+
326330
// Get credentials for the host that not unscoped to specific repositories.
327331
hostCreds := credentials.get(host)
328332
credsForRequest := hostCreds.getCredentialsForRepo(allReposScopeIdentifier)
@@ -343,6 +347,12 @@ func getCredentialsForRequest(r *http.Request, credentials *gitCredentialsMap, e
343347
return credsForRequest
344348
}
345349

350+
// GitHub release download URLs are public
351+
// and do not require authentication
352+
func isPublicGitHubDownload(host string, path string) bool {
353+
return host == "github.com" && strings.Contains(path, "/releases/download/")
354+
}
355+
346356
// HandleResponse handles retrying failed auth responses with alternate credentials
347357
// when there are multiple tokens configured for the git server.
348358
//

internal/handlers/git_server_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,31 @@ func TestGitServerHandler(t *testing.T) {
124124
"valid github request")
125125
}
126126

127+
func TestGitServerPublicReleaseDownload(t *testing.T) {
128+
installationCred := testGitSourceCred("github.com", "x-access-token", "v1.token")
129+
gheCred := testGitSourceCred("ghe.some-corp.com", "x-access-token", "corp")
130+
mavenCred := config.Credential{
131+
"type": "maven_repository",
132+
"host": "myHost.com",
133+
}
134+
135+
credentials := config.Credentials{
136+
installationCred,
137+
gheCred,
138+
mavenCred,
139+
}
140+
handler := NewGitServerHandler(credentials, nil)
141+
142+
req := httptest.NewRequest("HEAD", "https://github.com/gradle/gradle-distributions/releases/download/v9.3.0/gradle-9.3.0-bin.zip", nil)
143+
req, _ = handler.HandleRequest(req, nil)
144+
assertUnauthenticated(t, req, "Public release download URL should not be authenticated")
145+
146+
req = httptest.NewRequest("HEAD", "https://myHost.com/releases/download/v9.3.0/gradle-9.3.0-bin.zip", nil)
147+
req, _ = handler.HandleRequest(req, nil)
148+
assertUnauthenticated(t, req, "Public release download URL should not be authenticated by the git handler")
149+
150+
}
151+
127152
func TestGitServerHandler_AuthenticatedAccessToGitHubRepos(t *testing.T) {
128153
installationToken1 := "v1.token1"
129154
privateRepo1Cred := testGitSourceCred("github.com", "x-access-token", installationToken1, withAccessibleRepos([]string{"github/private-repo-1"}))

0 commit comments

Comments
 (0)