HDFS-15252. HttpFS: setWorkingDirectory should not accept invalid paths. Contributed by hemanthboyina.

(cherry picked from commit 736659e0e1)
This commit is contained in:
Takanobu Asanuma 2020-04-02 19:21:02 +09:00 committed by Wei-Chiu Chuang
parent 2da3356428
commit 14fddba715
2 changed files with 17 additions and 1 deletions

View File

@ -47,6 +47,7 @@ import org.apache.hadoop.fs.permission.AclStatus;
import org.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.DFSUtilClient;
import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy;
import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy;
import org.apache.hadoop.hdfs.protocol.FsPermissionExtension;
@ -802,6 +803,11 @@ public class HttpFSFileSystem extends FileSystem
*/
@Override
public void setWorkingDirectory(Path newDir) {
String result = newDir.toUri().getPath();
if (!DFSUtilClient.isValidName(result)) {
throw new IllegalArgumentException(
"Invalid DFS directory name " + result);
}
workingDir = newDir;
}

View File

@ -59,6 +59,7 @@ import org.apache.hadoop.ipc.RemoteException;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.test.HFSTestCase;
import org.apache.hadoop.test.HadoopUsersConfTestHelper;
import org.apache.hadoop.test.LambdaTestUtils;
import org.apache.hadoop.test.TestDir;
import org.apache.hadoop.test.TestDirHelper;
import org.apache.hadoop.test.TestHdfs;
@ -557,9 +558,18 @@ public abstract class BaseTestHttpFSWith extends HFSTestCase {
fs = getHttpFSFileSystem();
fs.setWorkingDirectory(new Path("/tmp"));
workingDir = fs.getWorkingDirectory();
fs.close();
assertEquals(workingDir.toUri().getPath(),
new Path("/tmp").toUri().getPath());
final FileSystem httpFs = getHttpFSFileSystem();
LambdaTestUtils.intercept(IllegalArgumentException.class,
"Invalid DFS directory name /foo:bar",
() -> httpFs.setWorkingDirectory(new Path("/foo:bar")));
fs.setWorkingDirectory(new Path("/bar"));
workingDir = fs.getWorkingDirectory();
httpFs.close();
fs.close();
assertEquals(workingDir.toUri().getPath(),
new Path("/bar").toUri().getPath());
}
private void testTrashRoot() throws Exception {