diff options
author | Robert May <rmay@gitlab.com> | 2021-06-28 16:18:00 +0100 |
---|---|---|
committer | Robert May <rmay@gitlab.com> | 2021-06-29 14:18:11 +0100 |
commit | fcff692b596270483fba4496d3fb7d971367f9d8 (patch) | |
tree | 925f0fd00196fcc7c37403c644e59941f6dbce06 | |
parent | 99f2163b0d8ef53a8c53b85664f34602b92d8cc6 (diff) | |
download | gitlab-shell-fcff692b596270483fba4496d3fb7d971367f9d8.tar.gz |
Modify regex to prevent partial matches
-rw-r--r-- | internal/command/commandargs/command_args_test.go | 9 | ||||
-rw-r--r-- | internal/command/commandargs/shell.go | 4 |
2 files changed, 9 insertions, 4 deletions
diff --git a/internal/command/commandargs/command_args_test.go b/internal/command/commandargs/command_args_test.go index 0329c82..7b9f0ad 100644 --- a/internal/command/commandargs/command_args_test.go +++ b/internal/command/commandargs/command_args_test.go @@ -23,14 +23,19 @@ func TestParseSuccess(t *testing.T) { env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, arguments: []string{}, expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{}, CommandType: Discover, Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, - }, - { + }, { desc: "It finds the key id in any passed arguments", executable: &executable.Executable{Name: executable.GitlabShell}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, arguments: []string{"hello", "key-123"}, expectedArgs: &Shell{Arguments: []string{"hello", "key-123"}, SshArgs: []string{}, CommandType: Discover, GitlabKeyId: "123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, }, { + desc: "It finds the key id only if the argument is of <key-id> format", + executable: &executable.Executable{Name: executable.GitlabShell}, + env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, + arguments: []string{"hello", "username-key-123"}, + expectedArgs: &Shell{Arguments: []string{"hello", "username-key-123"}, SshArgs: []string{}, CommandType: Discover, GitlabUsername: "key-123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, + }, { desc: "It finds the username in any passed arguments", executable: &executable.Executable{Name: executable.GitlabShell}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, diff --git a/internal/command/commandargs/shell.go b/internal/command/commandargs/shell.go index 9cf6720..589f58d 100644 --- a/internal/command/commandargs/shell.go +++ b/internal/command/commandargs/shell.go @@ -20,8 +20,8 @@ const ( ) var ( - whoKeyRegex = regexp.MustCompile(`\bkey-(?P<keyid>\d+)\b`) - whoUsernameRegex = regexp.MustCompile(`\busername-(?P<username>\S+)\b`) + whoKeyRegex = regexp.MustCompile(`\Akey-(?P<keyid>\d+)\z`) + whoUsernameRegex = regexp.MustCompile(`\Ausername-(?P<username>\S+)\z`) ) type Shell struct { |