Skip to content

Commit

Permalink
Sanitizing path input in api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rxtur committed Apr 29, 2019
1 parent a4e5c3d commit ae39067
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions BlogEngine/BlogEngine.Core/Data/FileManagerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public IEnumerable<FileInstance> Find(int take = 10, int skip = 0, string path =
var rwr = Utils.RelativeWebRoot;
var responsePath = "root";

path = path.SanitizePath();

if(string.IsNullOrEmpty(path))
path = Blog.CurrentInstance.StorageLocation + Utils.FilesFolder;

Expand Down
22 changes: 22 additions & 0 deletions BlogEngine/BlogEngine.Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,27 @@ public static bool TryParse<T>(this string theString, out T output)

return success;
}

/// <summary>
/// Sanitize path by removing invalid characters. Valid path should look similar to "path/to/sub/folder"
/// </summary>
/// <param name="str">String to sanitize</param>
/// <param name="root">Optionally validate datastore root</param>
/// <returns>String out</returns>
public static string SanitizePath(this string str, string root = "")
{
if (str.Contains(".."))
return "";

if (str.StartsWith("~/") && !string.IsNullOrEmpty(root) && !str.StartsWith(root))
return "";

str = str.Replace(".", "").Replace("\\", "").Replace("%2F", "");

if (str.Contains("//"))
return "";

return str;
}
}
}
4 changes: 4 additions & 0 deletions BlogEngine/BlogEngine.NET/AppCode/Api/UploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public HttpResponseMessage Post(string action, string dirPath = "")
fileName = fileName.Replace("image.jpg", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg");
fileName = fileName.Replace("image.png", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png");

var root = Blog.CurrentInstance.StorageLocation + Utils.FilesFolder;

dirPath = dirPath.SanitizePath(root);

if (!string.IsNullOrEmpty(dirPath))
dirName = dirPath;

Expand Down

0 comments on commit ae39067

Please sign in to comment.