enforce rules on pathes

main
Paul Schneider 9 years ago
parent a520cb00a0
commit 789182ec9c
1 changed files with 11 additions and 9 deletions

@ -33,16 +33,19 @@ namespace Yavsc.Helpers
{ {
return !name.Any(c => !Constants.ValidChars.Contains(c)); return !name.Any(c => !Constants.ValidChars.Contains(c));
} }
// Ensure this path is canonical,
// No "dirto/./this", neither "dirt/to/that/"
// no .. and each char must be listed as valid in constants
public static bool IsValidPath(this string path) public static bool IsValidPath(this string path)
{ {
if (path == null) return true; if (path == null) return true;
foreach (var name in path.Split(Path.DirectorySeparatorChar)) foreach (var name in path.Split(Path.DirectorySeparatorChar))
{ {
if (name != null) if (!IsValidDirectoryName(name) || name.Equals("..") || name.Equals("."))
if (!IsValidDirectoryName(name)
|| name.Equals(".."))
return false; return false;
} }
if (path.EndsWith($"{Path.DirectorySeparatorChar}")) return false;
return true; return true;
} }
public static string InitPostToFileSystem( public static string InitPostToFileSystem(
@ -52,14 +55,13 @@ namespace Yavsc.Helpers
var root = Path.Combine(Startup.UserFilesDirName, user.Identity.Name); var root = Path.Combine(Startup.UserFilesDirName, user.Identity.Name);
var diRoot = new DirectoryInfo(root); var diRoot = new DirectoryInfo(root);
if (!diRoot.Exists) diRoot.Create(); if (!diRoot.Exists) diRoot.Create();
if (subpath != null) if (!string.IsNullOrWhiteSpace(subpath)) {
if (subpath.IsValidPath()) if (!subpath.IsValidPath())
{ {
throw new InvalidPathException();
}
root = Path.Combine(root, subpath); root = Path.Combine(root, subpath);
diRoot = new DirectoryInfo(root);
if (!diRoot.Exists) diRoot.Create();
} }
else throw new InvalidPathException();
return root; return root;
} }

Loading…