[ 
https://issues.apache.org/jira/browse/SSHD-730?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16371909#comment-16371909
 ] 

Guillaume Nodet commented on SSHD-730:
--------------------------------------

So what about the following patch then:
{code}
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 7fa19279..53bca902 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
      * &quot;slashification&quot; rules, converting it to a local path and
      * then translating its separator to the target file system one (if 
different
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 2e043fbf..262bd053 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));
         }
 
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 51066141..1c56379f 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);
-    }
 }
{code}

> Relative symbolic links with .. is not resolved properly by sftp readLink
> -------------------------------------------------------------------------
>
>                 Key: SSHD-730
>                 URL: https://issues.apache.org/jira/browse/SSHD-730
>             Project: MINA SSHD
>          Issue Type: Bug
>    Affects Versions: 1.1.0, 1.2.0, 1.3.0
>            Reporter: Lukas Waldmann
>            Assignee: Goldstein Lyor
>            Priority: Minor
>
> Using sftp to resolve symbolic links doesn't return proper resolution in case 
> symbolic link contains ..
> Since version 1.1 the sendLink function of SftpSubsystem uses
> normalizedPath = SelectorUtils.normalizePath(unixPath, "/");
> to normalize path
> This function however return invalid path in case link contains ".."
> So for example if link is "../test/file" than normalizePath returns 
> "test/file" which is invalid because in can not be resolved properly in the 
> context of the current directory which symbolic link is referring to.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to