git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 1/2 v2] Ignore unreadable SSH private keys when autoloading identities

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 28, 2008, 00:24 UTC
Message-ID
<20080828002406.GU26523@spearce.org>
In-Reply-To
<20080827232946.GS26523@spearce.org>

During SSH startup we read all keys in the user's ~/.ssh, even if we may not need them for this particular transport session.

If a file is not really a key, or it contains a key that JSch doesn't recognize we shouldn't crash the transport. Instead we should skip the file and move on. Later on we just don't have that identity available to us, or we'll crash if we try to add that identity file explicitly from ~/.ssh/config.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
  "Shawn O. Pearce" <spearce@spearce.org> wrote:
  > Marek Zawirski <marek.zawirski@gmail.com> wrote:
  > > Shawn O. Pearce wrote:
  > >> diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
  > > (...)
  > >> +			try {
  > >> +				addIdentity(k);
  > >> +			} catch (JSchException e) {
  > >> +				if (e.getMessage().startsWith("invalid privatekey: "))
  > >> +					continue;
  > >> +				throw e;
  > >> +			}
  > >
  > > That's extreme error handling with JSch;) Do you really think it's  
  > > better to rely on internal error message instead of continuing in any  
  > > case? Which other exceptions we would like to pass level up?
  > 
  > I guess I can just change this to ignore everything.
 .../jgit/transport/DefaultSshSessionFactory.java   |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
index a2437c2..74fca66 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
@@ -165,14 +165,21 @@ private void identities() throws JSchException {
 			final File k = new File(sshdir, n.substring(0, n.length() - 4));
 			if (!k.isFile())
 				continue;
-			addIdentity(k);
+
+			try {
+				addIdentity(k);
+			} catch (JSchException e) {
+				continue;
+			}
 		}
 	}
 
 	private void addIdentity(final File identityFile) throws JSchException {
 		final String path = identityFile.getAbsolutePath();
-		if (loadedIdentities.add(path))
+		if (!loadedIdentities.contains(path)) {
 			userJSch.addIdentity(path);
+			loadedIdentities.add(path);
+		}
 	}
 
 	private static class AWT_UserInfo implements UserInfo,
-- 
1.6.0.174.gd789c
Previous: Shawn O. Pearce
Message 5 of 5 in “Ignore unreadable SSH private keys when autoloading identities”
  1. 1/2 Ignore unreadable SSH private keys when autoloading identitiesShawn O. Pearce, Aug 27, 2008
  2. 2/2 pgm.push: Ensure SSH connections are closedShawn O. Pearce, Aug 27, 2008
  3. Marek ZawirskiAug 27, 2008
  4. Shawn O. PearceAug 27, 2008
  5. 1/2 Ignore unreadable SSH private keys when autoloading identitiesShawn O. Pearce, Aug 28, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.