aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-09 19:02:06 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-09 19:02:06 -0500
commitc00f611d37342dab1d0c0c5f5cd3264b2c8d0616 (patch)
tree2026fac69505cf9777a5ed71e9bb346aab903687 /tests
parent40d8fcc3e80c2c518d87d37e2a60ab53c7c4c81f (diff)
downloadslskd-c00f611d37342dab1d0c0c5f5cd3264b2c8d0616.tar.gz
strip path traversal segments from configured paths
Diffstat (limited to 'tests')
-rw-r--r--tests/slskd.Tests.Unit/Common/Validation/AbsolutePathAttributeTests.cs231
-rw-r--r--tests/slskd.Tests.Unit/Common/Validation/RelativePathAttributeTests.cs131
2 files changed, 362 insertions, 0 deletions
diff --git a/tests/slskd.Tests.Unit/Common/Validation/AbsolutePathAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/AbsolutePathAttributeTests.cs
new file mode 100644
index 00000000..03729eab
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/Validation/AbsolutePathAttributeTests.cs
@@ -0,0 +1,231 @@
+using System.ComponentModel.DataAnnotations;
+using slskd.Validation;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common.Validation;
+
+public class AbsolutePathAttributeTests
+{
+ private static (bool IsValid, string ErrorMessage) Validate(string value)
+ {
+ var attribute = new AbsolutePathAttribute();
+ var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
+ var result = attribute.GetValidationResult(value, context);
+ return (result == null, result?.ErrorMessage);
+ }
+
+ // paths can be null or empty; [Required] dictates whether they must be defined
+ [Fact]
+ public void Null_Passes()
+ {
+ var (isValid, _) = Validate(null);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void EmptyString_Passes()
+ {
+ var (isValid, _) = Validate(string.Empty);
+ Assert.True(isValid);
+ }
+
+ public class Passes
+ {
+ // Standard Windows drive-letter absolute paths
+ [Theory]
+ [InlineData("C:\\Music")] // basic
+ [InlineData("C:/Music")] // forward slash separator
+ [InlineData("C:\\Music\\Artist\\Album")] // multi-level backslash
+ [InlineData("C:\\Music\\Artist/Album")] // mixed separators
+ [InlineData("C:\\Music\\My Artist")] // spaces in component
+ [InlineData("C:\\Program Files\\App")] // spaces, typical Windows path
+ [InlineData("D:\\path")] // different drive letter
+ [InlineData("Z:\\downloads")] // drive letter Z
+ [InlineData("C:")] // bare drive — IsPathRooted returns true
+ [InlineData("C:\\Users\\Ünïcödé\\Music")] // latin extended
+ [InlineData("C:\\Users\\пользователь\\Музыка")] // Cyrillic
+ [InlineData("C:\\Users\\用户\\音乐")] // Chinese
+ [InlineData("C:\\Users\\ユーザー\\音楽")] // Japanese
+ [InlineData("C:\\Users\\사용자\\음악")] // Korean
+ [InlineData("C:\\Users\\مستخدم\\موسيقى")] // Arabic
+ [InlineData("C:\\Users\\χρήστης\\μουσική")] // Greek
+ [InlineData("C:Music")] // drive-relative (no separator); IsPathRooted returns true
+ public void Windows_DriveLetterPaths_Pass(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+
+ // Windows root paths (drive root with trailing separator)
+ [Theory]
+ [InlineData("C:\\")] // Windows C: root
+ [InlineData("D:\\")] // Windows D: root
+ [InlineData("Z:\\")] // Windows Z: root
+ [InlineData("C:/")] // root with forward slash
+ public void Windows_RootPaths_Pass(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+
+ // Windows root-relative paths (rooted to the current drive, no drive letter)
+ [Theory]
+ [InlineData("\\Music")] // root-relative, backslash
+ [InlineData("\\Music\\Artist\\Album")] // deep, backslash
+ [InlineData("/Music")] // root-relative, forward slash
+ [InlineData("/Music/Artist/Album")] // deep, forward slash
+ [InlineData("\\Ünïcödé")] // latin extended
+ [InlineData("\\пользователь\\Музыка")] // Cyrillic
+ [InlineData("\\用户\\音乐")] // Chinese
+ [InlineData("\\ユーザー\\音楽")] // Japanese
+ [InlineData("\\사용자\\음악")] // Korean
+ public void Windows_RootRelativePaths_Pass(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+
+ // Windows UNC (Universal Naming Convention) paths.
+ // Server name is DNS-constrained (ASCII), but share names and path components support Unicode.
+ [Theory]
+ [InlineData("\\\\server\\share")] // basic UNC
+ [InlineData("\\\\server\\share\\folder")] // UNC with subfolder
+ [InlineData("\\\\server\\share\\My Music")] // UNC with spaces
+ [InlineData("\\\\192.168.1.1\\share")] // UNC with IP address
+ [InlineData("\\\\DESKTOP-ABC\\SharedFolder")] // UNC with machine name
+ [InlineData("\\\\192.168.1.1\\Ünïcödé")] // latin extended share name
+ [InlineData("\\\\server\\Müsïk")] // latin extended share name
+ [InlineData("\\\\server\\share\\Ünïcödé Artist")] // latin extended path component
+ [InlineData("\\\\server\\音楽")] // Japanese share name
+ [InlineData("\\\\server\\share\\пользователь")] // Cyrillic path component
+ [InlineData("\\\\server\\share\\用户\\音乐")] // Chinese path components
+ [InlineData("\\\\server\\share\\사용자\\음악")] // Korean path components
+ public void Windows_UNCPaths_Pass(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+
+ // Unix/Linux absolute paths — always start with a leading /
+ [Theory]
+ [InlineData("/")] // filesystem root
+ [InlineData("/home")] // single component
+ [InlineData("/home/user")] // typical home directory
+ [InlineData("/home/user/Music")] // deep path
+ [InlineData("/home/user/My Music")] // spaces in component
+ [InlineData("/etc/hosts")] // config file path
+ [InlineData("/usr/local/bin")] // system path
+ [InlineData("/var/log/app.log")] // log file
+ [InlineData("/tmp")] // temp directory
+ [InlineData("/media/usb0")] // mounted device
+ [InlineData("/mnt/storage/Music")] // mounted storage
+ [InlineData("/home/user/Ünïcödé")] // latin extended
+ [InlineData("/home/пользователь/Музыка")] // Cyrillic
+ [InlineData("/home/用户/音乐")] // Chinese
+ [InlineData("/home/ユーザー/音楽")] // Japanese
+ [InlineData("/home/사용자/음악")] // Korean
+ [InlineData("/home/مستخدم/موسيقى")] // Arabic
+ [InlineData("/home/χρήστης/μουσική")] // Greek
+ [InlineData("/home/משתמש/מוזיקה")] // Hebrew
+ public void Unix_Paths_Pass(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+
+ // macOS absolute paths — same format as Unix, with macOS-specific conventions
+ [Theory]
+ [InlineData("/Users/username")] // home directory
+ [InlineData("/Users/username/Music")] // music library
+ [InlineData("/Users/username/Music/Artist/Album")] // deep path
+ [InlineData("/Volumes/External Drive")] // mounted external drive
+ [InlineData("/Volumes/External Drive/Music")] // file on external drive
+ [InlineData("/Applications/App.app")] // application bundle
+ [InlineData("/Library/Application Support/App")] // system library
+ [InlineData("/Users/Ünïcödé")] // latin extended username
+ [InlineData("/Users/пользователь/Музыка")] // Cyrillic
+ [InlineData("/Users/用户/音乐")] // Chinese
+ [InlineData("/Users/ユーザー/音楽")] // Japanese
+ [InlineData("/Users/사용자/음악")] // Korean
+ [InlineData("/Volumes/Ünïcödé Drive/Music")] // latin extended volume name
+ [InlineData("/Volumes/音楽ドライブ")] // Japanese volume name
+ [InlineData("/Volumes/외장 드라이브/음악")] // Korean volume name
+ public void MacOS_Paths_Pass(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+
+ // Unix-style UNC equivalents (// prefix, used on some systems)
+ [Theory]
+ [InlineData("//server/share")] // basic
+ [InlineData("//server/share/folder")] // with subfolder
+ [InlineData("//server/Ünïcödé")] // latin extended share name
+ [InlineData("//server/share/Ünïcödé Artist")] // latin extended path component
+ [InlineData("//server/音楽")] // Japanese share name
+ [InlineData("//server/share/пользователь")] // Cyrillic path component
+ public void Unix_UNCPaths_Pass(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+ }
+
+ // Paths containing . or .. segments, which are valid directory names in isolation
+ // but indicate traversal or ambiguity and must be rejected regardless of whether
+ // the path is otherwise absolute or relative.
+ public class Traversal_Fails
+ {
+ [Theory]
+ // Windows-style traversal
+ [InlineData("C:\\Music\\..\\Windows")] // traversal to sibling via backslash
+ [InlineData("C:\\Music\\.\\Artist")] // current-dir segment via backslash
+ [InlineData("C:/Music/../Windows")] // traversal via forward slash
+ [InlineData("C:/Music/./Artist")] // current-dir via forward slash
+ [InlineData("C:\\..")] // traversal at drive root
+ [InlineData("D:\\a\\b\\..\\c")] // traversal deep in path
+ // Unix-style traversal
+ [InlineData("/../etc")] // traversal at Unix root
+ [InlineData("/home/user/../etc")] // traversal to sibling on Unix
+ [InlineData("/home/user/./Music")] // current-dir on Unix
+ [InlineData("/home/../../etc")] // double traversal on Unix
+ // UNC traversal (backslash)
+ [InlineData("\\\\server\\..\\Windows")] // traversal past UNC server
+ [InlineData("\\\\server\\share\\..\\other")] // traversal within UNC share
+ [InlineData("\\\\server\\share\\.\\folder")] // current-dir within UNC share
+ // UNC traversal (forward slash)
+ [InlineData("//server/../share")] // traversal past server, forward slash
+ [InlineData("//server/share/../other")] // traversal within share, forward slash
+ public void TraversalSegment_Fails(string value)
+ {
+ var (isValid, errorMessage) = Validate(value);
+ Assert.False(isValid);
+ Assert.Contains("traversal segments", errorMessage);
+ }
+ }
+
+ // Relative paths that have no empty segments and no traversal reach the
+ // final IsPathRooted check and fail there.
+ public class Relative_Fails
+ {
+ [Theory]
+ [InlineData("subdir")] // simple name
+ [InlineData("sub/dir")] // forward slash
+ [InlineData("sub\\dir")] // backslash
+ [InlineData("Artist\\Album")] // multi-level
+ [InlineData("music.flac")] // file name with extension
+ [InlineData("My Music")] // spaces
+ [InlineData("Ünïcödé")] // latin extended, still relative
+ [InlineData("Artist/Album/Song")] // deep relative
+ [InlineData("...dir")] // three dots — relative, not traversal
+ [InlineData(".hidden")] // dotfile — relative, not traversal
+ [InlineData("..")] // traversal-like but fundamentally not absolute
+ [InlineData(".")] // current-dir but fundamentally not absolute
+ public void RelativePath_Fails(string value)
+ {
+ var (isValid, errorMessage) = Validate(value);
+ Assert.False(isValid);
+ Assert.Equal("The Field field must specify an absolute file path.", errorMessage);
+ }
+ }
+}
diff --git a/tests/slskd.Tests.Unit/Common/Validation/RelativePathAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/RelativePathAttributeTests.cs
new file mode 100644
index 00000000..1692bceb
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/Validation/RelativePathAttributeTests.cs
@@ -0,0 +1,131 @@
+using System.ComponentModel.DataAnnotations;
+using slskd.Validation;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common.Validation;
+
+public class RelativePathAttributeTests
+{
+ private static (bool IsValid, string ErrorMessage) Validate(string value)
+ {
+ var attribute = new RelativePathAttribute();
+ var context = new ValidationContext(new object()) { DisplayName = "Field", MemberName = "Field" };
+ var result = attribute.GetValidationResult(value, context);
+ return (result == null, result?.ErrorMessage);
+ }
+
+ // null and empty are explicitly allowed; presence is enforced by [Required] separately
+ [Fact]
+ public void Null_Passes()
+ {
+ var (isValid, _) = Validate(null);
+ Assert.True(isValid);
+ }
+
+ [Fact]
+ public void EmptyString_Passes()
+ {
+ var (isValid, _) = Validate(string.Empty);
+ Assert.True(isValid);
+ }
+
+ public class Passes
+ {
+ [Theory]
+ [InlineData("Artist")] // simple name
+ [InlineData("music.flac")] // file with extension
+ [InlineData("My Artist")] // spaces
+ [InlineData("Artist.Name")] // dots that are not traversal
+ [InlineData("Artist/Album")] // forward slash
+ [InlineData("Artist\\Album")] // backslash
+ [InlineData("Artist/Album/Song.flac")] // multi-level forward slash
+ [InlineData("Artist\\Album\\Song.flac")] // multi-level backslash
+ [InlineData("Artist/Album\\Song.flac")] // mixed separators
+ [InlineData("...dir")] // three dots — not a traversal segment
+ [InlineData("..hidden")] // starts with .. but is a name, not traversal
+ [InlineData(".hidden")] // dotfile name, not a traversal segment
+ [InlineData("dir/..dir")] // segment starts with .. but is not exactly ..
+ [InlineData("Ünïcödé Àrtïst")] // latin extended
+ [InlineData("Ünïcödé/Àrtïst/Àlbüm")] // latin extended, forward slash
+ [InlineData("Ünïcödé\\Àrtïst\\Àlbüm")] // latin extended, backslash
+ [InlineData("пользователь/Музыка")] // Cyrillic, forward slash
+ [InlineData("пользователь\\Музыка\\Альбом")] // Cyrillic, backslash
+ [InlineData("用户/音乐/专辑")] // Chinese, forward slash
+ [InlineData("用户\\音乐\\专辑")] // Chinese, backslash
+ [InlineData("ユーザー/音楽/アルバム")] // Japanese, forward slash
+ [InlineData("ユーザー\\音楽\\アルバム")] // Japanese, backslash
+ [InlineData("사용자/음악/앨범")] // Korean, forward slash
+ [InlineData("사용자\\음악\\앨범")] // Korean, backslash
+ [InlineData("مستخدم/موسيقى")] // Arabic
+ [InlineData("χρήστης/μουσική")] // Greek
+ [InlineData("משתמש/מוזיקה")] // Hebrew
+ public void RelativePath_Passes(string value)
+ {
+ var (isValid, _) = Validate(value);
+ Assert.True(isValid);
+ }
+ }
+
+ public class Traversal_Fails
+ {
+ [Theory]
+ [InlineData("..")] // bare traversal
+ [InlineData(".")] // bare current-dir
+ [InlineData("../music")] // traversal prefix, forward slash
+ [InlineData("./music")] // current-dir prefix
+ [InlineData("music/../other")] // traversal in middle
+ [InlineData("music/./other")] // current-dir in middle
+ [InlineData("sub/../../escape")] // double traversal
+ [InlineData("music\\..")] // traversal via backslash
+ [InlineData("music\\.")] // current-dir via backslash
+ [InlineData("music/dir/..")] // traversal at end
+ [InlineData("a/b/c/../../d")] // multi-level traversal
+ [InlineData("Ünïcödé/../escape")] // latin extended with traversal
+ [InlineData("пользователь/../etc")] // Cyrillic with traversal
+ [InlineData("用户/音乐/../../escape")] // Chinese with deep traversal
+ [InlineData("ユーザー\\..\\escape")] // Japanese with backslash traversal
+ public void TraversalSegment_Fails(string value)
+ {
+ var (isValid, errorMessage) = Validate(value);
+ Assert.False(isValid);
+ Assert.Contains("traversal segments", errorMessage);
+ }
+ }
+
+ public class Absolute_Fails
+ {
+ [Theory]
+ // Windows drive-letter paths
+ [InlineData("C:\\Music")]
+ [InlineData("C:/Music")]
+ [InlineData("D:\\path\\to\\file")]
+ [InlineData("Z:/downloads")]
+ [InlineData("C:")]
+ // Windows root-relative (no drive letter)
+ [InlineData("\\Music")]
+ [InlineData("\\Music\\Artist\\Album")]
+ // Windows UNC
+ [InlineData("\\\\server\\share")]
+ [InlineData("\\\\server\\share\\folder")]
+ [InlineData("\\\\192.168.1.1\\share")]
+ // Unix/Linux
+ [InlineData("/")]
+ [InlineData("/home/user")]
+ [InlineData("/etc/hosts")]
+ [InlineData("/usr/local/bin")]
+ // macOS
+ [InlineData("/Users/username")]
+ [InlineData("/Volumes/External Drive")]
+ // Unix-style UNC (// prefix)
+ [InlineData("//server/share")]
+ [InlineData("//server/share/folder")]
+ // Windows drive-relative (no separator after colon; still rooted)
+ [InlineData("C:Music")]
+ public void AbsolutePath_Fails(string value)
+ {
+ var (isValid, errorMessage) = Validate(value);
+ Assert.False(isValid);
+ Assert.Equal("The Field field must be a relative path.", errorMessage);
+ }
+ }
+}