2424import java .io .InputStream ;
2525import java .io .InputStreamReader ;
2626import java .io .Reader ;
27- import java .io .StreamCorruptedException ;
2827import java .net .URL ;
2928import java .nio .charset .StandardCharsets ;
3029import java .nio .file .Files ;
3433import java .util .Collections ;
3534import java .util .List ;
3635
37- import org .apache .sshd .common .config .ConfigFileReaderSupport ;
3836import org .apache .sshd .common .config .keys .AuthorizedKeyEntry ;
3937import org .apache .sshd .common .config .keys .PublicKeyEntry ;
4038import org .apache .sshd .common .util .GenericUtils ;
4139import org .apache .sshd .common .util .ValidateUtils ;
4240import org .apache .sshd .common .util .io .input .NoCloseInputStream ;
4341import org .apache .sshd .common .util .io .input .NoCloseReader ;
42+ import org .slf4j .Logger ;
43+ import org .slf4j .LoggerFactory ;
4444
4545/**
4646 * Contains a representation of an entry in the <code>known_hosts</code> file
@@ -59,6 +59,8 @@ public class KnownHostEntry extends HostPatternsHolder {
5959 */
6060 public static final String STD_HOSTS_FILENAME = "known_hosts" ;
6161
62+ private static final Logger LOG = LoggerFactory .getLogger (KnownHostEntry .class );
63+
6264 private static final class LazyDefaultConfigFileHolder {
6365 private static final Path HOSTS_FILE = PublicKeyEntry .getDefaultKeysFolderPath ().resolve (STD_HOSTS_FILENAME );
6466
@@ -183,7 +185,7 @@ public static List<KnownHostEntry> readKnownHostEntries(BufferedReader rdr) thro
183185 continue ;
184186 }
185187
186- int pos = line .indexOf (ConfigFileReaderSupport .COMMENT_CHAR );
188+ int pos = line .indexOf (PublicKeyEntry .COMMENT_CHAR );
187189 if (pos == 0 ) {
188190 continue ;
189191 }
@@ -203,9 +205,8 @@ public static List<KnownHostEntry> readKnownHostEntries(BufferedReader rdr) thro
203205 entries = new ArrayList <>();
204206 }
205207 entries .add (entry );
206- } catch (RuntimeException | Error e ) { // TODO consider consulting a user callback
207- throw new StreamCorruptedException ("Failed (" + e .getClass ().getSimpleName () + ") to parse line #"
208- + lineNumber + " '" + line + "': " + e .getMessage ());
208+ } catch (RuntimeException e ) { // TODO consider consulting a user callback
209+ LOG .warn ("Invalid known_hosts line #" + lineNumber + " '" + line + "': " + e .getMessage ());
209210 }
210211 }
211212
@@ -217,13 +218,16 @@ public static List<KnownHostEntry> readKnownHostEntries(BufferedReader rdr) thro
217218 }
218219
219220 public static KnownHostEntry parseKnownHostEntry (String line ) {
220- return parseKnownHostEntry ( GenericUtils .isEmpty (line ) ? null : new KnownHostEntry (), line );
221+ return GenericUtils .isEmpty (line ) ? null : parseKnownHostEntry ( new KnownHostEntry (), line );
221222 }
222223
223224 public static <E extends KnownHostEntry > E parseKnownHostEntry (E entry , String data ) {
225+ if (data == null ) {
226+ return null ;
227+ }
224228 String line = GenericUtils .replaceWhitespaceAndTrim (data );
225229 if (GenericUtils .isEmpty (line ) || (line .charAt (0 ) == PublicKeyEntry .COMMENT_CHAR )) {
226- return entry ;
230+ return null ;
227231 }
228232
229233 entry .setConfigLine (line );
@@ -254,6 +258,9 @@ public static <E extends KnownHostEntry> E parseKnownHostEntry(E entry, String d
254258 }
255259 AuthorizedKeyEntry key = PublicKeyEntry .parsePublicKeyEntry (new AuthorizedKeyEntry (),
256260 ValidateUtils .checkNotNullAndNotEmpty (line , "No valid key entry recovered from line=%s" , data ));
261+ if (key == null ) {
262+ return null ;
263+ }
257264 entry .setKeyEntry (key );
258265 return entry ;
259266 }
0 commit comments