Skip to content

Commit 8effc3e

Browse files
lunnyzeripath
authored and
Sysoev, Vladimir
committed
Add disable download source configuration (go-gitea#20548)
Add configuration to enable/disable download source from UI. Co-authored-by: zeripath <[email protected]>
1 parent 68ebf11 commit 8effc3e

File tree

9 files changed

+39
-16
lines changed

9 files changed

+39
-16
lines changed

custom/conf/app.example.ini

+3
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ ROUTER = console
879879
;; Allow deletion of unadopted repositories
880880
;ALLOW_DELETION_OF_UNADOPTED_REPOSITORIES = false
881881

882+
;; Don't allow download source archive files from UI
883+
;DISABLE_DOWNLOAD_SOURCE_ARCHIVES = false
884+
882885
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
883886
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
884887
;[repository.editor]

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
7878
- `DEFAULT_BRANCH`: **main**: Default branch name of all repositories.
7979
- `ALLOW_ADOPTION_OF_UNADOPTED_REPOSITORIES`: **false**: Allow non-admin users to adopt unadopted repositories
8080
- `ALLOW_DELETION_OF_UNADOPTED_REPOSITORIES`: **false**: Allow non-admin users to delete unadopted repositories
81+
- `DISABLE_DOWNLOAD_SOURCE_ARCHIVES`: **false**: Don't allow download source archive files from UI
8182

8283
### Repository - Editor (`repository.editor`)
8384

modules/setting/repository.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
DefaultBranch string
4949
AllowAdoptionOfUnadoptedRepositories bool
5050
AllowDeleteOfUnadoptedRepositories bool
51+
DisableDownloadSourceArchives bool
5152

5253
// Repository editor settings
5354
Editor struct {

modules/templates/base.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ func BaseVars() Vars {
3535
"IsLandingPageExplore": setting.LandingPageURL == setting.LandingPageExplore,
3636
"IsLandingPageOrganizations": setting.LandingPageURL == setting.LandingPageOrganizations,
3737

38-
"ShowRegistrationButton": setting.Service.ShowRegistrationButton,
39-
"ShowMilestonesDashboardPage": setting.Service.ShowMilestonesDashboardPage,
40-
"ShowFooterBranding": setting.ShowFooterBranding,
41-
"ShowFooterVersion": setting.ShowFooterVersion,
38+
"ShowRegistrationButton": setting.Service.ShowRegistrationButton,
39+
"ShowMilestonesDashboardPage": setting.Service.ShowMilestonesDashboardPage,
40+
"ShowFooterBranding": setting.ShowFooterBranding,
41+
"ShowFooterVersion": setting.ShowFooterVersion,
42+
"DisableDownloadSourceArchives": setting.Repository.DisableDownloadSourceArchives,
4243

4344
"EnableSwagger": setting.API.EnableSwagger,
4445
"EnableOpenIDSignIn": setting.Service.EnableOpenIDSignIn,

routers/web/web.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ func RegisterRoutes(m *web.Route) {
290290
}
291291
}
292292

293+
dlSourceEnabled := func(ctx *context.Context) {
294+
if setting.Repository.DisableDownloadSourceArchives {
295+
ctx.Error(http.StatusNotFound)
296+
return
297+
}
298+
}
299+
293300
// FIXME: not all routes need go through same middleware.
294301
// Especially some AJAX requests, we can reduce middleware number to improve performance.
295302
// Routers.
@@ -1106,7 +1113,7 @@ func RegisterRoutes(m *web.Route) {
11061113
m.Group("/archive", func() {
11071114
m.Get("/*", repo.Download)
11081115
m.Post("/*", repo.InitiateDownload)
1109-
}, repo.MustBeNotEmpty, reqRepoCodeReader)
1116+
}, repo.MustBeNotEmpty, dlSourceEnabled, reqRepoCodeReader)
11101117

11111118
m.Group("/branches", func() {
11121119
m.Get("", repo.Branches)

templates/mail/release.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
<br>
3232
{{.locale.Tr "mail.release.downloads"}}
3333
<ul>
34+
{{if not .DisableDownloadSourceArchives}}
3435
<li>
3536
<a href="{{.Release.Repo.Link}}/archive/{{.Release.TagName | PathEscapeSegments}}.zip" rel="nofollow"><strong>{{.locale.Tr "mail.release.download.zip"}}</strong></a>
3637
</li>
3738
<li>
3839
<a href="{{.Release.Repo.Link}}/archive/{{.Release.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow"><strong>{{.locale.Tr "mail.release.download.targz"}}</strong></a>
3940
</li>
41+
{{end}}
4042
{{if .Release.Attachments}}
4143
{{range .Release.Attachments}}
4244
<li>

templates/repo/branch/list.tmpl

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
{{svg "octicon-git-branch"}}
2727
</div>
2828
{{end}}
29-
<div class="ui basic jump dropdown icon button tooltip" data-content="{{$.locale.Tr "repo.branch.download" ($.DefaultBranch)}}" data-position="top right">
30-
{{svg "octicon-download"}}
31-
<div class="menu">
32-
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
33-
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
29+
{{if not $.DisableDownloadSourceArchives}}
30+
<div class="ui basic jump dropdown icon button tooltip" data-content="{{$.locale.Tr "repo.branch.download" ($.DefaultBranch)}}" data-position="top right">
31+
{{svg "octicon-download"}}
32+
<div class="menu">
33+
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
34+
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
35+
</div>
3436
</div>
35-
</div>
37+
{{end}}
3638
</td>
3739
</tr>
3840
</tbody>

templates/repo/home.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
{{if eq $n 0}}
125125
<div class="ui action tiny input" id="clone-panel">
126126
{{template "repo/clone_buttons" .}}
127+
{{if not .DisableDownloadSourceArchives}}
127128
<button id="download-btn" class="ui basic jump dropdown icon button tooltip" data-content="{{.locale.Tr "repo.download_archive"}}" data-position="top right">
128129
{{svg "octicon-download"}}
129130
<div class="menu">
@@ -133,6 +134,7 @@
133134
<a class="item" href="vscode://vscode.git/clone?url={{$.RepoCloneLink.HTTPS}}">{{svg "gitea-vscode" 16 "mr-3"}}{{.locale.Tr "repo.clone_in_vsc"}}</a>
134135
</div>
135136
</button>
137+
{{end}}
136138
</div>
137139
{{end}}
138140
</div>

templates/repo/release/list.tmpl

+9-5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
<div class="download df ac">
3838
{{if $.Permission.CanRead $.UnitTypeCode}}
3939
<a class="mr-3 mono" href="{{$.RepoLink}}/src/commit/{{.Sha1}}" rel="nofollow">{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Sha1}}</a>
40-
<a class="archive-link mr-3" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-2"}}ZIP</a>
41-
<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>
40+
{{if not $.DisableDownloadSourceArchives}}
41+
<a class="archive-link mr-3" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-2"}}ZIP</a>
42+
<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>
43+
{{end}}
4244
{{if (and $.CanCreateRelease $release.IsTag)}}
4345
<a class="mr-3" href="{{$.RepoLink}}/releases/new?tag={{.TagName}}">{{svg "octicon-tag" 16 "mr-2"}}{{$.locale.Tr "repo.release.new_release"}}</a>
4446
{{end}}
@@ -104,8 +106,10 @@
104106
<div class="download">
105107
{{if $.Permission.CanRead $.UnitTypeCode}}
106108
<a class="mono" href="{{$.RepoLink}}/src/commit/{{.Sha1}}" rel="nofollow">{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Sha1}}</a>
107-
<a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
108-
<a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
109+
{{if not $.DisableDownloadSourceArchives}}
110+
<a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
111+
<a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
112+
{{end}}
109113
{{end}}
110114
</div>
111115
{{else}}
@@ -146,7 +150,7 @@
146150
{{$.locale.Tr "repo.release.downloads"}}
147151
</summary>
148152
<ul class="list">
149-
{{if and (not .IsDraft) ($.Permission.CanRead $.UnitTypeCode)}}
153+
{{if and (not $.DisableDownloadSourceArchives) (not .IsDraft) ($.Permission.CanRead $.UnitTypeCode)}}
150154
<li>
151155
<a class="archive-link" href="{{$.RepoLink}}/archive/{{.TagName | PathEscapeSegments}}.zip" rel="nofollow"><strong>{{svg "octicon-file-zip" 16 "mr-2"}}{{$.locale.Tr "repo.release.source_code"}} (ZIP)</strong></a>
152156
</li>

0 commit comments

Comments
 (0)