From f70236b507fb54eff2460d056fcbfbee33a5a5d5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 7 Sep 2026 12:04:21 -0500 Subject: add IsRootDirectoryOf and IsSubDirectoryOf to FileSafety --- src/slskd/Common/FileSafety.cs | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index 6a6dea22..cb7991d2 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -232,6 +232,61 @@ public static class FileSafety /// True if the path is relative, false otherwise. public static bool IsPathRelative(string path, OperatingSystem? os = null) => !IsPathAbsolute(path, os); + /// + /// Indicates whether the specified is a subdirectory within (or is) the . + /// + /// The potential root path. + /// The path to check. + /// An optional operating system override, for testing. + /// A value indicating whether the specified path is a subdirectory of (or is) the root. + /// Thrown if the specified root is null, empty, or consists of only whitespace. + /// Thrown if the specified root or subdirectories are not absolute or contain traversal segments. + public static bool IsRootDirectoryOf(string root, string subDirectory, OperatingSystem? os = null) + { + ArgumentNullException.ThrowIfNullOrWhiteSpace(root); + + if (!IsPathAbsolute(root, os)) + { + throw new ArgumentException("Specified root path must be absolute"); + } + + if (ContainsTraversalSegments(root)) + { + throw new ArgumentException("Specified root path contains traversal segments"); + } + + if (!IsPathAbsolute(subDirectory, os)) + { + throw new ArgumentException("Specified subdirectory path must be absolute"); + } + + if (ContainsTraversalSegments(subDirectory)) + { + throw new ArgumentException("Specified subdirectory path contains traversal segments"); + } + + var stringComparison = StringComparison.Ordinal; + + if (os.HasValue ? os.Value == OperatingSystem.Windows : System.OperatingSystem.IsWindows()) + { + stringComparison = StringComparison.OrdinalIgnoreCase; + } + + return subDirectory.TrimEnd('/', '\\').Equals(root.TrimEnd('/', '\\'), stringComparison) || + subDirectory.StartsWith(root.TrimEnd('/', '\\') + Path.DirectorySeparatorChar, stringComparison); + } + + /// + /// Indicates whether the specified is the parent path of (or is) the . + /// + /// The potential subdirectory. + /// The path to check. + /// An optional operating system override, for testing. + /// A value indicating whether the specified path is the parent of (or is) the subdirectory. + /// Thrown if the specified root is null, empty, or consists of only whitespace. + /// Thrown if the specified root or subdirectories are not absolute or contain traversal segments. + public static bool IsSubDirectoryOf(string subDirectory, string root, OperatingSystem? os = null) => IsRootDirectoryOf(root, subDirectory, os); + /// /// Returns the filename from the specified , properly /// handling both forward and backslashes and removing invalid characters. -- cgit v1.2.3