21 lines
782 B
C#
21 lines
782 B
C#
namespace Zcbot.WindowsNode;
|
|
|
|
internal static class PathGuard
|
|
{
|
|
internal static string CombineUnderRoot(string root, params string[] segments)
|
|
{
|
|
var normalizedRoot = Path.TrimEndingDirectorySeparator(Path.GetFullPath(root));
|
|
if (segments.Any(Path.IsPathFullyQualified))
|
|
{
|
|
throw new InvalidDataException("A persisted path segment must be relative.");
|
|
}
|
|
var candidate = Path.GetFullPath(Path.Combine([normalizedRoot, .. segments]));
|
|
var prefix = normalizedRoot + Path.DirectorySeparatorChar;
|
|
if (!candidate.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
throw new InvalidDataException("A persisted path escaped its expected root.");
|
|
}
|
|
return candidate;
|
|
}
|
|
}
|