]> source.dussan.org Git - gitea.git/commitdiff
Add disable download source configuration (#20548) (#20579)
authorLunny Xiao <xiaolunwen@gmail.com>
Fri, 12 Aug 2022 15:53:10 +0000 (23:53 +0800)
committerGitHub <noreply@github.com>
Fri, 12 Aug 2022 15:53:10 +0000 (23:53 +0800)
* Add disable download source configuration (#20548)

Add configuration to enable/disable download source from UI.

Co-authored-by: zeripath <art27@cantab.net>
* Fix BaseVars not used in renderering

* Fix disabled open in vscode menu when disabling download source from UI

Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
custom/conf/app.example.ini
docs/content/doc/advanced/config-cheat-sheet.en-us.md
modules/context/context.go
modules/setting/repository.go
modules/templates/base.go
routers/web/web.go
templates/mail/release.tmpl
templates/repo/branch/list.tmpl
templates/repo/home.tmpl
templates/repo/release/list.tmpl

index ecd7febebad2141b9506ae055204bf55960875ff..e4d0d4d1f5305635987351a7c287ef6a3ec7db2b 100644 (file)
@@ -892,6 +892,9 @@ ROUTER = console
 ;; Allow deletion of unadopted repositories
 ;ALLOW_DELETION_OF_UNADOPTED_REPOSITORIES = false
 
+;; Don't allow download source archive files from UI
+;DISABLE_DOWNLOAD_SOURCE_ARCHIVES = false
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;[repository.editor]
index ca756c2e6af31211c5c8a50ec68ba07769b2bd55..f59465ce29338959ff2b18ec7cabd881b529fde6 100644 (file)
@@ -78,6 +78,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
 - `DEFAULT_BRANCH`: **main**: Default branch name of all repositories.
 - `ALLOW_ADOPTION_OF_UNADOPTED_REPOSITORIES`: **false**: Allow non-admin users to adopt unadopted repositories
 - `ALLOW_DELETION_OF_UNADOPTED_REPOSITORIES`: **false**: Allow non-admin users to delete unadopted repositories
+- `DISABLE_DOWNLOAD_SOURCE_ARCHIVES`: **false**: Don't allow download source archive files from UI
 
 ### Repository - Editor (`repository.editor`)
 
index a199721197a87780a1522555ad9b74512fda83c9..d34dbb5e648346b0990998c59b84a828ef81a9cf 100644 (file)
@@ -224,7 +224,7 @@ func (ctx *Context) HTML(status int, name base.TplName) {
        ctx.Data["TemplateLoadTimes"] = func() string {
                return strconv.FormatInt(time.Since(tmplStartTime).Nanoseconds()/1e6, 10) + "ms"
        }
-       if err := ctx.Render.HTML(ctx.Resp, status, string(name), ctx.Data); err != nil {
+       if err := ctx.Render.HTML(ctx.Resp, status, string(name), templates.BaseVars().Merge(ctx.Data)); err != nil {
                if status == http.StatusInternalServerError && name == base.TplName("status/500") {
                        ctx.PlainText(http.StatusInternalServerError, "Unable to find status/500 template")
                        return
index 733bc6d90e6003e09f97fca6199720896148dfc0..d0406dbf9028431d132018f7bbf137014264c7ee 100644 (file)
@@ -48,6 +48,7 @@ var (
                DefaultBranch                           string
                AllowAdoptionOfUnadoptedRepositories    bool
                AllowDeleteOfUnadoptedRepositories      bool
+               DisableDownloadSourceArchives           bool
 
                // Repository editor settings
                Editor struct {
index 282019f826c1dd1c9f6f40917b78324bae041c48..9563650e127b7ec47cf57aad4c0de82139b91dbe 100644 (file)
@@ -35,10 +35,11 @@ func BaseVars() Vars {
                "IsLandingPageExplore":       setting.LandingPageURL == setting.LandingPageExplore,
                "IsLandingPageOrganizations": setting.LandingPageURL == setting.LandingPageOrganizations,
 
-               "ShowRegistrationButton":      setting.Service.ShowRegistrationButton,
-               "ShowMilestonesDashboardPage": setting.Service.ShowMilestonesDashboardPage,
-               "ShowFooterBranding":          setting.ShowFooterBranding,
-               "ShowFooterVersion":           setting.ShowFooterVersion,
+               "ShowRegistrationButton":        setting.Service.ShowRegistrationButton,
+               "ShowMilestonesDashboardPage":   setting.Service.ShowMilestonesDashboardPage,
+               "ShowFooterBranding":            setting.ShowFooterBranding,
+               "ShowFooterVersion":             setting.ShowFooterVersion,
+               "DisableDownloadSourceArchives": setting.Repository.DisableDownloadSourceArchives,
 
                "EnableSwagger":      setting.API.EnableSwagger,
                "EnableOpenIDSignIn": setting.Service.EnableOpenIDSignIn,
index d594caf643401283252dd717e54346074a58def4..b604337715476cac3160adbf811f8ddb29085f53 100644 (file)
@@ -289,6 +289,13 @@ func RegisterRoutes(m *web.Route) {
                }
        }
 
+       dlSourceEnabled := func(ctx *context.Context) {
+               if setting.Repository.DisableDownloadSourceArchives {
+                       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.
@@ -1096,7 +1103,7 @@ func RegisterRoutes(m *web.Route) {
                m.Group("/archive", func() {
                        m.Get("/*", repo.Download)
                        m.Post("/*", repo.InitiateDownload)
-               }, repo.MustBeNotEmpty, reqRepoCodeReader)
+               }, repo.MustBeNotEmpty, dlSourceEnabled, reqRepoCodeReader)
 
                m.Group("/branches", func() {
                        m.Get("", repo.Branches)
index 813aba556ca465d12eb8ed8528c0cf2649166eb0..c5c7185480b273c1bbe1761489c98f1daa58f133 100644 (file)
                <br>
                {{.i18n.Tr "mail.release.downloads"}}
                <ul>
+                       {{if not .DisableDownloadSourceArchives}}
                        <li>
                                <a href="{{.Release.Repo.Link}}/archive/{{.Release.TagName | PathEscapeSegments}}.zip" rel="nofollow"><strong>{{.i18n.Tr "mail.release.download.zip"}}</strong></a>
                        </li>
                        <li>
                                <a href="{{.Release.Repo.Link}}/archive/{{.Release.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow"><strong>{{.i18n.Tr "mail.release.download.targz"}}</strong></a>
                        </li>
+                       {{end}}
                        {{if .Release.Attachments}}
                                {{range .Release.Attachments}}
                                        <li>
index 6c34ba4c19398f371a0b1ef9f87a55e50f740724..4ae58ad3192fb994c7c2d61f1d07b6576184570a 100644 (file)
                                                                                {{svg "octicon-git-branch"}}
                                                                        </div>
                                                                {{end}}
-                                                               <div class="ui basic jump dropdown icon button tooltip" data-content="{{$.i18n.Tr "repo.branch.download" ($.DefaultBranch)}}" data-position="top right">
+                                                               {{if not $.DisableDownloadSourceArchives}}
+                                                                       <div class="ui basic jump dropdown icon button tooltip" data-content="{{$.i18n.Tr "repo.branch.download" ($.DefaultBranch)}}" data-position="top right">
                                                                        {{svg "octicon-download"}}
                                                                        <div class="menu">
                                                                                <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
                                                                                <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
+                                                                               </div>
                                                                        </div>
-                                                               </div>
+                                                               {{end}}
                                                        </td>
                                                </tr>
                                        </tbody>
                                                                                                {{svg "octicon-git-branch"}}
                                                                                        </div>
                                                                                {{end}}
-                                                                               {{if (not .IsDeleted)}}
+                                                                               {{if and (not .IsDeleted) (not $.DisableDownloadSourceArchives)}}
                                                                                        <div class="ui basic jump dropdown icon button tooltip" data-content="{{$.i18n.Tr "repo.branch.download" (.Name)}}" data-position="top right">
                                                                                                {{svg "octicon-download"}}
                                                                                                <div class="menu">
index 28cb9df6368f3a49ecab3e47cd5414ae3983950d..2ed87d8a91fef11c0770b4aa799a256153674d21 100644 (file)
                                                <button id="download-btn" class="ui basic jump dropdown icon button tooltip" data-content="{{.i18n.Tr "repo.download_archive"}}" data-position="top right">
                                                        {{svg "octicon-download"}}
                                                        <div class="menu">
-                                                               <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.i18n.Tr "repo.download_zip"}}</a>
-                                                               <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.i18n.Tr "repo.download_tar"}}</a>
-                                                               <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.bundle" rel="nofollow">{{svg "octicon-package" 16 "mr-3"}}{{.i18n.Tr "repo.download_bundle"}}</a>
+                                                               {{if not $.DisableDownloadSourceArchives}}
+                                                                       <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.i18n.Tr "repo.download_zip"}}</a>
+                                                                       <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.i18n.Tr "repo.download_tar"}}</a>
+                                                                       <a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.BranchName}}.bundle" rel="nofollow">{{svg "octicon-package" 16 "mr-3"}}{{.i18n.Tr "repo.download_bundle"}}</a>
+                                                               {{end}}
                                                                <a class="item" href="vscode://vscode.git/clone?url={{$.RepoCloneLink.HTTPS}}">{{svg "gitea-vscode" 16 "mr-3"}}{{.i18n.Tr "repo.clone_in_vsc"}}</a>
                                                        </div>
                                                </button>
index 956c60ad78c63bd3d3e765ac98fae0a45060b24a..57c7bc0c92e419bd3b7a4386bcc45e50c13bc22e 100644 (file)
                                                                <div class="download df ac">
                                                                        {{if $.Permission.CanRead $.UnitTypeCode}}
                                                                                <a class="mr-3 mono" href="{{$.RepoLink}}/src/commit/{{.Sha1}}" rel="nofollow">{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Sha1}}</a>
-                                                                               <a class="archive-link mr-3" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-2"}}ZIP</a>
-                                                                               <a class="archive-link mr-3" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-2"}}TAR.GZ</a>
+                                                                               {{if not $.DisableDownloadSourceArchives}}
+                                                                                       <a class="archive-link mr-3" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-2"}}ZIP</a>
+                                                                                       <a class="archive-link mr-3" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-2"}}TAR.GZ</a>
+                                                                               {{end}}
                                                                                {{if (and $.CanCreateRelease $release.IsTag)}}
                                                                                        <a class="mr-3" href="{{$.RepoLink}}/releases/new?tag={{.TagName}}">{{svg "octicon-tag" 16 "mr-2"}}{{$.i18n.Tr "repo.release.new_release"}}</a>
                                                                                {{end}}
                                                        <div class="download">
                                                        {{if $.Permission.CanRead $.UnitTypeCode}}
                                                                <a class="mono" href="{{$.RepoLink}}/src/commit/{{.Sha1}}" rel="nofollow">{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Sha1}}</a>
-                                                               <a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
-                                                               <a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
+                                                               {{if not $.DisableDownloadSourceArchives}}
+                                                                       <a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
+                                                                       <a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
+                                                               {{end}}
                                                        {{end}}
                                                        </div>
                                                {{else}}
                                                                        {{$.i18n.Tr "repo.release.downloads"}}
                                                                </summary>
                                                                <ul class="list">
-                                                                       {{if and (not .IsDraft) ($.Permission.CanRead $.UnitTypeCode)}}
+                                                                       {{if and (not $.DisableDownloadSourceArchives) (not .IsDraft) ($.Permission.CanRead $.UnitTypeCode)}}
                                                                                <li>
                                                                                        <a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow"><strong>{{svg "octicon-file-zip" 16 "mr-2"}}{{$.i18n.Tr "repo.release.source_code"}} (ZIP)</strong></a>
                                                                                </li>