diff options
author | donoghuc <cas.donoghue@gmail.com> | 2020-02-15 09:06:54 -0800 |
---|---|---|
committer | donoghuc <cas.donoghue@gmail.com> | 2020-02-15 09:06:54 -0800 |
commit | 5778afc12397b24f2420b524d436f425dbd38e4f (patch) | |
tree | 241a4acec3889ac318265fa8a09f7b20aa61c31d | |
parent | 71e2f31bb970096f6257031c57ccc035c6e07d93 (diff) | |
download | net-ssh-5778afc12397b24f2420b524d436f425dbd38e4f.tar.gz |
(GH-737) Allow known_hosts to have empty lines and comments
Previously empty lines in a known_hosts file would result in an exception parsing the file. This commit updates the parser to allow empty lines as well as comments. Note that comments were already supported.
-rw-r--r-- | lib/net/ssh/known_hosts.rb | 3 | ||||
-rw-r--r-- | test/known_hosts/known_hosts_ignore | 3 | ||||
-rw-r--r-- | test/test_known_hosts.rb | 4 |
3 files changed, 9 insertions, 1 deletions
diff --git a/lib/net/ssh/known_hosts.rb b/lib/net/ssh/known_hosts.rb index 559bb4b..23a5606 100644 --- a/lib/net/ssh/known_hosts.rb +++ b/lib/net/ssh/known_hosts.rb @@ -131,7 +131,8 @@ module Net File.open(source) do |file| file.each_line do |line| hosts, type, key_content = line.split(' ') - next unless hosts || hosts.start_with?('#') + # Skip empty line or one that is commented + next if hosts.nil? || hosts.start_with?('#') hostlist = hosts.split(',') diff --git a/test/known_hosts/known_hosts_ignore b/test/known_hosts/known_hosts_ignore new file mode 100644 index 0000000..04e6c81 --- /dev/null +++ b/test/known_hosts/known_hosts_ignore @@ -0,0 +1,3 @@ +# commented entry followed by empty line + +github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
\ No newline at end of file diff --git a/test/test_known_hosts.rb b/test/test_known_hosts.rb index 602fbcf..6b1fda6 100644 --- a/test/test_known_hosts.rb +++ b/test/test_known_hosts.rb @@ -16,6 +16,10 @@ class TestKnownHosts < NetSSHTest perform_test(path("known_hosts/github_hash")) end + def test_parsing_known_hosts_empty_lines_and_comments + perform_test(path("known_hosts/known_hosts_ignore")) + end + def test_missing_then_add Tempfile.open('github') do |f| f.write(File.read(path("known_hosts/github"))) |