mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-02 05:14:25 +02:00
fix(notif): refactor and fix json payload validation and handling
This commit is contained in:
parent
24f949f053
commit
5631b1540a
2 changed files with 33 additions and 6 deletions
8
internal/notif/mime_types.go
Normal file
8
internal/notif/mime_types.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package notif
|
||||
|
||||
const (
|
||||
MimeTypeJSON = "application/json"
|
||||
MimeTypeForm = "application/x-www-form-urlencoded"
|
||||
MimeTypeText = "text/plain"
|
||||
MimeTypeMarkdown = "text/markdown"
|
||||
)
|
|
@ -33,16 +33,18 @@ func (webhook *Webhook) Validate() gperr.Error {
|
|||
|
||||
switch webhook.MIMEType {
|
||||
case "":
|
||||
webhook.MIMEType = "application/json"
|
||||
case "application/json", "application/x-www-form-urlencoded", "text/plain":
|
||||
webhook.MIMEType = MimeTypeJSON
|
||||
case MimeTypeJSON, MimeTypeForm, MimeTypeText:
|
||||
default:
|
||||
return gperr.New("invalid mime_type, expect empty, 'application/json', 'application/x-www-form-urlencoded' or 'text/plain'")
|
||||
return gperr.Errorf("invalid mime_type, expect %s", strings.Join([]string{"empty", MimeTypeJSON, MimeTypeForm, MimeTypeText}, ", "))
|
||||
}
|
||||
|
||||
switch webhook.Template {
|
||||
case "":
|
||||
if webhook.MIMEType == "application/json" && !json.Valid([]byte(webhook.Payload)) {
|
||||
return gperr.New("invalid payload, expect valid JSON")
|
||||
if webhook.MIMEType == MimeTypeJSON {
|
||||
if !validateJSONPayload(webhook.Payload) {
|
||||
return gperr.New("invalid payload, expect valid JSON")
|
||||
}
|
||||
}
|
||||
if webhook.Payload == "" {
|
||||
return gperr.New("invalid payload, expect non-empty")
|
||||
|
@ -50,7 +52,7 @@ func (webhook *Webhook) Validate() gperr.Error {
|
|||
case "discord":
|
||||
webhook.ColorMode = "dec"
|
||||
webhook.Method = http.MethodPost
|
||||
webhook.MIMEType = "application/json"
|
||||
webhook.MIMEType = MimeTypeJSON
|
||||
if webhook.Payload == "" {
|
||||
webhook.Payload = discordPayload
|
||||
}
|
||||
|
@ -115,6 +117,12 @@ func (webhook *Webhook) MarshalMessage(logMsg *LogMessage) ([]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if webhook.MIMEType == MimeTypeJSON {
|
||||
message, err = json.Marshal(string(message))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
plTempl := strings.NewReplacer(
|
||||
"$title", string(title),
|
||||
"$message", string(message),
|
||||
|
@ -130,3 +138,14 @@ func (webhook *Webhook) MarshalMessage(logMsg *LogMessage) ([]byte, error) {
|
|||
pl = plTempl.Replace(pl)
|
||||
return []byte(pl), nil
|
||||
}
|
||||
|
||||
func validateJSONPayload(payload string) bool {
|
||||
replacer := strings.NewReplacer(
|
||||
"$title", `""`,
|
||||
"$message", `""`,
|
||||
"$fields", `""`,
|
||||
"$color", "",
|
||||
)
|
||||
payload = replacer.Replace(payload)
|
||||
return json.Valid([]byte(payload))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue