git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonas Fonseca <fonseca@diku.dk>
To: sverre@rabbelier.nl
Cc: Robin Rosenberg <robin.rosenberg@dewire.com>,
	"Shawn O. Pearce" <spearce@spearce.org>,
	git@vger.kernel.org
Subject: [JGIT PATCH]  Test and fix handling of quotes in ~/.ssh/config
Date: Sun, 21 Sep 2008 13:25:19 +0200	[thread overview]
Message-ID: <20080921112519.GA24200@diku.dk> (raw)
In-Reply-To: <bd6139dc0809201819o5d6eb5b1r7bf0e46702c711d7@mail.gmail.com>

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 <fonseca@diku.dk>
---
 .../spearce/jgit/transport/OpenSshConfigTest.java  |   26 ++++++++++++++++++++
 .../org/spearce/jgit/transport/OpenSshConfig.java  |    5 ++-
 2 files changed, 29 insertions(+), 2 deletions(-)

 Sverre Rabbelier <alturin@gmail.com> 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 <fonseca@diku.dk> 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

  parent reply	other threads:[~2008-09-21 11:33 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     ` Jonas Fonseca [this message]
2008-09-22 20:42       ` [JGIT PATCH] Test and fix handling of quotes in ~/.ssh/config 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               ` [JGIT PATCH] Add tests for handling of parsing errors in OpenSshConfig Jonas Fonseca
2008-09-25 11:33                 ` 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=20080921112519.GA24200@diku.dk \
    --to=fonseca@diku.dk \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).