All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonas Fonseca <fonseca@diku.dk>
To: "Shawn O. Pearce" <spearce@spearce.org>
Cc: Robin Rosenberg <robin.rosenberg.lists@dewire.com>,
	sverre@rabbelier.nl, git@vger.kernel.org
Subject: [JGIT PATCH] Add tests for handling of parsing errors in OpenSshConfig
Date: Thu, 25 Sep 2008 10:39:34 +0200	[thread overview]
Message-ID: <20080925083934.GB10273@diku.dk> (raw)
In-Reply-To: <20080924233104.GG3669@spearce.org>

Badly quoted entries are now ignored similar to how bad port number are
currently ignored. A check for negative port numbers is now performed
so that they also will be ignored.

Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
---
 .../spearce/jgit/transport/OpenSshConfigTest.java  |   32 +++++++++++++++---
 .../org/spearce/jgit/transport/OpenSshConfig.java  |   34 +++++++++++++++----
 2 files changed, 53 insertions(+), 13 deletions(-)

 Shawn O. Pearce <spearce@spearce.org> wrote Wed, Sep 24, 2008:
 > Jonas Fonseca <fonseca@diku.dk> wrote:
 > > I propose to simply remove these hosts from the host map and clear
 > > the current host list so that no values will be saved, effectively
 > > causing invalid hosts to result in the same as unknown hosts.
 > 
 > Yea, that seems quite reasonable.
 > 
 > If you want more debugging than that on your ~/.ssh/config file then
 > run OpenSSH tools on it.  Hell, I can't count the number of times
 > I've made typos in there and couldn't figure out why it was still
 > asking for a password, etc.  And that's just the OpenSSH command
 > line tools.

 Heh, so below makes JGit compatible with OpenSSH's silent treatment. I
 also fixed the .ssh/config parser to ignore negative port numbers.

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java
index ad6e79c..ccd7400 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/OpenSshConfigTest.java
@@ -114,11 +114,7 @@ config("Host \"good\"\n" +
 			" Port=\"2222\"\n" +
 			"Host \"spaced\"\n" +
 			"# Bad host name, but testing preservation of spaces\n" +
-			" HostName=\" spaced\ttld \"\n" +
-			"# Misbalanced quotes\n" +
-			"Host \"bad\"\n" +
-			"# OpenSSH doesn't allow this but ...\n" +
-			" HostName=bad.tld\"\n");
+			" HostName=\" spaced\ttld \"\n");
 		assertEquals("good.tld", osc.lookup("good").getHostName());
 		assertEquals("gooduser", osc.lookup("good").getUser());
 		assertEquals(6007, osc.lookup("good").getPort());
@@ -128,7 +124,31 @@ config("Host \"good\"\n" +
 		assertEquals(2222, osc.lookup("unquoted").getPort());
 		assertEquals(2222, osc.lookup("hosts").getPort());
 		assertEquals(" spaced\ttld ", osc.lookup("spaced").getHostName());
-		assertEquals("bad.tld\"", osc.lookup("bad").getHostName());
+	}
+
+	public void testParsingErrors() throws Exception {
+		config("Host negativeportnumber\n" +
+		       "Port -200\n" +
+		       "Host badportnumber\n" +
+		       "Port twentytwo\n" +
+		       "Host badportquote\n" +
+		       "Port \"2222\n" +
+		       "Host badportquote2\n" +
+		       "Port 2222\"\n" +
+		       "# Misbalanced quotes\n" +
+		       "Host badly \"quoted\n" +
+		       "HostName=unknown\n");
+
+		assertEquals(DefaultSshSessionFactory.SSH_PORT,
+			     osc.lookup("negativeportnumber").getPort());
+		assertEquals(DefaultSshSessionFactory.SSH_PORT,
+			     osc.lookup("badportnumber").getPort());
+		assertEquals(DefaultSshSessionFactory.SSH_PORT,
+			     osc.lookup("badportquote").getPort());
+		assertEquals(DefaultSshSessionFactory.SSH_PORT,
+			     osc.lookup("badportquote2").getPort());
+		assertNotSame("unknown", osc.lookup("badly").getHostName());
+		assertNotSame("unknown", osc.lookup("\"quoted"));
 	}
 
 	public void testAlias_DoesNotMatch() throws Exception {
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java b/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java
index b08d5c6..6a3f2c1 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java
@@ -46,6 +46,7 @@
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -175,6 +176,15 @@ public Host lookup(final String hostName) {
 				current.clear();
 				for (final String pattern : argValue.split("[ \t]")) {
 					final String name = dequote(pattern);
+					if (name == null) {
+						/* Prune all the current hosts. */
+						Iterator<Host> it = m.values().iterator();
+						while (it.hasNext())
+							if (current.contains(it.next()))
+								it.remove();
+						current.clear();
+						break;
+					}
 					Host c = m.get(name);
 					if (c == null) {
 						c = new Host();
@@ -192,17 +202,23 @@ public Host lookup(final String hostName) {
 				continue;
 			}
 
+			final String value = dequote(argValue);
+			if (value == null)
+				continue;
+
 			if ("HostName".equalsIgnoreCase(keyword)) {
 				for (final Host c : current)
 					if (c.hostName == null)
-						c.hostName = dequote(argValue);
+						c.hostName = value;
 			} else if ("User".equalsIgnoreCase(keyword)) {
 				for (final Host c : current)
 					if (c.user == null)
-						c.user = dequote(argValue);
+						c.user = value;
 			} else if ("Port".equalsIgnoreCase(keyword)) {
 				try {
-					final int port = Integer.parseInt(dequote(argValue));
+					final int port = Integer.parseInt(value);
+					if (port < 0)
+						continue;
 					for (final Host c : current)
 						if (c.port == 0)
 							c.port = port;
@@ -212,15 +228,15 @@ public Host lookup(final String hostName) {
 			} else if ("IdentityFile".equalsIgnoreCase(keyword)) {
 				for (final Host c : current)
 					if (c.identityFile == null)
-						c.identityFile = toFile(dequote(argValue));
+						c.identityFile = toFile(value);
 			} else if ("PreferredAuthentications".equalsIgnoreCase(keyword)) {
 				for (final Host c : current)
 					if (c.preferredAuthentications == null)
-						c.preferredAuthentications = nows(dequote(argValue));
+						c.preferredAuthentications = nows(value);
 			} else if ("BatchMode".equalsIgnoreCase(keyword)) {
 				for (final Host c : current)
 					if (c.batchMode == null)
-						c.batchMode = yesno(dequote(argValue));
+						c.batchMode = yesno(value);
 			}
 		}
 
@@ -243,8 +259,12 @@ private static boolean isHostMatch(final String pattern, final String name) {
 	}
 
 	private static String dequote(final String value) {
-		if (value.startsWith("\"") && value.endsWith("\""))
+		final boolean hasStart = value.startsWith("\"");
+		final boolean hasEnd = value.endsWith("\"");
+		if (hasStart && hasEnd)
 			return value.substring(1, value.length() - 1);
+		if (hasStart || hasEnd)
+			return null;
 		return value;
 	}
 
-- 
tg: (cf67dfc..) jf/opensshconfigquote (depends on: master)

-- 
Jonas Fonseca

  reply	other threads:[~2008-09-25  8:41 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-20 20:33 [JGIT PATCH] Fixed a bug that caused tabs in ~/.ssh/config to break parsing Gilion Goudsmit
2008-09-20 21:48 ` [JGIT PATCH] Add test for OpenSshConfig separator parsing Jonas Fonseca
2008-09-20 22:33   ` Jonas Fonseca
2008-09-20 22:18 ` [JGIT PATCH] Fixed a bug that caused tabs in ~/.ssh/config to break parsing Robin Rosenberg
2008-09-20 22:29 ` Jonas Fonseca
     [not found]   ` <bd6139dc0809201819o5d6eb5b1r7bf0e46702c711d7@mail.gmail.com>
2008-09-21 11:25     ` [JGIT PATCH] Test and fix handling of quotes in ~/.ssh/config Jonas Fonseca
2008-09-22 20:42       ` Robin Rosenberg
2008-09-22 21:07         ` Shawn O. Pearce
2008-09-24 23:25           ` Jonas Fonseca
2008-09-24 23:31             ` Shawn O. Pearce
2008-09-25  8:39               ` Jonas Fonseca [this message]
2008-09-25 11:33                 ` [JGIT PATCH] Add tests for handling of parsing errors in OpenSshConfig Robin Rosenberg
2008-09-25 13:29                   ` [JGIT RFC PATCH] Improve " Jonas Fonseca
2008-09-25 14:30                     ` Jonas Fonseca
2008-09-25 15:16                     ` Shawn O. Pearce
2008-09-25  6:26             ` [JGIT PATCH] Test and fix handling of quotes in ~/.ssh/config Robin Rosenberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080925083934.GB10273@diku.dk \
    --to=fonseca@diku.dk \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg.lists@dewire.com \
    --cc=spearce@spearce.org \
    --cc=sverre@rabbelier.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.