Skip to content

Commit ce47595

Browse files
committed
Upgrade to MINA SSHD 2.10.0
* Fix test environment for SFTP server with custom `VirtualFileSystemFactory` and `RootedFileSystemProvider` to solve the problem with non-normalized returned path
1 parent 20d5f62 commit ce47595

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ext {
4747
modifiedFiles =
4848
files(grgit.status().unstaged.modified).filter { f -> f.name.endsWith('.java') || f.name.endsWith('.kt') }
4949

50-
apacheSshdVersion = '2.9.2'
50+
apacheSshdVersion = '2.10.0'
5151
artemisVersion = '2.29.0'
5252
aspectjVersion = '1.9.19'
5353
assertjVersion = '3.24.2'

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/SftpTestSupport.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 the original author or authors.
2+
* Copyright 2016-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,19 @@
1717
package org.springframework.integration.sftp;
1818

1919
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.file.FileSystem;
22+
import java.nio.file.InvalidPathException;
23+
import java.nio.file.Path;
2024
import java.util.Collections;
25+
import java.util.Objects;
2126

27+
import org.apache.sshd.common.file.root.RootedFileSystem;
28+
import org.apache.sshd.common.file.root.RootedFileSystemProvider;
29+
import org.apache.sshd.common.file.root.RootedPath;
2230
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
31+
import org.apache.sshd.common.session.SessionContext;
32+
import org.apache.sshd.common.util.io.IoUtils;
2333
import org.apache.sshd.server.SshServer;
2434
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
2535
import org.apache.sshd.sftp.client.SftpClient;
@@ -71,7 +81,7 @@ public static void createServer() throws Exception {
7181
});
7282
sftpFactory.addSftpEventListener(EVENT_LISTENER);
7383
server.setSubsystemFactories(Collections.singletonList(sftpFactory));
74-
server.setFileSystemFactory(new VirtualFileSystemFactory(getRemoteTempFolder().toPath()));
84+
server.setFileSystemFactory(new FixedVirtualFileSystemFactory(getRemoteTempFolder()));
7585
server.start();
7686
port = server.getPort();
7787
}
@@ -101,4 +111,37 @@ public static void stopServer() throws Exception {
101111
}
102112
}
103113

114+
private static class FixedVirtualFileSystemFactory extends VirtualFileSystemFactory {
115+
116+
FixedVirtualFileSystemFactory(File defaultHomeDir) {
117+
super(defaultHomeDir.toPath());
118+
}
119+
120+
@Override
121+
public FileSystem createFileSystem(SessionContext session) throws IOException {
122+
Path dir = getUserHomeDir(session);
123+
if (dir == null) {
124+
throw new InvalidPathException(session.getUsername(), "Cannot resolve home directory");
125+
}
126+
127+
return new FixedRootedFileSystemProvider().newFileSystem(dir, Collections.emptyMap());
128+
}
129+
130+
private static class FixedRootedFileSystemProvider extends RootedFileSystemProvider {
131+
132+
protected Path resolveLocalPath(RootedPath path) {
133+
Objects.requireNonNull(path, "No rooted path to resolve");
134+
RootedFileSystem rfs = path.getFileSystem();
135+
Path root = rfs.getRoot();
136+
Path resolved = IoUtils.chroot(root, path).normalize();
137+
if (!resolved.startsWith(root)) {
138+
throw new InvalidPathException(root.toString(), "Not under root");
139+
}
140+
return resolved;
141+
}
142+
143+
}
144+
145+
}
146+
104147
}

0 commit comments

Comments
 (0)