diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-30 18:35:15 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-30 18:35:15 -0500 |
| commit | bebeb114426e717596b1cf485e358eae65cb08e0 (patch) | |
| tree | 8915c5a6cd416aa4435c72fc4e2c32ba68cc20de | |
| parent | 957dd89b04e941a17efb76dbfa439a2a0f5157ad (diff) | |
| download | slskd-bebeb114426e717596b1cf485e358eae65cb08e0.tar.gz | |
use full sanitized path for incomplete files
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index d09bb71b..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(); @@ -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,17 +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), - FileSafety.SanitizePath(transfer.Filename))); + /* + 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; |
