diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-22 11:56:59 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-22 11:56:59 -0500 |
| commit | 9376c6a3b47ecffc1f1acc880c931d784db3c18b (patch) | |
| tree | e893dc41718b99d4710bca03b299828f2362a429 /src | |
| parent | 8b5505ba5e1c102cd639666df96e139a5858ed55 (diff) | |
| download | slskd-9376c6a3b47ecffc1f1acc880c931d784db3c18b.tar.gz | |
adjust FileSafety tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/FileSafety.cs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index 1e25ccd0..c024aa86 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -51,7 +51,19 @@ public static class FileSafety /// <returns>The combined path.</returns> /// <exception cref="ArgumentNullException">Thrown if the root, list of segments, or one or more segments is null.</exception> /// <exception cref="ArgumentException">Thrown if any of the specified segments is rooted, or if any contain path traversal characters.</exception> - public static string CombineSafely(string root, params string[] segments) + public static string CombineSafely(string root, params string[] segments) => CombineSafely(root, os: null, segments: segments); + + /// <summary> + /// An alternative to <see cref="Path.Combine(string, string)"/> that disallows rooted segments in the second or + /// subsequent position, and disallows segments containing path traversal characters "." and "..". + /// </summary> + /// <param name="root">The root directory.</param> + /// <param name="os">An optional operating system override, for testing.</param> + /// <param name="segments">The segments to append.</param> + /// <returns>The combined path.</returns> + /// <exception cref="ArgumentNullException">Thrown if the root, list of segments, or one or more segments is null.</exception> + /// <exception cref="ArgumentException">Thrown if any of the specified segments is rooted, or if any contain path traversal characters.</exception> + public static string CombineSafely(string root, OSPlatform? os, params string[] segments) { // ensure no inputs are null. matches the behavior of Path.Combine() if (string.IsNullOrWhiteSpace(root)) @@ -59,6 +71,11 @@ public static class FileSafety throw new ArgumentNullException(nameof(root), $"Specified root directory is null or consists only of whitespace"); } + if (ContainsTraversalSegments(root)) + { + throw new ArgumentException("Specified root directory contains path traversal segments", nameof(root)); + } + if (segments is null || segments.Any(s => s is null)) { throw new ArgumentNullException(nameof(segments), $"One or more segments is null"); @@ -69,9 +86,9 @@ 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 (IsPathAbsolute(segment)) + if (IsPathAbsolute(segment, os)) { - throw new ArgumentException($"Rooted paths are not permitted in segments: '{segment}'"); + throw new ArgumentException($"Absolute paths are not permitted in segments: '{segment}'"); } var parts = segment.Split(Path.DirectorySeparatorChar, '\\', '/'); |
