aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Transfers/Downloads
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-05-31 21:17:39 -0500
committerJP Dillingham <jp@dillingham.me>2026-05-31 21:17:39 -0500
commita19fcd796c3acd497388daa0e38f5fa37bf511ee (patch)
tree9cff5b7e687da2bf5819164887edf7ae39adc4bb /src/slskd/Transfers/Downloads
parent845da6c1c95ed18ec88c93a48f0a04c539f28bc3 (diff)
parent9c8a0cdbb1bf86c1d313919b37446b22a353c7c3 (diff)
downloadslskd-a19fcd796c3acd497388daa0e38f5fa37bf511ee.tar.gz
Merge branch 'destination' of github.com:slskd/slskd into destination
Diffstat (limited to 'src/slskd/Transfers/Downloads')
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs58
1 files changed, 42 insertions, 16 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index af7a4699..ee7f2efd 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -802,11 +802,13 @@ namespace slskd.Transfers.Downloads
var filename = FileSafety.SanitizePath(transfer.Filename, replacement: '_');
- // full remote path, e.g. '@abcd\Foo\Bar\Baz\Qux.ext' => '@abcd\Foo\Bar\Baz'
- source_path = (Path.GetDirectoryName(filename) ?? string.Empty).TrimEnd('/', '\\');
+ // 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('/', '\\');
// name of the remote parent directory, so using the example above, 'Baz'
- source_directory = (Path.GetFileName(source_path) ?? string.Empty).TrimEnd('/', '\\');
+ source_directory = (FileSafety.GetFileNameSafely(source_path) ?? string.Empty).TrimEnd('/', '\\');
batch_id = (batch?.Id ?? Guid.Empty) == Guid.Empty ? "unknown_batch_id" : batch.Id.ToString();
@@ -818,7 +820,7 @@ namespace slskd.Transfers.Downloads
search_id = (search?.Id ?? Guid.Empty) == Guid.Empty ? "unknown_search_id" : search.Id.ToString();
search_text = FileSafety.SanitizeFilename(
- filename: search?.SearchText ?? "unknown_search_text",
+ filename: string.IsNullOrWhiteSpace(search?.SearchText) ? "unknown_search_text" : search.SearchText,
replacement: '_');
}
catch (Exception ex)
@@ -859,13 +861,13 @@ namespace slskd.Transfers.Downloads
destination = FileSafety.SanitizePath(destination);
- Log.Debug("Derived destination for download of {Filename} from {User}: {Destination} (id: {Id}, configuration: {Pattern}, args: {Args})", Path.GetFileName(transfer.Filename), transfer.Username, destination, transfer.Id, pattern, new { source_username, source_path, source_directory, batch_id, batch_external_id, search_id, search_text });
+ Log.Debug("Derived destination for download of {Filename} from {User}: {Destination} (id: {Id}, configuration: {Pattern}, args: {Args})", FileSafety.GetFileNameSafely(transfer.Filename), transfer.Username, destination, transfer.Id, pattern, new { source_username, source_path, source_directory, batch_id, batch_external_id, search_id, search_text });
// the only way to get here is through a regression in the logic above; if we are seeing this, ensure we are
// properly sanitizing the final output to strip leading slashes and replace traversal segments
if (FileSafety.IsPathAbsolute(destination) || FileSafety.ContainsTraversalSegments(destination))
{
- Log.Error("Derived destination for download of {Filename} from {User}: {Destination} (id: {Id}, configuration: {Pattern}, args: {Args})", Path.GetFileName(transfer.Filename), transfer.Username, destination, transfer.Id, pattern, new { source_username, source_path, source_directory, batch_id, batch_external_id, search_id, search_text });
+ Log.Error("Derived destination for download of {Filename} from {User}: {Destination} (id: {Id}, configuration: {Pattern}, args: {Args})", FileSafety.GetFileNameSafely(transfer.Filename), transfer.Username, destination, transfer.Id, pattern, new { source_username, source_path, source_directory, batch_id, batch_external_id, search_id, search_text });
throw new SlskdException("Derived a transfer destination that is either not relative or contains traversal segments; neither is allowed. Please report this on GitHub.");
}
@@ -1214,14 +1216,38 @@ namespace slskd.Transfers.Downloads
? OptionsMonitor.CurrentValue.Transfers.Download.Destination.Permissions.Mode.ToUnixFileMode()
: null;
- // store incomplete files under a username subdirectory to prevent collisions between users and to make
- // it safe to resume partial downloads without risking picking up an unrelated file. if the remote file
- // changes size between attempts, Soulseek.NET's DownloadAsync() will throw, so the only real risk is
- // in a case where the file contents have changed but the size remains. the resulting file will be
- // corrupted in that case. users that are concerned about this (extremely unlikely) case should
- // configure slskd to always overwrite partial files.
- var incompleteFilename = transfer.Filename.ToLocalFilename(
- baseDirectory: FileSafety.CombineSafely(OptionsMonitor.CurrentValue.Directories.Incomplete, FileSafety.SanitizeFilename(transfer.Username)));
+ /*
+ store incomplete files under a username subdirectory to prevent collisions between users and to make
+ it safe to resume partial downloads without risking picking up an unrelated file.
+
+ we're going to store the file as:
+
+ <incomplete directory>/<username>/<full path to file on remote client>/<filename>
+
+ so, a remote file 'C:\Music\Public_Domain\Foo\Bar\Baz.ext' from user 'FooBar' becomes:
+
+ linux: incomplete/C:/FooBar/Music/Public_Domain/Foo/Bar/Baz.ext
+ windows: incomplete\C_\FooBar\Music\Public_Domain\Foo\Bar\Baz.ext
+
+ slashes are normalized to the current OS and any invalid characters are replaced with an underscore, _.
+ this mitigates the risk of an improper partial resume to a great extent.
+
+ if the remote file changes size between attempts, Soulseek.NET's DownloadAsync() will throw because
+ the size we send originates from the original file, and there will be a mismatch.
+
+ the only remaining risk is in a case where the file contents have changed but the size remains the same.
+ the resulting file will be corrupted in that case, and there's nothing we can really do about it. users
+ that are concerned about this (extremely unlikely) case should configure slskd to always overwrite
+ partial files.
+ */
+ 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 incompleteFilename = FileSafety.CombineSafely(incompleteDirectory, sanitizedUsername, sanitizedRemotePath, sanitizedFilename);
+
+ Log.Warning("Incomplete file for {Filename} from {Username} will be: {Filename}", FileSafety.GetFileNameSafely(transfer.Filename), transfer.Username, incompleteFilename);
Log.Debug("Invoking Soulseek DownloadAsync() for {Filename} from {Username}", transfer.Filename, transfer.Username);
transfer.Attempts = 1;
@@ -1320,7 +1346,7 @@ namespace slskd.Transfers.Downloads
var subdirectory = await DeriveDestination(transfer);
var destinationDirectory = FileSafety.CombineSafely(OptionsMonitor.CurrentValue.Directories.Downloads, subdirectory);
- var conflictStrategy = OptionsMonitor.CurrentValue.Transfers.Download.Destination.Conflict.ToEnum<DestinationConflictStrategy>();
+ var existsStrategy = OptionsMonitor.CurrentValue.Transfers.Download.Destination.Exists.ToEnum<DestinationExistsStrategy>();
Log.Debug("Destination directory for {Filename}: {Destination}", transfer.Filename, destinationDirectory);
@@ -1328,7 +1354,7 @@ namespace slskd.Transfers.Downloads
sourceFilename: incompleteFilename,
destinationDirectory: destinationDirectory,
unixFileMode: unixFileMode,
- overwrite: conflictStrategy == DestinationConflictStrategy.Overwrite,
+ overwrite: existsStrategy == DestinationExistsStrategy.Overwrite,
deleteSourceDirectoryIfEmptyAfterMove: true);
Log.Debug("Moved file {Filename} to {Destination}", transfer.Filename, finalFilename);