Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/com/trilead/ssh2/KnownHosts.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ private final boolean hostnameMatches(String[] hostpatterns, String hostname)
boolean isMatch = false;
boolean negate = false;

if (hostname == null)
return false;

hostname = hostname.toLowerCase(Locale.US);

for (int k = 0; k < hostpatterns.length; k++)
Expand Down Expand Up @@ -806,6 +809,9 @@ public void removeServerHostKey(String hostname, int port, String serverHostKeyA
if (!hostnameMatches(entry.patterns, hostname))
return false;

if (serverHostKeyAlgorithm == null)
return false;

String algo = publicKeyToAlgorithm(entry.key);
return serverHostKeyAlgorithm.equals(algo);
});
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/trilead/ssh2/KnownHostsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,25 @@ public void getPreferredServerHostkeyAlgorithmOrder_Unknown() throws Exception {
assertThat(obj.getPreferredServerHostkeyAlgorithmOrder("unknown"),
nullValue());
}

@Test
public void removeServerHostKey_NullHostname() throws Exception {
KnownHosts obj = new KnownHosts();
obj.addHostkeys(getKnownHosts("known_hosts"));
obj.removeServerHostKey(null, 22, "ssh-rsa", null);
}

@Test
public void removeServerHostKey_NullAlgorithm() throws Exception {
KnownHosts obj = new KnownHosts();
obj.addHostkeys(getKnownHosts("known_hosts"));
obj.removeServerHostKey("example.com", 22, null, null);
}

@Test
public void removeServerHostKey_BothNull() throws Exception {
KnownHosts obj = new KnownHosts();
obj.addHostkeys(getKnownHosts("known_hosts"));
obj.removeServerHostKey(null, 22, null, null);
}
}
Loading