From 01836889040aa76883bf81eb0f4b900f6e7fc4be Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 May 2026 16:36:10 -0500 Subject: make CombineSafely() even safer --- .../Common/FileSafety/CombineSafelyTests.cs | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) (limited to 'tests') diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs index dce61235..2d496c4b 100644 --- a/tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs +++ b/tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs @@ -51,6 +51,24 @@ public partial class FileSafetyTests Assert.EndsWith(Path.Combine("foo", "bar"), result); } + [Fact] + public void Throws_Given_Null_Segment() + { + var ex = Record.Exception(() => FileSafety.CombineSafely(Base, "foo", null, "bar")); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + + [Fact] + public void Throws_Given_Null_Segments() + { + var ex = Record.Exception(() => FileSafety.CombineSafely(Base, segments: null)); + + Assert.NotNull(ex); + Assert.IsType(ex); + } + [Fact] public void SingleSegment_ReturnsBaseAndSegment() { @@ -116,6 +134,83 @@ public partial class FileSafetyTests Assert.Contains("Absolute", ex.Message); } + [Theory] + [InlineData("foo:bar")] + [InlineData("C:relative")] + [InlineData("sub/foo:bar")] + [InlineData("sub/foo/bar:qux")] + public void Throws_ArgumentException_Given_Colon_In_Segment_On_Windows(string segment) + { + var ex = Record.Exception(() => FileSafety.CombineSafely(Base, OSPlatform.Windows, segment)); + + Assert.NotNull(ex); + Assert.Contains("Colon", ex.Message); + } + + [Theory] + [InlineData("foo:bar")] + [InlineData("sub/foo:bar")] + [InlineData("sub/foo/bar:qux")] + public void Accepts_Colon_In_Segment_On_Linux(string segment) + { + var result = Record.Exception(() => FileSafety.CombineSafely(Base, OSPlatform.Linux, segment)); + + Assert.Null(result); + } + + [Theory] + [InlineData("C:relative")] + public void Accepts_Colon_In_Segment_On_Linux_Hits_Backstop_On_Windows(string segment) + { + var result = Record.Exception(() => FileSafety.CombineSafely(Base, OSPlatform.Linux, segment)); + + if (!OperatingSystem.IsWindows()) + { + Assert.Null(result); + } + else + { + // this test breaks when run on windows + // this is valid on linux (which we're trying to test for) + // but invalid on windows because it is a drive-relative path and it successfully traverses + Assert.NotNull(result); + Assert.IsType(result); + Assert.Contains("traversal detected", result.Message); + } + } + + [Theory] + [InlineData("/Music")] + [InlineData("\\Music")] + public void Throws_ArgumentException_Given_Leading_Slash_In_Segment_On_Windows(string segment) + { + var ex = Record.Exception(() => FileSafety.CombineSafely(Base, OSPlatform.Windows, segment)); + + Assert.NotNull(ex); + Assert.Contains("Drive-relative", ex.Message); + } + + [Theory] + [InlineData("\\Music")] + public void Accepts_Leading_Backslash_In_Segment_On_Linux(string segment) + { + var result = Record.Exception(() => FileSafety.CombineSafely(Base, OSPlatform.Linux, segment)); + + if (!OperatingSystem.IsWindows()) + { + Assert.Null(result); + } + else + { + // this test breaks when run on windows + // this is valid on linux (which we're trying to test for) + // but invalid on windows because it is a drive-relative path and it successfully traverses + Assert.NotNull(result); + Assert.IsType(result); + Assert.Contains("traversal detected", result.Message); + } + } + [Theory] // Bare traversal [InlineData("..")] -- cgit v1.2.3