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 .PublicKeyEntry ;
3937import org .apache .sshd .common .util .GenericUtils ;
4038import org .apache .sshd .common .util .ValidateUtils ;
4139import org .apache .sshd .common .util .io .input .NoCloseInputStream ;
4240import org .apache .sshd .common .util .io .input .NoCloseReader ;
41+ import org .slf4j .Logger ;
42+ import org .slf4j .LoggerFactory ;
4343
4444/**
4545 * Contains a representation of an entry in the <code>known_hosts</code> file
@@ -58,6 +58,8 @@ public class KnownHostEntry extends HostPatternsHolder {
5858 */
5959 public static final String STD_HOSTS_FILENAME = "known_hosts" ;
6060
61+ private static final Logger LOG = LoggerFactory .getLogger (KnownHostEntry .class );
62+
6163 private static final class LazyDefaultConfigFileHolder {
6264 private static final Path HOSTS_FILE = PublicKeyEntry .getDefaultKeysFolderPath ().resolve (STD_HOSTS_FILENAME );
6365
@@ -181,17 +183,6 @@ public static List<KnownHostEntry> readKnownHostEntries(BufferedReader rdr) thro
181183 if (GenericUtils .isEmpty (line )) {
182184 continue ;
183185 }
184-
185- int pos = line .indexOf (ConfigFileReaderSupport .COMMENT_CHAR );
186- if (pos == 0 ) {
187- continue ;
188- }
189-
190- if (pos > 0 ) {
191- line = line .substring (0 , pos );
192- line = line .trim ();
193- }
194-
195186 try {
196187 KnownHostEntry entry = parseKnownHostEntry (line );
197188 if (entry == null ) {
@@ -202,9 +193,8 @@ public static List<KnownHostEntry> readKnownHostEntries(BufferedReader rdr) thro
202193 entries = new ArrayList <>();
203194 }
204195 entries .add (entry );
205- } catch (RuntimeException | Error e ) { // TODO consider consulting a user callback
206- throw new StreamCorruptedException ("Failed (" + e .getClass ().getSimpleName () + ") to parse line #"
207- + lineNumber + " '" + line + "': " + e .getMessage ());
196+ } catch (RuntimeException e ) { // TODO consider consulting a user callback
197+ LOG .warn ("Invalid known_hosts line #" + lineNumber + " '" + line + "': " + e .getMessage ());
208198 }
209199 }
210200
@@ -216,43 +206,50 @@ public static List<KnownHostEntry> readKnownHostEntries(BufferedReader rdr) thro
216206 }
217207
218208 public static KnownHostEntry parseKnownHostEntry (String line ) {
219- return parseKnownHostEntry (GenericUtils .isEmpty (line ) ? null : new KnownHostEntry (), line );
220- }
221-
222- public static <E extends KnownHostEntry > E parseKnownHostEntry (E entry , String data ) {
223- String line = GenericUtils .replaceWhitespaceAndTrim (data );
224- if (GenericUtils .isEmpty (line ) || (line .charAt (0 ) == PublicKeyEntry .COMMENT_CHAR )) {
225- return entry ;
209+ if (line == null ) {
210+ return null ;
211+ }
212+ String tmp = GenericUtils .replaceWhitespaceAndTrim (line );
213+ int i = tmp .indexOf (PublicKeyEntry .COMMENT_CHAR );
214+ if (i >= 0 ) {
215+ tmp = tmp .substring (0 , i ).trim ();
216+ }
217+ if (GenericUtils .isEmpty (tmp )) {
218+ return null ;
226219 }
227220
221+ KnownHostEntry entry = new KnownHostEntry ();
228222 entry .setConfigLine (line );
229223
230- if (line .charAt (0 ) == MARKER_INDICATOR ) {
231- int pos = line .indexOf (' ' );
232- ValidateUtils .checkTrue (pos > 0 , "Missing marker name end delimiter in line=%s" , data );
233- ValidateUtils .checkTrue (pos > 1 , "No marker name after indicator in line=%s" , data );
234- entry .setMarker (line .substring (1 , pos ));
235- line = line .substring (pos + 1 ).trim ();
224+ if (tmp .charAt (0 ) == MARKER_INDICATOR ) {
225+ int pos = tmp .indexOf (' ' );
226+ ValidateUtils .checkTrue (pos > 0 , "Missing marker name end delimiter in line=%s" , line );
227+ ValidateUtils .checkTrue (pos > 1 , "No marker name after indicator in line=%s" , line );
228+ entry .setMarker (tmp .substring (1 , pos ));
229+ tmp = tmp .substring (pos + 1 ).trim ();
236230 } else {
237231 entry .setMarker (null );
238232 }
239233
240- int pos = line .indexOf (' ' );
241- ValidateUtils .checkTrue (pos > 0 , "Missing host patterns end delimiter in line=%s" , data );
242- String hostPattern = line .substring (0 , pos );
243- line = line .substring (pos + 1 ).trim ();
234+ int pos = tmp .indexOf (' ' );
235+ ValidateUtils .checkTrue (pos > 0 , "Missing host patterns end delimiter in line=%s" , line );
236+ String hostPattern = tmp .substring (0 , pos );
237+ tmp = tmp .substring (pos + 1 ).trim ();
244238
245239 if (hostPattern .charAt (0 ) == KnownHostHashValue .HASHED_HOST_DELIMITER ) {
246240 KnownHostHashValue hash = ValidateUtils .checkNotNull (KnownHostHashValue .parse (hostPattern ),
247- "Failed to extract host hash value from line=%s" , data );
241+ "Failed to extract host hash value from line=%s" , line );
248242 entry .setHashedEntry (hash );
249243 entry .setPatterns (null );
250244 } else {
251245 entry .setHashedEntry (null );
252246 entry .setPatterns (parsePatterns (GenericUtils .split (hostPattern , ',' )));
253247 }
254248 PublicKeyEntry key = PublicKeyEntry .parsePublicKeyEntry (
255- ValidateUtils .checkNotNullAndNotEmpty (line , "No valid key entry recovered from line=%s" , data ));
249+ ValidateUtils .checkNotNullAndNotEmpty (tmp , "No valid key entry recovered from line=%s" , line ));
250+ if (key == null ) {
251+ return null ;
252+ }
256253 entry .setKeyEntry (key );
257254 return entry ;
258255 }
0 commit comments