From fb022461f40d3adac046e0a0627e26fba916516a Mon Sep 17 00:00:00 2001 From: Ashish Bhate Date: Tue, 22 Sep 2020 13:52:42 +0530 Subject: [PATCH] [MM-28100]: fix getChannelByNameForTeamName permissions (#15525) Summary: fix getChannelByNameForTeamName to allow team members to join public channels. I'm not sure why, but the authorization logic of getChannelByName and getChannelByNameForTeamName was different. After this change they behave the same. This fixes an issue with the mobile app. Ticket Link: https://mattermost.atlassian.net/browse/MM-28100 Related PR: mattermost/mattermost-mobile#4810 --- api4/channel.go | 10 +++++++++- api4/channel_test.go | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/api4/channel.go b/api4/channel.go index bb89bee9b6..e4f16e7ff2 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -1150,7 +1150,15 @@ func getChannelByNameForTeamName(c *Context, w http.ResponseWriter, r *http.Requ return } - if !c.App.SessionHasPermissionToChannel(*c.App.Session(), channel.Id, model.PERMISSION_READ_CHANNEL) { + teamOk := c.App.SessionHasPermissionToTeam(*c.App.Session(), channel.TeamId, model.PERMISSION_READ_PUBLIC_CHANNEL) + channelOk := c.App.SessionHasPermissionToChannel(*c.App.Session(), channel.Id, model.PERMISSION_READ_CHANNEL) + + if channel.Type == model.CHANNEL_OPEN { + if !teamOk && !channelOk { + c.SetPermissionError(model.PERMISSION_READ_PUBLIC_CHANNEL) + return + } + } else if !channelOk { c.Err = model.NewAppError("getChannelByNameForTeamName", "app.channel.get_by_name.missing.app_error", nil, "teamId="+channel.TeamId+", "+"name="+channel.Name+"", http.StatusNotFound) return } diff --git a/api4/channel_test.go b/api4/channel_test.go index a1cd943caa..f46c024e1a 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -1907,6 +1907,11 @@ func TestGetChannelByNameForTeamName(t *testing.T) { _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") CheckNoError(t, resp) + require.Equal(t, th.BasicChannel.Name, channel.Name, "names did not match") + + channel, resp = Client.GetChannelByNameForTeamName(th.BasicPrivateChannel.Name, th.BasicTeam.Name, "") + CheckNoError(t, resp) + require.Equal(t, th.BasicPrivateChannel.Name, channel.Name, "names did not match") _, resp = Client.GetChannelByNameForTeamName(th.BasicDeletedChannel.Name, th.BasicTeam.Name, "") CheckNotFoundStatus(t, resp) @@ -1915,6 +1920,14 @@ func TestGetChannelByNameForTeamName(t *testing.T) { CheckNoError(t, resp) require.Equal(t, th.BasicDeletedChannel.Name, channel.Name, "names did not match") + Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id) + _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") + CheckNoError(t, resp) + + Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id) + _, resp = Client.GetChannelByNameForTeamName(th.BasicPrivateChannel.Name, th.BasicTeam.Name, "") + CheckNotFoundStatus(t, resp) + _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, model.NewRandomString(15), "") CheckNotFoundStatus(t, resp) @@ -1928,7 +1941,7 @@ func TestGetChannelByNameForTeamName(t *testing.T) { user := th.CreateUser() Client.Login(user.Email, user.Password) _, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "") - CheckNotFoundStatus(t, resp) + CheckForbiddenStatus(t, resp) } func TestGetChannelMembers(t *testing.T) {