aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/slskd/Common/FileSafety.cs149
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafetyTests.cs545
2 files changed, 694 insertions, 0 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs
new file mode 100644
index 00000000..f94b9c7b
--- /dev/null
+++ b/src/slskd/Common/FileSafety.cs
@@ -0,0 +1,149 @@
+// <copyright file="FileSafety.cs" company="JP Dillingham">
+// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
+// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
+// █__ --█ █__ --█ ◄█ - █
+// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
+// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
+// │ Copyright (c) JP Dillingham.
+// │
+// │ This program is free software: you can redistribute it and/or modify
+// │ it under the terms of the GNU Affero General Public License as published
+// │ by the Free Software Foundation, version 3.
+// │
+// │ This program is distributed in the hope that it will be useful,
+// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
+// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// │ GNU Affero General Public License for more details.
+// │
+// │ You should have received a copy of the GNU Affero General Public License
+// │ along with this program. If not, see https://www.gnu.org/licenses/.
+// │
+// │ This program is distributed with Additional Terms pursuant to Section 7
+// │ of the AGPLv3. See the LICENSE file in the root directory of this
+// │ project for the complete terms and conditions.
+// │
+// │ https://slskd.org
+// │
+// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
+// │ SPDX-FileCopyrightText: JP Dillingham
+// │ SPDX-License-Identifier: AGPL-3.0-only
+// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
+// </copyright>
+
+namespace slskd;
+
+using System;
+using System.IO;
+using System.Linq;
+
+/// <summary>
+/// Utility functions to help safely work with paths from untrusted sources.
+/// </summary>
+public static class FileSafety
+{
+ /// <summary>
+ /// An alternative to <see cref="Path.Combine(string, string)"/> that disallows rooted segments in the second or
+ /// subsequent position, and disallows segments containing path traversal characters "." and "..".
+ /// </summary>
+ /// <param name="root">The root directory.</param>
+ /// <param name="segments">The segments to append.</param>
+ /// <returns>The combined path.</returns>
+ /// <exception cref="ArgumentNullException">Thrown if the root, list of segments, or one or more segments is null.</exception>
+ /// <exception cref="ArgumentException">Thrown if any of the specified segments is rooted, or if any contain path traversal characters.</exception>
+ public static string CombineSafely(string root, params string[] segments)
+ {
+ // ensure no inputs are null. matches the behavior of Path.Combine()
+ if (string.IsNullOrWhiteSpace(root))
+ {
+ throw new ArgumentNullException(nameof(root), $"Specified root directory is null or consists only of whitespace");
+ }
+
+ if (segments is null || segments.Any(s => s is null))
+ {
+ throw new ArgumentNullException(nameof(segments), $"One or more segments is null");
+ }
+
+ foreach (var segment in segments)
+ {
+ // if any segment is rooted (leading slash), Path.Combine drops everyting up to that segment
+ // and it becomes the new root. example: Path.Combine("/home/users/foo", "/etc") results in "/etc"
+ // not what we want!
+ if (IsPathAbsolute(segment))
+ {
+ throw new ArgumentException($"Rooted paths are not permitted in segments: '{segment}'");
+ }
+
+ var parts = segment.Split(Path.DirectorySeparatorChar, '\\', '/');
+
+ // ensure the segments we're combining don't contain traversal segments "." or ".."
+ // untrusted input could use this to "break out" of the root directory and access sensitive areas
+ if (parts.Any(s => s is "." or ".."))
+ {
+ throw new ArgumentException($"Path traversal is not permitted: '{segment}'");
+ }
+ }
+
+ var combined = Path.Combine(root, Path.Combine(segments));
+
+ // not sure how we could possibly get here, but if we do, throw.
+ if (!Path.GetFullPath(combined).StartsWith(Path.GetFullPath(root)))
+ {
+ throw new ArgumentException($"Path traversal detected in path {combined}, which is not allowed");
+ }
+
+ return combined;
+ }
+
+ /// <summary>
+ /// Returns a value indicating whether the specified <paramref name="path"/> contains path traversal segments.
+ /// </summary>
+ /// <param name="path">The path to check.</param>
+ /// <returns>True if one or more segments is present, false otherwise.</returns>
+ 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.
+ /// </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.
+ /// </remarks>
+ /// <param name="path">The path to check.</param>
+ /// <returns>True if the path is absolute, false otherwise.</returns>
+ public static bool IsPathAbsolute(string path)
+ {
+ 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 '\\')
+ {
+ return true;
+ }
+
+ // 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] == ':')
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ /// <summary>
+ /// Returns a value indicating whether the specified <paramref name="path"/>, using
+ /// platform-independent rules that recognize both Unix and Windows absolute path formats.
+ /// </summary>
+ /// <param name="path">The path to check.</param>
+ /// <returns>True if the path is relative, false otherwise.</returns>
+ public static bool IsPathRelative(string path) => !IsPathAbsolute(path);
+} \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Common/FileSafetyTests.cs b/tests/slskd.Tests.Unit/Common/FileSafetyTests.cs
new file mode 100644
index 00000000..ae1efccf
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/FileSafetyTests.cs
@@ -0,0 +1,545 @@
+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);
+ }
+ }
+} \ No newline at end of file