aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-18 19:14:25 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-18 19:14:25 -0500
commit96ea096105f4fe0371a58cdc9ecf5c6b82a937e3 (patch)
tree8dfd5ca59a049af8367e97916910550170e8a5ca /src
parentb4e6a6009fad6b6cdf890526dda0825464c22e94 (diff)
downloadslskd-96ea096105f4fe0371a58cdc9ecf5c6b82a937e3.tar.gz
fix path detection (again)
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/FileSafety.cs48
1 files changed, 30 insertions, 18 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs
index f94b9c7b..1e25ccd0 100644
--- a/src/slskd/Common/FileSafety.cs
+++ b/src/slskd/Common/FileSafety.cs
@@ -35,6 +35,7 @@ namespace slskd;
using System;
using System.IO;
using System.Linq;
+using System.Runtime.InteropServices;
/// <summary>
/// Utility functions to help safely work with paths from untrusted sources.
@@ -102,36 +103,46 @@ public static class FileSafety
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.
+ /// Returns a value indicating whether the specified <paramref name="path"/> is absolute (rooted) on the current
+ /// operating system.
/// </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.
+ /// implementing the nuclear option and doing it ourselves.
/// </remarks>
/// <param name="path">The path to check.</param>
+ /// <param name="os">An optional operating system override, for testing.</param>
/// <returns>True if the path is absolute, false otherwise.</returns>
- public static bool IsPathAbsolute(string path)
+ public static bool IsPathAbsolute(string path, OSPlatform? os = null)
{
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 '\\')
+ if (os.HasValue ? os.Value == OSPlatform.Windows : OperatingSystem.IsWindows())
{
- return true;
+ // Windows drive-letter path: X:\ or X:/
+ // X:foo is relative on windows
+ if (path.Length >= 3
+ && char.IsAsciiLetter(path[0])
+ && path[1] == ':'
+ && (path[2] == '\\' || path[2] == '/'))
+ {
+ return true;
+ }
+
+ // UNC path \\server\share
+ if (path.StartsWith("\\\\"))
+ {
+ return true;
+ }
+
+ return false;
}
- // 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] == ':')
+ // Unix/Linux/macOS absolute paths and UNC paths with forward slashes start with /
+ if (path.StartsWith('/'))
{
return true;
}
@@ -140,10 +151,11 @@ public static class FileSafety
}
/// <summary>
- /// Returns a value indicating whether the specified <paramref name="path"/>, using
- /// platform-independent rules that recognize both Unix and Windows absolute path formats.
+ /// Returns a value indicating whether the specified <paramref name="path"/> is relative on the current
+ /// operating system.
/// </summary>
/// <param name="path">The path to check.</param>
+ /// <param name="os">An optional operating system override, for testing.</param>
/// <returns>True if the path is relative, false otherwise.</returns>
- public static bool IsPathRelative(string path) => !IsPathAbsolute(path);
+ public static bool IsPathRelative(string path, OSPlatform? os = null) => !IsPathAbsolute(path, os);
} \ No newline at end of file