MM-37417: Push notification authz fix. (#18009)

* MM-37417: Push notification authz fix.

* MM-37417: Tests new app method.
Этот коммит содержится в:
Martin Kraft
2021-07-28 13:47:45 -04:00
коммит произвёл GitHub
родитель 285de45988
Коммит 37b1e6d048
7 изменённых файлов: 99 добавлений и 34 удалений

Просмотреть файл

@@ -348,30 +348,12 @@ func getPost(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
post, err := c.App.GetSinglePost(c.Params.PostId)
post, err := c.App.GetPostIfAuthorized(c.Params.PostId, c.AppContext.Session())
if err != nil {
c.Err = err
return
}
channel, err := c.App.GetChannel(post.ChannelId)
if err != nil {
c.Err = err
return
}
if !c.App.SessionHasPermissionToChannel(*c.AppContext.Session(), channel.Id, model.PermissionReadChannel) {
if channel.Type == model.ChannelTypeOpen {
if !c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), channel.TeamId, model.PermissionReadPublicChannel) {
c.SetPermissionError(model.PermissionReadPublicChannel)
return
}
} else {
c.SetPermissionError(model.PermissionReadChannel)
return
}
}
post = c.App.PreparePostForClient(post, false, false)
if c.HandleEtag(post.Etag(), "Get Post", w, r) {
@@ -442,24 +424,11 @@ func getPostThread(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
channel, err := c.App.GetChannel(post.ChannelId)
if err != nil {
if _, err = c.App.GetPostIfAuthorized(post.Id, c.AppContext.Session()); err != nil {
c.Err = err
return
}
if !c.App.SessionHasPermissionToChannel(*c.AppContext.Session(), channel.Id, model.PermissionReadChannel) {
if channel.Type == model.ChannelTypeOpen {
if !c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), channel.TeamId, model.PermissionReadPublicChannel) {
c.SetPermissionError(model.PermissionReadPublicChannel)
return
}
} else {
c.SetPermissionError(model.PermissionReadChannel)
return
}
}
if c.HandleEtag(list.Etag(), "Get Post Thread", w, r) {
return
}

Просмотреть файл

@@ -513,6 +513,11 @@ func pushNotificationAck(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if _, appErr := c.App.GetPostIfAuthorized(ack.PostId, c.AppContext.Session()); appErr != nil {
c.Err = appErr
return
}
if !*c.App.Config().EmailSettings.SendPushNotifications {
c.Err = model.NewAppError("pushNotificationAck", "api.push_notification.disabled.app_error", nil, "", http.StatusNotImplemented)
return

Просмотреть файл

@@ -4,6 +4,7 @@
package api4
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
@@ -733,10 +734,11 @@ func TestServerBusy503(t *testing.T) {
}
func TestPushNotificationAck(t *testing.T) {
th := Setup(t)
th := Setup(t).InitBasic()
api := Init(th.App, th.Server.Router)
session, _ := th.App.GetSession(th.Client.AuthToken)
defer th.TearDown()
t.Run("should return error when the ack body is not passed", func(t *testing.T) {
handler := api.ApiHandler(pushNotificationAck)
resp := httptest.NewRecorder()
@@ -747,4 +749,20 @@ func TestPushNotificationAck(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.NotNil(t, resp.Body)
})
t.Run("should return error when the ack post is not authorized for the user", func(t *testing.T) {
privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.ChannelTypePrivate)
privatePost := th.CreatePostWithClient(th.SystemAdminClient, privateChannel)
handler := api.ApiHandler(pushNotificationAck)
resp := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/api/v4/notifications/ack", nil)
req.Header.Set(model.HeaderAuth, "Bearer "+session.Token)
req.Body = ioutil.NopCloser(bytes.NewBufferString(fmt.Sprintf(`{"id":"123", "is_id_loaded":true, "post_id":"%s"}`, privatePost.Id)))
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusForbidden, resp.Code)
fmt.Printf("DEBUG/resp.Body: %+v\n", resp.Body)
assert.NotNil(t, resp.Body)
})
}