I noticed something weird in Server.MapPath(). If I have a folder with a space at the end I get:
HttpException: Failed to map the path.
This works fine:
Server.MapPath("/Folder1/Folder2/item.jpg")
This works fine:
Server.MapPath("/Folder1/ Folder2/item.jpg")
This works fine:
Server.MapPath("/Folder1/Fol der2/item.jpg")
This fails!:
Server.MapPath("/Folder1/Folder2 /item.jpg")
Could somebody explain to me why a space at the end fails while a space anywhere else doesn't?
Note: None of the folders exist.
Because you shouldn't:
The issue comes from the method
FileUtil.IsSuspiciousPhysicalPath(string physicalPath, out bool pathTooLong)
, which does a compare:Path.GetFullPath()
will trim trailing spaces from directory and file names (because it callsPath.NormalizePath()
which does so), which can be discovered callingPath.GetFullPath(@"C:\Foo \Bar.txt")
for example. Since that won't match the original path that contains the spaces, the method will returntrue
thus identifying the path as suspicious, after whichServer.MapPath
will throw the exception.