From ee7235ad723e5e653f764d9caa20b0e18f5fcc65 Mon Sep 17 00:00:00 2001 From: Alastair Flynn Date: Wed, 6 Mar 2024 16:57:34 -0500 Subject: [PATCH 1/3] Add check for new line is ssh key --- ssh/authorisedkeys.go | 3 +++ ssh/authorisedkeys_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/ssh/authorisedkeys.go b/ssh/authorisedkeys.go index 460c5e46..b2b937ee 100644 --- a/ssh/authorisedkeys.go +++ b/ssh/authorisedkeys.go @@ -55,6 +55,9 @@ func authKeysDir(username string) (string, error) { // authorized_keys file and returns the constituent parts. // Based on description in "man sshd". func ParseAuthorisedKey(line string) (*AuthorisedKey, error) { + if strings.Contains(line, "\n") { + return nil, errors.NotValidf("newline in authorized_key %q", line) + } key, comment, _, _, err := ssh.ParseAuthorizedKey([]byte(line)) if err != nil { return nil, errors.Errorf("invalid authorized_key %q", line) diff --git a/ssh/authorisedkeys_test.go b/ssh/authorisedkeys_test.go index 052c018d..bd3bbdd1 100644 --- a/ssh/authorisedkeys_test.go +++ b/ssh/authorisedkeys_test.go @@ -260,6 +260,9 @@ func (s *AuthorisedKeysKeysSuite) TestParseAuthorisedKey(c *gc.C) { }, { line: "ssh-rsa", err: "invalid authorized_key \"ssh-rsa\"", + }, { + line: sshtesting.ValidKeyOne.Key + " line1\nline2", + err: "newline in authorized_key \".*", }} { c.Logf("test %d: %s", i, test.line) ak, err := ssh.ParseAuthorisedKey(test.line) From da0f68ecedb17ee0dc41e0292def5dc4a7d5d22d Mon Sep 17 00:00:00 2001 From: Alastair Flynn Date: Thu, 7 Mar 2024 15:51:03 -0500 Subject: [PATCH 2/3] Add patch to stop tests flaking while using host machines termainal. Patch the "getTerminal" with nil to stop the terminal of the host machine being used by default. --- ssh/export_test.go | 6 ++++++ ssh/ssh_gocrypto_test.go | 5 +---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ssh/export_test.go b/ssh/export_test.go index 1568734b..1cc0ab90 100644 --- a/ssh/export_test.go +++ b/ssh/export_test.go @@ -37,3 +37,9 @@ func PatchTerminal(s *testing.CleanupSuite, rlw ReadLineWriter) { c.Assert(atomic.LoadInt64(&balance), gc.Equals, int64(0)) }) } + +func PatchNilTerminal(s *testing.CleanupSuite) { + s.PatchValue(&getTerminal, func() (readLineWriter, func(), error) { + return nil, func() {}, nil + }) +} diff --git a/ssh/ssh_gocrypto_test.go b/ssh/ssh_gocrypto_test.go index 2c8dc3a2..96f1cbdb 100644 --- a/ssh/ssh_gocrypto_test.go +++ b/ssh/ssh_gocrypto_test.go @@ -154,12 +154,9 @@ func (s *SSHGoCryptoCommandSuite) SetUpTest(c *gc.C) { generateKeyRestorer := overrideGenerateKey(c) s.AddCleanup(func(*gc.C) { generateKeyRestorer.Restore() }) - client, err := ssh.NewGoCryptoClient() - c.Assert(err, jc.ErrorIsNil) - s.client = client - s.knownHostsFile = filepath.Join(c.MkDir(), "known_hosts") ssh.SetGoCryptoKnownHostsFile(s.knownHostsFile) + ssh.PatchNilTerminal(&s.CleanupSuite) } func (s *SSHGoCryptoCommandSuite) newServer(c *gc.C, serverConfig cryptossh.ServerConfig) (*sshServer, cryptossh.PublicKey) { From d5433aa5374df21c4d5cc1f5e89036e22eaac66b Mon Sep 17 00:00:00 2001 From: Alastair Flynn Date: Thu, 7 Mar 2024 15:53:57 -0500 Subject: [PATCH 3/3] Add generic gitignore --- .gitignore | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d9ee8928 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# GoLand +.idea/ + +# Dependency directories (remove the comment below to include it) +# vendor/ +