From a519b86e8fb6ff6aebeb37ee90e853550635ab05 Mon Sep 17 00:00:00 2001 From: Ashish Bhate Date: Wed, 29 Sep 2021 19:30:23 +0530 Subject: [PATCH] [MM-36786] Check channel permissions before thread follow/unfollow (#18460) --- api4/user.go | 10 ++++++++++ api4/user_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/api4/user.go b/api4/user.go index 25054090cb..d576eb2703 100644 --- a/api4/user.go +++ b/api4/user.go @@ -3059,6 +3059,11 @@ func unfollowThreadByUser(c *Context, w http.ResponseWriter, r *http.Request) { return } + if !c.App.SessionHasPermissionToChannelByPost(*c.AppContext.Session(), c.Params.ThreadId, model.PermissionReadChannel) { + c.SetPermissionError(model.PermissionReadChannel) + return + } + err := c.App.UpdateThreadFollowForUser(c.Params.UserId, c.Params.TeamId, c.Params.ThreadId, false) if err != nil { c.Err = err @@ -3087,6 +3092,11 @@ func followThreadByUser(c *Context, w http.ResponseWriter, r *http.Request) { return } + if !c.App.SessionHasPermissionToChannelByPost(*c.AppContext.Session(), c.Params.ThreadId, model.PermissionReadChannel) { + c.SetPermissionError(model.PermissionReadChannel) + return + } + err := c.App.UpdateThreadFollowForUser(c.Params.UserId, c.Params.TeamId, c.Params.ThreadId, true) if err != nil { c.Err = err diff --git a/api4/user_test.go b/api4/user_test.go index 8247afdb54..8ac7da8479 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -5870,6 +5870,31 @@ func TestFollowThreads(t *testing.T) { require.GreaterOrEqual(t, uss.Threads[0].LastViewedAt, uss.Threads[0].LastReplyAt) }) + + t.Run("No permission to channel", func(t *testing.T) { + // Add user1 to private channel + _, appErr := th.App.AddUserToChannel(th.BasicUser, th.BasicPrivateChannel2, false) + require.Nil(t, appErr) + defer th.App.RemoveUserFromChannel(th.Context, th.BasicUser.Id, "", th.BasicPrivateChannel2) + + // create thread in private channel + rpost, resp, err := th.Client.CreatePost(&model.Post{ChannelId: th.BasicPrivateChannel2.Id, Message: "root post"}) + require.NoError(t, err) + CheckCreatedStatus(t, resp) + _, resp, err = th.Client.CreatePost(&model.Post{ChannelId: th.BasicPrivateChannel2.Id, Message: "testReply", RootId: rpost.Id}) + require.NoError(t, err) + CheckCreatedStatus(t, resp) + + // Try to follow thread as other user who is not in the private channel + resp, err = th.Client.UpdateThreadFollowForUser(th.BasicUser2.Id, th.BasicTeam.Id, rpost.Id, true) + require.Error(t, err) + CheckForbiddenStatus(t, resp) + + // Try to unfollow thread as other user who is not in the private channel + resp, err = th.Client.UpdateThreadFollowForUser(th.BasicUser2.Id, th.BasicTeam.Id, rpost.Id, false) + require.Error(t, err) + CheckForbiddenStatus(t, resp) + }) } func checkThreadListReplies(t *testing.T, th *TestHelper, client *model.Client4, userId string, expectedReplies, expectedThreads int, options *model.GetUserThreadsOpts) (*model.Threads, *model.Response) {