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

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

From
Jonas Fonseca <fonseca@diku.dk>
Date
Sep 21, 2008, 11:25 UTC
Message-ID
<20080921112519.GA24200@diku.dk>
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
Previous: Jonas FonsecaNext: Robin Rosenberg
Message 6 of 16 in “Fixed a bug that caused tabs in ~/.ssh/config to break parsing”
  1. Fixed a bug that caused tabs in ~/.ssh/config to break parsingGilion Goudsmit, Sep 20, 2008
  2. Add test for OpenSshConfig separator parsingJonas Fonseca, Sep 20, 2008
  3. Add test for OpenSshConfig separator parsingJonas Fonseca, Sep 20, 2008
  4. Robin RosenbergSep 20, 2008
  5. Jonas FonsecaSep 20, 2008
  6. Test and fix handling of quotes in ~/.ssh/configJonas Fonseca, Sep 21, 2008
  7. Robin RosenbergSep 22, 2008
  8. Shawn O. PearceSep 22, 2008
  9. Jonas FonsecaSep 24, 2008
  10. Shawn O. PearceSep 24, 2008
  11. Add tests for handling of parsing errors in OpenSshConfigJonas Fonseca, Sep 25, 2008
  12. Robin RosenbergSep 25, 2008
  13. Improve handling of parsing errors in OpenSshConfigJonas Fonseca, Sep 25, 2008
  14. Jonas FonsecaSep 25, 2008
  15. Shawn O. PearceSep 25, 2008
  16. Robin RosenbergSep 25, 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.