Repository: mina-sshd
Updated Branches:
  refs/heads/master c9524a24a -> 8b9024e4d


[SSHD-538] Handle correctly paths with double-slashes in them

Added similar code in ScpHelper


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/8b9024e4
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/8b9024e4
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/8b9024e4

Branch: refs/heads/master
Commit: 8b9024e4de50be566187aa4e31aa0312996ad479
Parents: c9524a2
Author: Lyor Goldstein <[email protected]>
Authored: Sun Jul 26 17:53:11 2015 +0300
Committer: Lyor Goldstein <[email protected]>
Committed: Sun Jul 26 17:53:11 2015 +0300

----------------------------------------------------------------------
 .../org/apache/sshd/common/scp/ScpHelper.java   | 35 ++++++++++++--------
 .../server/subsystem/sftp/SftpSubsystem.java    |  6 ++++
 2 files changed, 27 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8b9024e4/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java 
b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
index d61dbb8..0829d69 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
@@ -30,6 +30,7 @@ import java.nio.file.AccessDeniedException;
 import java.nio.file.DirectoryStream;
 import java.nio.file.FileSystem;
 import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
 import java.nio.file.LinkOption;
 import java.nio.file.Path;
 import java.nio.file.attribute.BasicFileAttributeView;
@@ -408,10 +409,11 @@ public class ScpHelper extends AbstractLoggingBean {
         for (String pattern : paths) {
             pattern = pattern.replace('/', File.separatorChar);
 
-            int idx = pattern.indexOf('*');
+            int idx = pattern.indexOf('*'); // check if wildcard used
             if (idx >= 0) {
                 String basedir = "";
-                int lastSep = pattern.substring(0, 
idx).lastIndexOf(File.separatorChar);
+                String fixedPart = pattern.substring(0, idx);
+                int lastSep = fixedPart.lastIndexOf(File.separatorChar);
                 if (lastSep >= 0) {
                     basedir = pattern.substring(0, lastSep);
                     pattern = pattern.substring(lastSep + 1);
@@ -435,14 +437,7 @@ public class ScpHelper extends AbstractLoggingBean {
                     }
                 }
             } else {
-                String basedir = "";
-                int lastSep = pattern.lastIndexOf(File.separatorChar);
-                if (lastSep >= 0) {
-                    basedir = pattern.substring(0, lastSep);
-                    pattern = pattern.substring(lastSep + 1);
-                }
-
-                send(resolveLocalPath(basedir, pattern), recursive, preserve, 
bufferSize, options);
+                send(resolveLocalPath(pattern), recursive, preserve, 
bufferSize, options);
             }
         }
     }
@@ -486,12 +481,24 @@ public class ScpHelper extends AbstractLoggingBean {
         }
     }
 
-    public Path resolveLocalPath(String remotePath) throws IOException {
+    /**
+     * @param commandPath The original command path using <U>local</U> 
separator
+     * @return The resolved absolute and normalized local path {@link Path}
+     * @throws IOException If failed to resolve the path
+     * @throws InvalidPathException If invalid local path value
+     */
+    public Path resolveLocalPath(String commandPath) throws IOException, 
InvalidPathException {
         // In case double slashes and other patterns are used 
-        String path = SelectorUtils.normalizePath(remotePath, "/");
+        String path = SelectorUtils.applySlashifyRules(commandPath, 
File.separatorChar);
         String localPath = SelectorUtils.translateToLocalPath(path);
-
-        return fileSystem.getPath(localPath);
+        Path lcl = fileSystem.getPath(localPath);
+        Path abs = lcl.isAbsolute() ? lcl : lcl.toAbsolutePath();
+        Path p = abs.normalize();
+        if (log.isTraceEnabled()) {
+            log.trace("resolveLocalPath({}) {}", commandPath, p);
+        }
+        
+        return p;
     }
 
     public void sendFile(Path path, boolean preserve, int bufferSize) throws 
IOException {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8b9024e4/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
 
b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
index 103c5db..1160d57 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
@@ -2925,6 +2925,12 @@ public class SftpSubsystem extends AbstractLoggingBean 
implements Command, Runna
         return abs.normalize();
     }
 
+    /**
+     * @param remotePath The remote path - separated by '/'
+     * @return The local {@link Path}
+     * @throws IOException If failed to resolve the local path
+     * @throws InvalidPathException If bad local path specification
+     */
     protected Path resolveFile(String remotePath) throws IOException, 
InvalidPathException {
         // In case double slashes and other patterns are used 
         String path = SelectorUtils.applySlashifyRules(remotePath, '/');

Reply via email to