|
1 | 1 | /*
|
2 |
| - * Copyright 2016-2022 the original author or authors. |
| 2 | + * Copyright 2016-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.integration.sftp;
|
18 | 18 |
|
19 | 19 | 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; |
20 | 24 | import java.util.Collections;
|
| 25 | +import java.util.Objects; |
21 | 26 |
|
| 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; |
22 | 30 | 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; |
23 | 33 | import org.apache.sshd.server.SshServer;
|
24 | 34 | import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
|
25 | 35 | import org.apache.sshd.sftp.client.SftpClient;
|
@@ -71,7 +81,7 @@ public static void createServer() throws Exception {
|
71 | 81 | });
|
72 | 82 | sftpFactory.addSftpEventListener(EVENT_LISTENER);
|
73 | 83 | server.setSubsystemFactories(Collections.singletonList(sftpFactory));
|
74 |
| - server.setFileSystemFactory(new VirtualFileSystemFactory(getRemoteTempFolder().toPath())); |
| 84 | + server.setFileSystemFactory(new FixedVirtualFileSystemFactory(getRemoteTempFolder())); |
75 | 85 | server.start();
|
76 | 86 | port = server.getPort();
|
77 | 87 | }
|
@@ -101,4 +111,37 @@ public static void stopServer() throws Exception {
|
101 | 111 | }
|
102 | 112 | }
|
103 | 113 |
|
| 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 | + |
104 | 147 | }
|
0 commit comments