From 8bcfe480bc0ec641dedb384a2af8e5f3866dcbb8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 7 Jan 2023 10:30:06 -0600 Subject: use named params to avoid issues with positional weirdness --- src/slskd/Relay/API/Controllers/RelayController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Relay/API/Controllers') diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index d4fb4810..69087310 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -200,7 +200,7 @@ namespace slskd.Relay // provide the encrypted value as the credential with the request. the validation below verifies a bunch of // things, including that the encrypted value matches the expected value. the goal here is to ensure that the // caller is the same caller that received the request, and that the caller knows the shared secret. - if (!Relay.TryValidateFileStreamResponseCredential(token: guid, agentName, filename, credential)) + if (!Relay.TryValidateFileStreamResponseCredential(token: guid, agentName: agentName, filename: filename, credential: credential)) { Log.Warning("Failed to authenticate file upload token {Token} from a caller claiming to be agent {Agent}", guid, agentName); return Unauthorized(); -- cgit v1.2.3 From c0b9ef897914242f4cb0556f5feae73f34914f9f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 7 Jan 2023 10:30:38 -0600 Subject: validate filename and stream from multipart uploads --- src/slskd/Relay/API/Controllers/RelayController.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/slskd/Relay/API/Controllers') diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index 69087310..a36e643d 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -185,6 +185,16 @@ namespace slskd.Relay var contentDisposition = ContentDispositionHeaderValue.Parse(fileSection.ContentDisposition); filename = contentDisposition.FileName.Value; stream = fileSection.Body; + + if (string.IsNullOrEmpty(filename)) + { + throw new ArgumentException("Upload filename is null or empty"); + } + + if (stream == null || !stream.CanRead) + { + throw new ArgumentException("Unable to obtain stream from request"); + } } catch (Exception ex) { -- cgit v1.2.3 From 87f72f7f482953c90ed50eca647f36827cdd64cd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 7 Jan 2023 16:14:00 -0600 Subject: polish logging --- src/slskd/Relay/API/Controllers/RelayController.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/slskd/Relay/API/Controllers') diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index a36e643d..f6c6d778 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -21,6 +21,7 @@ namespace slskd.Relay { using System; using System.Collections.Generic; + using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -115,7 +116,7 @@ namespace slskd.Relay return BadRequest(); } - Log.Information("Handling file download for token {Token} from a caller claiming to be agent {Agent}", token, agentName); + 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. // this is done to support retries @@ -127,9 +128,9 @@ namespace slskd.Relay var sourceFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, filename); - Log.Information("Agent {Agent} authenticated. Sending file {File}", agentName, filename); + Log.Information("Agent {Agent} authenticated for token {Token}. Sending file {Filename}", agentName, guid, filename); - var stream = new FileStream(Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, filename), FileMode.Open); + var stream = new FileStream(sourceFile, FileMode.Open); return File(stream, "application/octet-stream"); } @@ -204,7 +205,7 @@ namespace slskd.Relay return BadRequest(); } - Log.Information("Handling file upload for token {Token} from a caller claiming to be agent {Agent}", token, agentName); + Log.Information("Handling file upload of {Filename} ({Token}) from a caller claiming to be agent {Agent}", filename, token, agentName); // agents must encrypt the Id they were given in the request with the secret they share with the controller, and // provide the encrypted value as the credential with the request. the validation below verifies a bunch of @@ -216,7 +217,7 @@ namespace slskd.Relay return Unauthorized(); } - Log.Information("File upload of {Filename} ({Token}) from agent {Agent} validated and authenticated. Forwarding file stream.", filename, token, agentName); + Log.Information("Agent {Agent} authenticated for token {Token}. Forwarding file stream for {Filename}", agentName, guid, filename); // pass the stream back to the relay service, which will in turn pass it to the upload service, and use it to // feed data into the remote upload. await this call, it will complete when the upload is complete, one way or the other. @@ -279,6 +280,8 @@ namespace slskd.Relay return BadRequest(); } + Log.Information("Handling share upload ({Token}) from a caller claiming to be agent {Agent}", token, agentName); + if (!Relay.TryValidateShareUploadCredential(token: guid, agentName, credential)) { Log.Warning("Failed to authenticate share upload from caller claiming to be agent {Agent} using token {Token}", agentName, guid); @@ -290,14 +293,19 @@ namespace slskd.Relay try { - Log.Debug("Uploading share from {Agent} to {Filename}", agentName, temp); + Log.Information("Agent {Agent} authenticated for token {Token}. Beginning download of shares to {Filename}", agentName, guid, temp); + + var sw = new Stopwatch(); + sw.Start(); using var outputStream = new FileStream(temp, FileMode.CreateNew, FileAccess.Write); using var inputStream = database.OpenReadStream(); await inputStream.CopyToAsync(outputStream); - Log.Debug("Upload of share from {Agent} to {Filename} complete", agentName, temp); + sw.Stop(); + + Log.Information("Download of shares from {Agent} ({Token}) complete ({Size} in {Duration}ms)", agentName, guid, ((double)inputStream.Length).SizeSuffix(), sw.ElapsedMilliseconds); await Relay.HandleShareUpload(agentName, id: guid, shares, temp); -- cgit v1.2.3