From 338f6a9db73f2b09e9dfa0448fdb74d7055ccec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Tue, 20 Jul 2021 09:30:49 +0200 Subject: [PATCH] Consider empty body before unmarshal on do post action (#17879) * Consider empty body before unmarshal on do post action * Fix test and add test * Fix test --- app/integration_action.go | 9 +++++- app/integration_action_test.go | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/app/integration_action.go b/app/integration_action.go index bdd78bc3a7..37e646818f 100644 --- a/app/integration_action.go +++ b/app/integration_action.go @@ -249,10 +249,17 @@ func (a *App) DoPostActionWithCookie(c *request.Context, postID, actionId, userI defer resp.Body.Close() var response model.PostActionIntegrationResponse - if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { + respBytes, err := ioutil.ReadAll(resp.Body) + if err != nil { return "", model.NewAppError("DoPostActionWithCookie", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest) } + if len(respBytes) > 0 { + if err = json.Unmarshal(respBytes, &response); err != nil { + return "", model.NewAppError("DoPostActionWithCookie", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest) + } + } + if response.Update != nil { response.Update.Id = postID diff --git a/app/integration_action_test.go b/app/integration_action_test.go index 948b1151e1..e16169a1a1 100644 --- a/app/integration_action_test.go +++ b/app/integration_action_test.go @@ -69,6 +69,59 @@ func TestPostActionInvalidURL(t *testing.T) { require.True(t, strings.Contains(err.Error(), "missing protocol scheme")) } +func TestPostActionEmptyResponse(t *testing.T) { + t.Run("Empty response on post action", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + channel := th.BasicChannel + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost,127.0.0.1" + }) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + defer ts.Close() + + interactivePost := model.Post{ + Message: "Interactive post", + ChannelId: channel.Id, + PendingPostId: model.NewId() + ":" + fmt.Sprint(model.GetMillis()), + UserId: th.BasicUser.Id, + Props: model.StringInterface{ + "attachments": []*model.SlackAttachment{ + { + Text: "hello", + Actions: []*model.PostAction{ + { + Integration: &model.PostActionIntegration{ + Context: model.StringInterface{ + "s": "foo", + "n": 3, + }, + URL: ts.URL, + }, + Name: "action", + Type: "some_type", + DataSource: "some_source", + }, + }, + }, + }, + }, + } + + post, err := th.App.CreatePostAsUser(th.Context, &interactivePost, "", true) + require.Nil(t, err) + + attachments, ok := post.GetProp("attachments").([]*model.SlackAttachment) + require.True(t, ok) + + _, err = th.App.DoPostAction(th.Context, post.Id, attachments[0].Actions[0].Id, th.BasicUser.Id, "") + require.Nil(t, err) + }) +} + func TestPostAction(t *testing.T) { testCases := []struct { Description string