[SSHD-774] SshConfigFileReader cannot parse tab delimiter in config file
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/a5c23a55 Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/a5c23a55 Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/a5c23a55 Branch: refs/heads/master Commit: a5c23a5525d73c96a5226d72403df765326da381 Parents: 6fbed0c Author: Lyor Goldstein <lyor.goldst...@gmail.com> Authored: Sat Sep 23 17:50:11 2017 +0300 Committer: Lyor Goldstein <lyor.goldst...@gmail.com> Committed: Sat Sep 23 17:50:11 2017 +0300 ---------------------------------------------------------------------- .../java/org/apache/sshd/client/SshKeyScan.java | 4 +++- .../KnownHostsServerKeyVerifier.java | 2 +- .../sshd/client/subsystem/sftp/SftpCommand.java | 2 +- .../sshd/common/config/SshConfigFileReader.java | 2 +- .../common/config/SshConfigFileReaderTest.java | 25 ++++++++++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5c23a55/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java b/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java index 162732d..5ded6bf 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java @@ -244,7 +244,9 @@ public class SshKeyScan implements Channel, Callable<Void>, ServerKeyVerifier, S client.start(); for (String line = rdr.readLine(); line != null; line = rdr.readLine()) { - String[] hosts = GenericUtils.split(GenericUtils.trimToEmpty(line), ','); + line = GenericUtils.replaceWhitespaceAndTrim(line); + + String[] hosts = GenericUtils.split(line, ','); if (GenericUtils.isEmpty(hosts)) { continue; } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5c23a55/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifier.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifier.java b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifier.java index 1fc9429..f816fb1 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifier.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifier.java @@ -335,7 +335,7 @@ public class KnownHostsServerKeyVerifier List<String> lines = new ArrayList<>(); synchronized (updateLock) { - int matchingIndex = -1; // read all lines but replace the + int matchingIndex = -1; // read all lines but replace the updated one try (BufferedReader rdr = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { for (String line = rdr.readLine(); line != null; line = rdr.readLine()) { // skip if already replaced the original line http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5c23a55/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java index cc720c7..4865431 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpCommand.java @@ -123,7 +123,7 @@ public class SftpCommand implements Channel { break; } - line = line.trim(); + line = GenericUtils.replaceWhitespaceAndTrim(line); if (GenericUtils.isEmpty(line)) { continue; } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5c23a55/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java b/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java index 4a93294..6bb24d7 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java @@ -182,7 +182,7 @@ public final class SshConfigFileReader { Properties props = new Properties(); int lineNumber = 1; for (String line = rdr.readLine(); line != null; line = rdr.readLine(), lineNumber++) { - line = GenericUtils.trimToEmpty(line); + line = GenericUtils.replaceWhitespaceAndTrim(line); if (GenericUtils.isEmpty(line)) { continue; } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/a5c23a55/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java b/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java index 4477da1..536946a 100644 --- a/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/common/config/SshConfigFileReaderTest.java @@ -20,6 +20,9 @@ package org.apache.sshd.common.config; import java.io.IOException; +import java.io.Reader; +import java.io.StreamCorruptedException; +import java.io.StringReader; import java.net.URL; import java.util.Collection; import java.util.Collections; @@ -187,6 +190,28 @@ public class SshConfigFileReaderTest extends BaseTestSupport { testConfigureCompressionFromStringAcceptsCombinedValues(BuiltinCompressions.class, NamedResource.NAME_EXTRACTOR); } + @Test(expected = StreamCorruptedException.class) + public void testInvalidDelimiter() throws IOException { + String line = getClass().getSimpleName() + "+" + getCurrentTestName(); + try (Reader rdr = new StringReader(line)) { + Properties props = SshConfigFileReader.readConfigFile(rdr, true); + fail("Unexpected success: " + props); + } + } + + @Test // SSHD-774 + public void testTabDelimiter() throws IOException { + String name = getClass().getSimpleName(); + String expected = getCurrentTestName(); + Properties props; + try (Reader rdr = new StringReader(name + "\t" + expected)) { + props = SshConfigFileReader.readConfigFile(rdr, true); + } + + String actual = props.getProperty(name); + assertEquals("Mismatched read configuration value", expected, actual); + } + private static <E extends Enum<E> & CompressionFactory> void testConfigureCompressionFromStringAcceptsCombinedValues( Class<E> facs, Function<? super E, String> configValueXformer) { for (E expected : facs.getEnumConstants()) {