Skip to content

Commit 401ec9e

Browse files
committed
fix(webhook): prevent tag events from bypassing branch filters
1 parent b907b9f commit 401ec9e

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

services/webhook/webhook.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"net/http"
11-
"strings"
1211

1312
"code.gitea.io/gitea/models/db"
1413
repo_model "code.gitea.io/gitea/models/repo"
@@ -46,21 +45,25 @@ func IsValidHookTaskType(name string) bool {
4645
// hookQueue is a global queue of web hooks
4746
var hookQueue *queue.WorkerPoolQueue[int64]
4847

49-
// getPayloadBranch returns branch for hook event, if applicable.
50-
func getPayloadBranch(p api.Payloader) string {
48+
// getPayloadRef returns the full ref name for hook event, if applicable.
49+
func getPayloadRef(p api.Payloader) string {
5150
switch pp := p.(type) {
5251
case *api.CreatePayload:
53-
if pp.RefType == "branch" {
54-
return pp.Ref
52+
switch pp.RefType {
53+
case "branch":
54+
return git.BranchPrefix + pp.Ref
55+
case "tag":
56+
return git.TagPrefix + pp.Ref
5557
}
5658
case *api.DeletePayload:
57-
if pp.RefType == "branch" {
58-
return pp.Ref
59+
switch pp.RefType {
60+
case "branch":
61+
return git.BranchPrefix + pp.Ref
62+
case "tag":
63+
return git.TagPrefix + pp.Ref
5964
}
6065
case *api.PushPayload:
61-
if strings.HasPrefix(pp.Ref, git.BranchPrefix) {
62-
return pp.Ref[len(git.BranchPrefix):]
63-
}
66+
return pp.Ref
6467
}
6568
return ""
6669
}
@@ -144,11 +147,10 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook
144147
return nil
145148
}
146149

147-
// If payload has no associated branch (e.g. it's a new tag, issue, etc.),
148-
// branch filter has no effect.
149-
if branch := getPayloadBranch(p); branch != "" {
150-
if !checkBranch(w, branch) {
151-
log.Info("Branch %q doesn't match branch filter %q, skipping", branch, w.BranchFilter)
150+
// Apply the filter directly to the ref name
151+
if ref := getPayloadRef(p); ref != "" {
152+
if !checkBranch(w, ref) {
153+
log.Info("Ref %q doesn't match branch filter %q, skipping", ref, w.BranchFilter)
152154
return nil
153155
}
154156
}

0 commit comments

Comments
 (0)