Skip to content

Commit 8d1be2a

Browse files
authored
Fix git client accessing renamed repo (#34034) (#34043)
Backport #34034
1 parent e46f9ff commit 8d1be2a

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

routers/web/githttp.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,12 @@
44
package web
55

66
import (
7-
"net/http"
8-
9-
"code.gitea.io/gitea/modules/setting"
107
"code.gitea.io/gitea/modules/web"
118
"code.gitea.io/gitea/routers/web/repo"
129
"code.gitea.io/gitea/services/context"
1310
)
1411

1512
func addOwnerRepoGitHTTPRouters(m *web.Router) {
16-
reqGitSignIn := func(ctx *context.Context) {
17-
if !setting.Service.RequireSignInView {
18-
return
19-
}
20-
// rely on the results of Contexter
21-
if !ctx.IsSigned {
22-
// TODO: support digit auth - which would be Authorization header with digit
23-
ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="Gitea"`)
24-
ctx.Error(http.StatusUnauthorized)
25-
}
26-
}
2713
m.Group("/{username}/{reponame}", func() {
2814
m.Methods("POST,OPTIONS", "/git-upload-pack", repo.ServiceUploadPack)
2915
m.Methods("POST,OPTIONS", "/git-receive-pack", repo.ServiceReceivePack)
@@ -36,5 +22,5 @@ func addOwnerRepoGitHTTPRouters(m *web.Router) {
3622
m.Methods("GET,OPTIONS", "/objects/{head:[0-9a-f]{2}}/{hash:[0-9a-f]{38,62}}", repo.GetLooseObject)
3723
m.Methods("GET,OPTIONS", "/objects/pack/pack-{file:[0-9a-f]{40,64}}.pack", repo.GetPackFile)
3824
m.Methods("GET,OPTIONS", "/objects/pack/pack-{file:[0-9a-f]{40,64}}.idx", repo.GetIdxFile)
39-
}, optSignInIgnoreCsrf, reqGitSignIn, repo.HTTPGitEnabledHandler, repo.CorsHandler(), context.UserAssignmentWeb())
25+
}, optSignInIgnoreCsrf, repo.HTTPGitEnabledHandler, repo.CorsHandler(), context.UserAssignmentWeb())
4026
}

tests/integration/git_smart_http_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ import (
99
"net/url"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/test"
14+
1215
"github.com/stretchr/testify/assert"
1316
)
1417

1518
func TestGitSmartHTTP(t *testing.T) {
16-
onGiteaRun(t, testGitSmartHTTP)
19+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
20+
testGitSmartHTTP(t, u)
21+
testRenamedRepoRedirect(t)
22+
})
1723
}
1824

1925
func testGitSmartHTTP(t *testing.T, u *url.URL) {
@@ -66,3 +72,21 @@ func testGitSmartHTTP(t *testing.T, u *url.URL) {
6672
})
6773
}
6874
}
75+
76+
func testRenamedRepoRedirect(t *testing.T) {
77+
defer test.MockVariableValue(&setting.Service.RequireSignInView, true)()
78+
79+
// git client requires to get a 301 redirect response before 401 unauthorized response
80+
req := NewRequest(t, "GET", "/user2/oldrepo1/info/refs")
81+
resp := MakeRequest(t, req, http.StatusMovedPermanently)
82+
redirect := resp.Header().Get("Location")
83+
assert.Equal(t, "/user2/repo1/info/refs", redirect)
84+
85+
req = NewRequest(t, "GET", redirect)
86+
resp = MakeRequest(t, req, http.StatusUnauthorized)
87+
assert.Equal(t, "Unauthorized\n", resp.Body.String())
88+
89+
req = NewRequest(t, "GET", redirect).AddBasicAuth("user2")
90+
resp = MakeRequest(t, req, http.StatusOK)
91+
assert.Contains(t, resp.Body.String(), "65f1bf27bc3bf70f64657658635e66094edbcb4d\trefs/tags/v1.1")
92+
}

0 commit comments

Comments
 (0)