diff options
author | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-09-25 10:37:18 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <git@zjvandeweg.nl> | 2019-09-25 10:37:18 +0300 |
commit | afe710b7cbe1873b44171b196311ef2e0c699148 (patch) | |
tree | 07e622ffdc7f31d575e2f8d208df7589f6cb310b | |
parent | 2c7becb8d3fd9fc0033f602104183400abd49461 (diff) | |
parent | ac096b678698292f0b42772f62ee71646220293e (diff) |
Merge branch 'po-dsl-config-pairs' into 'master'
ConfigPair option for DSL
See merge request gitlab-org/gitaly!1507
-rw-r--r-- | changelogs/unreleased/po-dsl-config-pairs.yml | 5 | ||||
-rw-r--r-- | internal/git/safecmd.go | 21 | ||||
-rw-r--r-- | internal/git/safecmd_test.go | 24 |
3 files changed, 49 insertions, 1 deletions
diff --git a/changelogs/unreleased/po-dsl-config-pairs.yml b/changelogs/unreleased/po-dsl-config-pairs.yml new file mode 100644 index 000000000..b37383d28 --- /dev/null +++ b/changelogs/unreleased/po-dsl-config-pairs.yml @@ -0,0 +1,5 @@ +--- +title: ConfigPair option for DSL +merge_request: 1507 +author: +type: security diff --git a/internal/git/safecmd.go b/internal/git/safecmd.go index fa9a437eb..43d7ed037 100644 --- a/internal/git/safecmd.go +++ b/internal/git/safecmd.go @@ -100,6 +100,27 @@ func (sc SubSubCmd) ValidateArgs() ([]string, error) { return []string{sc.Name}, nil } +// ConfigPair is a sub-command option for use with commands like "git config" +type ConfigPair struct { + Key string + Value string +} + +// IsOption is a method present on all Flag interface implementations +func (ConfigPair) IsOption() {} + +var configKeyRegex = regexp.MustCompile(`^[[:alnum:]]+[-[:alnum:]]*\.(.+\.)*[[:alnum:]]+[-[:alnum:]]*$`) + +// ValidateArgs validates the config pair args +func (cp ConfigPair) ValidateArgs() ([]string, error) { + if !configKeyRegex.MatchString(cp.Key) { + return nil, &invalidArgErr{ + msg: fmt.Sprintf("config key %q failed regexp validation", cp.Key), + } + } + return []string{cp.Key, cp.Value}, nil +} + // Flag is a single token optional command line argument that enables or // disables functionality (e.g. "-L") type Flag struct { diff --git a/internal/git/safecmd_test.go b/internal/git/safecmd_test.go index 1be8fbeb5..4d8689cc5 100644 --- a/internal/git/safecmd_test.go +++ b/internal/git/safecmd_test.go @@ -32,7 +32,11 @@ func TestFlagValidation(t *testing.T) { // valid SubSubCmd inputs {option: git.SubSubCmd{"meow"}, valid: true}, - // valid FlagCombo inputs + // valid ConfigPair inputs + {option: git.ConfigPair{"a.b.c", "d"}, valid: true}, + {option: git.ConfigPair{"core.sound", "meow"}, valid: true}, + {option: git.ConfigPair{"asdf-qwer.1234-5678", ""}, valid: true}, + {option: git.ConfigPair{"http.https://user@example.com/repo.git.user", "kitty"}, valid: true}, // invalid Flag inputs {option: git.Flag{"-*"}}, // invalid character @@ -46,6 +50,15 @@ func TestFlagValidation(t *testing.T) { // invalid SubSubCmd inputs {option: git.SubSubCmd{"--meow"}}, // cannot start with dash + + // invalid ConfigPair inputs + {option: git.ConfigPair{"", ""}}, // key cannot be empty + {option: git.ConfigPair{" ", ""}}, // key cannot be whitespace + {option: git.ConfigPair{"asdf", ""}}, // two components required + {option: git.ConfigPair{"asdf.", ""}}, // 2nd component must be non-empty + {option: git.ConfigPair{"--asdf.asdf", ""}}, // key cannot start with dash + {option: git.ConfigPair{"as[[df.asdf", ""}}, // 1st component cannot contain non-alphanumeric + {option: git.ConfigPair{"asdf.as]]df", ""}}, // 2nd component cannot contain non-alphanumeric } { args, err := tt.option.ValidateArgs() if tt.valid { @@ -160,6 +173,15 @@ func TestSafeCmdValid(t *testing.T) { }, expectArgs: []string{"noun", "verb", "-", "--adjective"}, }, + { + subCmd: git.SubCmd{ + Name: "config", + Flags: []git.Option{ + git.ConfigPair{"user.name", "jramsay"}, + }, + }, + expectArgs: []string{"config", "user.name", "jramsay"}, + }, } { cmd, err := git.SafeCmd(ctx, testRepo, tt.globals, tt.subCmd) require.NoError(t, err) |