Skip to content

Commit 5f55609

Browse files
Codelaxremyleone
andauthored
fix(autocomplete): check completion arguments bounds (#2520)
Co-authored-by: Rémy Léone <[email protected]>
1 parent 2eb02be commit 5f55609

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/namespaces/autocomplete/autocomplete.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,17 @@ func autocompleteCompleteBashCommand() *core.Command {
255255
ArgsType: reflect.TypeOf(args.RawArgs{}),
256256
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
257257
rawArgs := *argsI.(*args.RawArgs)
258+
if len(rawArgs) < 3 {
259+
return nil, fmt.Errorf("not enough arguments")
260+
}
258261
wordIndex, err := strconv.Atoi(rawArgs[1])
259262
if err != nil {
260263
return nil, err
261264
}
262265
words := rawArgs[2:]
266+
if len(words) <= wordIndex {
267+
return nil, fmt.Errorf("index to complete is invalid")
268+
}
263269
leftWords := words[:wordIndex]
264270
wordToComplete := words[wordIndex]
265271
rightWords := words[wordIndex+1:]
@@ -293,6 +299,9 @@ func autocompleteCompleteFishCommand() *core.Command {
293299
ArgsType: reflect.TypeOf(args.RawArgs{}),
294300
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
295301
rawArgs := *argsI.(*args.RawArgs)
302+
if len(rawArgs) < 4 {
303+
return nil, fmt.Errorf("not enough arguments")
304+
}
296305
leftWords := rawArgs[3:]
297306
wordToComplete := rawArgs[2]
298307

@@ -325,6 +334,9 @@ func autocompleteCompleteZshCommand() *core.Command {
325334
ArgsType: reflect.TypeOf(args.RawArgs{}),
326335
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
327336
rawArgs := *argsI.(*args.RawArgs)
337+
if len(rawArgs) < 2 {
338+
return nil, fmt.Errorf("not enough arguments")
339+
}
328340

329341
// First arg is the word index.
330342
wordIndex, err := strconv.Atoi(rawArgs[0])
@@ -333,6 +345,10 @@ func autocompleteCompleteZshCommand() *core.Command {
333345
}
334346
wordIndex-- // In zsh word index starts at 1.
335347

348+
if wordIndex <= 0 {
349+
return nil, fmt.Errorf("index cannot be 1 (0) or lower")
350+
}
351+
336352
// Other args are all the words.
337353
words := rawArgs[1:]
338354
if len(words) <= wordIndex {

0 commit comments

Comments
 (0)