aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-09-07 12:04:21 -0500
committerJP Dillingham <jp@dillingham.me>2026-09-07 12:04:21 -0500
commitf70236b507fb54eff2460d056fcbfbee33a5a5d5 (patch)
treeeec0917043701cd2c0516b2724b12caca38bfd3e
parentf1771147eb2a217ff0992c343abb6019b60f4234 (diff)
downloadslskd-f70236b507fb54eff2460d056fcbfbee33a5a5d5.tar.gz
add IsRootDirectoryOf and IsSubDirectoryOf to FileSafety
-rw-r--r--src/slskd/Common/FileSafety.cs55
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafety/IsRootDirectoryOfTests.cs245
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafety/IsSubDirectoryOfTests.cs215
3 files changed, 515 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>
diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/IsRootDirectoryOfTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/IsRootDirectoryOfTests.cs
new file mode 100644
index 00000000..f035f703
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/FileSafety/IsRootDirectoryOfTests.cs
@@ -0,0 +1,245 @@
+using System;
+using System.IO;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common;
+
+public partial class FileSafetyTests
+{
+ public class IsRootDirectoryOfTests
+ {
+ [Fact]
+ public void Root_Null_Throws_ArgumentNullException()
+ {
+ string root = null;
+
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf(root, "C:\\foo"));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+
+ [Theory]
+ [InlineData("")]
+ [InlineData(" ")]
+ public void Root_Empty_Or_Whitespace_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf(root, "C:\\foo"));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ public class Windows
+ {
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("foo\\bar")]
+ [InlineData("foo/bar")]
+ [InlineData("\\foo")]
+ [InlineData("C:")]
+ [InlineData("C:foo")]
+ public void Relative_Root_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf(root, "C:\\foo", OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("C:\\foo\\..")]
+ [InlineData("C:\\foo\\..\\bar")]
+ [InlineData("C:\\foo\\.")]
+ [InlineData("C:\\..")]
+ [InlineData("\\\\foo\\..")]
+ public void Root_With_Traversal_Segments_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf(root, "C:\\foo", OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("foo\\bar")]
+ [InlineData("\\foo")]
+ [InlineData("C:")]
+ [InlineData("C:foo")]
+ public void Relative_Subdirectory_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf("C:\\foo", subdirectory, OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("C:\\foo\\..")]
+ [InlineData("C:\\foo\\..\\bar")]
+ [InlineData("C:\\foo\\.")]
+ public void Subdirectory_With_Traversal_Segments_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf("C:\\foo", subdirectory, OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("C:\\foo", "C:\\foo")]
+ [InlineData("C:\\FOO", "C:\\foo")]
+ [InlineData("C:\\foo", "C:\\FOO")]
+ [InlineData("\\\\foo\\bar", "\\\\foo\\bar")]
+ [InlineData("\\\\FOO\\bar", "\\\\foo\\BAR")]
+ public void Equal_Paths_Are_A_Case_Insensitive_Match(string root, string subdirectory)
+ {
+ Assert.True(FileSafety.IsRootDirectoryOf(root, subdirectory, OperatingSystem.Windows));
+ }
+
+ // the character joining root and subdirectory is always the runtime OS separator
+ // (Path.DirectorySeparatorChar) regardless of the `os` override passed to force
+ // Windows-style absolute-path and case-sensitivity rules, so the expected result
+ // is computed from the actual runtime separator rather than hardcoded - this keeps
+ // the case correct on whichever OS the suite runs on, while still exercising both
+ // slash styles, mixed styles, drive letters, and UNC-style roots via inline data.
+ [Theory]
+ [InlineData("C:\\foo", "C:\\foo\\bar", '\\')]
+ [InlineData("C:\\foo", "C:\\foo/bar", '/')]
+ [InlineData("C:/foo", "C:/foo/bar", '/')]
+ [InlineData("C:/foo", "C:/foo\\bar", '\\')]
+ [InlineData("\\\\foo\\bar", "\\\\foo\\bar\\baz", '\\')]
+ [InlineData("\\\\foo\\bar", "\\\\foo\\bar/baz", '/')]
+ [InlineData("C:\\foo\\", "C:\\foo\\bar", '\\')]
+ [InlineData("C:\\foo/", "C:\\foo\\bar", '\\')]
+ public void Subdirectory_Match_Depends_On_Native_Separator_At_The_Boundary(string root, string subdirectory, char boundarySeparator)
+ {
+ var expected = boundarySeparator == Path.DirectorySeparatorChar;
+
+ Assert.Equal(expected, FileSafety.IsRootDirectoryOf(root, subdirectory, OperatingSystem.Windows));
+ }
+
+ [Theory]
+ [InlineData("C:\\foo", "C:\\foobar")]
+ [InlineData("C:\\foo\\bar", "C:\\foo")]
+ [InlineData("C:\\foo", "C:\\bar")]
+ [InlineData("C:\\foo\\bar", "C:\\foo\\baz")]
+ [InlineData("C:\\foo", "D:\\foo\\bar")]
+ [InlineData("\\\\foo\\bar", "\\\\foo\\barbaz")]
+ public void Non_Subdirectory_Returns_False(string root, string subdirectory)
+ {
+ Assert.False(FileSafety.IsRootDirectoryOf(root, subdirectory, OperatingSystem.Windows));
+ }
+
+ [Fact]
+ public void StartsWith_Check_Is_Case_Insensitive()
+ {
+ var subdirectory = "C:\\FOO" + Path.DirectorySeparatorChar + "bar";
+
+ Assert.True(FileSafety.IsRootDirectoryOf("C:\\foo", subdirectory, OperatingSystem.Windows));
+ }
+ }
+
+ public class Linux
+ {
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("foo/bar")]
+ [InlineData("foo\\bar")]
+ [InlineData("C:\\foo")]
+ [InlineData("\\\\foo\\bar")]
+ public void Relative_Root_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf(root, "/foo", OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("/foo/..")]
+ [InlineData("/foo/../bar")]
+ [InlineData("/foo/.")]
+ [InlineData("/..")]
+ [InlineData("//foo/..")]
+ public void Root_With_Traversal_Segments_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf(root, "/foo", OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("foo/bar")]
+ [InlineData("C:\\foo")]
+ public void Relative_Subdirectory_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf("/foo", subdirectory, OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("/foo/..")]
+ [InlineData("/foo/../bar")]
+ [InlineData("/foo/.")]
+ public void Subdirectory_With_Traversal_Segments_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsRootDirectoryOf("/foo", subdirectory, OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("/foo", "/foo")]
+ [InlineData("//foo/bar", "//foo/bar")]
+ public void Equal_Paths_Match(string root, string subdirectory)
+ {
+ Assert.True(FileSafety.IsRootDirectoryOf(root, subdirectory, OperatingSystem.Linux));
+ }
+
+ [Fact]
+ public void Equal_Paths_Different_Case_Do_Not_Match()
+ {
+ Assert.False(FileSafety.IsRootDirectoryOf("/FOO", "/foo", OperatingSystem.Linux));
+ }
+
+ [Theory]
+ [InlineData("/foo", "/foo/bar", '/')]
+ [InlineData("/foo", "/foo\\bar", '\\')]
+ [InlineData("//foo/bar", "//foo/bar/baz", '/')]
+ [InlineData("//foo/bar", "//foo/bar\\baz", '\\')]
+ [InlineData("/foo/", "/foo/bar", '/')]
+ [InlineData("/foo\\", "/foo/bar", '/')]
+ public void Subdirectory_Match_Depends_On_Native_Separator_At_The_Boundary(string root, string subdirectory, char boundarySeparator)
+ {
+ var expected = boundarySeparator == Path.DirectorySeparatorChar;
+
+ Assert.Equal(expected, FileSafety.IsRootDirectoryOf(root, subdirectory, OperatingSystem.Linux));
+ }
+
+ [Theory]
+ [InlineData("/foo", "/foobar")]
+ [InlineData("/foo/bar", "/foo")]
+ [InlineData("/foo", "/bar")]
+ [InlineData("/foo/bar", "/foo/baz")]
+ public void Non_Subdirectory_Returns_False(string root, string subdirectory)
+ {
+ Assert.False(FileSafety.IsRootDirectoryOf(root, subdirectory, OperatingSystem.Linux));
+ }
+
+ [Fact]
+ public void StartsWith_Check_Is_Case_Sensitive()
+ {
+ var subdirectory = "/FOO" + Path.DirectorySeparatorChar + "bar";
+
+ Assert.False(FileSafety.IsRootDirectoryOf("/foo", subdirectory, OperatingSystem.Linux));
+ }
+ }
+ }
+}
diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/IsSubDirectoryOfTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/IsSubDirectoryOfTests.cs
new file mode 100644
index 00000000..2f5fe8ee
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/FileSafety/IsSubDirectoryOfTests.cs
@@ -0,0 +1,215 @@
+using System;
+using System.IO;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common;
+
+public partial class FileSafetyTests
+{
+ public class IsSubDirectoryOfTests
+ {
+ // IsSubDirectoryOf(subdirectory, root) forwards to IsRootDirectoryOf(root, subdirectory),
+ // so `root` (the second parameter here) lands on IsRootDirectoryOf's first parameter and
+ // is the one validated with ArgumentNullException.ThrowIfNullOrWhiteSpace.
+ [Fact]
+ public void Root_Null_Throws_ArgumentNullException()
+ {
+ string root = null;
+
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf("C:\\foo", root));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+
+ [Theory]
+ [InlineData("")]
+ [InlineData(" ")]
+ public void Root_Empty_Or_Whitespace_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf("C:\\foo", root));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ // subdirectory (the first parameter here) is never null-checked directly; it only fails
+ // the "must be absolute" check, so null/empty/whitespace all throw plain ArgumentException.
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData(" ")]
+ public void Subdirectory_Null_Empty_Or_Whitespace_Throws_ArgumentException_Not_ArgumentNullException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf(subdirectory, "C:\\foo", OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ public class Windows
+ {
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("\\foo")]
+ [InlineData("C:")]
+ [InlineData("C:foo")]
+ public void Relative_Root_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf("C:\\foo\\bar", root, OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("\\foo")]
+ [InlineData("C:")]
+ [InlineData("C:foo")]
+ public void Relative_Subdirectory_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf(subdirectory, "C:\\foo", OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("C:\\foo\\..")]
+ [InlineData("C:\\foo\\..\\bar")]
+ [InlineData("C:\\foo\\.")]
+ public void Root_With_Traversal_Segments_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf("C:\\foo\\bar", root, OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("C:\\foo\\..\\bar")]
+ [InlineData("C:\\foo\\.")]
+ public void Subdirectory_With_Traversal_Segments_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf(subdirectory, "C:\\foo", OperatingSystem.Windows));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("C:\\foo", "C:\\foo")]
+ [InlineData("C:\\FOO", "C:\\foo")]
+ [InlineData("C:\\foo", "C:\\FOO")]
+ public void Equal_Paths_Are_A_Case_Insensitive_Match(string subdirectory, string root)
+ {
+ Assert.True(FileSafety.IsSubDirectoryOf(subdirectory, root, OperatingSystem.Windows));
+ }
+
+ [Theory]
+ [InlineData("C:\\foo\\bar", "C:\\foo", '\\')]
+ [InlineData("C:\\foo/bar", "C:\\foo", '/')]
+ [InlineData("\\\\foo\\bar\\baz", "\\\\foo\\bar", '\\')]
+ [InlineData("\\\\foo\\bar/baz", "\\\\foo\\bar", '/')]
+ [InlineData("C:\\foo\\bar", "C:\\foo\\", '\\')]
+ public void Match_Depends_On_Native_Separator_At_The_Boundary(string subdirectory, string root, char boundarySeparator)
+ {
+ var expected = boundarySeparator == Path.DirectorySeparatorChar;
+
+ Assert.Equal(expected, FileSafety.IsSubDirectoryOf(subdirectory, root, OperatingSystem.Windows));
+ }
+
+ [Theory]
+ [InlineData("C:\\foobar", "C:\\foo")]
+ [InlineData("C:\\foo", "C:\\foo\\bar")]
+ [InlineData("C:\\bar", "C:\\foo")]
+ [InlineData("C:\\foo\\baz", "C:\\foo\\bar")]
+ public void Not_A_Child_Of_Root_Returns_False(string subdirectory, string root)
+ {
+ Assert.False(FileSafety.IsSubDirectoryOf(subdirectory, root, OperatingSystem.Windows));
+ }
+ }
+
+ public class Linux
+ {
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("C:\\foo")]
+ public void Relative_Root_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf("/foo/bar", root, OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("foo")]
+ [InlineData("C:\\foo")]
+ public void Relative_Subdirectory_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf(subdirectory, "/foo", OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("/foo/../bar")]
+ [InlineData("/foo/.")]
+ public void Root_With_Traversal_Segments_Throws_ArgumentException(string root)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf("/foo/bar", root, OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Theory]
+ [InlineData("/foo/../bar")]
+ [InlineData("/foo/.")]
+ public void Subdirectory_With_Traversal_Segments_Throws_ArgumentException(string subdirectory)
+ {
+ var ex = Record.Exception(() => FileSafety.IsSubDirectoryOf(subdirectory, "/foo", OperatingSystem.Linux));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentException>(ex);
+ }
+
+ [Fact]
+ public void Equal_Paths_Match()
+ {
+ Assert.True(FileSafety.IsSubDirectoryOf("/foo", "/foo", OperatingSystem.Linux));
+ }
+
+ [Fact]
+ public void Equal_Paths_Different_Case_Do_Not_Match()
+ {
+ Assert.False(FileSafety.IsSubDirectoryOf("/FOO", "/foo", OperatingSystem.Linux));
+ }
+
+ [Theory]
+ [InlineData("/foo/bar", "/foo", '/')]
+ [InlineData("/foo\\bar", "/foo", '\\')]
+ [InlineData("//foo/bar/baz", "//foo/bar", '/')]
+ [InlineData("//foo/bar\\baz", "//foo/bar", '\\')]
+ public void Match_Depends_On_Native_Separator_At_The_Boundary(string subdirectory, string root, char boundarySeparator)
+ {
+ var expected = boundarySeparator == Path.DirectorySeparatorChar;
+
+ Assert.Equal(expected, FileSafety.IsSubDirectoryOf(subdirectory, root, OperatingSystem.Linux));
+ }
+
+ [Theory]
+ [InlineData("/foobar", "/foo")]
+ [InlineData("/foo", "/foo/bar")]
+ [InlineData("/bar", "/foo")]
+ [InlineData("/foo/baz", "/foo/bar")]
+ public void Not_A_Child_Of_Root_Returns_False(string subdirectory, string root)
+ {
+ Assert.False(FileSafety.IsSubDirectoryOf(subdirectory, root, OperatingSystem.Linux));
+ }
+ }
+ }
+}