为什么FTPWebRequest或WebRequest一般都不接受/../路径?

时间:2022-05-21 05:43:29

I am trying to automate some upload/download tasks from an ftp web server. When I connect to the server through client, or through Firefox even, in order to get to my directory, I have to specify a path like this:

我试图从ftp Web服务器自动执行一些上传/下载任务。当我通过客户端或通过Firefox连接到服务器时,为了进入我的目录,我必须指定这样的路径:

ftp://ftpserver.com/../AB00000/incoming/files

If I try to access this:

如果我尝试访问此内容:

ftp://ftpserver.com/AB00000/incoming/files

The server throws an error that the directory does not exist. So, the problem:

服务器抛出该目录不存在的错误。那么,问题是:

I am trying to create an FTPWebRequest with the first ftp address, but it always parses out the "/../" part and then my server says the path doesn't exist.

我试图用第一个ftp地址创建一个FTPWebRequest,但它总是解析出“/../”部分然后我的服务器说该路径不存在。

I've tried these:

我试过这些:

    Uri target = new Uri("ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);

and

string target = "ftp://ftpserver.com/../AB00000/incoming/files";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);

In the first bit, the path is already incorrect when the Uri object is instantiated, in the second bit, it's after the WebRequest.Create method. Any ideas what's going on?

在第一位,当实例化Uri对象时,路径已经不正确,在第二位,它在WebRequest.Create方法之后。有什么想法正在发生什么?

EDIT:

Additionally, since I posted this, I have tried creating the URI with the no parse option. I have also tried something like this:

另外,由于我发布了这个,我尝试使用no parse选项创建URI。我也尝试过这样的事情:

string ftpserver = "ftp://ftpserver.com/../";
string path = "12345/01/01/file.toupload";

Uri = new Uri(ftpserver, path, true);

And it always parses out the root part ("/../").

它总是解析根部分(“/../”)。

3 个解决方案

#1


Try escaping the .. with something like:

尝试使用类似的东西逃避..

Uri target = new Uri("ftp://ftpserver.com/%2E%2E/AB00000/incoming/files");

That works according to this blog which I found in this discussion.

根据我在本次讨论中发现的博客,这是有效的。

#2


Not really sure about it, but it may be for security reasons, since allowing "/../" URIs would potentially let people navigate freely on any server's file system.

不太确定,但可能出于安全原因,因为允许“/../”URI可能会让人们在任何服务器的文件系统上*导航。

Also, the official URI RFC states that when resolving an URI one of the steps performed is actually the removal of "/../" segments, so it's not a problem in the C# library but it's regular URI behavior.

此外,官方URI RFC指出,在解析URI时,执行的步骤之一实际上是删除“/../”段,因此它不是C#库中的问题,而是常规URI行为。

#3


Have you tried using the @ symbol like so?

您是否尝试使用@符号?

Uri target = new Uri(@"ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);

#1


Try escaping the .. with something like:

尝试使用类似的东西逃避..

Uri target = new Uri("ftp://ftpserver.com/%2E%2E/AB00000/incoming/files");

That works according to this blog which I found in this discussion.

根据我在本次讨论中发现的博客,这是有效的。

#2


Not really sure about it, but it may be for security reasons, since allowing "/../" URIs would potentially let people navigate freely on any server's file system.

不太确定,但可能出于安全原因,因为允许“/../”URI可能会让人们在任何服务器的文件系统上*导航。

Also, the official URI RFC states that when resolving an URI one of the steps performed is actually the removal of "/../" segments, so it's not a problem in the C# library but it's regular URI behavior.

此外,官方URI RFC指出,在解析URI时,执行的步骤之一实际上是删除“/../”段,因此它不是C#库中的问题,而是常规URI行为。

#3


Have you tried using the @ symbol like so?

您是否尝试使用@符号?

Uri target = new Uri(@"ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);