aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-23 16:36:10 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-23 16:36:10 -0500
commit01836889040aa76883bf81eb0f4b900f6e7fc4be (patch)
treee60eff27860a53d2c7dab7fb1b9b476bac63ac5f
parentb2a19fb23959b932d53deef4ece7c761e614dce2 (diff)
downloadslskd-01836889040aa76883bf81eb0f4b900f6e7fc4be.tar.gz
make CombineSafely() even safer
-rw-r--r--src/slskd/Common/FileSafety.cs24
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafety/CombineSafelyTests.cs95
2 files changed, 116 insertions, 3 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs
index c024aa86..f67af8d5 100644
--- a/src/slskd/Common/FileSafety.cs
+++ b/src/slskd/Common/FileSafety.cs
@@ -81,6 +81,8 @@ public static class FileSafety
throw new ArgumentNullException(nameof(segments), $"One or more segments is null");
}
+ var isWindows = os.HasValue ? os.Value == OSPlatform.Windows : OperatingSystem.IsWindows();
+
foreach (var segment in segments)
{
// if any segment is rooted (leading slash), Path.Combine drops everyting up to that segment
@@ -91,22 +93,38 @@ public static class FileSafety
throw new ArgumentException($"Absolute paths are not permitted in segments: '{segment}'");
}
+ if (isWindows)
+ {
+ if (segment.Contains(':'))
+ {
+ throw new ArgumentException($"Colons are not permitted in segments: '{segment}'");
+ }
+
+ // on Windows, a segment beginning with a slash is considered 'drive rooted' and something like
+ // "\foo" translates to "C:\foo" when passed to Path.Combine()
+ if (segment.StartsWith('\\') || segment.StartsWith('/'))
+ {
+ throw new ArgumentException($"Drive-relative segments or those containing leading slashes 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}'");
+ throw new ArgumentException($"Path traversal is not permitted in segments: '{segment}'");
}
}
var combined = Path.Combine(root, Path.Combine(segments));
- // not sure how we could possibly get here, but if we do, throw.
+ // this is a backstop to catch any condition we haven't thought of; it makes sure that the resulting
+ // path is actually rooted in the root we provided; if not the input was successful in traversal
if (!Path.GetFullPath(combined).StartsWith(Path.GetFullPath(root)))
{
- throw new ArgumentException($"Path traversal detected in path {combined}, which is not allowed");
+ throw new ArgumentException($"Path traversal detected in combined path '{combined}', which is not allowed");
}
return combined;
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
@@ -52,6 +52,24 @@ public partial class FileSafetyTests
}
[Fact]
+ public void Throws_Given_Null_Segment()
+ {
+ var ex = Record.Exception(() => FileSafety.CombineSafely(Base, "foo", null, "bar"));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+
+ [Fact]
+ public void Throws_Given_Null_Segments()
+ {
+ var ex = Record.Exception(() => FileSafety.CombineSafely(Base, segments: null));
+
+ Assert.NotNull(ex);
+ Assert.IsType<ArgumentNullException>(ex);
+ }
+
+ [Fact]
public void SingleSegment_ReturnsBaseAndSegment()
{
var result = FileSafety.CombineSafely(Base, "subdir");
@@ -117,6 +135,83 @@ public partial class FileSafetyTests
}
[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<ArgumentException>(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<ArgumentException>(result);
+ Assert.Contains("traversal detected", result.Message);
+ }
+ }
+
+ [Theory]
// Bare traversal
[InlineData("..")]
[InlineData(".")]