From 882b0324b580873808a4dcb7c328d655851a6150 Mon Sep 17 00:00:00 2001 From: Shibasis Patel Date: Thu, 7 May 2020 22:57:35 +0530 Subject: [PATCH] MM_22682_Centralize_ID_Validation (#14237) Co-authored-by: mattermod --- api4/channel.go | 14 ++++----- api4/command.go | 2 +- api4/reaction.go | 2 +- api4/team.go | 8 ++--- app/license.go | 2 +- migrations/advanced_permissions_phase_2.go | 6 ++-- model/authorize.go | 6 ++-- model/channel.go | 2 +- model/channel_member.go | 4 +-- model/client4.go | 4 +-- model/cluster_discovery.go | 2 +- model/command.go | 6 ++-- model/command_webhook.go | 12 ++++---- model/compliance.go | 2 +- model/emoji.go | 2 +- model/file_info.go | 6 ++-- model/group.go | 2 +- model/incoming_webhook.go | 8 ++--- model/job.go | 2 +- model/license.go | 2 +- model/oauth.go | 4 +-- model/outgoing_webhook.go | 8 ++--- model/post.go | 10 +++---- model/preference.go | 2 +- model/reaction.go | 4 +-- model/role.go | 2 +- model/scheme.go | 2 +- model/team.go | 2 +- model/team_member.go | 4 +-- model/terms_of_service.go | 4 +-- model/user.go | 2 +- model/user_access_token.go | 4 +-- model/user_terms_of_service.go | 4 +-- web/context.go | 34 +++++++++++----------- web/oauth.go | 4 +-- wsapi/user.go | 2 +- 36 files changed, 93 insertions(+), 93 deletions(-) diff --git a/api4/channel.go b/api4/channel.go index ab78f01082..a34606feb9 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -428,7 +428,7 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) { } for _, id := range userIds { - if len(id) != 26 { + if !model.IsValidId(id) { c.SetInvalidParam("user_id") return } @@ -507,7 +507,7 @@ func createGroupChannel(c *Context, w http.ResponseWriter, r *http.Request) { found := false for _, id := range userIds { - if len(id) != 26 { + if !model.IsValidId(id) { c.SetInvalidParam("user_id") return } @@ -774,7 +774,7 @@ func getPublicChannelsByIdsForTeam(c *Context, w http.ResponseWriter, r *http.Re } for _, cid := range channelIds { - if len(cid) != 26 { + if !model.IsValidId(cid) { c.SetInvalidParam("channel_id") return } @@ -1359,7 +1359,7 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { props := model.StringInterfaceFromJson(r.Body) userId, ok := props["user_id"].(string) - if !ok || len(userId) != 26 { + if !ok || !model.IsValidId(userId) { c.SetInvalidParam("user_id") return } @@ -1370,7 +1370,7 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { } postRootId, ok := props["post_root_id"].(string) - if ok && len(postRootId) != 0 && len(postRootId) != 26 { + if ok && len(postRootId) != 0 && !model.IsValidId(postRootId) { c.SetInvalidParam("post_root_id") return } @@ -1539,7 +1539,7 @@ func updateChannelScheme(c *Context, w http.ResponseWriter, r *http.Request) { } schemeID := model.SchemeIDFromJson(r.Body) - if schemeID == nil || len(*schemeID) != 26 { + if schemeID == nil || !model.IsValidId(*schemeID) { c.SetInvalidParam("scheme_id") return } @@ -1606,7 +1606,7 @@ func channelMembersMinusGroupMembers(c *Context, w http.ResponseWriter, r *http. groupIDs := []string{} for _, gid := range strings.Split(c.Params.GroupIDs, ",") { - if len(gid) != 26 { + if !model.IsValidId(gid) { c.SetInvalidParam("group_ids") return } diff --git a/api4/command.go b/api4/command.go index 49904276e9..0a317d7cd2 100644 --- a/api4/command.go +++ b/api4/command.go @@ -296,7 +296,7 @@ func executeCommand(c *Context, w http.ResponseWriter, r *http.Request) { return } - if len(commandArgs.Command) <= 1 || strings.Index(commandArgs.Command, "/") != 0 || len(commandArgs.ChannelId) != 26 { + if len(commandArgs.Command) <= 1 || strings.Index(commandArgs.Command, "/") != 0 || !model.IsValidId(commandArgs.ChannelId) { c.Err = model.NewAppError("executeCommand", "api.command.execute_command.start.app_error", nil, "", http.StatusBadRequest) return } diff --git a/api4/reaction.go b/api4/reaction.go index 96d8a3bfd3..70c00ebf97 100644 --- a/api4/reaction.go +++ b/api4/reaction.go @@ -23,7 +23,7 @@ func saveReaction(c *Context, w http.ResponseWriter, r *http.Request) { return } - if len(reaction.UserId) != 26 || len(reaction.PostId) != 26 || len(reaction.EmojiName) == 0 || len(reaction.EmojiName) > model.EMOJI_NAME_MAX_LENGTH { + if !model.IsValidId(reaction.UserId) || !model.IsValidId(reaction.PostId) || len(reaction.EmojiName) == 0 || len(reaction.EmojiName) > model.EMOJI_NAME_MAX_LENGTH { c.Err = model.NewAppError("saveReaction", "api.reaction.save_reaction.invalid.app_error", nil, "", http.StatusBadRequest) return } diff --git a/api4/team.go b/api4/team.go index 3e1e164f8d..855cee8911 100644 --- a/api4/team.go +++ b/api4/team.go @@ -570,7 +570,7 @@ func addTeamMember(c *Context, w http.ResponseWriter, r *http.Request) { return } - if len(member.UserId) != 26 { + if !model.IsValidId(member.UserId) { c.SetInvalidParam("user_id") return } @@ -737,7 +737,7 @@ func addTeamMembers(c *Context, w http.ResponseWriter, r *http.Request) { return } - if len(member.UserId) != 26 { + if !model.IsValidId(member.UserId) { c.SetInvalidParam("user_id") return } @@ -1447,7 +1447,7 @@ func updateTeamScheme(c *Context, w http.ResponseWriter, r *http.Request) { } schemeID := model.SchemeIDFromJson(r.Body) - if schemeID == nil || (len(*schemeID) != 26 && *schemeID != "") { + if schemeID == nil || (!model.IsValidId(*schemeID) && *schemeID != "") { c.SetInvalidParam("scheme_id") return } @@ -1513,7 +1513,7 @@ func teamMembersMinusGroupMembers(c *Context, w http.ResponseWriter, r *http.Req groupIDs := []string{} for _, gid := range strings.Split(c.Params.GroupIDs, ",") { - if len(gid) != 26 { + if !model.IsValidId(gid) { c.SetInvalidParam("group_ids") return } diff --git a/app/license.go b/app/license.go index 2dc9b40d32..d09c2abd4d 100644 --- a/app/license.go +++ b/app/license.go @@ -19,7 +19,7 @@ func (a *App) LoadLicense() { licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID] } - if len(licenseId) != 26 { + if !model.IsValidId(licenseId) { // Lets attempt to load the file from disk since it was missing from the DB license, licenseBytes := utils.GetAndValidateLicenseFileFromDisk(*a.Config().ServiceSettings.LicenseFileLocation) diff --git a/migrations/advanced_permissions_phase_2.go b/migrations/advanced_permissions_phase_2.go index 1630c9f243..cdbccb6ace 100644 --- a/migrations/advanced_permissions_phase_2.go +++ b/migrations/advanced_permissions_phase_2.go @@ -31,15 +31,15 @@ func AdvancedPermissionsPhase2ProgressFromJson(data io.Reader) *AdvancedPermissi } func (p *AdvancedPermissionsPhase2Progress) IsValid() bool { - if len(p.LastChannelId) != 26 { + if !model.IsValidId(p.LastChannelId) { return false } - if len(p.LastTeamId) != 26 { + if !model.IsValidId(p.LastTeamId) { return false } - if len(p.LastUserId) != 26 { + if !model.IsValidId(p.LastUserId) { return false } diff --git a/model/authorize.go b/model/authorize.go index 15b4d9c82e..0191a6705b 100644 --- a/model/authorize.go +++ b/model/authorize.go @@ -39,11 +39,11 @@ type AuthorizeRequest struct { // correctly. func (ad *AuthData) IsValid() *AppError { - if len(ad.ClientId) != 26 { + if !IsValidId(ad.ClientId) { return NewAppError("AuthData.IsValid", "model.authorize.is_valid.client_id.app_error", nil, "", http.StatusBadRequest) } - if len(ad.UserId) != 26 { + if !IsValidId(ad.UserId) { return NewAppError("AuthData.IsValid", "model.authorize.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) } @@ -78,7 +78,7 @@ func (ad *AuthData) IsValid() *AppError { // correctly. func (ar *AuthorizeRequest) IsValid() *AppError { - if len(ar.ClientId) != 26 { + if !IsValidId(ar.ClientId) { return NewAppError("AuthData.IsValid", "model.authorize.is_valid.client_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/channel.go b/model/channel.go index ae95f2eb78..6a84b355bc 100644 --- a/model/channel.go +++ b/model/channel.go @@ -198,7 +198,7 @@ func (o *Channel) Etag() string { } func (o *Channel) IsValid() *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("Channel.IsValid", "model.channel.is_valid.id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/channel_member.go b/model/channel_member.go index ea559d3dc5..e38bfffe48 100644 --- a/model/channel_member.go +++ b/model/channel_member.go @@ -113,11 +113,11 @@ func ChannelMemberFromJson(data io.Reader) *ChannelMember { func (o *ChannelMember) IsValid() *AppError { - if len(o.ChannelId) != 26 { + if !IsValidId(o.ChannelId) { return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.UserId) != 26 { + if !IsValidId(o.UserId) { return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/client4.go b/model/client4.go index 230efb1995..1a27400e96 100644 --- a/model/client4.go +++ b/model/client4.go @@ -2708,7 +2708,7 @@ func (c *Client4) GetFlaggedPostsForUser(userId string, page int, perPage int) ( // GetFlaggedPostsForUserInTeam returns flagged posts in team of a user based on user id string. func (c *Client4) GetFlaggedPostsForUserInTeam(userId string, teamId string, page int, perPage int) (*PostList, *Response) { - if len(teamId) == 0 || len(teamId) != 26 { + if !IsValidId(teamId) { return nil, &Response{StatusCode: http.StatusBadRequest, Error: NewAppError("GetFlaggedPostsForUserInTeam", "model.client.get_flagged_posts_in_team.missing_parameter.app_error", nil, "", http.StatusBadRequest)} } @@ -2723,7 +2723,7 @@ func (c *Client4) GetFlaggedPostsForUserInTeam(userId string, teamId string, pag // GetFlaggedPostsForUserInChannel returns flagged posts in channel of a user based on user id string. func (c *Client4) GetFlaggedPostsForUserInChannel(userId string, channelId string, page int, perPage int) (*PostList, *Response) { - if len(channelId) == 0 || len(channelId) != 26 { + if !IsValidId(channelId) { return nil, &Response{StatusCode: http.StatusBadRequest, Error: NewAppError("GetFlaggedPostsForUserInChannel", "model.client.get_flagged_posts_in_channel.missing_parameter.app_error", nil, "", http.StatusBadRequest)} } diff --git a/model/cluster_discovery.go b/model/cluster_discovery.go index bd045c6789..f6c9275a9d 100644 --- a/model/cluster_discovery.go +++ b/model/cluster_discovery.go @@ -89,7 +89,7 @@ func FilterClusterDiscovery(vs []*ClusterDiscovery, f func(*ClusterDiscovery) bo } func (o *ClusterDiscovery) IsValid() *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/command.go b/model/command.go index 0c99486dd9..73620c54d0 100644 --- a/model/command.go +++ b/model/command.go @@ -61,7 +61,7 @@ func CommandListFromJson(data io.Reader) []*Command { func (o *Command) IsValid() *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("Command.IsValid", "model.command.is_valid.id.app_error", nil, "", http.StatusBadRequest) } @@ -77,11 +77,11 @@ func (o *Command) IsValid() *AppError { return NewAppError("Command.IsValid", "model.command.is_valid.update_at.app_error", nil, "", http.StatusBadRequest) } - if len(o.CreatorId) != 26 { + if !IsValidId(o.CreatorId) { return NewAppError("Command.IsValid", "model.command.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.TeamId) != 26 { + if !IsValidId(o.TeamId) { return NewAppError("Command.IsValid", "model.command.is_valid.team_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/command_webhook.go b/model/command_webhook.go index 035a4004db..42a16cc72b 100644 --- a/model/command_webhook.go +++ b/model/command_webhook.go @@ -33,7 +33,7 @@ func (o *CommandWebhook) PreSave() { } func (o *CommandWebhook) IsValid() *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("CommandWebhook.IsValid", "model.command_hook.id.app_error", nil, "", http.StatusBadRequest) } @@ -41,23 +41,23 @@ func (o *CommandWebhook) IsValid() *AppError { return NewAppError("CommandWebhook.IsValid", "model.command_hook.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest) } - if len(o.CommandId) != 26 { + if !IsValidId(o.CommandId) { return NewAppError("CommandWebhook.IsValid", "model.command_hook.command_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.UserId) != 26 { + if !IsValidId(o.UserId) { return NewAppError("CommandWebhook.IsValid", "model.command_hook.user_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.ChannelId) != 26 { + if !IsValidId(o.ChannelId) { return NewAppError("CommandWebhook.IsValid", "model.command_hook.channel_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.RootId) != 0 && len(o.RootId) != 26 { + if len(o.RootId) != 0 && !IsValidId(o.RootId) { return NewAppError("CommandWebhook.IsValid", "model.command_hook.root_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.ParentId) != 0 && len(o.ParentId) != 26 { + if len(o.ParentId) != 0 && !IsValidId(o.ParentId) { return NewAppError("CommandWebhook.IsValid", "model.command_hook.parent_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/compliance.go b/model/compliance.go index cf1077e2e7..a86087c1ff 100644 --- a/model/compliance.go +++ b/model/compliance.go @@ -71,7 +71,7 @@ func (c *Compliance) JobName() string { func (c *Compliance) IsValid() *AppError { - if len(c.Id) != 26 { + if !IsValidId(c.Id) { return NewAppError("Compliance.IsValid", "model.compliance.is_valid.id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/emoji.go b/model/emoji.go index 67cb300e58..aeee9b3838 100644 --- a/model/emoji.go +++ b/model/emoji.go @@ -37,7 +37,7 @@ func GetSystemEmojiId(emojiName string) (string, bool) { } func (emoji *Emoji) IsValid() *AppError { - if len(emoji.Id) != 26 { + if !IsValidId(emoji.Id) { return NewAppError("Emoji.IsValid", "model.emoji.id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/file_info.go b/model/file_info.go index 20134cf456..8a3a5cc0cd 100644 --- a/model/file_info.go +++ b/model/file_info.go @@ -102,15 +102,15 @@ func (fi *FileInfo) PreSave() { } func (fi *FileInfo) IsValid() *AppError { - if len(fi.Id) != 26 { + if !IsValidId(fi.Id) { return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.id.app_error", nil, "", http.StatusBadRequest) } - if len(fi.CreatorId) != 26 && fi.CreatorId != "nouser" { + if !IsValidId(fi.CreatorId) && fi.CreatorId != "nouser" { return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.user_id.app_error", nil, "id="+fi.Id, http.StatusBadRequest) } - if len(fi.PostId) != 0 && len(fi.PostId) != 26 { + if len(fi.PostId) != 0 && !IsValidId(fi.PostId) { return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.post_id.app_error", nil, "id="+fi.Id, http.StatusBadRequest) } diff --git a/model/group.go b/model/group.go index 25a93cebf9..9892a6fcff 100644 --- a/model/group.go +++ b/model/group.go @@ -142,7 +142,7 @@ func (group *Group) requiresRemoteId() bool { } func (group *Group) IsValidForUpdate() *AppError { - if len(group.Id) != 26 { + if !IsValidId(group.Id) { return NewAppError("Group.IsValidForUpdate", "model.group.id.app_error", nil, "", http.StatusBadRequest) } if group.CreateAt == 0 { diff --git a/model/incoming_webhook.go b/model/incoming_webhook.go index 957d2152d0..78f1e4e896 100644 --- a/model/incoming_webhook.go +++ b/model/incoming_webhook.go @@ -65,7 +65,7 @@ func IncomingWebhookListFromJson(data io.Reader) []*IncomingWebhook { func (o *IncomingWebhook) IsValid() *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("IncomingWebhook.IsValid", "model.incoming_hook.id.app_error", nil, "", http.StatusBadRequest) } @@ -78,15 +78,15 @@ func (o *IncomingWebhook) IsValid() *AppError { return NewAppError("IncomingWebhook.IsValid", "model.incoming_hook.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest) } - if len(o.UserId) != 26 { + if !IsValidId(o.UserId) { return NewAppError("IncomingWebhook.IsValid", "model.incoming_hook.user_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.ChannelId) != 26 { + if !IsValidId(o.ChannelId) { return NewAppError("IncomingWebhook.IsValid", "model.incoming_hook.channel_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.TeamId) != 26 { + if !IsValidId(o.TeamId) { return NewAppError("IncomingWebhook.IsValid", "model.incoming_hook.team_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/job.go b/model/job.go index aa267a67d0..1320f75e44 100644 --- a/model/job.go +++ b/model/job.go @@ -40,7 +40,7 @@ type Job struct { } func (j *Job) IsValid() *AppError { - if len(j.Id) != 26 { + if !IsValidId(j.Id) { return NewAppError("Job.IsValid", "model.job.is_valid.id.app_error", nil, "id="+j.Id, http.StatusBadRequest) } diff --git a/model/license.go b/model/license.go index 6a7c75f611..ba5b2f3dde 100644 --- a/model/license.go +++ b/model/license.go @@ -236,7 +236,7 @@ func LicenseFromJson(data io.Reader) *License { } func (lr *LicenseRecord) IsValid() *AppError { - if len(lr.Id) != 26 { + if !IsValidId(lr.Id) { return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/oauth.go b/model/oauth.go index da2b6da042..4a345a6e08 100644 --- a/model/oauth.go +++ b/model/oauth.go @@ -37,7 +37,7 @@ type OAuthApp struct { // correctly. func (a *OAuthApp) IsValid() *AppError { - if len(a.Id) != 26 { + if !IsValidId(a.Id) { return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.app_id.app_error", nil, "", http.StatusBadRequest) } @@ -49,7 +49,7 @@ func (a *OAuthApp) IsValid() *AppError { return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.update_at.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) } - if len(a.CreatorId) != 26 { + if !IsValidId(a.CreatorId) { return NewAppError("OAuthApp.IsValid", "model.oauth.is_valid.creator_id.app_error", nil, "app_id="+a.Id, http.StatusBadRequest) } diff --git a/model/outgoing_webhook.go b/model/outgoing_webhook.go index c7a8a577ff..f4278de0d0 100644 --- a/model/outgoing_webhook.go +++ b/model/outgoing_webhook.go @@ -117,7 +117,7 @@ func OutgoingWebhookResponseFromJson(data io.Reader) (*OutgoingWebhookResponse, func (o *OutgoingWebhook) IsValid() *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.id.app_error", nil, "", http.StatusBadRequest) } @@ -133,15 +133,15 @@ func (o *OutgoingWebhook) IsValid() *AppError { return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest) } - if len(o.CreatorId) != 26 { + if !IsValidId(o.CreatorId) { return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.ChannelId) != 0 && len(o.ChannelId) != 26 { + if len(o.ChannelId) != 0 && !IsValidId(o.ChannelId) { return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.TeamId) != 26 { + if !IsValidId(o.TeamId) { return NewAppError("OutgoingWebhook.IsValid", "model.outgoing_hook.is_valid.team_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/post.go b/model/post.go index d932ff2c10..817ca08a72 100644 --- a/model/post.go +++ b/model/post.go @@ -241,7 +241,7 @@ func (o *Post) Etag() string { } func (o *Post) IsValid(maxPostSize int) *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("Post.IsValid", "model.post.is_valid.id.app_error", nil, "", http.StatusBadRequest) } @@ -253,19 +253,19 @@ func (o *Post) IsValid(maxPostSize int) *AppError { return NewAppError("Post.IsValid", "model.post.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest) } - if len(o.UserId) != 26 { + if !IsValidId(o.UserId) { return NewAppError("Post.IsValid", "model.post.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.ChannelId) != 26 { + if !IsValidId(o.ChannelId) { return NewAppError("Post.IsValid", "model.post.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest) } - if !(len(o.RootId) == 26 || len(o.RootId) == 0) { + if !(IsValidId(o.RootId) || len(o.RootId) == 0) { return NewAppError("Post.IsValid", "model.post.is_valid.root_id.app_error", nil, "", http.StatusBadRequest) } - if !(len(o.ParentId) == 26 || len(o.ParentId) == 0) { + if !(IsValidId(o.ParentId) || len(o.ParentId) == 0) { return NewAppError("Post.IsValid", "model.post.is_valid.parent_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/preference.go b/model/preference.go index 4d4abde87a..346f88f832 100644 --- a/model/preference.go +++ b/model/preference.go @@ -68,7 +68,7 @@ func PreferenceFromJson(data io.Reader) *Preference { } func (o *Preference) IsValid() *AppError { - if len(o.UserId) != 26 { + if !IsValidId(o.UserId) { return NewAppError("Preference.IsValid", "model.preference.is_valid.id.app_error", nil, "user_id="+o.UserId, http.StatusBadRequest) } diff --git a/model/reaction.go b/model/reaction.go index dfa079665f..50879c67b4 100644 --- a/model/reaction.go +++ b/model/reaction.go @@ -64,11 +64,11 @@ func ReactionsFromJson(data io.Reader) []*Reaction { } func (o *Reaction) IsValid() *AppError { - if len(o.UserId) != 26 { + if !IsValidId(o.UserId) { return NewAppError("Reaction.IsValid", "model.reaction.is_valid.user_id.app_error", nil, "user_id="+o.UserId, http.StatusBadRequest) } - if len(o.PostId) != 26 { + if !IsValidId(o.PostId) { return NewAppError("Reaction.IsValid", "model.reaction.is_valid.post_id.app_error", nil, "post_id="+o.PostId, http.StatusBadRequest) } diff --git a/model/role.go b/model/role.go index 337db748f4..31ebfed1b9 100644 --- a/model/role.go +++ b/model/role.go @@ -327,7 +327,7 @@ func (r *Role) RolePatchFromChannelModerationsPatch(channelModerationsPatch []*C } func (r *Role) IsValid() bool { - if len(r.Id) != 26 { + if !IsValidId(r.Id) { return false } diff --git a/model/scheme.go b/model/scheme.go index 3be3b3ab63..630f14a6d6 100644 --- a/model/scheme.go +++ b/model/scheme.go @@ -107,7 +107,7 @@ func SchemesFromJson(data io.Reader) []*Scheme { } func (scheme *Scheme) IsValid() bool { - if len(scheme.Id) != 26 { + if !IsValidId(scheme.Id) { return false } diff --git a/model/team.go b/model/team.go index ba92b51d22..ca0f94fee7 100644 --- a/model/team.go +++ b/model/team.go @@ -136,7 +136,7 @@ func (o *Team) Etag() string { func (o *Team) IsValid() *AppError { - if len(o.Id) != 26 { + if !IsValidId(o.Id) { return NewAppError("Team.IsValid", "model.team.is_valid.id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/team_member.go b/model/team_member.go index 1eab55a231..b747f17cd1 100644 --- a/model/team_member.go +++ b/model/team_member.go @@ -167,11 +167,11 @@ func TeamsUnreadFromJson(data io.Reader) []*TeamUnread { func (o *TeamMember) IsValid() *AppError { - if len(o.TeamId) != 26 { + if !IsValidId(o.TeamId) { return NewAppError("TeamMember.IsValid", "model.team_member.is_valid.team_id.app_error", nil, "", http.StatusBadRequest) } - if len(o.UserId) != 26 { + if !IsValidId(o.UserId) { return NewAppError("TeamMember.IsValid", "model.team_member.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/terms_of_service.go b/model/terms_of_service.go index 3eb8283cd4..8ce5d3504b 100644 --- a/model/terms_of_service.go +++ b/model/terms_of_service.go @@ -21,7 +21,7 @@ type TermsOfService struct { } func (t *TermsOfService) IsValid() *AppError { - if len(t.Id) != 26 { + if !IsValidId(t.Id) { return InvalidTermsOfServiceError("id", "") } @@ -29,7 +29,7 @@ func (t *TermsOfService) IsValid() *AppError { return InvalidTermsOfServiceError("create_at", t.Id) } - if len(t.UserId) != 26 { + if !IsValidId(t.UserId) { return InvalidTermsOfServiceError("user_id", t.Id) } diff --git a/model/user.go b/model/user.go index d499d774a1..91ec785954 100644 --- a/model/user.go +++ b/model/user.go @@ -237,7 +237,7 @@ func (u *User) DeepCopy() *User { // correctly. func (u *User) IsValid() *AppError { - if len(u.Id) != 26 { + if !IsValidId(u.Id) { return InvalidUserError("id", "") } diff --git a/model/user_access_token.go b/model/user_access_token.go index 834391fc92..f458a6d92c 100644 --- a/model/user_access_token.go +++ b/model/user_access_token.go @@ -18,7 +18,7 @@ type UserAccessToken struct { } func (t *UserAccessToken) IsValid() *AppError { - if len(t.Id) != 26 { + if !IsValidId(t.Id) { return NewAppError("UserAccessToken.IsValid", "model.user_access_token.is_valid.id.app_error", nil, "", http.StatusBadRequest) } @@ -26,7 +26,7 @@ func (t *UserAccessToken) IsValid() *AppError { return NewAppError("UserAccessToken.IsValid", "model.user_access_token.is_valid.token.app_error", nil, "", http.StatusBadRequest) } - if len(t.UserId) != 26 { + if !IsValidId(t.UserId) { return NewAppError("UserAccessToken.IsValid", "model.user_access_token.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/user_terms_of_service.go b/model/user_terms_of_service.go index af0954e4a3..9a0f4f18bb 100644 --- a/model/user_terms_of_service.go +++ b/model/user_terms_of_service.go @@ -17,11 +17,11 @@ type UserTermsOfService struct { } func (ut *UserTermsOfService) IsValid() *AppError { - if len(ut.UserId) != 26 { + if !IsValidId(ut.UserId) { return InvalidUserTermsOfServiceError("user_id", ut.UserId) } - if len(ut.TermsOfServiceId) != 26 { + if !IsValidId(ut.TermsOfServiceId) { return InvalidUserTermsOfServiceError("terms_of_service_id", ut.UserId) } diff --git a/web/context.go b/web/context.go index 8e60af9983..68a4bcdda2 100644 --- a/web/context.go +++ b/web/context.go @@ -274,7 +274,7 @@ func (c *Context) RequireUserId() *Context { c.Params.UserId = c.App.Session().UserId } - if len(c.Params.UserId) != 26 { + if !model.IsValidId(c.Params.UserId) { c.SetInvalidUrlParam("user_id") } return c @@ -285,7 +285,7 @@ func (c *Context) RequireTeamId() *Context { return c } - if len(c.Params.TeamId) != 26 { + if !model.IsValidId(c.Params.TeamId) { c.SetInvalidUrlParam("team_id") } return c @@ -307,7 +307,7 @@ func (c *Context) RequireTokenId() *Context { return c } - if len(c.Params.TokenId) != 26 { + if !model.IsValidId(c.Params.TokenId) { c.SetInvalidUrlParam("token_id") } return c @@ -318,7 +318,7 @@ func (c *Context) RequireChannelId() *Context { return c } - if len(c.Params.ChannelId) != 26 { + if !model.IsValidId(c.Params.ChannelId) { c.SetInvalidUrlParam("channel_id") } return c @@ -341,7 +341,7 @@ func (c *Context) RequirePostId() *Context { return c } - if len(c.Params.PostId) != 26 { + if !model.IsValidId(c.Params.PostId) { c.SetInvalidUrlParam("post_id") } return c @@ -352,7 +352,7 @@ func (c *Context) RequireAppId() *Context { return c } - if len(c.Params.AppId) != 26 { + if !model.IsValidId(c.Params.AppId) { c.SetInvalidUrlParam("app_id") } return c @@ -363,7 +363,7 @@ func (c *Context) RequireFileId() *Context { return c } - if len(c.Params.FileId) != 26 { + if !model.IsValidId(c.Params.FileId) { c.SetInvalidUrlParam("file_id") } @@ -399,7 +399,7 @@ func (c *Context) RequireReportId() *Context { return c } - if len(c.Params.ReportId) != 26 { + if !model.IsValidId(c.Params.ReportId) { c.SetInvalidUrlParam("report_id") } return c @@ -410,7 +410,7 @@ func (c *Context) RequireEmojiId() *Context { return c } - if len(c.Params.EmojiId) != 26 { + if !model.IsValidId(c.Params.EmojiId) { c.SetInvalidUrlParam("emoji_id") } return c @@ -507,7 +507,7 @@ func (c *Context) RequireHookId() *Context { return c } - if len(c.Params.HookId) != 26 { + if !model.IsValidId(c.Params.HookId) { c.SetInvalidUrlParam("hook_id") } @@ -519,7 +519,7 @@ func (c *Context) RequireCommandId() *Context { return c } - if len(c.Params.CommandId) != 26 { + if !model.IsValidId(c.Params.CommandId) { c.SetInvalidUrlParam("command_id") } return c @@ -530,7 +530,7 @@ func (c *Context) RequireJobId() *Context { return c } - if len(c.Params.JobId) != 26 { + if !model.IsValidId(c.Params.JobId) { c.SetInvalidUrlParam("job_id") } return c @@ -552,7 +552,7 @@ func (c *Context) RequireRoleId() *Context { return c } - if len(c.Params.RoleId) != 26 { + if !model.IsValidId(c.Params.RoleId) { c.SetInvalidUrlParam("role_id") } return c @@ -563,7 +563,7 @@ func (c *Context) RequireSchemeId() *Context { return c } - if len(c.Params.SchemeId) != 26 { + if !model.IsValidId(c.Params.SchemeId) { c.SetInvalidUrlParam("scheme_id") } return c @@ -586,7 +586,7 @@ func (c *Context) RequireGroupId() *Context { return c } - if len(c.Params.GroupId) != 26 { + if !model.IsValidId(c.Params.GroupId) { c.SetInvalidUrlParam("group_id") } return c @@ -608,7 +608,7 @@ func (c *Context) RequireSyncableId() *Context { return c } - if len(c.Params.SyncableId) != 26 { + if !model.IsValidId(c.Params.SyncableId) { c.SetInvalidUrlParam("syncable_id") } return c @@ -630,7 +630,7 @@ func (c *Context) RequireBotUserId() *Context { return c } - if len(c.Params.BotUserId) != 26 { + if !model.IsValidId(c.Params.BotUserId) { c.SetInvalidUrlParam("bot_user_id") } return c diff --git a/web/oauth.go b/web/oauth.go index 43db22944a..f842d5b571 100644 --- a/web/oauth.go +++ b/web/oauth.go @@ -79,7 +79,7 @@ func deauthorizeOAuthApp(c *Context, w http.ResponseWriter, r *http.Request) { requestData := model.MapFromJson(r.Body) clientId := requestData["client_id"] - if len(clientId) != 26 { + if !model.IsValidId(clientId) { c.SetInvalidParam("client_id") return } @@ -200,7 +200,7 @@ func getAccessToken(c *Context, w http.ResponseWriter, r *http.Request) { } clientId := r.FormValue("client_id") - if len(clientId) != 26 { + if !model.IsValidId(clientId) { c.Err = model.NewAppError("getAccessToken", "api.oauth.get_access_token.bad_client_id.app_error", nil, "", http.StatusBadRequest) return } diff --git a/wsapi/user.go b/wsapi/user.go index b30314c4ee..c317ae09fe 100644 --- a/wsapi/user.go +++ b/wsapi/user.go @@ -22,7 +22,7 @@ func (api *API) userTyping(req *model.WebSocketRequest) (map[string]interface{}, var ok bool var channelId string - if channelId, ok = req.Data["channel_id"].(string); !ok || len(channelId) != 26 { + if channelId, ok = req.Data["channel_id"].(string); !ok || !model.IsValidId(channelId) { return nil, NewInvalidWebSocketParamError(req.Action, "channel_id") }