user7617437
user7617437

Reputation:

Check for parent directory access to a request(URL) using predefined functions

Need to know how i can give a check for parent directory access.

case 1: Path = https://18.56.199.56/Project/Docs/../Hello.File.txt
case 2: Path = https://18.56.199.56/Project/Docs/Hello..File.txt

Using some check i need to find the parent directory access(like case 1) and not ".." in the file name(like case 2)

With the below code i am getting the expected output but i need some predefined methods like Uri.AbsoluteUri Properties to do the same job.

class Program
{
    static void Main(string[] args)
    {
       if (Path.Contains("../")) {
          // do something
       }
    }
}

Requirement: Need some predefined methods like below to do this job. Using below code i am getting isFile as false.

var isFile = new Uri(Path).AbsoluteUri.Split('/').Contains("..");

Upvotes: 3

Views: 109

Answers (1)

StuiterSlurf
StuiterSlurf

Reputation: 2572

To check if the path contains ../ you can use string.Contains()

https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.8

filePath1 = filePath1.Replace("../", "")

// To skip ../ in the path we check in the first contains if it's in the path.
// The definition of || is OR  
// To skip ./ in the path we check it in the second part of the if statement.
// So in pseudo code it says:
// if filepath1 conatains ../ OR if filepath1 contains ./ return null 
if (filePath1.Contains("../") || filePath1.Contains("./"))
    return null;

Upvotes: 3

Related Questions