diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-03-26 15:57:12 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-03-29 15:57:53 +0300 |
commit | 087fa1246cda035485e4964a980fda61ac91992f (patch) | |
tree | b41774ec560abee5b1676441ee4dacc4112bc222 | |
parent | 2b4abebb3d9ea9a6c360aa8c11f20331c4431f6d (diff) |
git: Allow per-command validation of positional args
Currently, all git commands use the same logic to validate global,
positional and post-separator arguments. This is inflexible, because
different commands use different syntax and also have different
behaviour. Until now, this hasn't bitten us yet and has served its
purpose of disallowing injection of command line options. But for some
commands, the existing sets of rules bar us from using some features.
Introduce a new optional function for command descriptions which allow
us to override validation of positional arguments per command. This can
be used in arbitrary ways: either to strengthen validation and assert
that certain kinds of arguments cannot ever be passed, or to weaken
validation in case we know that a set of arguments is safe to be
accepted as positional argument.
A user of this is going to be introduced in the following commit.
-rw-r--r-- | internal/git/command_description.go | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/internal/git/command_description.go b/internal/git/command_description.go index 0a3dc7a85..158420a7e 100644 --- a/internal/git/command_description.go +++ b/internal/git/command_description.go @@ -15,8 +15,9 @@ const ( ) type commandDescription struct { - flags uint - opts []GlobalOption + flags uint + opts []GlobalOption + validatePositionalArgs func([]string) error } // commandDescriptions is a curated list of Git command descriptions for special @@ -226,12 +227,18 @@ func (c commandDescription) args(flags []Option, args []string, postSepArgs []st commandArgs = append(commandArgs, args...) } - for _, a := range args { - if err := validatePositionalArg(a); err != nil { + if c.validatePositionalArgs != nil { + if err := c.validatePositionalArgs(args); err != nil { return nil, err } - commandArgs = append(commandArgs, a) + } else { + for _, a := range args { + if err := validatePositionalArg(a); err != nil { + return nil, err + } + } } + commandArgs = append(commandArgs, args...) if c.supportsEndOfOptions() { commandArgs = append(commandArgs, "--end-of-options") |