Skip to content

Commit 894821d

Browse files
wxiaoguangGiteaBot
andauthored
Fix git client accessing renamed repo (#34034)
Fix #28460 The `reqGitSignIn` is just copied-pasted code (from githtttp.go) and causes the regression bug. Co-authored-by: Giteabot <[email protected]>
1 parent c1b9ecc commit 894821d

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

routers/web/githttp.go

+1-15
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.HTTPError(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

+24-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ import (
99
"net/url"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/test"
1214
"code.gitea.io/gitea/modules/util"
1315

1416
"github.com/stretchr/testify/assert"
1517
"github.com/stretchr/testify/require"
1618
)
1719

1820
func TestGitSmartHTTP(t *testing.T) {
19-
onGiteaRun(t, testGitSmartHTTP)
21+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
22+
testGitSmartHTTP(t, u)
23+
testRenamedRepoRedirect(t)
24+
})
2025
}
2126

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

0 commit comments

Comments
 (0)