Skip to content

Commit

Permalink
Check file attributes if . is not part of a directory listing
Browse files Browse the repository at this point in the history
  • Loading branch information
robtimus committed Jul 31, 2019
1 parent 08bfbc4 commit 6c98dd8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ DirectoryStream<Path> newDirectoryStream(final SFTPPath path, Filter<? super Pat
}

if (!isDirectory) {
throw new NotDirectoryException(path.path());
// https://github.com/robtimus/sftp-fs/issues/4: don't fail immediately but check the attributes
// Follow links to ensure the directory attribute can be read correctly
SftpATTRS attributes = channel.readAttributes(path.path(), true);
if (!attributes.isDir()) {
throw new NotDirectoryException(path.path());
}
}
return new SFTPPathDirectoryStream(path, entries, filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -72,6 +73,10 @@ public abstract class AbstractSFTPFileSystemTest {

@BeforeClass
public static void setupClass() throws NoSuchAlgorithmException, IOException {
setupClass(new FixedSftpSubsystem.Factory());
}

protected static void setupClass(SftpSubsystemFactory subSystemFactory) throws NoSuchAlgorithmException, IOException {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = generator.generateKeyPair();

Expand All @@ -86,7 +91,7 @@ public boolean authenticate(String username, String password, ServerSession sess
return USERNAME.equals(username) && PASSWORD.equals(password);
}
});
sshServer.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new FixedSftpSubsystem.Factory()));
sshServer.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(subSystemFactory));

rootPath = Files.createTempDirectory("sftp-fs");
defaultDir = rootPath.resolve("home");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SFTPFileSystemWithoutSystemDirsTest.java
* Copyright 2019 Rob Spoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.robtimus.filesystems.sftp;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.junit.BeforeClass;
import com.github.robtimus.filesystems.sftp.server.FixedSftpSubsystem;

@SuppressWarnings("javadoc")
public class SFTPFileSystemWithoutSystemDirsTest extends SFTPFileSystemTest {

@BeforeClass
public static void setupClass() throws NoSuchAlgorithmException, IOException {
setupClass(new FixedSftpSubsystem.FactoryWithoutSystemDirs());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.io.IoUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.server.subsystem.sftp.DirectoryHandle;
import org.apache.sshd.server.subsystem.sftp.Handle;
import org.apache.sshd.server.subsystem.sftp.SftpEventListener;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystem;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
Expand Down Expand Up @@ -63,4 +66,30 @@ public Command create() {
return subsystem;
}
}

public static final class FactoryWithoutSystemDirs extends SftpSubsystemFactory {

@Override
public Command create() {
SftpSubsystem subsystem = new FixedSftpSubsystem(getExecutorService(), isShutdownOnExit(), getUnsupportedAttributePolicy());
Collection<? extends SftpEventListener> listeners = getRegisteredListeners();
if (GenericUtils.size(listeners) > 0) {
for (SftpEventListener l : listeners) {
subsystem.addSftpEventListener(l);
}
}
subsystem.addSftpEventListener(new SftpEventListener() {
@Override
public void open(ServerSession session, String remoteHandle, Handle localHandle) throws IOException {
if (localHandle instanceof DirectoryHandle) {
DirectoryHandle directoryHandle = (DirectoryHandle) localHandle;
directoryHandle.markDotSent();
directoryHandle.markDotDotSent();
}
}
});

return subsystem;
}
}
}

0 comments on commit 6c98dd8

Please sign in to comment.