Skip to content

Commit

Permalink
fix(incoming-webhook): simple script DoS protection
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Nov 11, 2022
1 parent 3cf9823 commit eb0b7e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pkg/scripting/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ type Operation struct {
Args []string
}

// GetFirstArg retrn first operation argument
func (op Operation) GetFirstArg() string {
if len(op.Args) > 0 {
return op.Args[0]
}
return ""
}

// OperationStack is a stack of operation
type OperationStack []Operation

Expand Down
26 changes: 18 additions & 8 deletions pkg/service/scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,31 @@ func (reg *Registry) processArticleByScriptEngine(ctx context.Context, alias str

func (reg *Registry) execSetOperations(ctx context.Context, ops scripting.OperationStack, article *model.ArticleCreateForm) {
uid := getCurrentUserIDFromContext(ctx)
category := ""
for _, op := range ops {
value := op.Args[0]
switch op.Name {
case scripting.OpSetCategory:
// set category
if cat, err := reg.db.GetCategoryByUserAndTitle(uid, value); err == nil && cat != nil {
article.CategoryID = cat.ID
}
// only execute last setCategory operation
category = op.GetFirstArg()
case scripting.OpSetText:
// set text
article.Text = &value
text := op.GetFirstArg()
article.Text = &text
case scripting.OpSetTitle:
// set title
article.Title = value
article.Title = op.GetFirstArg()
}
}
if category != "" {
if cat, err := reg.db.GetCategoryByUserAndTitle(uid, category); err == nil && cat != nil {
article.CategoryID = cat.ID
}
}
}

func (reg *Registry) execOtherOperations(ctx context.Context, ops scripting.OperationStack, article *model.Article) error {
// allows only 2 webhook trigger
hardLimitCounter := 2
for _, op := range ops {
switch op.Name {
case scripting.OpSendNotification:
Expand All @@ -99,7 +105,11 @@ func (reg *Registry) execOtherOperations(ctx context.Context, ops scripting.Oper
return err
}
case scripting.OpTriggerWebhook:
name := op.Args[0]
if hardLimitCounter == 0 {
continue
}
hardLimitCounter--
name := op.GetFirstArg()
if err := reg.SendArticle(ctx, article.ID, &name); err != nil {
return err
}
Expand Down

0 comments on commit eb0b7e9

Please sign in to comment.