aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-18 19:14:25 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-18 19:14:25 -0500
commit96ea096105f4fe0371a58cdc9ecf5c6b82a937e3 (patch)
tree8dfd5ca59a049af8367e97916910550170e8a5ca
parentb4e6a6009fad6b6cdf890526dda0825464c22e94 (diff)
downloadslskd-96ea096105f4fe0371a58cdc9ecf5c6b82a937e3.tar.gz
fix path detection (again)
-rw-r--r--src/slskd/Common/FileSafety.cs48
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs285
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafety/ContainsTraversalSegmentsTests.cs114
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafety/IsPathTests.cs213
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafetyTests.cs545
-rw-r--r--tests/slskd.Tests.Unit/Transfers/API/DTO/QueueDownloadBatchRequestTests.cs29
6 files changed, 665 insertions, 569 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs
index f94b9c7b..1e25ccd0 100644
--- a/src/slskd/Common/FileSafety.cs
+++ b/src/slskd/Common/FileSafety.cs
@@ -35,6 +35,7 @@ namespace slskd;
using System;
using System.IO;
using System.Linq;
+using System.Runtime.InteropServices;
/// <summary>
/// Utility functions to help safely work with paths from untrusted sources.
@@ -102,36 +103,46 @@ public static class FileSafety
public static bool ContainsTraversalSegments(string path) => path?.Split('\\', '/')?.Any(s => s is "." or "..") ?? false;
/// <summary>
- /// Returns a value indicating whether the specified <paramref name="path"/> is absolute (rooted), using
- /// platform-independent rules that recognize both Unix and Windows absolute path formats.
+ /// Returns a value indicating whether the specified <paramref name="path"/> is absolute (rooted) on the current
+ /// operating system.
/// </summary>
/// <remarks>
/// This is necessary because the base library is opaque and untestable in a cross-platform way, so we are
- /// implementing the nuclear option and merging the logic for Windows and non-Windows.
+ /// implementing the nuclear option and doing it ourselves.
/// </remarks>
/// <param name="path">The path to check.</param>
+ /// <param name="os">An optional operating system override, for testing.</param>
/// <returns>True if the path is absolute, false otherwise.</returns>
- public static bool IsPathAbsolute(string path)
+ public static bool IsPathAbsolute(string path, OSPlatform? os = null)
{
if (string.IsNullOrEmpty(path))
{
return false;
}
- // Unix/Linux/macOS absolute paths and UNC paths with forward slashes start with /
- // Windows root-relative paths and UNC paths start with \
- if (path[0] is '/' or '\\')
+ if (os.HasValue ? os.Value == OSPlatform.Windows : OperatingSystem.IsWindows())
{
- return true;
+ // Windows drive-letter path: X:\ or X:/
+ // X:foo is relative on windows
+ if (path.Length >= 3
+ && char.IsAsciiLetter(path[0])
+ && path[1] == ':'
+ && (path[2] == '\\' || path[2] == '/'))
+ {
+ return true;
+ }
+
+ // UNC path \\server\share
+ if (path.StartsWith("\\\\"))
+ {
+ return true;
+ }
+
+ return false;
}
- // Windows drive-letter paths: X:\ or X:/
- // X:foo is a "drive relative" path so it doesn't _technically_ count, but
- // it's questionable and the user probably intended to root it anyway
- // this matches the built in IsPathRooted but not IsPathFullyQualified
- if (path.Length >= 2
- && char.IsAsciiLetter(path[0])
- && path[1] == ':')
+ // Unix/Linux/macOS absolute paths and UNC paths with forward slashes start with /
+ if (path.StartsWith('/'))
{
return true;
}
@@ -140,10 +151,11 @@ public static class FileSafety
}
/// <summary>
- /// Returns a value indicating whether the specified <paramref name="path"/>, using
- /// platform-independent rules that recognize both Unix and Windows absolute path formats.
+ /// Returns a value indicating whether the specified <paramref name="path"/> is relative on the current
+ /// operating system.
/// </summary>
/// <param name="path">The path to check.</param>
+ /// <param name="os">An optional operating system override, for testing.</param>
/// <returns>True if the path is relative, false otherwise.</returns>
- public static bool IsPathRelative(string path) => !IsPathAbsolute(path);
+ public static bool IsPathRelative(string path, OSPlatform? os = null) => !IsPathAbsolute(path, os);
} \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs
new file mode 100644
index 00000000..4cac90c5
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs
@@ -0,0 +1,285 @@
+using System;
+using System.IO;
+using Xunit;
+
+namespace slskd.Tests.Unit.Files;
+
+internal static class CombineSafelyTestHelpers
+{
+ // Use a platform-appropriate absolute base path. Backslash roots are not recognised on Linux/macOS,
+ // so a Windows-style base would make containment assertions meaningless on those platforms.
+ public static readonly string Base = OperatingSystem.IsWindows() ? "C:\\base" : "/base";
+
+ public static void AssertContainedInBase(string result)
+ {
+ var resolvedBase = Path.GetFullPath(Base);
+ var resolvedResult = Path.GetFullPath(result);
+ Assert.True(
+ resolvedResult == resolvedBase ||
+ resolvedResult.StartsWith(resolvedBase + Path.DirectorySeparatorChar),
+ $"Result '{resolvedResult}' escapes base '{resolvedBase}'");
+ }
+}
+
+public class CombineSafely_Returns
+{
+ [Fact]
+ public void NoSegments_ReturnsBasePath()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base);
+ Assert.Equal(CombineSafelyTestHelpers.Base, result);
+ }
+
+ [Fact]
+ public void EmptySegment_ReturnsBasePath()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, string.Empty);
+ Assert.Equal(CombineSafelyTestHelpers.Base, result);
+ }
+
+ [Fact]
+ public void SingleSegment_ReturnsBaseAndSegment()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "subdir");
+ Assert.Equal(Path.Combine(CombineSafelyTestHelpers.Base, "subdir"), result);
+ }
+
+ [Fact]
+ public void MultipleSegments_CombinesAll()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub", "dir", "file.flac");
+ Assert.Equal(Path.Combine(CombineSafelyTestHelpers.Base, "sub", "dir", "file.flac"), result);
+ }
+
+ [Fact]
+ public void SegmentWithForwardSlash_CombinesCorrectly()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub/dir");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void SegmentWithBackslash_CombinesCorrectly()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub\\dir");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void SegmentWithDoubleSlash_CollapsedAndContained()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub//dir");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void SegmentWithDoubleBackslash_CollapsedAndContained()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub\\\\dir");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void SegmentWithSpaces_Contained()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "My Artist", "My Album");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void SegmentWithUnicode_Contained()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "Ünïcödé", "пользователь", "音楽");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void DotfileSegment_Contained()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, ".hidden");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void ThreeDotsSegment_Contained()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "...dir");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+
+ [Fact]
+ public void DotDotPrefixedName_Contained()
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "..hidden");
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+}
+
+public class CombineSafely_AlwaysContainedInBase
+{
+ [Theory]
+ [InlineData("subdir")]
+ [InlineData("sub/dir")]
+ [InlineData("sub\\dir")]
+ [InlineData("sub//dir")]
+ [InlineData("sub\\\\dir")]
+ [InlineData("My Artist")]
+ [InlineData("Artist.Name")]
+ [InlineData("...dir")]
+ [InlineData("..hidden")]
+ [InlineData(".hidden")]
+ [InlineData("dir/..dir")]
+ [InlineData("Ünïcödé")]
+ [InlineData("пользователь/Музыка")]
+ [InlineData("用户/音乐")]
+ [InlineData("ユーザー/音楽")]
+ [InlineData("사용자/음악")]
+ public void SingleSegment_IsContained(string segment)
+ {
+ var result = FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, segment);
+ CombineSafelyTestHelpers.AssertContainedInBase(result);
+ }
+}
+
+public class CombineSafely_Throws_ForRootedSegment
+{
+ [Theory]
+ [InlineData("/Music")] // Unix absolute / Windows root-relative, forward slash
+ [InlineData("//server/share")] // Unix-style UNC
+ [InlineData("C:\\Music")] // Windows drive-letter, backslash
+ [InlineData("C:/Music")] // Windows drive-letter, forward slash
+ [InlineData("D:\\path")] // alternate drive letter
+ [InlineData("C:relative")] // Windows drive-relative
+ [InlineData("\\Music")] // Windows root-relative, backslash
+ [InlineData("\\\\server\\share")] // Windows UNC
+ public void Throws_ArgumentException(string segment)
+ {
+ var ex = Assert.Throws<ArgumentException>(() => FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, segment));
+ Assert.Contains("Rooted", ex.Message);
+ }
+
+ [Fact]
+ public void Throws_WhenLaterSegmentIsRooted()
+ {
+ var ex = Assert.Throws<ArgumentException>(() =>
+ FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "valid", "/escape"));
+ Assert.Contains("Rooted", ex.Message);
+ }
+
+ [Fact]
+ public void Throws_WhenLaterSegmentIsRooted_Windows()
+ {
+ var ex = Assert.Throws<ArgumentException>(() =>
+ FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "valid", "C:\\escape"));
+ Assert.Contains("Rooted", ex.Message);
+ }
+
+ [Fact]
+ public void Throws_WhenLastSegmentIsRooted()
+ {
+ var ex = Assert.Throws<ArgumentException>(() =>
+ FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub", "dir", "/escape"));
+ Assert.Contains("Rooted", ex.Message);
+ }
+
+ [Fact]
+ public void Throws_WhenLastSegmentIsRooted_Windows()
+ {
+ var ex = Assert.Throws<ArgumentException>(() =>
+ FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub", "dir", "C:\\escape"));
+ Assert.Contains("Rooted", ex.Message);
+ }
+}
+
+public class CombineSafely_Throws_ForTraversalSegment
+{
+ [Theory]
+ // Bare traversal
+ [InlineData("..")]
+ [InlineData(".")]
+ // Traversal as first component
+ [InlineData("../escape")]
+ [InlineData("./dir")]
+ // Traversal in middle
+ [InlineData("sub/../escape")]
+ [InlineData("sub/./dir")]
+ // Traversal at end
+ [InlineData("sub/..")]
+ [InlineData("sub/.")]
+ // Double traversal
+ [InlineData("sub/../../escape")]
+ [InlineData("a/b/c/../../..")]
+ // Via backslash
+ [InlineData("sub\\..")]
+ [InlineData("sub\\.")]
+ [InlineData("sub\\..\\escape")]
+ // Unicode path components mixed with traversal
+ [InlineData("Ünïcödé/../escape")]
+ [InlineData("пользователь/../../escape")]
+ [InlineData("用户\\..\\escape")]
+ public void Throws_ArgumentException(string segment)
+ {
+ var ex = Assert.Throws<ArgumentException>(() => FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, segment));
+ Assert.Contains("traversal", ex.Message);
+ }
+
+ [Fact]
+ public void Throws_WhenLaterSegmentContainsTraversal()
+ {
+ var ex = Assert.Throws<ArgumentException>(() =>
+ FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "valid", "../escape"));
+ Assert.Contains("traversal", ex.Message);
+ }
+
+ [Fact]
+ public void Throws_WhenLastSegmentIsTraversal()
+ {
+ var ex = Assert.Throws<ArgumentException>(() =>
+ FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, "sub", "dir", ".."));
+ Assert.Contains("traversal", ex.Message);
+ }
+}
+
+public class CombineSafely_NullAndEdgeCases
+{
+ [Fact]
+ public void NullSegmentsArray_Throws()
+ {
+ // Passing null without an explicit cast is interpreted as a null params array.
+ // foreach over a null array throws NullReferenceException.
+ Assert.ThrowsAny<Exception>(() => FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, null));
+ }
+
+ [Fact]
+ public void NullSegment_Throws()
+ {
+ // A null element inside the params array reaches Path.IsPathRooted(null),
+ // which throws ArgumentNullException.
+ Assert.Throws<ArgumentNullException>(() => FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, null, "subdir"));
+ }
+
+ [Fact]
+ public void NullSegmentAlone_Throws()
+ {
+ // Explicit cast to string forces a single-element array containing null.
+ // Path.IsPathRooted(null) throws ArgumentNullException.
+ Assert.Throws<ArgumentNullException>(() => FileSafety.CombineSafely(CombineSafelyTestHelpers.Base, (string)null));
+ }
+
+ [Fact]
+ public void NullBasePath_Throws()
+ {
+ Assert.ThrowsAny<Exception>(() => FileSafety.CombineSafely(null, "subdir"));
+ }
+
+ [Fact]
+ public void TraversalInRoot_IsNotValidated()
+ {
+ // CombineSafely only validates segments, not the root parameter — root is assumed
+ // to be a known-good value supplied by the application, not untrusted input.
+ // This test documents that a traversal-containing root is NOT rejected.
+ var root = OperatingSystem.IsWindows() ? "C:\\base\\..\\etc" : "/base/../etc";
+ var result = FileSafety.CombineSafely(root, "subdir");
+ Assert.NotNull(result);
+ }
+}
diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/ContainsTraversalSegmentsTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/ContainsTraversalSegmentsTests.cs
new file mode 100644
index 00000000..9667789e
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/FileSafety/ContainsTraversalSegmentsTests.cs
@@ -0,0 +1,114 @@
+using Xunit;
+
+namespace slskd.Tests.Unit.Files;
+
+public class ContainsTraversalSegments_ReturnsTrue
+{
+ [Theory]
+ // Bare traversal segments
+ [InlineData("..")]
+ [InlineData(".")]
+ // Traversal as first component — forward slash
+ [InlineData("../escape")]
+ [InlineData("./dir")]
+ // Traversal at end — forward slash
+ [InlineData("sub/..")]
+ [InlineData("sub/.")]
+ // Traversal in middle — forward slash
+ [InlineData("sub/../escape")]
+ [InlineData("sub/./dir")]
+ // Double traversal — forward slash
+ [InlineData("sub/../../escape")]
+ [InlineData("a/b/c/../../..")]
+ // Traversal via backslash
+ [InlineData("sub\\..")]
+ [InlineData("sub\\.")]
+ [InlineData("sub\\..\\escape")]
+ [InlineData("sub\\.\\dir")]
+ // Mixed separators
+ [InlineData("sub/..\\escape")]
+ [InlineData("sub\\./../escape")]
+ // Unicode path components mixed with forward-slash traversal
+ [InlineData("Ünïcödé/../escape")]
+ [InlineData("Ünïcödé/./escape")]
+ [InlineData("пользователь/../etc")]
+ [InlineData("пользователь/./etc")]
+ [InlineData("用户/../escape")]
+ [InlineData("ユーザー/../../escape")]
+ [InlineData("사용자/../escape")]
+ [InlineData("مستخدم/../escape")]
+ [InlineData("χρήστης/../μουσική")]
+ [InlineData("משתמש/../מוזיקה")]
+ // Unicode path components mixed with backslash traversal
+ [InlineData("用户\\..\\escape")]
+ [InlineData("ユーザー\\..\\escape")]
+ [InlineData("пользователь\\..\\Музыка")]
+ // Traversal between two valid Unicode segments
+ [InlineData("Ünïcödé/../пользователь")]
+ [InlineData("用户/music/../escape")]
+ // Traversal at end after unicode
+ [InlineData("Ünïcödé/..")]
+ [InlineData("пользователь/..")]
+ [InlineData("用户\\..")]
+ public void Path_ContainsTraversal(string path)
+ {
+ Assert.True(FileSafety.ContainsTraversalSegments(path));
+ }
+}
+
+public class ContainsTraversalSegments_ReturnsFalse
+{
+ [Theory]
+ // Simple names — no separators, no dots
+ [InlineData("subdir")]
+ [InlineData("file.flac")]
+ [InlineData("My Artist")]
+ [InlineData("Artist.Name")]
+ // Dotfile and dot-prefixed names — not exactly . or ..
+ [InlineData(".hidden")]
+ [InlineData("..hidden")]
+ [InlineData("...")]
+ [InlineData("...dir")]
+ // Segments that start with .. but are not exactly ..
+ [InlineData("dir/..dir")]
+ [InlineData("dir/...")]
+ // Forward-slash paths — no traversal
+ [InlineData("sub/dir")]
+ [InlineData("Artist/Album")]
+ [InlineData("Artist/Album/Song.flac")]
+ // Backslash paths — no traversal
+ [InlineData("sub\\dir")]
+ [InlineData("Artist\\Album")]
+ [InlineData("Artist\\Album\\Song.flac")]
+ // Mixed separators — no traversal
+ [InlineData("Artist/Album\\Song.flac")]
+ // Double separators — empty segment is not . or ..
+ [InlineData("sub//dir")]
+ [InlineData("sub\\\\dir")]
+ // Null and empty string — both return false without throwing
+ [InlineData(null)]
+ [InlineData("")]
+ // Unicode — no traversal, forward slash
+ [InlineData("Ünïcödé")]
+ [InlineData("Ünïcödé/Àrtïst/Àlbüm")]
+ [InlineData("пользователь/Музыка")]
+ [InlineData("用户/音乐/专辑")]
+ [InlineData("ユーザー/音楽/アルバム")]
+ [InlineData("사용자/음악/앨범")]
+ [InlineData("مستخدم/موسيقى")]
+ [InlineData("χρήστης/μουσική")]
+ [InlineData("משתמש/מוזיקה")]
+ // Unicode — no traversal, backslash
+ [InlineData("пользователь\\Музыка\\Альбом")]
+ [InlineData("用户\\音乐\\专辑")]
+ [InlineData("ユーザー\\音楽\\アルバム")]
+ [InlineData("사용자\\음악\\앨범")]
+ // Unicode names that start with dots — not traversal
+ [InlineData("Ünïcödé/..hidden")]
+ [InlineData("пользователь/...dir")]
+ [InlineData("用户/..名前")]
+ public void Path_DoesNotContainTraversal(string path)
+ {
+ Assert.False(FileSafety.ContainsTraversalSegments(path));
+ }
+}
diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/IsPathTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/IsPathTests.cs
new file mode 100644
index 00000000..723879a0
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/FileSafety/IsPathTests.cs
@@ -0,0 +1,213 @@
+using System.Runtime.InteropServices;
+using Xunit;
+
+namespace slskd.Tests.Unit.Files;
+
+public class IsPath_Windows
+{
+ public class Absolute
+ {
+ [Theory]
+ // drive-letter paths (X:\ or X:/)
+ [InlineData("C:\\")]
+ [InlineData("C:/")]
+ [InlineData("C:\\Music")]
+ [InlineData("C:/Music")]
+ [InlineData("C:\\Music\\Artist\\Album")]
+ [InlineData("D:\\path")]
+ [InlineData("Z:\\downloads")]
+ [InlineData("C:\\Users\\Ünïcödé\\Music")]
+ [InlineData("C:\\Users\\пользователь\\Музыка")]
+ [InlineData("C:\\Users\\用户\\音乐")]
+ // UNC paths (\\ prefix)
+ [InlineData("\\\\server\\share")]
+ [InlineData("\\\\server\\share\\folder")]
+ [InlineData("\\\\server\\share\\My Music")]
+ [InlineData("\\\\192.168.1.1\\share")]
+ [InlineData("\\\\server\\Ünïcödé")]
+ [InlineData("\\\\server\\share\\пользователь")]
+ [InlineData("\\\\server\\share\\用户\\音乐")]
+ public void IsAbsolute_NotRelative(string path)
+ {
+ Assert.True(FileSafety.IsPathAbsolute(path, OSPlatform.Windows));
+ Assert.False(FileSafety.IsPathRelative(path, OSPlatform.Windows));
+ }
+ }
+
+ public class Relative
+ {
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData("subdir")]
+ [InlineData("sub/dir")]
+ [InlineData("sub\\dir")]
+ [InlineData("Artist\\Album")]
+ [InlineData("Artist/Album/Song")]
+ [InlineData("music.flac")]
+ [InlineData("My Music")]
+ [InlineData(".hidden")]
+ [InlineData("..hidden")]
+ [InlineData("...dir")]
+ [InlineData("../escape")]
+ [InlineData("./dir")]
+ [InlineData("..")]
+ [InlineData(".")]
+ [InlineData("Ünïcödé")]
+ [InlineData("пользователь/Музыка")]
+ [InlineData("/Music")] // root-relative — no drive letter, not absolute on Windows
+ [InlineData("\\Music")] // root-relative — no drive letter, not absolute on Windows
+ [InlineData("/home/user")] // Unix-style — not recognised as absolute on Windows
+ [InlineData("//server/share")] // Unix UNC — not recognised as absolute on Windows
+ [InlineData("C:")] // bare drive letter — length < 3, not absolute
+ [InlineData("C:Music")] // drive-relative — no separator after colon, not absolute
+ [InlineData("D:path")] // drive-relative — no separator after colon, not absolute
+ public void IsRelative_NotAbsolute(string path)
+ {
+ Assert.False(FileSafety.IsPathAbsolute(path, OSPlatform.Windows));
+ Assert.True(FileSafety.IsPathRelative(path, OSPlatform.Windows));
+ }
+ }
+}
+
+public class IsPath_Linux
+{
+ public class Absolute
+ {
+ [Theory]
+ [InlineData("/")]
+ [InlineData("/home")]
+ [InlineData("/home/user/Music")]
+ [InlineData("/home/user/My Music")]
+ [InlineData("/etc/hosts")]
+ [InlineData("/tmp")]
+ [InlineData("/home/Ünïcödé")]
+ [InlineData("/home/пользователь/Музыка")]
+ [InlineData("/home/用户/音乐")]
+ [InlineData("/home/ユーザー/音楽")]
+ [InlineData("/home/사용자/음악")]
+ [InlineData("//server/share")]
+ [InlineData("//server/share/folder")]
+ [InlineData("//192.168.1.1/share")]
+ public void IsAbsolute_NotRelative(string path)
+ {
+ Assert.True(FileSafety.IsPathAbsolute(path, OSPlatform.Linux));
+ Assert.False(FileSafety.IsPathRelative(path, OSPlatform.Linux));
+ }
+ }
+
+ public class Relative
+ {
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData("subdir")]
+ [InlineData("sub/dir")]
+ [InlineData("sub\\dir")]
+ [InlineData("music.flac")]
+ [InlineData(".hidden")]
+ [InlineData("../escape")]
+ [InlineData("./dir")]
+ [InlineData("..")]
+ [InlineData(".")]
+ [InlineData("Ünïcödé")]
+ [InlineData("пользователь/Музыка")]
+ [InlineData("C:\\Music")] // Windows drive-letter — not absolute on Linux
+ [InlineData("C:/Music")] // Windows drive-letter — not absolute on Linux
+ [InlineData("\\\\server\\share")] // Windows UNC — not absolute on Linux
+ [InlineData("\\Music")] // Windows root-relative — not absolute on Linux
+ public void IsRelative_NotAbsolute(string path)
+ {
+ Assert.False(FileSafety.IsPathAbsolute(path, OSPlatform.Linux));
+ Assert.True(FileSafety.IsPathRelative(path, OSPlatform.Linux));
+ }
+ }
+}
+
+public class IsPath_MacOS
+{
+ public class Absolute
+ {
+ [Theory]
+ [InlineData("/")]
+ [InlineData("/Users")]
+ [InlineData("/Users/user/Music")]
+ [InlineData("/Volumes/ExternalDrive")]
+ [InlineData("/home/Ünïcödé")]
+ [InlineData("/home/пользователь/Музыка")]
+ [InlineData("//server/share")]
+ [InlineData("//server/share/folder")]
+ [InlineData("//192.168.1.1/share")]
+ public void IsAbsolute_NotRelative(string path)
+ {
+ Assert.True(FileSafety.IsPathAbsolute(path, OSPlatform.OSX));
+ Assert.False(FileSafety.IsPathRelative(path, OSPlatform.OSX));
+ }
+ }
+
+ public class Relative
+ {
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData("subdir")]
+ [InlineData("sub/dir")]
+ [InlineData("sub\\dir")]
+ [InlineData("music.flac")]
+ [InlineData(".hidden")]
+ [InlineData("../escape")]
+ [InlineData("C:\\Music")]
+ [InlineData("C:/Music")]
+ [InlineData("\\\\server\\share")]
+ [InlineData("\\Music")]
+ public void IsRelative_NotAbsolute(string path)
+ {
+ Assert.False(FileSafety.IsPathAbsolute(path, OSPlatform.OSX));
+ Assert.True(FileSafety.IsPathRelative(path, OSPlatform.OSX));
+ }
+ }
+}
+
+public class IsPath_FreeBSD
+{
+ public class Absolute
+ {
+ [Theory]
+ [InlineData("/")]
+ [InlineData("/usr")]
+ [InlineData("/usr/local/music")]
+ [InlineData("/home/user/Music")]
+ [InlineData("/home/Ünïcödé")]
+ [InlineData("/home/пользователь/Музыка")]
+ [InlineData("//server/share")]
+ [InlineData("//server/share/folder")]
+ [InlineData("//192.168.1.1/share")]
+ public void IsAbsolute_NotRelative(string path)
+ {
+ Assert.True(FileSafety.IsPathAbsolute(path, OSPlatform.FreeBSD));
+ Assert.False(FileSafety.IsPathRelative(path, OSPlatform.FreeBSD));
+ }
+ }
+
+ public class Relative
+ {
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData("subdir")]
+ [InlineData("sub/dir")]
+ [InlineData("sub\\dir")]
+ [InlineData("music.flac")]
+ [InlineData(".hidden")]
+ [InlineData("../escape")]
+ [InlineData("C:\\Music")]
+ [InlineData("C:/Music")]
+ [InlineData("\\\\server\\share")]
+ [InlineData("\\Music")]
+ public void IsRelative_NotAbsolute(string path)
+ {
+ Assert.False(FileSafety.IsPathAbsolute(path, OSPlatform.FreeBSD));
+ Assert.True(FileSafety.IsPathRelative(path, OSPlatform.FreeBSD));
+ }
+ }
+}
diff --git a/tests/slskd.Tests.Unit/Common/FileSafetyTests.cs b/tests/slskd.Tests.Unit/Common/FileSafetyTests.cs
deleted file mode 100644
index a37e9e74..00000000
--- a/tests/slskd.Tests.Unit/Common/FileSafetyTests.cs
+++ /dev/null
@@ -1,545 +0,0 @@
-using System;
-using System.IO;
-using Xunit;
-
-namespace slskd.Tests.Unit.Files;
-
-public class FileSafetyTests
-{
- // Use a platform-appropriate absolute base path. Backslash roots are not recognised on Linux/macOS,
- // so a Windows-style base would make containment assertions meaningless on those platforms.
- private static readonly string Base = OperatingSystem.IsWindows() ? "C:\\base" : "/base";
-
- private static void AssertContainedInBase(string result)
- {
- var resolvedBase = Path.GetFullPath(Base);
- var resolvedResult = Path.GetFullPath(result);
- Assert.True(
- resolvedResult == resolvedBase ||
- resolvedResult.StartsWith(resolvedBase + Path.DirectorySeparatorChar),
- $"Result '{resolvedResult}' escapes base '{resolvedBase}'");
- }
-
- public class ContainsTraversalSegments_ReturnsTrue
- {
- [Theory]
- // Bare traversal segments
- [InlineData("..")]
- [InlineData(".")]
- // Traversal as first component — forward slash
- [InlineData("../escape")]
- [InlineData("./dir")]
- // Traversal at end — forward slash
- [InlineData("sub/..")]
- [InlineData("sub/.")]
- // Traversal in middle — forward slash
- [InlineData("sub/../escape")]
- [InlineData("sub/./dir")]
- // Double traversal — forward slash
- [InlineData("sub/../../escape")]
- [InlineData("a/b/c/../../..")]
- // Traversal via backslash
- [InlineData("sub\\..")]
- [InlineData("sub\\.")]
- [InlineData("sub\\..\\escape")]
- [InlineData("sub\\.\\dir")]
- // Mixed separators
- [InlineData("sub/..\\escape")]
- [InlineData("sub\\./../escape")]
- // Unicode path components mixed with forward-slash traversal
- [InlineData("Ünïcödé/../escape")]
- [InlineData("Ünïcödé/./escape")]
- [InlineData("пользователь/../etc")]
- [InlineData("пользователь/./etc")]
- [InlineData("用户/../escape")]
- [InlineData("ユーザー/../../escape")]
- [InlineData("사용자/../escape")]
- [InlineData("مستخدم/../escape")]
- [InlineData("χρήστης/../μουσική")]
- [InlineData("משתמש/../מוזיקה")]
- // Unicode path components mixed with backslash traversal
- [InlineData("用户\\..\\escape")]
- [InlineData("ユーザー\\..\\escape")]
- [InlineData("пользователь\\..\\Музыка")]
- // Traversal between two valid Unicode segments
- [InlineData("Ünïcödé/../пользователь")]
- [InlineData("用户/music/../escape")]
- // Traversal at end after unicode
- [InlineData("Ünïcödé/..")]
- [InlineData("пользователь/..")]
- [InlineData("用户\\..")]
- public void Path_ContainsTraversal(string path)
- {
- Assert.True(FileSafety.ContainsTraversalSegments(path));
- }
- }
-
- public class ContainsTraversalSegments_ReturnsFalse
- {
- [Theory]
- // Simple names — no separators, no dots
- [InlineData("subdir")]
- [InlineData("file.flac")]
- [InlineData("My Artist")]
- [InlineData("Artist.Name")]
- // Dotfile and dot-prefixed names — not exactly . or ..
- [InlineData(".hidden")]
- [InlineData("..hidden")]
- [InlineData("...")]
- [InlineData("...dir")]
- // Segments that start with .. but are not exactly ..
- [InlineData("dir/..dir")]
- [InlineData("dir/...")]
- // Forward-slash paths — no traversal
- [InlineData("sub/dir")]
- [InlineData("Artist/Album")]
- [InlineData("Artist/Album/Song.flac")]
- // Backslash paths — no traversal
- [InlineData("sub\\dir")]
- [InlineData("Artist\\Album")]
- [InlineData("Artist\\Album\\Song.flac")]
- // Mixed separators — no traversal
- [InlineData("Artist/Album\\Song.flac")]
- // Double separators — empty segment is not . or ..
- [InlineData("sub//dir")]
- [InlineData("sub\\\\dir")]
- // Null and empty string — both return false without throwing
- [InlineData(null)]
- [InlineData("")]
- // Unicode — no traversal, forward slash
- [InlineData("Ünïcödé")]
- [InlineData("Ünïcödé/Àrtïst/Àlbüm")]
- [InlineData("пользователь/Музыка")]
- [InlineData("用户/音乐/专辑")]
- [InlineData("ユーザー/音楽/アルバム")]
- [InlineData("사용자/음악/앨범")]
- [InlineData("مستخدم/موسيقى")]
- [InlineData("χρήστης/μουσική")]
- [InlineData("משתמש/מוזיקה")]
- // Unicode — no traversal, backslash
- [InlineData("пользователь\\Музыка\\Альбом")]
- [InlineData("用户\\音乐\\专辑")]
- [InlineData("ユーザー\\音楽\\アルバム")]
- [InlineData("사용자\\음악\\앨범")]
- // Unicode names that start with dots — not traversal
- [InlineData("Ünïcödé/..hidden")]
- [InlineData("пользователь/...dir")]
- [InlineData("用户/..名前")]
- public void Path_DoesNotContainTraversal(string path)
- {
- Assert.False(FileSafety.ContainsTraversalSegments(path));
- }
- }
-
- public class CombineSafely_Returns
- {
- [Fact]
- public void NoSegments_ReturnsBasePath()
- {
- var result = FileSafety.CombineSafely(Base);
- Assert.Equal(Base, result);
- }
-
- [Fact]
- public void EmptySegment_ReturnsBasePath()
- {
- var result = FileSafety.CombineSafely(Base, string.Empty);
- Assert.Equal(Base, result);
- }
-
- [Fact]
- public void SingleSegment_ReturnsBaseAndSegment()
- {
- var result = FileSafety.CombineSafely(Base, "subdir");
- Assert.Equal(Path.Combine(Base, "subdir"), result);
- }
-
- [Fact]
- public void MultipleSegments_CombinesAll()
- {
- var result = FileSafety.CombineSafely(Base, "sub", "dir", "file.flac");
- Assert.Equal(Path.Combine(Base, "sub", "dir", "file.flac"), result);
- }
-
- [Fact]
- public void SegmentWithForwardSlash_CombinesCorrectly()
- {
- var result = FileSafety.CombineSafely(Base, "sub/dir");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void SegmentWithBackslash_CombinesCorrectly()
- {
- var result = FileSafety.CombineSafely(Base, "sub\\dir");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void SegmentWithDoubleSlash_CollapsedAndContained()
- {
- var result = FileSafety.CombineSafely(Base, "sub//dir");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void SegmentWithDoubleBackslash_CollapsedAndContained()
- {
- var result = FileSafety.CombineSafely(Base, "sub\\\\dir");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void SegmentWithSpaces_Contained()
- {
- var result = FileSafety.CombineSafely(Base, "My Artist", "My Album");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void SegmentWithUnicode_Contained()
- {
- var result = FileSafety.CombineSafely(Base, "Ünïcödé", "пользователь", "音楽");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void DotfileSegment_Contained()
- {
- var result = FileSafety.CombineSafely(Base, ".hidden");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void ThreeDotsSegment_Contained()
- {
- var result = FileSafety.CombineSafely(Base, "...dir");
- AssertContainedInBase(result);
- }
-
- [Fact]
- public void DotDotPrefixedName_Contained()
- {
- var result = FileSafety.CombineSafely(Base, "..hidden");
- AssertContainedInBase(result);
- }
- }
-
- public class CombineSafely_AlwaysContainedInBase
- {
- [Theory]
- [InlineData("subdir")]
- [InlineData("sub/dir")]
- [InlineData("sub\\dir")]
- [InlineData("sub//dir")]
- [InlineData("sub\\\\dir")]
- [InlineData("My Artist")]
- [InlineData("Artist.Name")]
- [InlineData("...dir")]
- [InlineData("..hidden")]
- [InlineData(".hidden")]
- [InlineData("dir/..dir")]
- [InlineData("Ünïcödé")]
- [InlineData("пользователь/Музыка")]
- [InlineData("用户/音乐")]
- [InlineData("ユーザー/音楽")]
- [InlineData("사용자/음악")]
- public void SingleSegment_IsContained(string segment)
- {
- var result = FileSafety.CombineSafely(Base, segment);
- var resolvedBase = Path.GetFullPath(Base);
- var resolvedResult = Path.GetFullPath(result);
- Assert.True(
- resolvedResult == resolvedBase ||
- resolvedResult.StartsWith(resolvedBase + Path.DirectorySeparatorChar),
- $"Result '{resolvedResult}' escapes base '{resolvedBase}'");
- }
- }
-
- public class CombineSafely_Throws_ForRootedSegment
- {
- [Theory]
- [InlineData("/Music")] // Unix absolute / Windows root-relative, forward slash
- [InlineData("//server/share")] // Unix-style UNC
- [InlineData("C:\\Music")] // Windows drive-letter, backslash
- [InlineData("C:/Music")] // Windows drive-letter, forward slash
- [InlineData("D:\\path")] // alternate drive letter
- [InlineData("C:relative")] // Windows drive-relative
- [InlineData("\\Music")] // Windows root-relative, backslash
- [InlineData("\\\\server\\share")] // Windows UNC
- public void Throws_ArgumentException(string segment)
- {
- var ex = Assert.Throws<ArgumentException>(() => FileSafety.CombineSafely(Base, segment));
- Assert.Contains("Rooted", ex.Message);
- }
-
- [Fact]
- public void Throws_WhenLaterSegmentIsRooted()
- {
- var ex = Assert.Throws<ArgumentException>(() =>
- FileSafety.CombineSafely(Base, "valid", "/escape"));
- Assert.Contains("Rooted", ex.Message);
- }
-
- [Fact]
- public void Throws_WhenLaterSegmentIsRooted_Windows()
- {
- var ex = Assert.Throws<ArgumentException>(() =>
- FileSafety.CombineSafely(Base, "valid", "C:\\escape"));
- Assert.Contains("Rooted", ex.Message);
- }
-
- [Fact]
- public void Throws_WhenLastSegmentIsRooted()
- {
- var ex = Assert.Throws<ArgumentException>(() =>
- FileSafety.CombineSafely(Base, "sub", "dir", "/escape"));
- Assert.Contains("Rooted", ex.Message);
- }
-
- [Fact]
- public void Throws_WhenLastSegmentIsRooted_Windows()
- {
- var ex = Assert.Throws<ArgumentException>(() =>
- FileSafety.CombineSafely(Base, "sub", "dir", "C:\\escape"));
- Assert.Contains("Rooted", ex.Message);
- }
- }
-
- public class CombineSafely_Throws_ForTraversalSegment
- {
- [Theory]
- // Bare traversal
- [InlineData("..")]
- [InlineData(".")]
- // Traversal as first component
- [InlineData("../escape")]
- [InlineData("./dir")]
- // Traversal in middle
- [InlineData("sub/../escape")]
- [InlineData("sub/./dir")]
- // Traversal at end
- [InlineData("sub/..")]
- [InlineData("sub/.")]
- // Double traversal
- [InlineData("sub/../../escape")]
- [InlineData("a/b/c/../../..")]
- // Via backslash
- [InlineData("sub\\..")]
- [InlineData("sub\\.")]
- [InlineData("sub\\..\\escape")]
- // Unicode path components mixed with traversal
- [InlineData("Ünïcödé/../escape")]
- [InlineData("пользователь/../../escape")]
- [InlineData("用户\\..\\escape")]
- public void Throws_ArgumentException(string segment)
- {
- var ex = Assert.Throws<ArgumentException>(() => FileSafety.CombineSafely(Base, segment));
- Assert.Contains("traversal", ex.Message);
- }
-
- [Fact]
- public void Throws_WhenLaterSegmentContainsTraversal()
- {
- var ex = Assert.Throws<ArgumentException>(() =>
- FileSafety.CombineSafely(Base, "valid", "../escape"));
- Assert.Contains("traversal", ex.Message);
- }
-
- [Fact]
- public void Throws_WhenLastSegmentIsTraversal()
- {
- var ex = Assert.Throws<ArgumentException>(() =>
- FileSafety.CombineSafely(Base, "sub", "dir", ".."));
- Assert.Contains("traversal", ex.Message);
- }
- }
-
- public class IsPathAbsolute_ReturnsTrue
- {
- // Unix/Linux/macOS absolute paths
- [Theory]
- [InlineData("/")] // filesystem root
- [InlineData("/home")] // single component
- [InlineData("/home/user/Music")] // deep path
- [InlineData("/home/user/My Music")] // spaces in component
- [InlineData("/etc/hosts")] // config file
- [InlineData("/tmp")] // temp directory
- [InlineData("/home/Ünïcödé")] // latin extended
- [InlineData("/home/пользователь/Музыка")] // Cyrillic
- [InlineData("/home/用户/音乐")] // Chinese
- [InlineData("/home/ユーザー/音楽")] // Japanese
- [InlineData("/home/사용자/음악")] // Korean
- public void Unix_Paths(string path)
- {
- Assert.True(FileSafety.IsPathAbsolute(path));
- }
-
- // Unix-style UNC paths (// prefix)
- [Theory]
- [InlineData("//server/share")]
- [InlineData("//server/share/folder")]
- [InlineData("//192.168.1.1/share")]
- [InlineData("//server/Ünïcödé")]
- [InlineData("//server/share/пользователь")]
- public void Unix_UNCPaths(string path)
- {
- Assert.True(FileSafety.IsPathAbsolute(path));
- }
-
- // Windows root-relative paths (rooted to the current drive, no drive letter)
- [Theory]
- [InlineData("\\Music")]
- [InlineData("\\Music\\Artist\\Album")]
- [InlineData("/Music")]
- [InlineData("/Music/Artist/Album")]
- [InlineData("\\Ünïcödé")]
- [InlineData("\\пользователь\\Музыка")]
- public void Windows_RootRelativePaths(string path)
- {
- Assert.True(FileSafety.IsPathAbsolute(path));
- }
-
- // Windows UNC paths (\\ prefix)
- [Theory]
- [InlineData("\\\\server\\share")]
- [InlineData("\\\\server\\share\\folder")]
- [InlineData("\\\\server\\share\\My Music")]
- [InlineData("\\\\192.168.1.1\\share")]
- [InlineData("\\\\server\\Ünïcödé")]
- [InlineData("\\\\server\\share\\пользователь")]
- [InlineData("\\\\server\\share\\用户\\音乐")]
- public void Windows_UNCPaths(string path)
- {
- Assert.True(FileSafety.IsPathAbsolute(path));
- }
-
- // Windows drive-letter paths — with or without a separator after the colon
- [Theory]
- [InlineData("C:\\")] // drive root, backslash
- [InlineData("C:/")] // drive root, forward slash
- [InlineData("C:\\Music")] // basic, backslash
- [InlineData("C:/Music")] // basic, forward slash
- [InlineData("C:\\Music\\Artist\\Album")] // deep, backslash
- [InlineData("D:\\path")] // different drive letter
- [InlineData("Z:\\downloads")] // drive letter Z
- [InlineData("C:")] // bare drive letter
- [InlineData("C:Music")] // drive-relative, no separator
- [InlineData("D:path")] // different drive letter, no separator
- [InlineData("C:\\Users\\Ünïcödé\\Music")] // latin extended
- [InlineData("C:\\Users\\пользователь\\Музыка")] // Cyrillic
- [InlineData("C:\\Users\\用户\\音乐")] // Chinese
- public void Windows_DriveLetterPaths(string path)
- {
- Assert.True(FileSafety.IsPathAbsolute(path));
- }
- }
-
- public class IsPathAbsolute_ReturnsFalse
- {
- [Theory]
- [InlineData(null)] // null
- [InlineData("")] // empty string
- [InlineData("subdir")] // simple name
- [InlineData("sub/dir")] // forward slash
- [InlineData("sub\\dir")] // backslash
- [InlineData("Artist\\Album")] // multi-level backslash
- [InlineData("Artist/Album/Song")] // deep forward slash
- [InlineData("music.flac")] // file name with extension
- [InlineData("My Music")] // spaces
- [InlineData(".hidden")] // dotfile
- [InlineData("..hidden")] // dot-prefixed name
- [InlineData("...dir")] // three dots — relative, not traversal
- [InlineData("../escape")] // traversal — still relative
- [InlineData("./dir")] // current-dir segment — still relative
- [InlineData("..")] // bare double-dot
- [InlineData(".")] // bare single-dot
- [InlineData("Ünïcödé")] // unicode, no root
- [InlineData("пользователь/Музыка")] // unicode with separator, no root
- public void Relative_Paths(string path)
- {
- Assert.False(FileSafety.IsPathAbsolute(path));
- }
- }
-
- public class IsPathRelative_ReturnsTrue
- {
- [Theory]
- [InlineData(null)]
- [InlineData("")]
- [InlineData("subdir")]
- [InlineData("sub/dir")]
- [InlineData("sub\\dir")]
- [InlineData("music.flac")]
- [InlineData(".hidden")]
- [InlineData("../escape")]
- public void Relative_Paths(string path)
- {
- Assert.True(FileSafety.IsPathRelative(path));
- }
-
- }
-
- public class IsPathRelative_ReturnsFalse
- {
- [Theory]
- [InlineData("/home/user")]
- [InlineData("//server/share")]
- [InlineData("\\Music")]
- [InlineData("\\\\server\\share")]
- [InlineData("C:\\Music")]
- [InlineData("C:/Music")]
- [InlineData("C:\\")]
- [InlineData("C:")]
- [InlineData("C:Music")]
- [InlineData("D:path")]
- public void Absolute_Paths(string path)
- {
- Assert.False(FileSafety.IsPathRelative(path));
- }
- }
-
- public class CombineSafely_NullAndEdgeCases
- {
- [Fact]
- public void NullSegmentsArray_Throws()
- {
- // Passing null without an explicit cast is interpreted as a null params array.
- // foreach over a null array throws NullReferenceException.
- Assert.ThrowsAny<Exception>(() => FileSafety.CombineSafely(Base, null));
- }
-
- [Fact]
- public void NullSegment_Throws()
- {
- // A null element inside the params array reaches Path.IsPathRooted(null),
- // which throws ArgumentNullException.
- Assert.Throws<ArgumentNullException>(() => FileSafety.CombineSafely(Base, null, "subdir"));
- }
-
- [Fact]
- public void NullSegmentAlone_Throws()
- {
- // Explicit cast to string forces a single-element array containing null.
- // Path.IsPathRooted(null) throws ArgumentNullException.
- Assert.Throws<ArgumentNullException>(() => FileSafety.CombineSafely(Base, (string)null));
- }
-
- [Fact]
- public void NullBasePath_Throws()
- {
- Assert.ThrowsAny<Exception>(() => FileSafety.CombineSafely(null, "subdir"));
- }
-
- [Fact]
- public void TraversalInRoot_IsNotValidated()
- {
- // CombineSafely only validates segments, not the root parameter — root is assumed
- // to be a known-good value supplied by the application, not untrusted input.
- // This test documents that a traversal-containing root is NOT rejected.
- var root = OperatingSystem.IsWindows() ? "C:\\base\\..\\etc" : "/base/../etc";
- var result = FileSafety.CombineSafely(root, "subdir");
- Assert.NotNull(result);
- }
- }
-}
diff --git a/tests/slskd.Tests.Unit/Transfers/API/DTO/QueueDownloadBatchRequestTests.cs b/tests/slskd.Tests.Unit/Transfers/API/DTO/QueueDownloadBatchRequestTests.cs
index a9f30129..137fd73e 100644
--- a/tests/slskd.Tests.Unit/Transfers/API/DTO/QueueDownloadBatchRequestTests.cs
+++ b/tests/slskd.Tests.Unit/Transfers/API/DTO/QueueDownloadBatchRequestTests.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using slskd.Transfers.API;
@@ -276,15 +277,31 @@ public class QueueDownloadBatchRequestTests
[Theory]
[InlineData("C:\\Music")] // Windows absolute
[InlineData("C:/Music")] // Windows absolute, forward slash
+ [InlineData("\\\\server\\share")] // UNC
+ public void AbsolutePath_Fails_Windows(string value)
+ {
+ if (OperatingSystem.IsWindows())
+ {
+ var (isValid, results) = Validate(new EnqueueDownloadBatchOptions { Destination = value });
+ Assert.False(isValid);
+ Assert.Single(results);
+ Assert.Equal("The Destination field must be a relative path.", results[0].ErrorMessage);
+ }
+ }
+
+ [Theory]
[InlineData("/music")] // Unix absolute
[InlineData("/home/user/Music")] // Unix absolute, deep
- [InlineData("\\\\server\\share")] // UNC
- public void AbsolutePath_Fails(string value)
+ [InlineData("//server/share")] // UNC
+ public void AbsolutePath_Fails_Non_Windows(string value)
{
- var (isValid, results) = Validate(new EnqueueDownloadBatchOptions { Destination = value });
- Assert.False(isValid);
- Assert.Single(results);
- Assert.Equal("The Destination field must be a relative path.", results[0].ErrorMessage);
+ if (!OperatingSystem.IsWindows())
+ {
+ var (isValid, results) = Validate(new EnqueueDownloadBatchOptions { Destination = value });
+ Assert.False(isValid);
+ Assert.Single(results);
+ Assert.Equal("The Destination field must be a relative path.", results[0].ErrorMessage);
+ }
}
[Theory]