aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Relay
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-06-07 18:46:25 -0500
committerJP Dillingham <jp@dillingham.me>2026-06-07 18:46:25 -0500
commit294fcabd3928b420a4dfbd7ddd0dc38ada523165 (patch)
treebcc92e42c71f2a9b67f7750c5ecd0a396b5897e5 /src/slskd/Relay
parent60bc0c0c2ea6d9c80a7f091f022567b1d5e3240a (diff)
parent8e152ed6eccd5650179f2875eef4aa21243c8c15 (diff)
downloadslskd-294fcabd3928b420a4dfbd7ddd0dc38ada523165.tar.gz
Merge remote-tracking branch 'origin/master' into reports
Diffstat (limited to 'src/slskd/Relay')
-rw-r--r--src/slskd/Relay/API/Controllers/RelayController.cs27
-rw-r--r--src/slskd/Relay/RelayClient.cs40
2 files changed, 51 insertions, 16 deletions
diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs
index 0d2ef9c5..ddb8d0b0 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.
@@ -159,7 +167,12 @@ namespace slskd.Relay
return Unauthorized();
}
- var sourceFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, filename);
+ // if we get this far, the caller has supplied a valid credential and filename, meaning the request
+ // to download is being made in response to the controller signaling the agent that a download had completed.
+ // it's not possible for the client to download arbitrary files; credentials are computed from a token, and
+ // this token is cached in the controller, so only file downloads that the controller solicited are possible
+ // still, for the abundance of caution, use the CombineSafely method in case the filename has traversal segments
+ var sourceFile = FileSafety.CombineSafely(OptionsMonitor.CurrentValue.Directories.Downloads, filename);
Log.Information("Agent {Agent} authenticated for token {Token}. Sending file {Filename}", agentName, guid, filename);
@@ -225,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");
@@ -323,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_{FileSafety.SanitizeFilename(agentName)}_{Path.GetRandomFileName()}.db");
try
{
diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs
index 9aec7b00..5c5f9d38 100644
--- a/src/slskd/Relay/RelayClient.cs
+++ b/src/slskd/Relay/RelayClient.cs
@@ -273,7 +273,7 @@ namespace slskd.Relay
StartCancellationTokenSource?.Cancel();
HubConnection = new HubConnectionBuilder()
- .WithUrl($"{options.Relay.Controller.Address}/hub/relay", builder =>
+ .WithUrl($"{options.Relay.Controller.Address.TrimEnd('/')}/hub/relay", builder =>
{
builder.AccessTokenProvider = () => Task.FromResult(options.Relay.Controller.ApiKey);
builder.HttpMessageHandlerFactory = (message) =>
@@ -338,7 +338,7 @@ namespace slskd.Relay
}
client.Timeout = TimeSpan.FromMilliseconds(int.MaxValue);
- client.BaseAddress = new(options.Address);
+ client.BaseAddress = new(options.Address.TrimEnd('/'));
return client;
}
@@ -370,6 +370,12 @@ namespace slskd.Relay
private async Task HandleFileInfoRequest(string filename, Guid id)
{
+ if (FileSafety.ContainsTraversalSegments(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);
try
@@ -387,8 +393,14 @@ 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))
+ {
+ 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 () =>
{
Log.Information("Relay controller requested file {Filename} with ID {Id}", filename, token);
@@ -442,33 +454,37 @@ 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))
+ {
+ 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);
_ = Task.Run(async () =>
{
- var destinationFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, filename);
+ var destinationFile = FileSafety.CombineSafely(OptionsMonitor.CurrentValue.Directories.Downloads, filename);
if (OptionsMonitor.CurrentValue.Relay.Mode.ToEnum<RelayMode>() == RelayMode.Debug)
{
// if we're debugging, we're referencing the same file for both the controller and agent which will lead to an
// access violation. prefix the destination file to avoid this.
- destinationFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, $"{filename}.relayed");
+ destinationFile = FileSafety.CombineSafely(OptionsMonitor.CurrentValue.Directories.Downloads, $"{filename}.relayed");
}
// if the controller is Windows and the agent is Linux or vice versa, we need to translate the filename to the
// local OS or we're going to get funny results when we go to write the file
- destinationFile = destinationFile.LocalizePath();
+ destinationFile = FileSafety.LocalizePath(destinationFile);
await Retry.Do(task: async () =>
{
@@ -498,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)
@@ -579,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
{