fix reaction's name validation with + sign in it (#6221)

Этот коммит содержится в:
Saturnino Abril
2017-04-26 23:11:32 +09:00
коммит произвёл Harrison Healey
родитель f9502ff14b
Коммит 6fa7082833
5 изменённых файлов: 62 добавлений и 18 удалений

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

@@ -90,7 +90,7 @@ type Routes struct {
Emojis *mux.Router // 'api/v4/emoji' Emojis *mux.Router // 'api/v4/emoji'
Emoji *mux.Router // 'api/v4/emoji/{emoji_id:[A-Za-z0-9]+}' Emoji *mux.Router // 'api/v4/emoji/{emoji_id:[A-Za-z0-9]+}'
ReactionByNameForPostForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}/reactions/{emoji_name:[A-Za-z0-9_-]+}' ReactionByNameForPostForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}/reactions/{emoji_name:[A-Za-z0-9_-+]+}'
Webrtc *mux.Router // 'api/v4/webrtc' Webrtc *mux.Router // 'api/v4/webrtc'
} }
@@ -170,7 +170,7 @@ func InitApi(full bool) {
BaseRoutes.Emojis = BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter() BaseRoutes.Emojis = BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter()
BaseRoutes.Emoji = BaseRoutes.Emojis.PathPrefix("/{emoji_id:[A-Za-z0-9]+}").Subrouter() BaseRoutes.Emoji = BaseRoutes.Emojis.PathPrefix("/{emoji_id:[A-Za-z0-9]+}").Subrouter()
BaseRoutes.ReactionByNameForPostForUser = BaseRoutes.PostForUser.PathPrefix("/reactions/{emoji_name:[A-Za-z0-9_-]+}").Subrouter() BaseRoutes.ReactionByNameForPostForUser = BaseRoutes.PostForUser.PathPrefix("/reactions/{emoji_name:[A-Za-z0-9\\_\\-\\+]+}").Subrouter()
BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter() BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()

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

@@ -6,6 +6,7 @@ package api4
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"regexp"
"strings" "strings"
"time" "time"
@@ -504,7 +505,9 @@ func (c *Context) RequireEmojiName() *Context {
return c return c
} }
if len(c.Params.EmojiName) == 0 || len(c.Params.EmojiName) > 64 || !model.IsValidAlphaNumHyphenUnderscore(c.Params.EmojiName, false) { validName := regexp.MustCompile(`^[a-zA-Z0-9\-\+_]+$`)
if len(c.Params.EmojiName) == 0 || len(c.Params.EmojiName) > 64 || !validName.MatchString(c.Params.EmojiName) {
c.SetInvalidUrlParam("emoji_name") c.SetInvalidUrlParam("emoji_name")
} }

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

@@ -70,6 +70,20 @@ func TestSaveReaction(t *testing.T) {
t.Fatal("should have save multiple reactions") t.Fatal("should have save multiple reactions")
} }
// saving special case
reaction.EmojiName = "+1"
rr, resp = Client.SaveReaction(reaction)
CheckNoError(t, resp)
if rr.EmojiName != reaction.EmojiName {
t.Fatal("EmojiName did not match")
}
if reactions, err := app.GetReactionsForPost(postId); err != nil && len(reactions) != 3 {
t.Fatal("should have save multiple reactions")
}
reaction.PostId = GenerateTestId() reaction.PostId = GenerateTestId()
_, resp = Client.SaveReaction(reaction) _, resp = Client.SaveReaction(reaction)
@@ -244,22 +258,41 @@ func TestDeleteReaction(t *testing.T) {
t.Fatal("should have deleted 1 reaction only") t.Fatal("should have deleted 1 reaction only")
} }
// deleting a reaction made by another user // deleting one reaction of name +1
r3 := &model.Reaction{ r3 := &model.Reaction{
UserId: userId,
PostId: postId,
EmojiName: "+1",
}
app.SaveReactionForPost(r3)
if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 2 {
t.Fatal("didn't save reactions correctly")
}
_, resp = Client.DeleteReaction(r3)
CheckNoError(t, resp)
if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 1 || *reactions[0] != *r1 {
t.Fatal("should have deleted 1 reaction only")
}
// deleting a reaction made by another user
r4 := &model.Reaction{
UserId: user2Id, UserId: user2Id,
PostId: postId, PostId: postId,
EmojiName: "smile_", EmojiName: "smile_",
} }
th.LoginBasic2() th.LoginBasic2()
app.SaveReactionForPost(r3) app.SaveReactionForPost(r4)
if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 2 { if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 2 {
t.Fatal("didn't save reaction correctly") t.Fatal("didn't save reaction correctly")
} }
th.LoginBasic() th.LoginBasic()
ok, resp = Client.DeleteReaction(r3) ok, resp = Client.DeleteReaction(r4)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
if ok { if ok {
@@ -310,7 +343,7 @@ func TestDeleteReaction(t *testing.T) {
_, resp = th.SystemAdminClient.DeleteReaction(r1) _, resp = th.SystemAdminClient.DeleteReaction(r1)
CheckNoError(t, resp) CheckNoError(t, resp)
_, resp = th.SystemAdminClient.DeleteReaction(r3) _, resp = th.SystemAdminClient.DeleteReaction(r4)
CheckNoError(t, resp) CheckNoError(t, resp)
if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 0 { if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 0 {

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

@@ -6,6 +6,7 @@ package model
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"regexp"
) )
type Reaction struct { type Reaction struct {
@@ -60,7 +61,9 @@ func (o *Reaction) IsValid() *AppError {
return NewLocAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId) return NewLocAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId)
} }
if len(o.EmojiName) == 0 || len(o.EmojiName) > 64 || !IsValidAlphaNumHyphenUnderscore(o.EmojiName, false) { validName := regexp.MustCompile(`^[a-zA-Z0-9\-\+_]+$`)
if len(o.EmojiName) == 0 || len(o.EmojiName) > 64 || !validName.MatchString(o.EmojiName) {
return NewLocAppError("Reaction.IsValid", "model.reaction.is_valid.emoji_name.app_error", nil, "emoji_name="+o.EmojiName) return NewLocAppError("Reaction.IsValid", "model.reaction.is_valid.emoji_name.app_error", nil, "emoji_name="+o.EmojiName)
} }

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

@@ -42,16 +42,6 @@ func TestReactionIsValid(t *testing.T) {
} }
reaction.PostId = NewId() reaction.PostId = NewId()
reaction.EmojiName = ""
if err := reaction.IsValid(); err == nil {
t.Fatal("emoji name should be invalid")
}
reaction.EmojiName = strings.Repeat("a", 65)
if err := reaction.IsValid(); err == nil {
t.Fatal("emoji name should be invalid")
}
reaction.EmojiName = strings.Repeat("a", 64) reaction.EmojiName = strings.Repeat("a", 64)
if err := reaction.IsValid(); err != nil { if err := reaction.IsValid(); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -67,11 +57,26 @@ func TestReactionIsValid(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
reaction.EmojiName = "+1"
if err := reaction.IsValid(); err != nil {
t.Fatal(err)
}
reaction.EmojiName = "emoji:" reaction.EmojiName = "emoji:"
if err := reaction.IsValid(); err == nil { if err := reaction.IsValid(); err == nil {
t.Fatal(err) t.Fatal(err)
} }
reaction.EmojiName = ""
if err := reaction.IsValid(); err == nil {
t.Fatal("emoji name should be invalid")
}
reaction.EmojiName = strings.Repeat("a", 65)
if err := reaction.IsValid(); err == nil {
t.Fatal("emoji name should be invalid")
}
reaction.CreateAt = 0 reaction.CreateAt = 0
if err := reaction.IsValid(); err == nil { if err := reaction.IsValid(); err == nil {
t.Fatal("create at should be invalid") t.Fatal("create at should be invalid")