From 6367208d8255f33af83a3cfd7cf2d29897caee5c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 12 May 2026 18:59:44 -0500 Subject: swap out remaining path.combine --- src/slskd/Relay/API/Controllers/RelayController.cs | 20 ++++++++++++++-- src/slskd/Relay/RelayClient.cs | 27 ++++++++-------------- 2 files changed, 28 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index f0ab0457..fdc6dbe6 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -149,6 +149,14 @@ namespace slskd.Relay return BadRequest(); } + // check the path; the caller has a valid API key but we haven't validated the credential yet, so this is worth + // doing here instead of relying on the CombineSafely() backstop + if (FileSafety.ContainsTraversalSegments(filename)) + { + Log.Warning("Suspicious attempt to download a file containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}", filename); + return BadRequest(); + } + Log.Information("Handling file download of {Filename} ({Token}) from a caller claiming to be agent {Agent}", filename, token, agentName); // note: the token remains valid after the validation attempt, unlike most other endpoints. @@ -230,6 +238,14 @@ namespace slskd.Relay throw new ArgumentException("Upload filename is null or empty"); } + // use an abundance of caution; the caller has a valid API key but we haven't validated the credential yet + // this is a really unlikely attack vector and it wouldn't do anything, but we're taking a hard stance on input validation + if (FileSafety.ContainsTraversalSegments(filename)) + { + Log.Warning("Suspicious attempt to upload a file containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}", filename); + return BadRequest(); + } + if (stream == null || !stream.CanRead) { throw new ArgumentException("Unable to obtain stream from request"); @@ -328,8 +344,8 @@ namespace slskd.Relay return Unauthorized(); } - Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Program.AppName)); - var temp = Path.Combine(Path.GetTempPath(), Program.AppName, $"share_{agentName}_{Path.GetRandomFileName()}.db"); + Directory.CreateDirectory(FileSafety.CombineSafely(Path.GetTempPath(), Program.AppName)); + var temp = FileSafety.CombineSafely(Path.GetTempPath(), Program.AppName, $"share_{agentName.ReplaceInvalidFileNameCharacters()}_{Path.GetRandomFileName()}.db"); try { diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 6d4b872a..2e001bdc 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -372,9 +372,8 @@ namespace slskd.Relay { if (FileSafety.ContainsTraversalSegments(filename)) { - var msg = $"Suspicious attempt to request file info containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}"; - Log.Warning(msg, filename); - throw new ArgumentException(msg, nameof(filename)); + Log.Warning("Suspicious attempt to request file info containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}", filename); + await HubConnection.InvokeAsync(nameof(RelayHub.ReturnFileInfo), id, false, 0); } Log.Information("Relay controller requested file info for {Filename} with ID {Id}", filename, id); @@ -394,13 +393,12 @@ namespace slskd.Relay } } - private Task HandleFileUploadRequest(string filename, long startOffset, Guid token) + private async Task HandleFileUploadRequest(string filename, long startOffset, Guid token) { if (FileSafety.ContainsTraversalSegments(filename)) { - var msg = $"Suspicious attempt to request a file upload containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}"; - Log.Warning(msg, filename); - throw new ArgumentException(msg, nameof(filename)); + Log.Warning("Suspicious attempt to request a file upload containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}", filename); + await HubConnection.InvokeAsync(nameof(RelayHub.NotifyFileUploadFailed), token); } _ = Task.Run(async () => @@ -456,22 +454,19 @@ namespace slskd.Relay await HubConnection.InvokeAsync(nameof(RelayHub.NotifyFileUploadFailed), token); } }); - - return Task.CompletedTask; } - private Task HandleNotifyFileDownloadCompleted(string filename, Guid token) + private async Task HandleNotifyFileDownloadCompleted(string filename, Guid token) { if (FileSafety.ContainsTraversalSegments(filename)) { - var msg = $"Suspicious attempt to solicit a file download containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}"; - Log.Warning(msg, filename); - throw new ArgumentException(msg, nameof(filename)); + Log.Warning("Suspicious attempt to solicit a file download containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}", filename); + return; } if (!OptionsMonitor.CurrentValue.Relay.Controller.Downloads) { - return Task.CompletedTask; + return; } Log.Information("Relay controller sent a download notification for {Filename} ({Token})", filename, token); @@ -519,8 +514,6 @@ namespace slskd.Relay Log.Information("File {Filename} successfully downloaded to {Destination}", filename, destinationFile); }); - - return Task.CompletedTask; } private Task HubConnection_Closed(Exception arg) @@ -600,7 +593,7 @@ namespace slskd.Relay return; } - var temp = Path.Combine(Path.GetTempPath(), Program.AppName, $"share_backup_{Path.GetRandomFileName()}.db"); + var temp = FileSafety.CombineSafely(Path.GetTempPath(), Program.AppName, $"share_backup_{Path.GetRandomFileName()}.db"); try { -- cgit v1.2.3