aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-06-05 19:18:18 -0500
committerJP Dillingham <jp@dillingham.me>2026-06-06 07:45:22 -0500
commit12cf9ad6b33ae29e9e7caea20b205fadd45798bf (patch)
tree3c185c49812d427faf8d0cb9c0134c759a984f4c /src
parent62134e78472a51ef0becff9c0850f635933ab506 (diff)
downloadslskd-12cf9ad6b33ae29e9e7caea20b205fadd45798bf.tar.gz
small tweaks
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/FileSafety.cs14
-rw-r--r--src/slskd/Common/Validation/AbsolutePathAttribute.cs2
-rw-r--r--src/slskd/Common/Validation/RelativePathAttribute.cs4
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs4
4 files changed, 13 insertions, 11 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs
index 1634ca7a..bf8302d1 100644
--- a/src/slskd/Common/FileSafety.cs
+++ b/src/slskd/Common/FileSafety.cs
@@ -102,7 +102,7 @@ public static class FileSafety
throw new ArgumentNullException(nameof(segments), $"One or more segments is null");
}
- root = LocalizePath(root, os).TrimEnd('/').TrimEnd('\\');
+ root = root.TrimEnd('/').TrimEnd('\\');
if (segments.Length == 0)
{
@@ -148,16 +148,18 @@ public static class FileSafety
}
segments = [.. segments
- .Where(s => !string.IsNullOrEmpty(s))
- .Select(s => LocalizePath(s))];
+ .Where(s => !string.IsNullOrEmpty(s))];
var combined = string.Join(sep, [root, .. segments]);
+ if (combined.TrimEnd('/').TrimEnd('\\') == root.TrimEnd('/').TrimEnd('\\'))
+ {
+ return combined;
+ }
+
// 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
- var expectedPrefix = root + sep;
-
- if (combined != root && !combined.StartsWith(expectedPrefix))
+ if (!combined.StartsWith($"{root}/") && !combined.StartsWith($"{root}\\"))
{
throw new ArgumentException($"Path traversal detected in combined path '{combined}', which is not allowed");
}
diff --git a/src/slskd/Common/Validation/AbsolutePathAttribute.cs b/src/slskd/Common/Validation/AbsolutePathAttribute.cs
index f2615315..217a4eea 100644
--- a/src/slskd/Common/Validation/AbsolutePathAttribute.cs
+++ b/src/slskd/Common/Validation/AbsolutePathAttribute.cs
@@ -49,7 +49,7 @@ namespace slskd.Validation
OS = os;
}
- public OperatingSystem? OS { get; }
+ public OperatingSystem OS { get; }
private OperatingSystem? Injected { get; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
diff --git a/src/slskd/Common/Validation/RelativePathAttribute.cs b/src/slskd/Common/Validation/RelativePathAttribute.cs
index f691d4e5..ea2b627b 100644
--- a/src/slskd/Common/Validation/RelativePathAttribute.cs
+++ b/src/slskd/Common/Validation/RelativePathAttribute.cs
@@ -63,7 +63,7 @@ namespace slskd.Validation
{
if (!FileSafety.IsPathRelative(path, os: Injected ?? Compute.OperatingSystem()))
{
- return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path on all operating systems.");
+ return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path on the current operating system.");
}
return ValidationResult.Success;
@@ -72,7 +72,7 @@ namespace slskd.Validation
// OS == OperatingSystem.All or .Any;
if (!FileSafety.IsPathRelative(path, os: OperatingSystem.Linux) || !FileSafety.IsPathRelative(path, os: OperatingSystem.Windows))
{
- return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path.");
+ return new ValidationResult($"The {validationContext.DisplayName} field must be a relative path on all operating systems.");
}
}
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 0f7805ba..ad91ba2c 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -815,7 +815,7 @@ namespace slskd.Transfers.Downloads
// full remote path, e.g. '@@abcd\Foo\Bar\Baz\Qux.ext' => '@@abcd\Foo\Bar\Baz'
// retains the path root, if there is one (Soulseek NS does this), which may not be what
// people want, so possibly revisit and add StripPathRoot()
- source_path = (FileSafety.GetDirectoryNameSafely(filename) ?? string.Empty).TrimEnd('/', '\\');
+ source_path = (FileSafety.GetDirectoryNameSafely(filename, retainRoot: false) ?? string.Empty).TrimEnd('/', '\\');
// name of the remote parent directory, so using the example above, 'Baz'
source_directory = (FileSafety.GetFileNameSafely(source_path) ?? string.Empty).TrimEnd('/', '\\');
@@ -1249,7 +1249,7 @@ namespace slskd.Transfers.Downloads
var incompleteDirectory = OptionsMonitor.CurrentValue.Directories.Incomplete;
var sanitizedUsername = FileSafety.SanitizePathSegment(transfer.Username);
var sanitizedFilename = FileSafety.GetFileNameSafely(transfer.Filename, sanitize: true);
- var sanitizedRemotePath = FileSafety.GetDirectoryNameSafely(transfer.Filename, sanitize: true);
+ var sanitizedRemotePath = FileSafety.GetDirectoryNameSafely(transfer.Filename, sanitize: true) ?? string.Empty;
var incompleteFilename = FileSafety.CombineSafely(incompleteDirectory, sanitizedUsername, sanitizedRemotePath, sanitizedFilename);