From 2fe3ca6c7e287762f5829c94dbd8e8cbe2245c4f Mon Sep 17 00:00:00 2001 From: Xinyu Zhou Date: Fri, 28 Oct 2022 17:52:44 +0800 Subject: [PATCH 1/2] Allow disable RSS/Atom feed This patch provide a mechanism to disable RSS/Atom feed. Signed-off-by: Xinyu Zhou --- custom/conf/app.example.ini | 2 ++ .../doc/advanced/config-cheat-sheet.en-us.md | 1 + modules/context/repo.go | 6 ++++-- modules/setting/setting.go | 2 ++ routers/web/repo/view.go | 15 +++++++++------ routers/web/web.go | 13 +++++++++++-- templates/base/head.tmpl | 2 +- templates/org/home.tmpl | 4 +++- templates/repo/header.tmpl | 4 +++- templates/user/profile.tmpl | 4 +++- 10 files changed, 39 insertions(+), 14 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index ec0b7c5235d55..d912697fa0cb5 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2196,6 +2196,8 @@ ROUTER = console ;SHOW_FOOTER_VERSION = true ;; Show template execution time in the footer ;SHOW_FOOTER_TEMPLATE_LOAD_TIME = true +;; Enable/Disable RSS/Atom feed +;ENABLE_FEED = true ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index ebc860c45742a..fff8ffaee6dcc 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -1233,3 +1233,4 @@ PROXY_HOSTS = *.github.com - `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer. - `SHOW_FOOTER_VERSION`: **true**: Show Gitea and Go version information in the footer. - `SHOW_FOOTER_TEMPLATE_LOAD_TIME`: **true**: Show time of template execution in the footer. +- `ENABLE_FEED`: **true**: Enable/Disable RSS/Atom feed. diff --git a/modules/context/repo.go b/modules/context/repo.go index 1a0263a3307a1..c363c36994736 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -441,8 +441,10 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { userName := ctx.Params(":username") repoName := ctx.Params(":reponame") repoName = strings.TrimSuffix(repoName, ".git") - repoName = strings.TrimSuffix(repoName, ".rss") - repoName = strings.TrimSuffix(repoName, ".atom") + if setting.EnableFeed { + repoName = strings.TrimSuffix(repoName, ".rss") + repoName = strings.TrimSuffix(repoName, ".atom") + } // Check if the user is the same as the repository owner if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index f93be2fbd166d..b8119d4fe7487 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -439,6 +439,7 @@ var ( ShowFooterBranding bool ShowFooterVersion bool ShowFooterTemplateLoadTime bool + EnableFeed bool // Global setting objects Cfg *ini.File @@ -1094,6 +1095,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) { ShowFooterBranding = Cfg.Section("other").Key("SHOW_FOOTER_BRANDING").MustBool(false) ShowFooterVersion = Cfg.Section("other").Key("SHOW_FOOTER_VERSION").MustBool(true) ShowFooterTemplateLoadTime = Cfg.Section("other").Key("SHOW_FOOTER_TEMPLATE_LOAD_TIME").MustBool(true) + EnableFeed = Cfg.Section("other").Key("ENABLE_FEED").MustBool(true) UI.ShowUserEmail = Cfg.Section("ui").Key("SHOW_USER_EMAIL").MustBool(true) UI.DefaultShowFullName = Cfg.Section("ui").Key("DEFAULT_SHOW_FULL_NAME").MustBool(false) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index d35ec48df048d..8a7c7de41e9b1 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -732,13 +732,16 @@ func checkHomeCodeViewable(ctx *context.Context) { // Home render repository home page func Home(ctx *context.Context) { - isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req) - if isFeed { - feed.ShowRepoFeed(ctx, ctx.Repo.Repository, showFeedType) - return - } + if setting.EnableFeed { + isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req) + if isFeed { + feed.ShowRepoFeed(ctx, ctx.Repo.Repository, showFeedType) + return + } - ctx.Data["FeedURL"] = ctx.Repo.Repository.HTMLURL() + ctx.Data["EnableFeed"] = true + ctx.Data["FeedURL"] = ctx.Repo.Repository.HTMLURL() + } checkHomeCodeViewable(ctx) if ctx.Written() { diff --git a/routers/web/web.go b/routers/web/web.go index 9b814c3f54246..39adc46453816 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -296,6 +296,13 @@ func RegisterRoutes(m *web.Route) { } } + feedEnabled := func(ctx *context.Context) { + if !setting.EnableFeed { + ctx.Error(http.StatusNotFound) + return + } + } + // FIXME: not all routes need go through same middleware. // Especially some AJAX requests, we can reduce middleware number to improve performance. // Routers. @@ -603,9 +610,11 @@ func RegisterRoutes(m *web.Route) { m.Get(".png", func(ctx *context.Context) { ctx.Error(http.StatusNotFound) }) m.Get(".keys", user.ShowSSHKeys) m.Get(".gpg", user.ShowGPGKeys) - m.Get(".rss", feed.ShowUserFeedRSS) - m.Get(".atom", feed.ShowUserFeedAtom) + m.Get(".rss", feedEnabled, feed.ShowUserFeedRSS) + m.Get(".atom", feedEnabled, feed.ShowUserFeedAtom) m.Get("", user.Profile) + }, func(ctx *context.Context) { + ctx.Data["EnableFeed"] = setting.EnableFeed }, context_service.UserAssignmentWeb()) m.Get("/attachments/{uuid}", repo.GetAttachment) }, ignSignIn) diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 70a88ff75569a..e2fc8434aaa68 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -15,7 +15,7 @@ {{end}} -{{if .FeedURL}} +{{if and .EnableFeed .FeedURL}} {{end}} diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index 3ff86259d53f2..dbf60aaf415f9 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -5,7 +5,9 @@
{{.Org.DisplayName}} - {{svg "octicon-rss" 36}} + {{if .EnableFeed}} + {{svg "octicon-rss" 36}} + {{end}} {{if .Org.Visibility.IsLimited}}
{{.locale.Tr "org.settings.visibility.limited_shortname"}}
{{end}} {{if .Org.Visibility.IsPrivate}}
{{.locale.Tr "org.settings.visibility.private_shortname"}}
{{end}} diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index 831784a623058..e6bd839f5704e 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -13,7 +13,9 @@ {{.Owner.Name}}
/
{{.Name}} - {{svg "octicon-rss" 18}} + {{if $.EnableFeed}} + {{svg "octicon-rss" 18}} + {{end}}
{{if .IsTemplate}} {{if .IsPrivate}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 3b776bc16d5e4..70a49235ede78 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -18,7 +18,9 @@
{{if .Owner.FullName}}{{.Owner.FullName}}{{end}} {{.Owner.Name}} - {{svg "octicon-rss" 18}} + {{if .EnableFeed}} + {{svg "octicon-rss" 18}} + {{end}} From 4efdb1d438cc04369aca74d9510de0cab141866a Mon Sep 17 00:00:00 2001 From: Xinyu Zhou Date: Sat, 29 Oct 2022 07:25:02 +0800 Subject: [PATCH 2/2] fix typo Signed-off-by: Xinyu Zhou --- custom/conf/app.example.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index b4b9b8fc60510..b7da993d34e64 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2197,7 +2197,7 @@ ROUTER = console ;; Show template execution time in the footer ;SHOW_FOOTER_TEMPLATE_LOAD_TIME = true ;; Generate sitemap. Defaults to `true`. -; ENABLE_SITEMAP = true +;ENABLE_SITEMAP = true ;; Enable/Disable RSS/Atom feed ;ENABLE_FEED = true