fix client4 post sanitization (#8219)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
f28ee8d7c8
Коммит
cf929476bd
@@ -1729,7 +1729,7 @@ func (c *Client4) RemoveUserFromChannel(channelId, userId string) (bool, *Respon
|
|||||||
|
|
||||||
// CreatePost creates a post based on the provided post struct.
|
// CreatePost creates a post based on the provided post struct.
|
||||||
func (c *Client4) CreatePost(post *Post) (*Post, *Response) {
|
func (c *Client4) CreatePost(post *Post) (*Post, *Response) {
|
||||||
if r, err := c.DoApiPost(c.GetPostsRoute(), post.ToJson()); err != nil {
|
if r, err := c.DoApiPost(c.GetPostsRoute(), post.ToUnsanitizedJson()); err != nil {
|
||||||
return nil, BuildErrorResponse(r, err)
|
return nil, BuildErrorResponse(r, err)
|
||||||
} else {
|
} else {
|
||||||
defer closeBody(r)
|
defer closeBody(r)
|
||||||
@@ -1739,7 +1739,7 @@ func (c *Client4) CreatePost(post *Post) (*Post, *Response) {
|
|||||||
|
|
||||||
// UpdatePost updates a post based on the provided post struct.
|
// UpdatePost updates a post based on the provided post struct.
|
||||||
func (c *Client4) UpdatePost(postId string, post *Post) (*Post, *Response) {
|
func (c *Client4) UpdatePost(postId string, post *Post) (*Post, *Response) {
|
||||||
if r, err := c.DoApiPut(c.GetPostRoute(postId), post.ToJson()); err != nil {
|
if r, err := c.DoApiPut(c.GetPostRoute(postId), post.ToUnsanitizedJson()); err != nil {
|
||||||
return nil, BuildErrorResponse(r, err)
|
return nil, BuildErrorResponse(r, err)
|
||||||
} else {
|
} else {
|
||||||
defer closeBody(r)
|
defer closeBody(r)
|
||||||
|
|||||||
58
model/client4_test.go
Обычный файл
58
model/client4_test.go
Обычный файл
@@ -0,0 +1,58 @@
|
|||||||
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://github.com/mattermost/mattermost-server/issues/8205
|
||||||
|
func TestClient4CreatePost(t *testing.T) {
|
||||||
|
post := &Post{
|
||||||
|
Props: map[string]interface{}{
|
||||||
|
"attachments": []*SlackAttachment{
|
||||||
|
&SlackAttachment{
|
||||||
|
Actions: []*PostAction{
|
||||||
|
&PostAction{
|
||||||
|
Integration: &PostActionIntegration{
|
||||||
|
Context: map[string]interface{}{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
URL: "http://foo.com",
|
||||||
|
},
|
||||||
|
Name: "Foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
attachments := PostFromJson(r.Body).Attachments()
|
||||||
|
assert.Equal(t, []*SlackAttachment{
|
||||||
|
&SlackAttachment{
|
||||||
|
Actions: []*PostAction{
|
||||||
|
&PostAction{
|
||||||
|
Integration: &PostActionIntegration{
|
||||||
|
Context: map[string]interface{}{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
URL: "http://foo.com",
|
||||||
|
},
|
||||||
|
Name: "Foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, attachments)
|
||||||
|
}))
|
||||||
|
|
||||||
|
client := NewAPIv4Client(server.URL)
|
||||||
|
_, resp := client.CreatePost(post)
|
||||||
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
}
|
||||||
@@ -122,12 +122,13 @@ type PostActionIntegrationResponse struct {
|
|||||||
func (o *Post) ToJson() string {
|
func (o *Post) ToJson() string {
|
||||||
copy := *o
|
copy := *o
|
||||||
copy.StripActionIntegrations()
|
copy.StripActionIntegrations()
|
||||||
b, err := json.Marshal(©)
|
b, _ := json.Marshal(©)
|
||||||
if err != nil {
|
return string(b)
|
||||||
return ""
|
}
|
||||||
} else {
|
|
||||||
return string(b)
|
func (o *Post) ToUnsanitizedJson() string {
|
||||||
}
|
b, _ := json.Marshal(o)
|
||||||
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PostFromJson(data io.Reader) *Post {
|
func PostFromJson(data io.Reader) *Post {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user