fix: add length limit to discord webhook icon_url

(cherry picked from commit cf3ebab4ba)
This commit is contained in:
Kidsan 2024-10-07 22:47:45 +02:00 committed by forgejo-backport-action
parent 092cb967b0
commit 2c0c6f408e
2 changed files with 26 additions and 6 deletions

View file

@ -2356,6 +2356,7 @@ settings.slack_icon_url = Icon URL
settings.slack_color = Color settings.slack_color = Color
settings.discord_username = Username settings.discord_username = Username
settings.discord_icon_url = Icon URL settings.discord_icon_url = Icon URL
settings.discord_icon_url.exceeds_max_length = Icon URL must be less than or equal to 2048 characters
settings.event_desc = Trigger on: settings.event_desc = Trigger on:
settings.event_push_only = Push events settings.event_push_only = Push events
settings.event_send_everything = All events settings.event_send_everything = All events

View file

@ -14,6 +14,8 @@ import (
"strings" "strings"
"unicode/utf8" "unicode/utf8"
"gitea.com/go-chi/binding"
webhook_model "code.gitea.io/gitea/models/webhook" webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
@ -22,6 +24,7 @@ import (
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
webhook_module "code.gitea.io/gitea/modules/webhook" webhook_module "code.gitea.io/gitea/modules/webhook"
gitea_context "code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/forms"
"code.gitea.io/gitea/services/webhook/shared" "code.gitea.io/gitea/services/webhook/shared"
) )
@ -31,13 +34,29 @@ type discordHandler struct{}
func (discordHandler) Type() webhook_module.HookType { return webhook_module.DISCORD } func (discordHandler) Type() webhook_module.HookType { return webhook_module.DISCORD }
func (discordHandler) Icon(size int) template.HTML { return shared.ImgIcon("discord.png", size) } func (discordHandler) Icon(size int) template.HTML { return shared.ImgIcon("discord.png", size) }
func (discordHandler) UnmarshalForm(bind func(any)) forms.WebhookForm { type discordForm struct {
var form struct {
forms.WebhookCoreForm forms.WebhookCoreForm
PayloadURL string `binding:"Required;ValidUrl"` PayloadURL string `binding:"Required;ValidUrl"`
Username string Username string
IconURL string IconURL string
} }
var _ binding.Validator = &discordForm{}
// Validate implements binding.Validator.
func (d *discordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := gitea_context.GetWebContext(req)
if len([]rune(d.IconURL)) > 2048 {
errs = append(errs, binding.Error{
FieldNames: []string{"IconURL"},
Message: ctx.Locale.TrString("repo.settings.discord_icon_url.exceeds_max_length"),
})
}
return errs
}
func (discordHandler) UnmarshalForm(bind func(any)) forms.WebhookForm {
var form discordForm
bind(&form) bind(&form)
return forms.WebhookForm{ return forms.WebhookForm{