Subject: [JGIT PATCH] Test and fix handling of quotes in ~/.ssh/config

From: Jonas Fonseca <[email-masked]>
Date: Sun, 21 Sep 2008 13:25:19 +0200

Removal of quoting had an off-by-one error, and was not handled for the
patterns used for the Host entry.

Signed-off-by: Jonas Fonseca <[email-masked]>
---
 .../spearce/jgit/transport/OpenSshConfigTest.java  |   26 ++++++++++++++++++++
 .../org/spearce/jgit/transport/OpenSshConfig.java  |    5 ++-
 2 files changed, 29 insertions(+), 2 deletions(-)

 Sverre Rabbelier <[email-masked]> wrote Sun, Sep 21, 2008:
 > Heya,
 
 Allo, 
 
 > I'm not involved with JGit, so feel free to discared this mail :).
 
 Thanks, it made me dig a bit further, and find a bug.
 
 > On Sun, Sep 21, 2008 at 00:29, Jonas Fonseca <[email-masked]> wrote:
 > > -                       final String[] parts = line.split("[ \t=]", 2);
 > > +                       final String[] parts = line.split("[ \t]*[= \t]", 2);
 > 
 > Unless I'm guessing the purpose of this split wrong, wouldn't it be
 > even better to go for "[ \t]*=[ \t]+", or something like that (e.g.,
 > to allow for multiple tabs/spaces at the end as well).
 
 The code using the split result trims tabs and spaces at the end, so the
 main purpose of the regex is to find something that splits. This
 something can be tabs/spaces or _optionally_ one '='. 

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 8c1133d..ad6e79c 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
@@ -105,6 +105,32 @@ config("Host\tfirst\n" +
 		assertEquals("last.tld", osc.lookup("last").getHostName());
 	}
 
+	public void testQuoteParsing() throws Exception {
+		config("Host \"good\"\n" +
+			" HostName=\"good.tld\"\n" +
+			" Port=\"6007\"\n" +
+			" User=\"gooduser\"\n" +
+			"Host multiple unquoted and \"quoted\" \"hosts\"\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");
+		assertEquals("good.tld", osc.lookup("good").getHostName());
+		assertEquals("gooduser", osc.lookup("good").getUser());
+		assertEquals(6007, osc.lookup("good").getPort());
+		assertEquals(2222, osc.lookup("multiple").getPort());
+		assertEquals(2222, osc.lookup("quoted").getPort());
+		assertEquals(2222, osc.lookup("and").getPort());
+		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 testAlias_DoesNotMatch() throws Exception {
 		config("Host orcz\n" + "\tHostName repo.or.cz\n");
 		final Host h = osc.lookup("repo.or.cz");
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 a9c6c12..b08d5c6 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java
@@ -173,7 +173,8 @@ public Host lookup(final String hostName) {
 
 			if ("Host".equalsIgnoreCase(keyword)) {
 				current.clear();
-				for (final String name : argValue.split("[ \t]")) {
+				for (final String pattern : argValue.split("[ \t]")) {
+					final String name = dequote(pattern);
 					Host c = m.get(name);
 					if (c == null) {
 						c = new Host();
@@ -243,7 +244,7 @@ private static boolean isHostMatch(final String pattern, final String name) {
 
 	private static String dequote(final String value) {
 		if (value.startsWith("\"") && value.endsWith("\""))
-			return value.substring(1, value.length() - 2);
+			return value.substring(1, value.length() - 1);
 		return value;
 	}
 
-- 
1.6.0.2.444.gf2494

-- 
Jonas Fonseca