Repository: mina-sshd Updated Branches: refs/heads/master f8a3e7299 -> 16fae05ab
[SSHD-730] Fix normalization problem with links Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/16fae05a Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/16fae05a Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/16fae05a Branch: refs/heads/master Commit: 16fae05ab9b474f601ec44652c589df345b54ff3 Parents: f8a3e72 Author: Guillaume Nodet <gno...@apache.org> Authored: Thu Feb 22 13:26:15 2018 +0100 Committer: Guillaume Nodet <gno...@apache.org> Committed: Thu Feb 22 13:26:28 2018 +0100 ---------------------------------------------------------------------- .../apache/sshd/common/util/SelectorUtils.java | 67 +------------------- .../sftp/AbstractSftpSubsystemHelper.java | 14 +--- .../sshd/common/util/SelectorUtilsTest.java | 7 -- 3 files changed, 4 insertions(+), 84 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/16fae05a/sshd-core/src/main/java/org/apache/sshd/common/util/SelectorUtils.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/SelectorUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/SelectorUtils.java index 7fa1927..53bca90 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/SelectorUtils.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/SelectorUtils.java @@ -533,72 +533,7 @@ public final class SelectorUtils { return ret; } - /** - * Normalizes the path by removing '.', '..' and double separators (e.g. '//') - * - * @param path Original path - ignored if {@code null}/empty - * @param separator The separator used for the path components - * @return normalized path - */ - public static String normalizePath(String path, String separator) { - if (GenericUtils.isEmpty(path)) { - return path; - } - - boolean startsWithSeparator = path.startsWith(separator); - List<String> tokens = tokenizePath(path, separator); - int removedDots = 0; - // clean up - for (int i = tokens.size() - 1; i >= 0; i--) { - String t = tokens.get(i); - if (GenericUtils.isEmpty(t)) { - tokens.remove(i); - } else if (t.equals(".")) { - tokens.remove(i); - removedDots++; - } else if (t.equals("..")) { - tokens.remove(i); - removedDots++; - if (i >= 1) { - tokens.remove(--i); - removedDots++; - } - } - } - - if (GenericUtils.isEmpty(tokens)) { - if (removedDots > 0) { - return ""; // had some "." and ".." after which we remained with no path - } else { - return separator; // it was all separators - } - } - - // serialize - StringBuilder buffer = new StringBuilder(path.length()); - for (int index = 0; index < tokens.size(); index++) { - String token = tokens.get(index); - if (index == 0) { - if (startsWithSeparator) { - buffer.append(separator); - } else if (OsUtils.isWin32() && isWindowsDriveSpecified(token)) { - buffer.append(separator); - } - } else { - buffer.append(separator); - } - buffer.append(token); - - // for root Windows drive we need to return "C:/" or we get errors from the local file system - if ((tokens.size() == 1) && OsUtils.isWin32() && isWindowsDriveSpecified(token)) { - buffer.append(separator); - } - } - - return buffer.toString(); - } - - /** + /** /** * Converts a path to one matching the target file system by applying the * "slashification" rules, converting it to a local path and * then translating its separator to the target file system one (if different http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/16fae05a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java index 2e043fb..262bd05 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/AbstractSftpSubsystemHelper.java @@ -1786,13 +1786,11 @@ public abstract class AbstractSftpSubsystemHelper protected void sendLink(Buffer buffer, int id, String link) throws IOException { //in case we are running on Windows String unixPath = link.replace(File.separatorChar, '/'); - //normalize the given path, use *nix style separator - String normalizedPath = SelectorUtils.normalizePath(unixPath, "/"); buffer.putByte((byte) SftpConstants.SSH_FXP_NAME); buffer.putInt(id); buffer.putInt(1); // one response - buffer.putString(normalizedPath); + buffer.putString(unixPath); /* * As per the spec (https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-6.10): @@ -1803,7 +1801,7 @@ public abstract class AbstractSftpSubsystemHelper Map<String, Object> attrs = Collections.emptyMap(); int version = getVersion(); if (version == SftpConstants.SFTP_V3) { - buffer.putString(SftpHelper.getLongName(normalizedPath, attrs)); + buffer.putString(SftpHelper.getLongName(unixPath, attrs)); } writeAttrs(buffer, attrs); @@ -1819,16 +1817,10 @@ public abstract class AbstractSftpSubsystemHelper String originalPath = f.toString(); //in case we are running on Windows String unixPath = originalPath.replace(File.separatorChar, '/'); - //normalize the given path, use *nix style separator - String normalizedPath = SelectorUtils.normalizePath(unixPath, "/"); - if (normalizedPath.length() == 0) { - normalizedPath = "/"; - } - buffer.putString(normalizedPath); + buffer.putString(unixPath); int version = getVersion(); if (version == SftpConstants.SFTP_V3) { - f = resolveFile(normalizedPath); buffer.putString(getLongName(f, getShortName(f), attrs)); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/16fae05a/sshd-core/src/test/java/org/apache/sshd/common/util/SelectorUtilsTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/SelectorUtilsTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/SelectorUtilsTest.java index 5106614..1c56379 100644 --- a/sshd-core/src/test/java/org/apache/sshd/common/util/SelectorUtilsTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/common/util/SelectorUtilsTest.java @@ -141,11 +141,4 @@ public class SelectorUtilsTest extends BaseTestSupport { } } - @Test - public void testNormalizeWindowsPath() { - Assume.assumeTrue("Not tested on Windows", OsUtils.isWin32()); - String path = detectTargetFolder().toString().replace(File.separatorChar, '/'); - String actual = SelectorUtils.normalizePath(path, "/"); - assertEquals("Mismatched result", "/" + path, actual); - } }