[PLT-539] Update incoming webhooks to accept multipart/form-data content (#7873)
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
6d8f122a51
Коммит
8c3a7b75de
@@ -8,7 +8,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/gorilla/schema"
|
||||||
"github.com/mattermost/mattermost-server/model"
|
"github.com/mattermost/mattermost-server/model"
|
||||||
"github.com/mattermost/mattermost-server/utils"
|
"github.com/mattermost/mattermost-server/utils"
|
||||||
)
|
)
|
||||||
@@ -447,34 +450,40 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
|
||||||
var payload io.Reader
|
var err *model.AppError
|
||||||
|
incomingWebhookPayload := &model.IncomingWebhookRequest{}
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
if strings.Split(contentType, "; ")[0] == "application/x-www-form-urlencoded" {
|
if strings.Split(contentType, "; ")[0] == "application/x-www-form-urlencoded" {
|
||||||
payload = strings.NewReader(r.FormValue("payload"))
|
payload := strings.NewReader(r.FormValue("payload"))
|
||||||
} else {
|
|
||||||
payload = r.Body
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.App.Config().LogSettings.EnableWebhookDebugging {
|
incomingWebhookPayload, err = decodePayload(payload)
|
||||||
var err error
|
|
||||||
payload, err = utils.InfoReader(
|
|
||||||
payload,
|
|
||||||
utils.T("api.webhook.incoming.debug"),
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Err = model.NewAppError("incomingWebhook", "api.webhook.incoming.debug.error", nil, err.Error(), http.StatusInternalServerError)
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if strings.HasPrefix(contentType, "multipart/form-data") {
|
||||||
|
r.ParseMultipartForm(0)
|
||||||
|
|
||||||
|
decoder := schema.NewDecoder()
|
||||||
|
err := decoder.Decode(incomingWebhookPayload, r.PostForm)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.Err = model.NewAppError("incomingWebhook", "api.webhook.incoming.error", nil, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
incomingWebhookPayload, err = decodePayload(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedRequest, decodeError := model.IncomingWebhookRequestFromJson(payload)
|
if c.App.Config().LogSettings.EnableWebhookDebugging {
|
||||||
|
l4g.Debug(utils.T("api.webhook.incoming.debug"), incomingWebhookPayload.ToJson())
|
||||||
if decodeError != nil {
|
|
||||||
c.Err = decodeError
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.App.HandleIncomingWebhook(id, parsedRequest)
|
err = c.App.HandleIncomingWebhook(id, incomingWebhookPayload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
return
|
return
|
||||||
@@ -499,3 +508,14 @@ func commandWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
w.Write([]byte("ok"))
|
w.Write([]byte("ok"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func decodePayload(payload io.Reader) (*model.IncomingWebhookRequest, *model.AppError) {
|
||||||
|
decodeError := &model.AppError{}
|
||||||
|
incomingWebhookPayload, decodeError := model.IncomingWebhookRequestFromJson(payload)
|
||||||
|
|
||||||
|
if decodeError != nil {
|
||||||
|
return nil, decodeError
|
||||||
|
}
|
||||||
|
|
||||||
|
return incomingWebhookPayload, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3062,6 +3062,10 @@
|
|||||||
"id": "api.webhook.incoming.debug.error",
|
"id": "api.webhook.incoming.debug.error",
|
||||||
"translation": "Could not read payload of incoming webhook."
|
"translation": "Could not read payload of incoming webhook."
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "api.webhook.incoming.error",
|
||||||
|
"translation": "Could not decode the multipart payload of incoming webhook."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "api.webhook.init.debug",
|
"id": "api.webhook.init.debug",
|
||||||
"translation": "Initializing webhook API routes"
|
"translation": "Initializing webhook API routes"
|
||||||
|
|||||||
@@ -204,3 +204,12 @@ func IncomingWebhookRequestFromJson(data io.Reader) (*IncomingWebhookRequest, *A
|
|||||||
|
|
||||||
return o, nil
|
return o, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *IncomingWebhookRequest) ToJson() string {
|
||||||
|
b, err := json.Marshal(o)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -133,6 +133,12 @@ func TestIncomingWebhook(t *testing.T) {
|
|||||||
if _, err := ApiClient.PostToWebhook("abc123", payload); err == nil {
|
if _, err := ApiClient.PostToWebhook("abc123", payload); err == nil {
|
||||||
t.Fatal("should have errored - bad hook")
|
t.Fatal("should have errored - bad hook")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payloadMultiPart := "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nwebhook-bot\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"text\"\r\n\r\nthis is a test :tada:\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
|
||||||
|
if _, err := ApiClient.DoPost("/hooks/"+hook1.Id, payloadMultiPart, "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); err != nil {
|
||||||
|
t.Fatal("should have errored - bad hook")
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if _, err := ApiClient.PostToWebhook("123", "123"); err == nil {
|
if _, err := ApiClient.PostToWebhook("123", "123"); err == nil {
|
||||||
t.Fatal("should have failed - webhooks turned off")
|
t.Fatal("should have failed - webhooks turned off")
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user