Skip to content

Commit

Permalink
Security: specify to take extra arguments explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Apr 27, 2024
1 parent f7b351a commit 9f54838
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/bot/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CommandInstance struct {
leadingMatcher []string
name string
description string
allowArgs bool
argsSyntax string
argsPrefix []string
operators []string
Expand Down Expand Up @@ -122,6 +123,7 @@ func compileCommands(templates map[string]string, cc []*config.CommandConfig, le
leadingMatcher: utils.Copy(leadingMatcher),
name: ci.Name,
description: ci.Description,
allowArgs: ci.AllowArgs,
argsSyntax: ci.ArgsSyntax,
argsPrefix: ci.ArgsPrefix,
operators: ci.Operators,
Expand Down Expand Up @@ -201,12 +203,23 @@ func (c *CommandInstance) execute(ctx *Context) error {
}
}

// Validate run command arguments (self)
if !c.allowArgs && len(ctx.Args) > 0 {
return ctx.ReplyBad(fmt.Sprintf(
"Command `%s` cannot have extra arguments (you supplied `%s`)\nTry setting allowArgs: true in config to allow extra arguments",
c.matcher(),
strings.Join(ctx.Args, " "),
))
}

// Run command (self)
_ = ctx.ReplyRunning()

var args []string
args = append(args, c.argsPrefix...)
args = append(args, ctx.Args...)
if c.allowArgs {
args = append(args, ctx.Args...)
}
var buf bytes.Buffer
cmd := exec.CommandContext(ctx, c.commandFile, args...)
cmd.Stdout = &buf
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type CommandConfig struct {
TemplateRef string `mapstructure:"templateRef" yaml:"templateRef"`
// Description should describe what this command does in one line.
Description string `mapstructure:"description" yaml:"description"`
// AllowArgs is a flag to allow passing extra user command arguments to exec arguments.
AllowArgs bool `mapstructure:"allowArgs" yaml:"allowArgs"`
// ArgsSyntax is an optional arguments syntax to display in help command.
ArgsSyntax string `mapstructure:"argsSyntax" yaml:"argsSyntax"`
// ArgsPrefix is always prefixed the arguments (before the user-provided arguments, if any) when executing the command template.
Expand Down

0 comments on commit 9f54838

Please sign in to comment.