Skip to content

Commit 07df661

Browse files
committed
Added option to disable web hooks
This mod introduces DISABLE_WEB_HOOKS parameter in [security] section of app.ini (by default set to false). If set to true it disables web hooks feature. Any existing undelivered web hook tasks will be cancelled. Any existing web hook definitions will be left untouched in db but its delivery tasks will be ignored. Author-Change-Id: IB#1105130
1 parent b222dbc commit 07df661

File tree

12 files changed

+62
-19
lines changed

12 files changed

+62
-19
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ IMPORT_LOCAL_PATHS = false
541541
; It also enables them to access other resources available to the user on the operating system that is running the Gitea instance and perform arbitrary actions in the name of the Gitea OS user.
542542
; WARNING: This maybe harmful to you website or your operating system.
543543
DISABLE_GIT_HOOKS = true
544+
; Set to false to disable web hooks feature.
545+
DISABLE_WEB_HOOKS = false
544546
; Set to false to allow pushes to gitea repositories despite having an incomplete environment - NOT RECOMMENDED
545547
ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET = true
546548
;Comma separated list of character classes required to pass minimum complexity.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ relation to port exhaustion.
400400
It also enables them to access other resources available to the user on the operating system that is running the
401401
Gitea instance and perform arbitrary actions in the name of the Gitea OS user.
402402
This maybe harmful to you website or your operating system.
403+
- `DISABLE_WEB_HOOKS`: **false**: Set to `true` to disable web hooks feature.
403404
- `ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET`: **true**: Set to `false` to allow local users to push to gitea-repositories without setting up the Gitea environment. This is not recommended and if you want local users to push to gitea repositories you should set the environment appropriately.
404405
- `IMPORT_LOCAL_PATHS`: **false**: Set to `false` to prevent all users (including admin) from importing local path on server.
405406
- `INTERNAL_TOKEN`: **\<random at every install if no uri set\>**: Secret used to validate communication within Gitea binary.

modules/setting/setting.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ var (
152152
MinPasswordLength int
153153
ImportLocalPaths bool
154154
DisableGitHooks bool
155+
DisableWebHooks bool
155156
OnlyAllowPushIfGiteaEnvironmentSet bool
156157
PasswordComplexity []string
157158
PasswordHashAlgo string
@@ -770,6 +771,7 @@ func NewContext() {
770771
MinPasswordLength = sec.Key("MIN_PASSWORD_LENGTH").MustInt(6)
771772
ImportLocalPaths = sec.Key("IMPORT_LOCAL_PATHS").MustBool(false)
772773
DisableGitHooks = sec.Key("DISABLE_GIT_HOOKS").MustBool(true)
774+
DisableWebHooks = sec.Key("DISABLE_WEB_HOOKS").MustBool(false)
773775
OnlyAllowPushIfGiteaEnvironmentSet = sec.Key("ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET").MustBool(true)
774776
PasswordHashAlgo = sec.Key("PASSWORD_HASH_ALGO").MustString("argon2")
775777
CSRFCookieHTTPOnly = sec.Key("CSRF_COOKIE_HTTP_ONLY").MustBool(true)

modules/templates/helper.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ func NewFuncMap() []template.FuncMap {
225225
"DisableGitHooks": func() bool {
226226
return setting.DisableGitHooks
227227
},
228+
"DisableWebHooks": func() bool {
229+
return setting.DisableWebHooks
230+
},
228231
"DisableImportLocal": func() bool {
229232
return !setting.ImportLocalPaths
230233
},

modules/webhook/deliver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ func Deliver(t *models.HookTask) error {
141141
}
142142
}()
143143

144+
if setting.DisableWebHooks {
145+
return fmt.Errorf("Sending webhook skipped (web hooks disabled): [%d]", t.ID)
146+
}
147+
144148
resp, err := webhookHTTPClient.Do(req)
145149
if err != nil {
146150
t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)

modules/webhook/webhook.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func checkBranch(w *models.Webhook, branch string) bool {
6868
}
6969

7070
func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.HookEventType, p api.Payloader) error {
71+
// Skip sending if web hooks are disabled.
72+
if setting.DisableWebHooks {
73+
return nil
74+
}
75+
7176
for _, e := range w.EventCheckers() {
7277
if event == e.Type {
7378
if !e.Has() {

routers/api/v1/api.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,16 @@ func reqGitHook() macaron.Handler {
374374
}
375375
}
376376

377+
// reqWebHooksEnabled requires web hooks to be enabled by admin.
378+
func reqWebHooksEnabled() macaron.Handler {
379+
return func(ctx *context.APIContext) {
380+
if setting.DisableWebHooks {
381+
ctx.Error(http.StatusForbidden, "", "web hooks disabled by administrator")
382+
return
383+
}
384+
}
385+
}
386+
377387
func orgAssignment(args ...bool) macaron.Handler {
378388
var (
379389
assignOrg bool
@@ -646,6 +656,14 @@ func RegisterRoutes(m *macaron.Macaron) {
646656
m.Combo("/notifications").
647657
Get(reqToken(), notify.ListRepoNotifications).
648658
Put(reqToken(), notify.ReadRepoNotifications)
659+
m.Group("/hooks/git", func() {
660+
m.Combo("").Get(repo.ListGitHooks)
661+
m.Group("/:id", func() {
662+
m.Combo("").Get(repo.GetGitHook).
663+
Patch(bind(api.EditGitHookOption{}), repo.EditGitHook).
664+
Delete(repo.DeleteGitHook)
665+
})
666+
}, reqToken(), reqAdmin(), reqGitHook(), context.ReferencesGitRepo(true))
649667
m.Group("/hooks", func() {
650668
m.Combo("").Get(repo.ListHooks).
651669
Post(bind(api.CreateHookOption{}), repo.CreateHook)
@@ -655,15 +673,7 @@ func RegisterRoutes(m *macaron.Macaron) {
655673
Delete(repo.DeleteHook)
656674
m.Post("/tests", context.RepoRef(), repo.TestHook)
657675
})
658-
m.Group("/git", func() {
659-
m.Combo("").Get(repo.ListGitHooks)
660-
m.Group("/:id", func() {
661-
m.Combo("").Get(repo.GetGitHook).
662-
Patch(bind(api.EditGitHookOption{}), repo.EditGitHook).
663-
Delete(repo.DeleteGitHook)
664-
})
665-
}, reqGitHook(), context.ReferencesGitRepo(true))
666-
}, reqToken(), reqAdmin())
676+
}, reqToken(), reqAdmin(), reqWebHooksEnabled())
667677
m.Group("/collaborators", func() {
668678
m.Get("", reqAnyRepoReader(), repo.ListCollaborators)
669679
m.Combo("/:collaborator").Get(reqAnyRepoReader(), repo.IsCollaborator).
@@ -914,7 +924,7 @@ func RegisterRoutes(m *macaron.Macaron) {
914924
m.Combo("/:id").Get(org.GetHook).
915925
Patch(bind(api.EditHookOption{}), org.EditHook).
916926
Delete(org.DeleteHook)
917-
}, reqToken(), reqOrgOwnership())
927+
}, reqToken(), reqOrgOwnership(), reqWebHooksEnabled())
918928
}, orgAssignment(true))
919929
m.Group("/teams/:teamid", func() {
920930
m.Combo("").Get(org.GetTeam).

routers/routes/routes.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,14 @@ func RegisterRoutes(m *macaron.Macaron) {
331331
}
332332
}
333333

334+
// webHooksEnabled requires web hooks to be enabled by admin.
335+
webHooksEnabled := func(ctx *context.Context) {
336+
if setting.DisableWebHooks {
337+
ctx.Error(403)
338+
return
339+
}
340+
}
341+
334342
m.Use(user.GetNotificationCount)
335343
m.Use(func(ctx *context.Context) {
336344
ctx.Data["UnitWikiGlobalDisabled"] = models.UnitTypeWiki.UnitGlobalDisabled()
@@ -550,7 +558,7 @@ func RegisterRoutes(m *macaron.Macaron) {
550558
m.Post("/matrix/:id", bindIgnErr(auth.NewMatrixHookForm{}), repo.MatrixHooksEditPost)
551559
m.Post("/msteams/:id", bindIgnErr(auth.NewMSTeamsHookForm{}), repo.MSTeamsHooksEditPost)
552560
m.Post("/feishu/:id", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksEditPost)
553-
})
561+
}, webHooksEnabled)
554562

555563
m.Group("/auths", func() {
556564
m.Get("", admin.Authentications)
@@ -655,7 +663,7 @@ func RegisterRoutes(m *macaron.Macaron) {
655663
m.Post("/matrix/:id", bindIgnErr(auth.NewMatrixHookForm{}), repo.MatrixHooksEditPost)
656664
m.Post("/msteams/:id", bindIgnErr(auth.NewMSTeamsHookForm{}), repo.MSTeamsHooksEditPost)
657665
m.Post("/feishu/:id", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksEditPost)
658-
})
666+
}, webHooksEnabled)
659667

660668
m.Group("/labels", func() {
661669
m.Get("", org.RetrieveLabels, org.Labels)
@@ -708,6 +716,12 @@ func RegisterRoutes(m *macaron.Macaron) {
708716
Post(bindIgnErr(auth.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost)
709717
}, repo.MustBeNotEmpty)
710718

719+
m.Group("/hooks/git", func() {
720+
m.Get("", repo.GitHooks)
721+
m.Combo("/:name").Get(repo.GitHooksEdit).
722+
Post(repo.GitHooksEditPost)
723+
}, context.GitHookService())
724+
711725
m.Group("/hooks", func() {
712726
m.Get("", repo.Webhooks)
713727
m.Post("/delete", repo.DeleteWebhook)
@@ -732,13 +746,7 @@ func RegisterRoutes(m *macaron.Macaron) {
732746
m.Post("/matrix/:id", bindIgnErr(auth.NewMatrixHookForm{}), repo.MatrixHooksEditPost)
733747
m.Post("/msteams/:id", bindIgnErr(auth.NewMSTeamsHookForm{}), repo.MSTeamsHooksEditPost)
734748
m.Post("/feishu/:id", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksEditPost)
735-
736-
m.Group("/git", func() {
737-
m.Get("", repo.GitHooks)
738-
m.Combo("/:name").Get(repo.GitHooksEdit).
739-
Post(repo.GitHooksEditPost)
740-
}, context.GitHookService())
741-
})
749+
}, webHooksEnabled)
742750

743751
m.Group("/keys", func() {
744752
m.Combo("").Get(repo.DeployKeys).

templates/admin/navbar.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
<a class="{{if .PageIsAdminRepositories}}active{{end}} item" href="{{AppSubUrl}}/admin/repos">
1212
{{.i18n.Tr "admin.repositories"}}
1313
</a>
14+
{{if not DisableWebHooks}}
1415
<a class="{{if .PageIsAdminHooks}}active{{end}} item" href="{{AppSubUrl}}/admin/hooks">
1516
{{.i18n.Tr "admin.hooks"}}
1617
</a>
1718
<a class="{{if .PageIsAdminSystemHooks}}active{{end}} item" href="{{AppSubUrl}}/admin/system-hooks">
1819
{{.i18n.Tr "admin.systemhooks"}}
1920
</a>
21+
{{end}}
2022
<a class="{{if .PageIsAdminAuthentications}}active{{end}} item" href="{{AppSubUrl}}/admin/auths">
2123
{{.i18n.Tr "admin.authentication"}}
2224
</a>

templates/org/settings/navbar.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
<a class="{{if .PageIsSettingsOptions}}active{{end}} item" href="{{.OrgLink}}/settings">
55
{{.i18n.Tr "org.settings.options"}}
66
</a>
7+
{{if not DisableWebHooks}}
78
<a class="{{if .PageIsSettingsHooks}}active{{end}} item" href="{{.OrgLink}}/settings/hooks">
89
{{.i18n.Tr "repo.settings.hooks"}}
910
</a>
11+
{{end}}
1012
<a class="{{if .PageIsOrgSettingsLabels}}active{{end}} item" href="{{.OrgLink}}/settings/labels">
1113
{{.i18n.Tr "repo.labels"}}
1214
</a>

templates/repo/settings/nav.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
<li {{if .PageIsSettingsOptions}}class="current"{{end}}><a href="{{.RepoLink}}/settings">{{.i18n.Tr "repo.settings.options"}}</a></li>
66
<li {{if .PageIsSettingsCollaboration}}class="current"{{end}}><a href="{{.RepoLink}}/settings/collaboration">{{.i18n.Tr "repo.settings.collaboration"}}</a></li>
77
<li {{if .PageIsSettingsBranches}}class="current"{{end}}><a href="{{.RepoLink}}/settings/branches">{{.i18n.Tr "repo.settings.branches"}}</a></li>
8+
{{if not DisableWebHooks}}
89
<li {{if .PageIsSettingsHooks}}class="current"{{end}}><a href="{{.RepoLink}}/settings/hooks">{{.i18n.Tr "repo.settings.hooks"}}</a></li>
10+
{{end}}
911
{{if or .SignedUser.AllowGitHook .SignedUser.IsAdmin}}
1012
<li {{if .PageIsSettingsGitHooks}}class="current"{{end}}><a href="{{.RepoLink}}/settings/hooks/git">{{.i18n.Tr "repo.settings.githooks"}}</a></li>
1113
{{end}}

templates/repo/settings/navbar.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
{{.i18n.Tr "repo.settings.branches"}}
1111
</a>
1212
{{end}}
13+
{{if not DisableWebHooks}}
1314
<a class="{{if .PageIsSettingsHooks}}active{{end}} item" href="{{.RepoLink}}/settings/hooks">
1415
{{.i18n.Tr "repo.settings.hooks"}}
1516
</a>
17+
{{end}}
1618
{{if .SignedUser.CanEditGitHook}}
1719
<a class="{{if .PageIsSettingsGitHooks}}active{{end}} item" href="{{.RepoLink}}/settings/hooks/git">
1820
{{.i18n.Tr "repo.settings.githooks"}}

0 commit comments

Comments
 (0)