Skip to content

Commit f24d73a

Browse files
wxiaoguangdelvh
andauthored
Fix "redirect link" handling (#33440)
`a%2fb` should not redirect to `a/b` --------- Co-authored-by: delvh <[email protected]>
1 parent f88dbf8 commit f24d73a

File tree

7 files changed

+65
-45
lines changed

7 files changed

+65
-45
lines changed

routers/web/repo/branch.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Branches(ctx *context.Context) {
9393

9494
// DeleteBranchPost responses for delete merged branch
9595
func DeleteBranchPost(ctx *context.Context) {
96-
defer redirect(ctx)
96+
defer jsonRedirectBranches(ctx)
9797
branchName := ctx.FormString("name")
9898

9999
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName, nil); err != nil {
@@ -120,7 +120,7 @@ func DeleteBranchPost(ctx *context.Context) {
120120

121121
// RestoreBranchPost responses for delete merged branch
122122
func RestoreBranchPost(ctx *context.Context) {
123-
defer redirect(ctx)
123+
defer jsonRedirectBranches(ctx)
124124

125125
branchID := ctx.FormInt64("branch_id")
126126
branchName := ctx.FormString("name")
@@ -170,7 +170,7 @@ func RestoreBranchPost(ctx *context.Context) {
170170
ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
171171
}
172172

173-
func redirect(ctx *context.Context) {
173+
func jsonRedirectBranches(ctx *context.Context) {
174174
ctx.JSONRedirect(ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")))
175175
}
176176

routers/web/repo/view_home.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,19 @@ func Home(ctx *context.Context) {
413413
ctx.HTML(http.StatusOK, tplRepoHome)
414414
}
415415

416-
// HomeRedirect redirects from /tree/* to /src/* in order to maintain a similar URL structure.
417-
func HomeRedirect(ctx *context.Context) {
418-
remainder := ctx.PathParam("*")
419-
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + util.PathEscapeSegments(remainder))
416+
func RedirectRepoTreeToSrc(ctx *context.Context) {
417+
// Redirect "/owner/repo/tree/*" requests to "/owner/repo/src/*",
418+
// then use the deprecated "/src/*" handler to guess the ref type and render a file list page.
419+
// This is done intentionally so that Gitea's repo URL structure matches other forges (GitHub/GitLab) provide,
420+
// allowing us to construct submodule URLs across forges easily.
421+
// For example, when viewing a submodule, we can simply construct the link as:
422+
// * "https://gitea/owner/repo/tree/{CommitID}"
423+
// * "https://github/owner/repo/tree/{CommitID}"
424+
// * "https://gitlab/owner/repo/tree/{CommitID}"
425+
// Then no matter which forge the submodule is using, the link works.
426+
redirect := ctx.Repo.RepoLink + "/src/" + ctx.PathParamRaw("*")
427+
if ctx.Req.URL.RawQuery != "" {
428+
redirect += "?" + ctx.Req.URL.RawQuery
429+
}
430+
ctx.Redirect(redirect)
420431
}

routers/web/web.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -1583,13 +1583,7 @@ func registerRoutes(m *web.Router) {
15831583
m.Get("/commit/*", context.RepoRefByType(git.RefTypeCommit), repo.Home)
15841584
m.Get("/*", context.RepoRefByType(""), repo.Home) // "/*" route is deprecated, and kept for backward compatibility
15851585
}, repo.SetEditorconfigIfExists)
1586-
1587-
// Add a /tree/* path to redirect to the /src/* path, which
1588-
// will redirect to the canonical URL for that ref. This is
1589-
// included so that Gitea's repo URL structure matches what
1590-
// other forges provide, allowing clients to construct URLs
1591-
// that work across forges.
1592-
m.Get("/tree/*", repo.HomeRedirect)
1586+
m.Get("/tree/*", repo.RedirectRepoTreeToSrc) // redirect "/owner/repo/tree/*" requests to "/owner/repo/src/*"
15931587

15941588
m.Get("/forks", context.RepoRef(), repo.Forks)
15951589
m.Get("/commit/{sha:([a-f0-9]{7,64})}.{ext:patch|diff}", repo.MustBeNotEmpty, repo.RawDiff)

services/context/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
293293
return
294294
}
295295

296-
refName, _ := getRefNameLegacy(ctx.Base, ctx.Repo, ctx.PathParam("*"), ctx.FormTrim("ref"))
296+
refName, _, _ := getRefNameLegacy(ctx.Base, ctx.Repo, ctx.PathParam("*"), ctx.FormTrim("ref"))
297297
var err error
298298

299299
if ctx.Repo.GitRepo.IsBranchExist(refName) {

services/context/repo.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -686,24 +686,24 @@ func getRefNameFromPath(repo *Repository, path string, isExist func(string) bool
686686
return ""
687687
}
688688

689-
func getRefNameLegacy(ctx *Base, repo *Repository, reqPath, extraRef string) (string, git.RefType) {
689+
func getRefNameLegacy(ctx *Base, repo *Repository, reqPath, extraRef string) (refName string, refType git.RefType, fallbackDefaultBranch bool) {
690690
reqRefPath := path.Join(extraRef, reqPath)
691691
reqRefPathParts := strings.Split(reqRefPath, "/")
692692
if refName := getRefName(ctx, repo, reqRefPath, git.RefTypeBranch); refName != "" {
693-
return refName, git.RefTypeBranch
693+
return refName, git.RefTypeBranch, false
694694
}
695695
if refName := getRefName(ctx, repo, reqRefPath, git.RefTypeTag); refName != "" {
696-
return refName, git.RefTypeTag
696+
return refName, git.RefTypeTag, false
697697
}
698698
if git.IsStringLikelyCommitID(git.ObjectFormatFromName(repo.Repository.ObjectFormatName), reqRefPathParts[0]) {
699699
// FIXME: this logic is different from other types. Ideally, it should also try to GetCommit to check if it exists
700700
repo.TreePath = strings.Join(reqRefPathParts[1:], "/")
701-
return reqRefPathParts[0], git.RefTypeCommit
701+
return reqRefPathParts[0], git.RefTypeCommit, false
702702
}
703703
// FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
704704
// "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
705705
repo.TreePath = reqPath
706-
return repo.Repository.DefaultBranch, git.RefTypeBranch
706+
return repo.Repository.DefaultBranch, git.RefTypeBranch, true
707707
}
708708

709709
func getRefName(ctx *Base, repo *Repository, path string, refType git.RefType) string {
@@ -838,8 +838,9 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
838838
}
839839
} else { // there is a path in request
840840
guessLegacyPath := refType == ""
841+
fallbackDefaultBranch := false
841842
if guessLegacyPath {
842-
refShortName, refType = getRefNameLegacy(ctx.Base, ctx.Repo, reqPath, "")
843+
refShortName, refType, fallbackDefaultBranch = getRefNameLegacy(ctx.Base, ctx.Repo, reqPath, "")
843844
} else {
844845
refShortName = getRefName(ctx.Base, ctx.Repo, reqPath, refType)
845846
}
@@ -897,12 +898,24 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) {
897898

898899
if guessLegacyPath {
899900
// redirect from old URL scheme to new URL scheme
900-
prefix := strings.TrimPrefix(setting.AppSubURL+strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path, ctx.PathParam("*"))), strings.ToLower(ctx.Repo.RepoLink))
901-
redirect := path.Join(
902-
ctx.Repo.RepoLink,
903-
util.PathEscapeSegments(prefix),
904-
ctx.Repo.RefTypeNameSubURL(),
905-
util.PathEscapeSegments(ctx.Repo.TreePath))
901+
// * /user2/repo1/commits/master => /user2/repo1/commits/branch/master
902+
// * /user2/repo1/src/master => /user2/repo1/src/branch/master
903+
// * /user2/repo1/src/README.md => /user2/repo1/src/branch/master/README.md (fallback to default branch)
904+
var redirect string
905+
refSubPath := "src"
906+
// remove the "/subpath/owner/repo/" prefix, the names are case-insensitive
907+
remainingLowerPath, cut := strings.CutPrefix(setting.AppSubURL+strings.ToLower(ctx.Req.URL.Path), strings.ToLower(ctx.Repo.RepoLink)+"/")
908+
if cut {
909+
refSubPath, _, _ = strings.Cut(remainingLowerPath, "/") // it could be "src" or "commits"
910+
}
911+
if fallbackDefaultBranch {
912+
redirect = fmt.Sprintf("%s/%s/%s/%s/%s", ctx.Repo.RepoLink, refSubPath, refType, util.PathEscapeSegments(refShortName), ctx.PathParamRaw("*"))
913+
} else {
914+
redirect = fmt.Sprintf("%s/%s/%s/%s", ctx.Repo.RepoLink, refSubPath, refType, ctx.PathParamRaw("*"))
915+
}
916+
if ctx.Req.URL.RawQuery != "" {
917+
redirect += "?" + ctx.Req.URL.RawQuery
918+
}
906919
ctx.Redirect(redirect)
907920
return
908921
}

tests/integration/links_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ func TestRedirectsNoLogin(t *testing.T) {
5252
redirects := []struct{ from, to string }{
5353
{"/user2/repo1/commits/master", "/user2/repo1/commits/branch/master"},
5454
{"/user2/repo1/src/master", "/user2/repo1/src/branch/master"},
55-
{"/user2/repo1/src/master/file.txt", "/user2/repo1/src/branch/master/file.txt"},
56-
{"/user2/repo1/src/master/directory/file.txt", "/user2/repo1/src/branch/master/directory/file.txt"},
57-
{"/user/avatar/Ghost/-1", "/assets/img/avatar_default.png"},
55+
{"/user2/repo1/src/master/a%2fb.txt", "/user2/repo1/src/branch/master/a%2fb.txt"},
56+
{"/user2/repo1/src/master/directory/file.txt?a=1", "/user2/repo1/src/branch/master/directory/file.txt?a=1"},
57+
{"/user2/repo1/tree/a%2fb?a=1", "/user2/repo1/src/a%2fb?a=1"},
58+
{"/user/avatar/GhosT/-1", "/assets/img/avatar_default.png"},
59+
{"/user/avatar/Gitea-ActionS/0", "/assets/img/avatar_default.png"},
5860
{"/api/v1/swagger", "/api/swagger"},
5961
}
6062
for _, c := range redirects {

tests/integration/nonascii_branches_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ func TestNonAsciiBranches(t *testing.T) {
4646
{
4747
from: "master/badfile",
4848
to: "branch/master/badfile",
49-
status: http.StatusNotFound, // it does not exists
49+
status: http.StatusNotFound, // it does not exist
5050
},
5151
{
5252
from: "ГлавнаяВетка",
53-
to: "branch/%D0%93%D0%BB%D0%B0%D0%B2%D0%BD%D0%B0%D1%8F%D0%92%D0%B5%D1%82%D0%BA%D0%B0",
53+
to: "branch/%d0%93%d0%bb%d0%b0%d0%b2%d0%bd%d0%b0%d1%8f%d0%92%d0%b5%d1%82%d0%ba%d0%b0",
5454
status: http.StatusOK,
5555
},
5656
{
5757
from: "а/б/в",
58-
to: "branch/%D0%B0/%D0%B1/%D0%B2",
58+
to: "branch/%d0%b0/%d0%b1/%d0%b2",
5959
status: http.StatusOK,
6060
},
6161
{
6262
from: "Grüßen/README.md",
63-
to: "branch/Gr%C3%BC%C3%9Fen/README.md",
63+
to: "branch/Gr%c3%bc%c3%9fen/README.md",
6464
status: http.StatusOK,
6565
},
6666
{
@@ -70,7 +70,7 @@ func TestNonAsciiBranches(t *testing.T) {
7070
},
7171
{
7272
from: "Plus+Is+Not+Space/Файл.md",
73-
to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
73+
to: "branch/Plus+Is+Not+Space/%d0%a4%d0%b0%d0%b9%d0%bb.md",
7474
status: http.StatusOK,
7575
},
7676
{
@@ -80,29 +80,29 @@ func TestNonAsciiBranches(t *testing.T) {
8080
},
8181
{
8282
from: "ブランチ",
83-
to: "branch/%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
83+
to: "branch/%e3%83%96%e3%83%a9%e3%83%b3%e3%83%81",
8484
status: http.StatusOK,
8585
},
8686

8787
// Tags
8888
{
8989
from: "Тэг",
90-
to: "tag/%D0%A2%D1%8D%D0%B3",
90+
to: "tag/%d0%a2%d1%8d%d0%b3",
9191
status: http.StatusOK,
9292
},
9393
{
9494
from: "Ё/人",
95-
to: "tag/%D0%81/%E4%BA%BA",
95+
to: "tag/%d0%81/%e4%ba%ba",
9696
status: http.StatusOK,
9797
},
9898
{
9999
from: "タグ",
100-
to: "tag/%E3%82%BF%E3%82%B0",
100+
to: "tag/%e3%82%bf%e3%82%b0",
101101
status: http.StatusOK,
102102
},
103103
{
104104
from: "タグ/ファイル.md",
105-
to: "tag/%E3%82%BF%E3%82%B0/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
105+
to: "tag/%e3%82%bf%e3%82%b0/%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab.md",
106106
status: http.StatusOK,
107107
},
108108

@@ -114,12 +114,12 @@ func TestNonAsciiBranches(t *testing.T) {
114114
},
115115
{
116116
from: "Файл.md",
117-
to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
117+
to: "branch/Plus+Is+Not+Space/%d0%a4%d0%b0%d0%b9%d0%bb.md",
118118
status: http.StatusOK,
119119
},
120120
{
121121
from: "ファイル.md",
122-
to: "branch/Plus+Is+Not+Space/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
122+
to: "branch/Plus+Is+Not+Space/%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab.md",
123123
status: http.StatusNotFound, // it's not on default branch
124124
},
125125

@@ -131,7 +131,7 @@ func TestNonAsciiBranches(t *testing.T) {
131131
},
132132
{
133133
from: "%E3%82%BF%E3%82%b0",
134-
to: "tag/%E3%82%BF%E3%82%B0",
134+
to: "tag/%E3%82%BF%E3%82%b0",
135135
status: http.StatusOK,
136136
},
137137
{
@@ -141,12 +141,12 @@ func TestNonAsciiBranches(t *testing.T) {
141141
},
142142
{
143143
from: "%D0%81%2F%E4%BA%BA",
144-
to: "tag/%D0%81/%E4%BA%BA",
144+
to: "tag/%D0%81%2F%E4%BA%BA",
145145
status: http.StatusOK,
146146
},
147147
{
148148
from: "Ё%2F%E4%BA%BA",
149-
to: "tag/%D0%81/%E4%BA%BA",
149+
to: "tag/%d0%81%2F%E4%BA%BA",
150150
status: http.StatusOK,
151151
},
152152
{

0 commit comments

Comments
 (0)