PC Review


Reply
Thread Tools Rate Thread

Combine root and path.

 
 
William Stacey [MVP]
Guest
Posts: n/a
 
      25th Sep 2005
I need a bullet proof way to combine a root and a relative path to form a FQ
rooted path (similar to a VDir in IIS). Path.Combine alone will not do the
job in all cases. I also need to be sure the no funny business can go on in
the passed "path" that would produce a path not in the root (i.e.
"..\..\dir1"). Here is my first stab at it, but not sure if this is too
much or not enouph to ensure this. Any thoughts are welcome. TIA.

/// <summary>
/// Combines the root and path to ensure the path always relative to
the root and not below it or in some other root.
/// This does not check if the resulting path exists or if access is
allowed.
/// Path can not contain ".." anywhere in the path. Path can not be
rooted, it must be a relative path.
/// </summary>
/// <param name="root"></param>
/// <param name="path"></param>
/// <returns></returns>
public static string CombineRootAndPath(string root, string path)
{
// Path can not be rooted. Must be realitive.
// Path can not contain ".." anywhere.
if ( root == null )
return null;
if ( path == null )
return null;
if ( ! Path.IsPathRooted(root) )
return null;
if ( root.EndsWith(@"\"))
root = root + @"\";
path = path.Trim();
if ( Path.IsPathRooted(path) )
return null;
string fullPath = Path.Combine(root, path);
// Final test to make sure nothing unexpected in path would
Combine
// to produce something outside the root.
if ( ! fullPath.StartsWith(root) )
return null;
if ( path.Contains("..") )
return null;
return fullPath;
}

--
William Stacey [MVP]



 
Reply With Quote
 
 
 
 
William Stacey [MVP]
Guest
Posts: n/a
 
      25th Sep 2005
Removed "if ( root.EndsWith)"

public static string CombineRootAndPath(string root, string path)
{
// Path can not be rooted. Must be realitive.
// Path can not contain ".." anywhere.
if ( root == null )
return null;
if ( path == null )
return null;
try
{
if ( !Path.IsPathRooted(root) )
return null;
}
catch
{
return null;
}
//if ( root.EndsWith(@"\"))
// root = root + @"\";
path = path.Trim();
try
{
if ( Path.IsPathRooted(path) )
return null;
}
catch
{
return null;
}
string fullPath = Path.Combine(root, path);
// Final test to make sure nothing unexpected in path would
Combine
// to produce something outside the root.
if ( ! fullPath.StartsWith(root) )
return null;
if ( path.Contains("..") )
return null;
return fullPath;
}

--
William Stacey [MVP]



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
App root path =?Utf-8?B?SGVjdG9yIFkuIE1hcnRpbmV6?= Microsoft VC .NET 2 23rd Mar 2006 07:50 AM
root file path ashishprem Microsoft Excel Programming 2 28th Feb 2006 09:13 AM
Path so site root? Jeff S Microsoft ASP .NET 2 23rd Mar 2004 09:15 PM
Root Certification Path ysh Microsoft Windows 2000 Setup 0 16th Mar 2004 05:14 AM
Root path Steve Peterson Microsoft ASP .NET 2 8th Jan 2004 09:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:29 PM.