aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-06-05 19:16:57 -0500
committerJP Dillingham <jp@dillingham.me>2026-06-05 19:16:57 -0500
commit62134e78472a51ef0becff9c0850f635933ab506 (patch)
treef412ca8a7ad7da0f1d4a5f08e07ae62d9e3599ea
parentee41b93605a380480a5f7d826e7ae0602cd52de1 (diff)
downloadslskd-62134e78472a51ef0becff9c0850f635933ab506.tar.gz
stop using any Path.* method, because they are all fucked up and untestable
-rw-r--r--src/slskd/Common/FileSafety.cs18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs
index bf81da0e..1634ca7a 100644
--- a/src/slskd/Common/FileSafety.cs
+++ b/src/slskd/Common/FileSafety.cs
@@ -102,7 +102,15 @@ public static class FileSafety
throw new ArgumentNullException(nameof(segments), $"One or more segments is null");
}
+ root = LocalizePath(root, os).TrimEnd('/').TrimEnd('\\');
+
+ if (segments.Length == 0)
+ {
+ return root;
+ }
+
var isWindows = os.HasValue ? os.Value == OperatingSystem.Windows : System.OperatingSystem.IsWindows();
+ var sep = isWindows ? '\\' : '/';
foreach (var segment in segments)
{
@@ -139,11 +147,17 @@ public static class FileSafety
}
}
- var combined = Path.Combine(root, Path.Combine(segments));
+ segments = [.. segments
+ .Where(s => !string.IsNullOrEmpty(s))
+ .Select(s => LocalizePath(s))];
+
+ var combined = string.Join(sep, [root, .. segments]);
// 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)))
+ var expectedPrefix = root + sep;
+
+ if (combined != root && !combined.StartsWith(expectedPrefix))
{
throw new ArgumentException($"Path traversal detected in combined path '{combined}', which is not allowed");
}