diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-09-07 12:04:21 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-09-07 12:04:21 -0500 |
| commit | f70236b507fb54eff2460d056fcbfbee33a5a5d5 (patch) | |
| tree | eec0917043701cd2c0516b2724b12caca38bfd3e /src/slskd/Common | |
| parent | f1771147eb2a217ff0992c343abb6019b60f4234 (diff) | |
| download | slskd-f70236b507fb54eff2460d056fcbfbee33a5a5d5.tar.gz | |
add IsRootDirectoryOf and IsSubDirectoryOf to FileSafety
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/FileSafety.cs | 55 |
1 files changed, 55 insertions, 0 deletions
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 @@ -233,6 +233,61 @@ public static class FileSafety public static bool IsPathRelative(string path, OperatingSystem? os = null) => !IsPathAbsolute(path, os); /// <summary> + /// Indicates whether the specified <paramref name="subDirectory"/> is a subdirectory within (or is) the <paramref name="root"/>. + /// </summary> + /// <param name="root">The potential root path.</param> + /// <param name="subDirectory">The path to check.</param> + /// <param name="os">An optional operating system override, for testing.</param> + /// <returns>A value indicating whether the specified path is a subdirectory of (or is) the root.</returns> + /// <exception cref="ArgumentNullException">Thrown if the specified root is null, empty, or consists of only whitespace.</exception> + /// <exception cref="ArgumentException">Thrown if the specified root or subdirectories are not absolute or contain traversal segments.</exception> + 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); + } + + /// <summary> + /// Indicates whether the specified <paramref name="root"/> is the parent path of (or is) the <paramref name="subDirectory"/>. + /// </summary> + /// <param name="subDirectory">The potential subdirectory.</param> + /// <param name="root">The path to check.</param> + /// <param name="os">An optional operating system override, for testing.</param> + /// <returns>A value indicating whether the specified path is the parent of (or is) the subdirectory.</returns> + /// <exception cref="ArgumentNullException">Thrown if the specified root is null, empty, or consists of only whitespace.</exception> + /// <exception cref="ArgumentException">Thrown if the specified root or subdirectories are not absolute or contain traversal segments.</exception> + public static bool IsSubDirectoryOf(string subDirectory, string root, OperatingSystem? os = null) => IsRootDirectoryOf(root, subDirectory, os); + + /// <summary> /// Returns the filename from the specified <paramref name="path"/>, properly /// handling both forward and backslashes and removing invalid characters. /// </summary> |
