From 84080ecf24c1c506793d0d9e33ebd8bdeb76af2d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 17:26:20 -0600 Subject: fix incorrect paths in relay endpoints --- src/web/src/lib/relay.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web/src/lib/relay.js b/src/web/src/lib/relay.js index 35149f02..956a8073 100644 --- a/src/web/src/lib/relay.js +++ b/src/web/src/lib/relay.js @@ -1,9 +1,9 @@ import api from './api'; export const connect = () => { - return api.put('/relay'); + return api.put('/relay/agent'); }; export const disconnect = () => { - return api.delete('/relay'); + return api.delete('/relay/agent'); }; -- cgit v1.2.3 From 2a3e057e5b3dbc7bf55c430f10ef91e036984b2b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 17:38:35 -0600 Subject: fix the usage of NotifyFileUploadFailed --- src/slskd/Relay/API/Hubs/RelayHub.cs | 8 ++++---- src/slskd/Relay/RelayClient.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/slskd/Relay/API/Hubs/RelayHub.cs b/src/slskd/Relay/API/Hubs/RelayHub.cs index e839cd1a..4e044bdb 100644 --- a/src/slskd/Relay/API/Hubs/RelayHub.cs +++ b/src/slskd/Relay/API/Hubs/RelayHub.cs @@ -223,9 +223,9 @@ namespace slskd.Relay /// Executed by the agent to notify the controller that the agent was unable to upload the file requested by a call to . /// /// The unique identifier of the request. - /// The Exception that caused the failure. + /// A message describing the failure. /// Thrown when the agent is not fully authenticated. - public void NotifyFileUploadFailed(Guid id, Exception exception) + public void NotifyFileUploadFailed(Guid id, string message) { if (!Relay.TryGetAgentRegistration(Context.ConnectionId, out var record)) { @@ -233,9 +233,9 @@ namespace slskd.Relay throw new UnauthorizedAccessException(); } - Log.Warning("Agent {Agent} (connection {ConnectionId}) from {IP} reported upload failure for {Id}: {Message}", record.Agent, Context.ConnectionId, GetRemoteIPAddress(), id, exception.Message); + Log.Warning("Agent {Agent} (connection {ConnectionId}) from {IP} reported upload failure for {Id}: {Message}", record.Agent.Name, Context.ConnectionId, GetRemoteIPAddress(), id, message); - Relay.NotifyFileStreamException(record.Agent.Name, id, exception); + Relay.NotifyFileStreamException(record.Agent.Name, id, new RelayException(message)); } /// diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 49935b4c..63eda434 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -463,7 +463,7 @@ namespace slskd.Relay Log.Error(ex, "Failed to handle file request: {Message}", ex.Message); // report the failure to the controller. this avoids a failure due to timeout. - await HubConnection.InvokeAsync(nameof(RelayHub.NotifyFileUploadFailed), token); + await HubConnection.InvokeAsync(nameof(RelayHub.NotifyFileUploadFailed), token, ex.Message); } }); } -- cgit v1.2.3 From e0a777403e15eaeefc7881d9451aef2f26bf3f4c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 17:40:00 -0600 Subject: ensure uploaded share stream is flushed before loading --- src/slskd/Relay/API/Controllers/RelayController.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index ddb8d0b0..0be59b13 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -354,14 +354,16 @@ namespace slskd.Relay 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); + // the streams must be closed (and the output flushed) before the file is handed off to be opened as a database + using (var outputStream = new FileStream(temp, FileMode.CreateNew, FileAccess.Write)) + using (var inputStream = database.OpenReadStream()) + { + await inputStream.CopyToAsync(outputStream); + } sw.Stop(); - Log.Information("Download of shares from {Agent} ({Token}) complete ({Size} in {Duration}ms)", agentName, guid, ((double)inputStream.Length).SizeSuffix(), sw.ElapsedMilliseconds); + Log.Information("Download of shares from {Agent} ({Token}) complete ({Size} in {Duration}ms)", agentName, guid, ((double)database.Length).SizeSuffix(), sw.ElapsedMilliseconds); await Relay.HandleShareUploadAsync(agentName, id: guid, shares, temp); -- cgit v1.2.3 From be6a4774e5beed6541d0f2d3f3f2ca6bd5b843d4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 17:55:18 -0600 Subject: extend challenge token cache to 60 seconds (from 10) --- src/slskd/Relay/RelayService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index 26a635a1..ed035852 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -366,8 +366,8 @@ namespace slskd.Relay { var token = Cryptography.Random.GetBytes(32).ToBase62(); - // this token is only valid for this connection id for a short time this is important to prevent replay-type attacks. - MemoryCache.Set(GetAuthTokenCacheKey(connectionId), token, TimeSpan.FromSeconds(10)); + // this token is only valid for this connection id for a short time; this is important to prevent replay-type attacks. + MemoryCache.Set(GetAuthTokenCacheKey(connectionId), token, TimeSpan.FromMinutes(1)); Log.Debug("Cached auth token {Token} for ID {Id}", token, connectionId); return token; } -- cgit v1.2.3 From ecb3a801e1f07eec4d54b3aff3071abd7bc88ccf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 17:56:31 -0600 Subject: remove challenge token after first use --- src/slskd/Relay/RelayService.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index ed035852..8742191e 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -765,6 +765,9 @@ namespace slskd.Relay return false; } + // challenge tokens can be used exactly once, pass or fail + MemoryCache.Remove(GetAuthTokenCacheKey(connectionId)); + var agentOptions = OptionsMonitor.CurrentValue.Relay.Agents.Values.SingleOrDefault(a => a.InstanceName == agentName); if (agentOptions == default) -- cgit v1.2.3 From cec4618304acfa807d76325875fc87533b6c797e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 17:56:51 -0600 Subject: use FixedTimeEquals for credential comparison --- src/slskd/Relay/RelayService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index 8742191e..3ad36ed6 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -780,7 +780,9 @@ namespace slskd.Relay var tokenBytes = System.Text.Encoding.UTF8.GetBytes((string)challengeToken); var expectedCredential = Aes.Encrypt(tokenBytes, key).ToBase62(); - if (expectedCredential != credential) + if (!System.Security.Cryptography.CryptographicOperations.FixedTimeEquals( + left: System.Text.Encoding.UTF8.GetBytes(expectedCredential), + right: System.Text.Encoding.UTF8.GetBytes(credential ?? string.Empty))) { Log.Debug("Validation failed: Supplied credential {Credential} does not match expected credential {Expected}", credential, expectedCredential); return false; -- cgit v1.2.3 From b626a5716a8d810ebba66327d563538fb8f54ee5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 18:23:43 -0600 Subject: dispose http clients --- src/slskd/Relay/RelayClient.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 63eda434..4f1e2924 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -447,7 +447,9 @@ namespace slskd.Relay request.Content = content; Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token); - var response = await CreateHttpClient().SendAsync(request); + + using var client = CreateHttpClient(); + using var response = await client.SendAsync(request); if (!response.IsSuccessStatusCode) { @@ -658,7 +660,9 @@ namespace slskd.Relay Log.Information("Beginning upload of shares ({Size})", size); Log.Debug("Shares: {Shares}", Shares.LocalHost.Shares.ToJson()); - var response = await CreateHttpClient().SendAsync(request, cancellationToken); + + using var client = CreateHttpClient(); + using var response = await client.SendAsync(request, cancellationToken); if (!response.IsSuccessStatusCode) { -- cgit v1.2.3 From 5da8ec43e7369656781df3bac8f894167529a50e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 18:31:31 -0600 Subject: ensure relay clients are disposed when switched --- src/slskd/Relay/RelayService.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index 3ad36ed6..c063ceb2 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -858,6 +858,8 @@ namespace slskd.Relay else { // the controller changed. disconnect and throw away the client and create a new one + var oldClient = Client; + Client = new RelayClient( shareService: Shares, fileService: Files, @@ -867,6 +869,8 @@ namespace slskd.Relay Client.StateMonitor.OnChange(clientState => State.SetValue(state => state with { Controller = state.Controller with { State = clientState.Current } })); + oldClient.TryDispose(); + State.SetValue(state => state with { Mode = mode, -- cgit v1.2.3 From abffd0c9c24a7b5d20962fc979c6eb581dcad9dd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Nov 2026 18:37:10 -0600 Subject: use fixedtimeequals when validating creds --- src/slskd/Relay/RelayService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index c063ceb2..c6c1f5d0 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -934,7 +934,9 @@ namespace slskd.Relay var tokenBytes = System.Text.Encoding.UTF8.GetBytes(token); var expectedCredential = Aes.Encrypt(tokenBytes, key).ToBase62(); - if (expectedCredential != credential) + if (!System.Security.Cryptography.CryptographicOperations.FixedTimeEquals( + left: System.Text.Encoding.UTF8.GetBytes(expectedCredential), + right: System.Text.Encoding.UTF8.GetBytes(credential ?? string.Empty))) { Log.Debug("Validation failed: Supplied credential {Credential} does not match expected credential {Expected}", credential, expectedCredential); return false; -- cgit v1.2.3