From e81fa3220de51350592e50b2fa3e0dc9425d43f8 Mon Sep 17 00:00:00 2001 From: David Lu Date: Fri, 20 May 2016 14:47:10 -0400 Subject: [PATCH] Added validation for command triggers (#3068) --- model/command.go | 5 ++- webapp/components/backstage/add_command.jsx | 48 ++++++++++++++++++++- webapp/i18n/en.json | 9 ++-- webapp/i18n/es.json | 6 +-- webapp/i18n/fr.json | 7 ++- webapp/i18n/ja.json | 5 +-- webapp/i18n/pt.json | 6 +-- webapp/utils/constants.jsx | 2 + 8 files changed, 70 insertions(+), 18 deletions(-) diff --git a/model/command.go b/model/command.go index 4d5f7ace99..decb647b70 100644 --- a/model/command.go +++ b/model/command.go @@ -6,11 +6,14 @@ package model import ( "encoding/json" "io" + "strings" ) const ( COMMAND_METHOD_POST = "P" COMMAND_METHOD_GET = "G" + MIN_TRIGGER_LENGTH = 1 + MAX_TRIGGER_LENGTH = 128 ) type Command struct { @@ -99,7 +102,7 @@ func (o *Command) IsValid() *AppError { return NewLocAppError("Command.IsValid", "model.command.is_valid.team_id.app_error", nil, "") } - if len(o.Trigger) == 0 || len(o.Trigger) > 128 { + if len(o.Trigger) < MIN_TRIGGER_LENGTH || len(o.Trigger) > MAX_TRIGGER_LENGTH || strings.Index(o.Trigger, "/") == 0 || strings.Contains(o.Trigger, " ") { return NewLocAppError("Command.IsValid", "model.command.is_valid.trigger.app_error", nil, "") } diff --git a/webapp/components/backstage/add_command.jsx b/webapp/components/backstage/add_command.jsx index f3208bc5f5..c817764aab 100644 --- a/webapp/components/backstage/add_command.jsx +++ b/webapp/components/backstage/add_command.jsx @@ -11,6 +11,7 @@ import {FormattedMessage} from 'react-intl'; import FormError from 'components/form_error.jsx'; import {browserHistory, Link} from 'react-router'; import SpinnerButton from 'components/spinner_button.jsx'; +import Constants from 'utils/constants.jsx'; const REQUEST_POST = 'P'; const REQUEST_GET = 'G'; @@ -92,6 +93,51 @@ export default class AddCommand extends React.Component { return; } + if (command.trigger.indexOf('/') === 0) { + this.setState({ + saving: false, + clientError: ( + + ) + }); + + return; + } + + if (command.trigger.indexOf(' ') !== -1) { + this.setState({ + saving: false, + clientError: ( + + ) + }); + return; + } + + if (command.trigger.length < Constants.MIN_TRIGGER_LENGTH || command.trigger.length > Constants.MAX_TRIGGER_LENGTH) { + this.setState({ + saving: false, + clientError: ( + + ) + }); + + return; + } + if (!command.url) { this.setState({ saving: false, @@ -323,7 +369,7 @@ export default class AddCommand extends React.Component {