fix: validate message request fields

This commit is contained in:
2026-10-30 14:01:30 +03:00
parent 9ccae0c0c3
commit f8a624a40b
3 changed files with 65 additions and 6 deletions
+20
View File
@@ -20,6 +20,26 @@ const maxRequestBytes = 1 << 20
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
type createMessageRequest struct {
Text json.RawMessage `json:"text"`
AttachmentIDs json.RawMessage `json:"attachment_ids"`
}
func (r createMessageRequest) values() (string, []string, bool) {
var text *string
if len(r.Text) == 0 || json.Unmarshal(r.Text, &text) != nil || text == nil {
return "", nil, false
}
if len(r.AttachmentIDs) == 0 {
return *text, nil, true
}
var attachmentIDs *[]string
if json.Unmarshal(r.AttachmentIDs, &attachmentIDs) != nil || attachmentIDs == nil {
return "", nil, false
}
return *text, *attachmentIDs, true
}
func readJSON(w http.ResponseWriter, r *http.Request, target any) *apiError {
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil || mediaType != "application/json" || (params["charset"] != "" && !strings.EqualFold(params["charset"], "utf-8")) {