From f5b8c8edea5d17ba51327684a6e8127ac0f09503 Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 20 Oct 2023 11:40:32 +0200 Subject: [PATCH] [GITEA] Add repo empty check for branch feed - If you attempted to get a branch feed on a empty repository, it would result in a panic as the code expects that the branch exists. - `context.RepoRefByType` would normally already 404 if the branch doesn't exist, however if a repository is empty, it would not do this check. - Fix bug where `/atom/branch/*` would return a RSS feed. (cherry picked from commit d27bcd98a41b69e313535e5e91e4272136a4bab1) (cherry picked from commit c58566403df728c1f71b1dd554a573c011a59d7e) (cherry picked from commit b8b3f6ab8b576a28ed06cc0e501b14950cf78282) (cherry picked from commit 195520100b214d6bf7a2740507f0a7ae10e5a7d1) (cherry picked from commit 6e417087ddf41e79a146366a5db157c7a76af615) (cherry picked from commit ff91e5957ac728118cddb06bddd95d32cb4df815) (cherry picked from commit 6626d5cc75681d3b16b4496a4e0e83a257a3f46a) (cherry picked from commit 62f8ab793b12251e1793bc14ace95cda76121baa) (cherry picked from commit e5bbf1a2d060b4ef1324afd8ed9b38e294b3dffb) --- routers/web/feed/render.go | 13 +++--- routers/web/repo/view.go | 15 +++++-- routers/web/web.go | 4 +- tests/integration/api_feed_user_test.go | 55 ++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 14 deletions(-) diff --git a/routers/web/feed/render.go b/routers/web/feed/render.go index 8931dae8cc..41f9af1c8c 100644 --- a/routers/web/feed/render.go +++ b/routers/web/feed/render.go @@ -8,11 +8,12 @@ import ( ) // RenderBranchFeed render format for branch or file -func RenderBranchFeed(ctx *context.Context) { - _, _, showFeedType := GetFeedType(ctx.Params(":reponame"), ctx.Req) - if ctx.Repo.TreePath == "" { - ShowBranchFeed(ctx, ctx.Repo.Repository, showFeedType) - } else { - ShowFileFeed(ctx, ctx.Repo.Repository, showFeedType) +func RenderBranchFeed(feedType string) func(ctx *context.Context) { + return func(ctx *context.Context) { + if ctx.Repo.TreePath == "" { + ShowBranchFeed(ctx, ctx.Repo.Repository, feedType) + } else { + ShowFileFeed(ctx, ctx.Repo.Repository, feedType) + } } } diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index c6ceca5021..576e17a4f9 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -728,12 +728,19 @@ func Home(ctx *context.Context) { if setting.Other.EnableFeed { isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req) if isFeed { - switch { - case ctx.Link == fmt.Sprintf("%s.%s", ctx.Repo.RepoLink, showFeedType): + if ctx.Link == fmt.Sprintf("%s.%s", ctx.Repo.RepoLink, showFeedType) { feed.ShowRepoFeed(ctx, ctx.Repo.Repository, showFeedType) - case ctx.Repo.TreePath == "": + return + } + + if ctx.Repo.Repository.IsEmpty { + ctx.NotFound("MustBeNotEmpty", nil) + return + } + + if ctx.Repo.TreePath == "" { feed.ShowBranchFeed(ctx, ctx.Repo.Repository, showFeedType) - case ctx.Repo.TreePath != "": + } else { feed.ShowFileFeed(ctx, ctx.Repo.Repository, showFeedType) } return diff --git a/routers/web/web.go b/routers/web/web.go index 63b8aac7dc..6ca4277faf 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1497,8 +1497,8 @@ func registerRoutes(m *web.Route) { m.Get("/cherry-pick/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.CherryPick) }, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader) - m.Get("/rss/branch/*", context.RepoRefByType(context.RepoRefBranch), feedEnabled, feed.RenderBranchFeed) - m.Get("/atom/branch/*", context.RepoRefByType(context.RepoRefBranch), feedEnabled, feed.RenderBranchFeed) + m.Get("/rss/branch/*", repo.MustBeNotEmpty, context.RepoRefByType(context.RepoRefBranch), feedEnabled, feed.RenderBranchFeed("rss")) + m.Get("/atom/branch/*", repo.MustBeNotEmpty, context.RepoRefByType(context.RepoRefBranch), feedEnabled, feed.RenderBranchFeed("atom")) m.Group("/src", func() { m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.Home) diff --git a/tests/integration/api_feed_user_test.go b/tests/integration/api_feed_user_test.go index c44f9a1951..608f7608ae 100644 --- a/tests/integration/api_feed_user_test.go +++ b/tests/integration/api_feed_user_test.go @@ -7,15 +7,19 @@ import ( "net/http" "testing" + "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" ) func TestFeed(t *testing.T) { + defer tests.PrepareTestEnv(t)() + t.Run("User", func(t *testing.T) { t.Run("Atom", func(t *testing.T) { - defer tests.PrepareTestEnv(t)() + defer tests.PrintCurrentTest(t)() req := NewRequest(t, "GET", "/user2.atom") resp := MakeRequest(t, req, http.StatusOK) @@ -25,7 +29,7 @@ func TestFeed(t *testing.T) { }) t.Run("RSS", func(t *testing.T) { - defer tests.PrepareTestEnv(t)() + defer tests.PrintCurrentTest(t)() req := NewRequest(t, "GET", "/user2.rss") resp := MakeRequest(t, req, http.StatusOK) @@ -34,4 +38,51 @@ func TestFeed(t *testing.T) { assert.Contains(t, data, `