diff options
author | Igor <idrozdov@gitlab.com> | 2019-05-31 12:08:54 +0000 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2019-05-31 12:08:54 +0000 |
commit | 033c81d546d31d07e5eadb50611543a7d2471254 (patch) | |
tree | 688dc50182c8429941d4d23edad3aedc08471233 /go/internal/command/commandargs/command_args.go | |
parent | 12ca54c2d998803a0564a5a2942121364a30678f (diff) | |
download | gitlab-shell-033c81d546d31d07e5eadb50611543a7d2471254.tar.gz |
Go implementation for git-receive-pack
Diffstat (limited to 'go/internal/command/commandargs/command_args.go')
-rw-r--r-- | go/internal/command/commandargs/command_args.go | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/go/internal/command/commandargs/command_args.go b/go/internal/command/commandargs/command_args.go index e801889..7e241ea 100644 --- a/go/internal/command/commandargs/command_args.go +++ b/go/internal/command/commandargs/command_args.go @@ -4,6 +4,8 @@ import ( "errors" "os" "regexp" + + "github.com/mattn/go-shellwords" ) type CommandType string @@ -11,6 +13,7 @@ type CommandType string const ( Discover CommandType = "discover" TwoFactorRecover CommandType = "2fa_recovery_codes" + ReceivePack CommandType = "git-receive-pack" ) var ( @@ -21,7 +24,7 @@ var ( type CommandArgs struct { GitlabUsername string GitlabKeyId string - SshCommand string + SshArgs []string CommandType CommandType } @@ -30,12 +33,15 @@ func Parse(arguments []string) (*CommandArgs, error) { return nil, errors.New("Only ssh allowed") } - info := &CommandArgs{} + args := &CommandArgs{} + args.parseWho(arguments) - info.parseWho(arguments) - info.parseCommand(os.Getenv("SSH_ORIGINAL_COMMAND")) + if err := args.parseCommand(os.Getenv("SSH_ORIGINAL_COMMAND")); err != nil { + return nil, errors.New("Invalid ssh command") + } + args.defineCommandType() - return info, nil + return args, nil } func (c *CommandArgs) parseWho(arguments []string) { @@ -74,14 +80,29 @@ func tryParseUsername(argument string) string { return "" } -func (c *CommandArgs) parseCommand(commandString string) { - c.SshCommand = commandString +func (c *CommandArgs) parseCommand(commandString string) error { + args, err := shellwords.Parse(commandString) + if err != nil { + return err + } - if commandString == "" { - c.CommandType = Discover + // Handle Git for Windows 2.14 using "git upload-pack" instead of git-upload-pack + if len(args) > 1 && args[0] == "git" { + command := args[0] + "-" + args[1] + commandArgs := args[2:] + + args = append([]string{command}, commandArgs...) } - if CommandType(commandString) == TwoFactorRecover { - c.CommandType = TwoFactorRecover + c.SshArgs = args + + return nil +} + +func (c *CommandArgs) defineCommandType() { + if len(c.SshArgs) == 0 { + c.CommandType = Discover + } else { + c.CommandType = CommandType(c.SshArgs[0]) } } |