diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-13 18:15:16 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-13 18:38:34 -0500 |
| commit | 8336a3c3ebdfc65dd7d7419f27c195a4aa729856 (patch) | |
| tree | 253c6dd1b62b8276d3fa880c92bd1c24db3c7a6e /src | |
| parent | fe1a6a3298116bc3980306cbb287f7137b378d1a (diff) | |
| download | slskd-8336a3c3ebdfc65dd7d7419f27c195a4aa729856.tar.gz | |
write our own logic for path relative/absolute
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/FileSafety.cs | 48 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/AbsolutePathAttribute.cs | 11 | ||||
| -rw-r--r-- | src/slskd/Common/Validation/RelativePathAttribute.cs | 9 |
3 files changed, 54 insertions, 14 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index a3899397..f94b9c7b 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -68,7 +68,7 @@ public static class FileSafety // 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 (Path.IsPathRooted(segment)) + if (IsPathAbsolute(segment)) { throw new ArgumentException($"Rooted paths are not permitted in segments: '{segment}'"); } @@ -100,4 +100,50 @@ public static class FileSafety /// <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/src/slskd/Common/Validation/AbsolutePathAttribute.cs b/src/slskd/Common/Validation/AbsolutePathAttribute.cs index 0eeb4a38..4c9f5e53 100644 --- a/src/slskd/Common/Validation/AbsolutePathAttribute.cs +++ b/src/slskd/Common/Validation/AbsolutePathAttribute.cs @@ -33,8 +33,6 @@ namespace slskd.Validation { using System.ComponentModel.DataAnnotations; - using System.IO; - using System.Linq; /// <summary> /// Validates that the specified path is absolute. @@ -47,14 +45,13 @@ namespace slskd.Validation { var path = value.ToString(); - if (!Path.IsPathRooted(path)) + if (!FileSafety.IsPathAbsolute(path)) { - return new ValidationResult($"The {validationContext.DisplayName} field must specify an absolute file path."); + return new ValidationResult($"The {validationContext.DisplayName} field must be an absolute file path."); } - var segments = path.Split('/', '\\'); - - if (segments.Any(s => s == ".." || s == ".")) + // unrelated to absolute/relative check; we disallow traversal segments across the board so check while we're here + if (FileSafety.ContainsTraversalSegments(path)) { return new ValidationResult($"The {validationContext.DisplayName} field contains one or more unsafe path traversal segments ('.' or '..')"); } diff --git a/src/slskd/Common/Validation/RelativePathAttribute.cs b/src/slskd/Common/Validation/RelativePathAttribute.cs index 824ff18d..e8865554 100644 --- a/src/slskd/Common/Validation/RelativePathAttribute.cs +++ b/src/slskd/Common/Validation/RelativePathAttribute.cs @@ -33,8 +33,6 @@ namespace slskd.Validation { using System.ComponentModel.DataAnnotations; - using System.IO; - using System.Linq; /// <summary> /// Validates that the specified path is relative. @@ -47,14 +45,13 @@ namespace slskd.Validation { var path = value.ToString(); - if (Path.IsPathRooted(path)) + if (!FileSafety.IsPathRelative(path)) { return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path."); } - var segments = path.Split('/', '\\'); - - if (segments.Any(s => s == ".." || s == ".")) + // unrelated to absolute/relative check; we disallow traversal segments across the board so check while we're here + if (FileSafety.ContainsTraversalSegments(path)) { return new ValidationResult($"The {validationContext.DisplayName} field contains one or more unsafe path traversal segments ('.' or '..')"); } |
