From b4f337f1911a0278729b123cf94f27766f2468d6 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Mon, 28 Oct 2024 12:52:23 -0600 Subject: [PATCH] [MM-60886] Fix TypeAssertionError in `api4.addChannelMember` (#28976) * Fix TypeAssertionError in api4.addChannelMember * Fix --- server/channels/api4/channel.go | 9 +++++++- server/channels/api4/channel_test.go | 33 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/server/channels/api4/channel.go b/server/channels/api4/channel.go index 01166da9fb..fbcf8fdab7 100644 --- a/server/channels/api4/channel.go +++ b/server/channels/api4/channel.go @@ -1780,7 +1780,14 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { return } for _, userId := range interfaceIds { - userIds = append(userIds, userId.(string)) + uid, isString := userId.(string) + + if !isString || !model.IsValidId(uid) { + c.SetInvalidParam("user_id in user_ids") + return + } + + userIds = append(userIds, uid) } } else { userId, ok2 := props["user_id"].(string) diff --git a/server/channels/api4/channel_test.go b/server/channels/api4/channel_test.go index c7ab7662a9..51527784a3 100644 --- a/server/channels/api4/channel_test.go +++ b/server/channels/api4/channel_test.go @@ -7,6 +7,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "sort" "strings" @@ -3491,6 +3492,38 @@ func TestAddChannelMember(t *testing.T) { _, _, err = client.AddChannelMember(context.Background(), privateChannel.Id, user.Id) require.NoError(t, err) }) + + t.Run("invalid request data", func(t *testing.T) { + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + // correct type for user ids (string) but invalid value. + requestBody := map[string]any{"user_ids": []string{"invalid", user2.Id}} + requestData, err := json.Marshal(requestBody) + require.NoError(t, err) + + res, err := client.DoAPIPost(context.Background(), "/channels/"+publicChannel.Id+"/members", string(requestData)) + if client == th.LocalClient { + require.EqualError(t, err, "Invalid or missing user_id in request body.") + } else { + require.EqualError(t, err, "Invalid or missing user_id in user_ids in request body.") + } + defer res.Body.Close() + io.Copy(io.Discard, res.Body) + + // invalid type for user ids (should be string). + requestBody = map[string]any{"user_ids": []any{45, user2.Id}} + requestData, err = json.Marshal(requestBody) + require.NoError(t, err) + + res, err = client.DoAPIPost(context.Background(), "/channels/"+privateChannel.Id+"/members", string(requestData)) + if client == th.LocalClient { + require.EqualError(t, err, "Invalid or missing user_id in request body.") + } else { + require.EqualError(t, err, "Invalid or missing user_id in user_ids in request body.") + } + defer res.Body.Close() + io.Copy(io.Discard, res.Body) + }) + }) } func TestAddChannelMembers(t *testing.T) {