Skip to content

Commit 01d79d0

Browse files
committed
clarify the concept about "argument option/value"
1 parent 36194cc commit 01d79d0

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

modules/git/command.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,18 @@ func (c *Command) SetDescription(desc string) *Command {
100100
return c
101101
}
102102

103-
func isSafeDynArg(s string) bool {
103+
// isSafeArgumentValue checks if the argument is safe to be used as a value (not an option)
104+
func isSafeArgumentValue(s string) bool {
104105
return s == "" || s[0] != '-'
105106
}
106107

107-
func isValidOption(s string) bool {
108+
// isValidArgumentOption checks if the argument is a valid option (starting with '-').
109+
// It doesn't check whether the option is supported or not
110+
func isValidArgumentOption(s string) bool {
108111
return s != "" && s[0] == '-'
109112
}
110113

111-
// AddArguments adds new git arguments to the command. It only accepts string literals, or trusted CmdArg.
114+
// AddArguments adds new git arguments (option/value) to the command. It only accepts string literals, or trusted CmdArg.
112115
// Type CmdArg is in the internal package, so it can not be used outside of this package directly,
113116
// it makes sure that user-provided arguments won't cause RCE risks.
114117
// User-provided arguments should be passed by other AddXxx functions
@@ -121,9 +124,9 @@ func (c *Command) AddArguments(args ...internal.CmdArg) *Command {
121124

122125
// AddOptionValues adds a new option with a list of non-option values
123126
// For example: AddOptionValues("--opt", val) means 2 arguments: {"--opt", val}.
124-
// The values are treated as dynamic arguments. It equals to: AddArguments("--opt") then AddDynamicArguments(val).
127+
// The values are treated as dynamic argument values. It equals to: AddArguments("--opt") then AddDynamicArguments(val).
125128
func (c *Command) AddOptionValues(opt internal.CmdArg, args ...string) *Command {
126-
if !isValidOption(string(opt)) {
129+
if !isValidArgumentOption(string(opt)) {
127130
c.brokenArgs = append(c.brokenArgs, string(opt))
128131
return c
129132
}
@@ -135,7 +138,7 @@ func (c *Command) AddOptionValues(opt internal.CmdArg, args ...string) *Command
135138
// AddOptionFormat adds a new option with a format string and arguments
136139
// For example: AddOptionFormat("--opt=%s %s", val1, val2) means 1 argument: {"--opt=val1 val2"}.
137140
func (c *Command) AddOptionFormat(opt string, args ...any) *Command {
138-
if !isValidOption(opt) {
141+
if !isValidArgumentOption(opt) {
139142
c.brokenArgs = append(c.brokenArgs, opt)
140143
return c
141144
}
@@ -145,11 +148,12 @@ func (c *Command) AddOptionFormat(opt string, args ...any) *Command {
145148
return c
146149
}
147150

148-
// AddDynamicArguments adds new dynamic arguments to the command.
151+
// AddDynamicArguments adds new dynamic argument values to the command.
149152
// The arguments may come from user input and can not be trusted, so no leading '-' is allowed to avoid passing options.
153+
// TODO: in the future, this function can be renamed to AddArgumentValues
150154
func (c *Command) AddDynamicArguments(args ...string) *Command {
151155
for _, arg := range args {
152-
if !isSafeDynArg(arg) {
156+
if !isSafeArgumentValue(arg) {
153157
c.brokenArgs = append(c.brokenArgs, arg)
154158
}
155159
}

modules/git/command_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ func TestRunWithContextStd(t *testing.T) {
4343
}
4444

4545
func TestGitArgument(t *testing.T) {
46-
assert.True(t, isValidOption("-x"))
47-
assert.True(t, isValidOption("--xx"))
48-
assert.False(t, isValidOption(""))
49-
assert.False(t, isValidOption("x"))
50-
51-
assert.True(t, isSafeDynArg(""))
52-
assert.True(t, isSafeDynArg("x"))
53-
assert.False(t, isSafeDynArg("-x"))
46+
assert.True(t, isValidArgumentOption("-x"))
47+
assert.True(t, isValidArgumentOption("--xx"))
48+
assert.False(t, isValidArgumentOption(""))
49+
assert.False(t, isValidArgumentOption("x"))
50+
51+
assert.True(t, isSafeArgumentValue(""))
52+
assert.True(t, isSafeArgumentValue("x"))
53+
assert.False(t, isSafeArgumentValue("-x"))
5454
}

0 commit comments

Comments
 (0)